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.corext.Assert;
30 * This operation deletes a collection of elements (and
31 * all of their children).
32 * If an element does not exist, it is ignored.
34 * <p>NOTE: This operation only deletes elements contained within leaf resources -
35 * that is, elements within compilation units. To delete a compilation unit or
36 * a package, etc (which have an actual resource), a DeleteResourcesOperation
39 public class DeleteElementsOperation extends MultiOperation {
41 * The elements this operation processes grouped by compilation unit
42 * @see processElements(). Keys are compilation units,
43 * values are <code>IRegion</code>s of elements to be processed in each
46 protected Map fChildrenToRemove;
48 * The <code>DOMFactory</code> used to manipulate the source code of
49 * <code>ICompilationUnit</code>s.
51 protected DOMFactory fFactory;
53 * When executed, this operation will delete the given elements. The elements
54 * to delete cannot be <code>null</code> or empty, and must be contained within a
57 public DeleteElementsOperation(IJavaElement[] elementsToDelete, boolean force) {
58 super(elementsToDelete, force);
59 fFactory = new DOMFactory();
65 protected String getMainTaskName() {
66 return Util.bind("operation.deleteElementProgress"); //$NON-NLS-1$
69 * Groups the elements to be processed by their compilation unit.
70 * If parent/child combinations are present, children are
71 * discarded (only the parents are processed). Removes any
72 * duplicates specified in elements to be processed.
74 protected void groupElements() throws JavaModelException {
75 fChildrenToRemove = new HashMap(1);
77 for (int i = 0, length = fElementsToProcess.length; i < length; i++) {
78 IJavaElement e = fElementsToProcess[i];
79 ICompilationUnit cu = getCompilationUnitFor(e);
81 throw new JavaModelException(new JavaModelStatus(JavaModelStatus.READ_ONLY, e));
83 IRegion region = (IRegion) fChildrenToRemove.get(cu);
85 region = new Region();
86 fChildrenToRemove.put(cu, region);
92 fElementsToProcess = new IJavaElement[uniqueCUs];
93 Iterator iter = fChildrenToRemove.keySet().iterator();
95 while (iter.hasNext()) {
96 fElementsToProcess[i++] = (IJavaElement) iter.next();
100 * Deletes this element from its compilation unit.
101 * @see MultiOperation
103 protected void processElement(IJavaElement element) throws JavaModelException {
104 ICompilationUnit cu = (ICompilationUnit) element;
106 // keep track of the import statements - if all are removed, delete
107 // the import container (and report it in the delta)
108 // int numberOfImports = cu.getImports().length;
110 IBuffer buffer = cu.getBuffer();
111 if (buffer == null) return;
112 JavaElementDelta delta = new JavaElementDelta(cu);
113 IJavaElement[] cuElements = ((IRegion) fChildrenToRemove.get(cu)).getElements();
114 for (int i = 0, length = cuElements.length; i < length; i++) {
115 IJavaElement e = cuElements[i];
117 char[] contents = buffer.getCharacters();
118 if (contents == null) continue;
119 IDOMCompilationUnit cuDOM = fFactory.createCompilationUnit(contents, cu.getElementName());
120 DOMNode node = (DOMNode)((JavaElement) e).findNode(cuDOM);
121 if (node == null) Assert.isTrue(false, "Failed to locate " + e.getElementName() + " in " + cuDOM.getName()); //$NON-NLS-1$//$NON-NLS-2$
123 int startPosition = node.getStartPosition();
124 buffer.replace(startPosition, node.getEndPosition() - startPosition + 1, CharOperation.NO_CHAR);
126 // if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
127 // numberOfImports--;
128 // if (numberOfImports == 0) {
129 // delta.removed(cu.getImportContainer());
134 if (delta.getAffectedChildren().length > 0) {
135 cu.save(getSubProgressMonitor(1), fForce);
136 if (!cu.isWorkingCopy()) { // if unit is working copy, then save will have already fired the delta
138 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
143 * @see MultiOperation
144 * This method first group the elements by <code>ICompilationUnit</code>,
145 * and then processes the <code>ICompilationUnit</code>.
147 protected void processElements() throws JavaModelException {
149 super.processElements();
152 * @see MultiOperation
154 protected void verify(IJavaElement element) throws JavaModelException {
155 IJavaElement[] children = ((IRegion) fChildrenToRemove.get(element)).getElements();
156 for (int i = 0; i < children.length; i++) {
157 IJavaElement child = children[i];
158 if (child.getCorrespondingResource() != null)
159 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, child);
160 if (child.isReadOnly())
161 error(IJavaModelStatusConstants.READ_ONLY, child);