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;
29 //import net.sourceforge.phpdt.internal.corext.Assert;
30 import org.eclipse.core.runtime.Assert;
33 * This operation deletes a collection of elements (and all of their children).
34 * If an element does not exist, it is ignored.
37 * NOTE: This operation only deletes elements contained within leaf resources -
38 * that is, elements within compilation units. To delete a compilation unit or a
39 * package, etc (which have an actual resource), a DeleteResourcesOperation
42 public class DeleteElementsOperation extends MultiOperation {
44 * The elements this operation processes grouped by compilation unit
46 * @see processElements(). Keys are compilation units, values are
47 * <code>IRegion</code>s of elements to be processed in each
50 protected Map fChildrenToRemove;
53 * The <code>DOMFactory</code> used to manipulate the source code of
54 * <code>ICompilationUnit</code>s.
56 protected DOMFactory fFactory;
59 * When executed, this operation will delete the given elements. The
60 * elements to delete cannot be <code>null</code> or empty, and must be
61 * contained within a compilation unit.
63 public DeleteElementsOperation(IJavaElement[] elementsToDelete,
65 super(elementsToDelete, force);
66 fFactory = new DOMFactory();
72 protected String getMainTaskName() {
73 return Util.bind("operation.deleteElementProgress"); //$NON-NLS-1$
77 * Groups the elements to be processed by their compilation unit. If
78 * parent/child combinations are present, children are discarded (only the
79 * parents are processed). Removes any duplicates specified in elements to
82 protected void groupElements() throws JavaModelException {
83 fChildrenToRemove = new HashMap(1);
85 for (int i = 0, length = fElementsToProcess.length; i < length; i++) {
86 IJavaElement e = fElementsToProcess[i];
87 ICompilationUnit cu = getCompilationUnitFor(e);
89 throw new JavaModelException(new JavaModelStatus(
90 JavaModelStatus.READ_ONLY, e));
92 IRegion region = (IRegion) fChildrenToRemove.get(cu);
94 region = new Region();
95 fChildrenToRemove.put(cu, region);
101 fElementsToProcess = new IJavaElement[uniqueCUs];
102 Iterator iter = fChildrenToRemove.keySet().iterator();
104 while (iter.hasNext()) {
105 fElementsToProcess[i++] = (IJavaElement) iter.next();
110 * Deletes this element from its compilation unit.
112 * @see MultiOperation
114 protected void processElement(IJavaElement element)
115 throws JavaModelException {
116 ICompilationUnit cu = (ICompilationUnit) element;
118 // keep track of the import statements - if all are removed, delete
119 // the import container (and report it in the delta)
120 // int numberOfImports = cu.getImports().length;
122 IBuffer buffer = cu.getBuffer();
125 JavaElementDelta delta = new JavaElementDelta(cu);
126 IJavaElement[] cuElements = ((IRegion) fChildrenToRemove.get(cu))
128 for (int i = 0, length = cuElements.length; i < length; i++) {
129 IJavaElement e = cuElements[i];
131 char[] contents = buffer.getCharacters();
132 if (contents == null)
134 IDOMCompilationUnit cuDOM = fFactory.createCompilationUnit(
135 contents, cu.getElementName());
136 DOMNode node = (DOMNode) ((JavaElement) e).findNode(cuDOM);
141 "Failed to locate " + e.getElementName() + " in " + cuDOM.getName()); //$NON-NLS-1$//$NON-NLS-2$
143 int startPosition = node.getStartPosition();
144 buffer.replace(startPosition, node.getEndPosition()
145 - startPosition + 1, CharOperation.NO_CHAR);
147 // if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
148 // numberOfImports--;
149 // if (numberOfImports == 0) {
150 // delta.removed(cu.getImportContainer());
155 if (delta.getAffectedChildren().length > 0) {
156 cu.save(getSubProgressMonitor(1), force);
157 if (!cu.isWorkingCopy()) { // if unit is working copy, then save
158 // will have already fired the delta
160 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
166 * @see MultiOperation This method first group the elements by
167 * <code>ICompilationUnit</code>, and then processes the
168 * <code>ICompilationUnit</code>.
170 protected void processElements() throws JavaModelException {
172 super.processElements();
176 * @see MultiOperation
178 protected void verify(IJavaElement element) throws JavaModelException {
179 IJavaElement[] children = ((IRegion) fChildrenToRemove.get(element))
181 for (int i = 0; i < children.length; i++) {
182 IJavaElement child = children[i];
183 if (child.getCorrespondingResource() != null)
184 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, child);
185 if (child.isReadOnly())
186 error(IJavaModelStatusConstants.READ_ONLY, child);