1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.HashMap;
14 import java.util.Iterator;
17 import net.sourceforge.phpdt.core.IBuffer;
18 import net.sourceforge.phpdt.core.ICompilationUnit;
19 import net.sourceforge.phpdt.core.IJavaElement;
20 import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
21 import net.sourceforge.phpdt.core.IRegion;
22 import net.sourceforge.phpdt.core.JavaModelException;
23 import net.sourceforge.phpdt.core.compiler.CharOperation;
24 import net.sourceforge.phpdt.core.jdom.DOMFactory;
25 import net.sourceforge.phpdt.core.jdom.IDOMCompilationUnit;
26 import net.sourceforge.phpdt.internal.core.jdom.DOMNode;
27 import net.sourceforge.phpdt.internal.core.util.Util;
28 import net.sourceforge.phpdt.internal.corext.Assert;
31 * This operation deletes a collection of elements (and
32 * all of their children).
33 * If an element does not exist, it is ignored.
35 * <p>NOTE: This operation only deletes elements contained within leaf resources -
36 * that is, elements within compilation units. To delete a compilation unit or
37 * a package, etc (which have an actual resource), a DeleteResourcesOperation
40 public class DeleteElementsOperation extends MultiOperation {
42 * The elements this operation processes grouped by compilation unit
43 * @see processElements(). Keys are compilation units,
44 * values are <code>IRegion</code>s of elements to be processed in each
47 protected Map fChildrenToRemove;
49 * The <code>DOMFactory</code> used to manipulate the source code of
50 * <code>ICompilationUnit</code>s.
52 protected DOMFactory fFactory;
54 * When executed, this operation will delete the given elements. The elements
55 * to delete cannot be <code>null</code> or empty, and must be contained within a
58 public DeleteElementsOperation(IJavaElement[] elementsToDelete, boolean force) {
59 super(elementsToDelete, force);
60 fFactory = new DOMFactory();
66 protected String getMainTaskName() {
67 return Util.bind("operation.deleteElementProgress"); //$NON-NLS-1$
70 * Groups the elements to be processed by their compilation unit.
71 * If parent/child combinations are present, children are
72 * discarded (only the parents are processed). Removes any
73 * duplicates specified in elements to be processed.
75 protected void groupElements() throws JavaModelException {
76 fChildrenToRemove = new HashMap(1);
78 for (int i = 0, length = fElementsToProcess.length; i < length; i++) {
79 IJavaElement e = fElementsToProcess[i];
80 ICompilationUnit cu = getCompilationUnitFor(e);
82 throw new JavaModelException(new JavaModelStatus(JavaModelStatus.READ_ONLY, e));
84 IRegion region = (IRegion) fChildrenToRemove.get(cu);
86 region = new Region();
87 fChildrenToRemove.put(cu, region);
93 fElementsToProcess = new IJavaElement[uniqueCUs];
94 Iterator iter = fChildrenToRemove.keySet().iterator();
96 while (iter.hasNext()) {
97 fElementsToProcess[i++] = (IJavaElement) iter.next();
101 * Deletes this element from its compilation unit.
102 * @see MultiOperation
104 protected void processElement(IJavaElement element) throws JavaModelException {
105 ICompilationUnit cu = (ICompilationUnit) element;
107 // keep track of the import statements - if all are removed, delete
108 // the import container (and report it in the delta)
109 // int numberOfImports = cu.getImports().length;
111 IBuffer buffer = cu.getBuffer();
112 if (buffer == null) return;
113 JavaElementDelta delta = new JavaElementDelta(cu);
114 IJavaElement[] cuElements = ((IRegion) fChildrenToRemove.get(cu)).getElements();
115 for (int i = 0, length = cuElements.length; i < length; i++) {
116 IJavaElement e = cuElements[i];
118 char[] contents = buffer.getCharacters();
119 if (contents == null) continue;
120 IDOMCompilationUnit cuDOM = fFactory.createCompilationUnit(contents, cu.getElementName());
121 DOMNode node = (DOMNode)((JavaElement) e).findNode(cuDOM);
122 if (node == null) Assert.isTrue(false, "Failed to locate " + e.getElementName() + " in " + cuDOM.getName()); //$NON-NLS-1$//$NON-NLS-2$
124 int startPosition = node.getStartPosition();
125 buffer.replace(startPosition, node.getEndPosition() - startPosition + 1, CharOperation.NO_CHAR);
127 // if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
128 // numberOfImports--;
129 // if (numberOfImports == 0) {
130 // delta.removed(cu.getImportContainer());
135 if (delta.getAffectedChildren().length > 0) {
136 cu.save(getSubProgressMonitor(1), force);
137 if (!cu.isWorkingCopy()) { // if unit is working copy, then save will have already fired the delta
139 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
144 * @see MultiOperation
145 * This method first group the elements by <code>ICompilationUnit</code>,
146 * and then processes the <code>ICompilationUnit</code>.
148 protected void processElements() throws JavaModelException {
150 super.processElements();
153 * @see MultiOperation
155 protected void verify(IJavaElement element) throws JavaModelException {
156 IJavaElement[] children = ((IRegion) fChildrenToRemove.get(element)).getElements();
157 for (int i = 0; i < children.length; i++) {
158 IJavaElement child = children[i];
159 if (child.getCorrespondingResource() != null)
160 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, child);
161 if (child.isReadOnly())
162 error(IJavaModelStatusConstants.READ_ONLY, child);