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 all of their children).
32 * If an element does not exist, it is ignored.
35 * NOTE: This operation only deletes elements contained within leaf resources -
36 * that is, elements within compilation units. To delete a compilation unit or a
37 * 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
44 * @see processElements(). Keys are compilation units, values are
45 * <code>IRegion</code>s of elements to be processed in each
48 protected Map fChildrenToRemove;
51 * The <code>DOMFactory</code> used to manipulate the source code of
52 * <code>ICompilationUnit</code>s.
54 protected DOMFactory fFactory;
57 * When executed, this operation will delete the given elements. The
58 * elements to delete cannot be <code>null</code> or empty, and must be
59 * contained within a compilation unit.
61 public DeleteElementsOperation(IJavaElement[] elementsToDelete,
63 super(elementsToDelete, force);
64 fFactory = new DOMFactory();
70 protected String getMainTaskName() {
71 return Util.bind("operation.deleteElementProgress"); //$NON-NLS-1$
75 * Groups the elements to be processed by their compilation unit. If
76 * parent/child combinations are present, children are discarded (only the
77 * parents are processed). Removes any duplicates specified in elements to
80 protected void groupElements() throws JavaModelException {
81 fChildrenToRemove = new HashMap(1);
83 for (int i = 0, length = fElementsToProcess.length; i < length; i++) {
84 IJavaElement e = fElementsToProcess[i];
85 ICompilationUnit cu = getCompilationUnitFor(e);
87 throw new JavaModelException(new JavaModelStatus(
88 JavaModelStatus.READ_ONLY, e));
90 IRegion region = (IRegion) fChildrenToRemove.get(cu);
92 region = new Region();
93 fChildrenToRemove.put(cu, region);
99 fElementsToProcess = new IJavaElement[uniqueCUs];
100 Iterator iter = fChildrenToRemove.keySet().iterator();
102 while (iter.hasNext()) {
103 fElementsToProcess[i++] = (IJavaElement) iter.next();
108 * Deletes this element from its compilation unit.
110 * @see MultiOperation
112 protected void processElement(IJavaElement element)
113 throws JavaModelException {
114 ICompilationUnit cu = (ICompilationUnit) element;
116 // keep track of the import statements - if all are removed, delete
117 // the import container (and report it in the delta)
118 // int numberOfImports = cu.getImports().length;
120 IBuffer buffer = cu.getBuffer();
123 JavaElementDelta delta = new JavaElementDelta(cu);
124 IJavaElement[] cuElements = ((IRegion) fChildrenToRemove.get(cu))
126 for (int i = 0, length = cuElements.length; i < length; i++) {
127 IJavaElement e = cuElements[i];
129 char[] contents = buffer.getCharacters();
130 if (contents == null)
132 IDOMCompilationUnit cuDOM = fFactory.createCompilationUnit(
133 contents, cu.getElementName());
134 DOMNode node = (DOMNode) ((JavaElement) e).findNode(cuDOM);
139 "Failed to locate " + e.getElementName() + " in " + cuDOM.getName()); //$NON-NLS-1$//$NON-NLS-2$
141 int startPosition = node.getStartPosition();
142 buffer.replace(startPosition, node.getEndPosition()
143 - startPosition + 1, CharOperation.NO_CHAR);
145 // if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
146 // numberOfImports--;
147 // if (numberOfImports == 0) {
148 // delta.removed(cu.getImportContainer());
153 if (delta.getAffectedChildren().length > 0) {
154 cu.save(getSubProgressMonitor(1), force);
155 if (!cu.isWorkingCopy()) { // if unit is working copy, then save
156 // will have already fired the delta
158 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
164 * @see MultiOperation This method first group the elements by
165 * <code>ICompilationUnit</code>, and then processes the
166 * <code>ICompilationUnit</code>.
168 protected void processElements() throws JavaModelException {
170 super.processElements();
174 * @see MultiOperation
176 protected void verify(IJavaElement element) throws JavaModelException {
177 IJavaElement[] children = ((IRegion) fChildrenToRemove.get(element))
179 for (int i = 0; i < children.length; i++) {
180 IJavaElement child = children[i];
181 if (child.getCorrespondingResource() != null)
182 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, child);
183 if (child.isReadOnly())
184 error(IJavaModelStatusConstants.READ_ONLY, child);