3m9 compatible;
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / DeleteElementsOperation.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Map;
16
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;
29
30 /**
31  * This operation deletes a collection of elements (and
32  * all of their children).
33  * If an element does not exist, it is ignored.
34  *
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
38  * should be used.
39  */
40 public class DeleteElementsOperation extends MultiOperation {
41         /**
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
45          * compilation unit.
46          */ 
47         protected Map fChildrenToRemove;
48         /**
49          * The <code>DOMFactory</code> used to manipulate the source code of
50          * <code>ICompilationUnit</code>s.
51          */
52         protected DOMFactory fFactory;
53         /**
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
56          * compilation unit.
57          */
58         public DeleteElementsOperation(IJavaElement[] elementsToDelete, boolean force) {
59                 super(elementsToDelete, force);
60                 fFactory = new DOMFactory();
61         }
62         
63         /**
64          * @see MultiOperation
65          */
66         protected String getMainTaskName() {
67                 return Util.bind("operation.deleteElementProgress"); //$NON-NLS-1$
68         }
69         /**
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.
74          */
75         protected void groupElements() throws JavaModelException {
76                 fChildrenToRemove = new HashMap(1);
77                 int uniqueCUs = 0;
78                 for (int i = 0, length = fElementsToProcess.length; i < length; i++) {
79                         IJavaElement e = fElementsToProcess[i];
80                         ICompilationUnit cu = getCompilationUnitFor(e);
81                         if (cu == null) {
82                                 throw new JavaModelException(new JavaModelStatus(JavaModelStatus.READ_ONLY, e));
83                         } else {
84                                 IRegion region = (IRegion) fChildrenToRemove.get(cu);
85                                 if (region == null) {
86                                         region = new Region();
87                                         fChildrenToRemove.put(cu, region);
88                                         uniqueCUs += 1;
89                                 }
90                                 region.add(e);
91                         }
92                 }
93                 fElementsToProcess = new IJavaElement[uniqueCUs];
94                 Iterator iter = fChildrenToRemove.keySet().iterator();
95                 int i = 0;
96                 while (iter.hasNext()) {
97                         fElementsToProcess[i++] = (IJavaElement) iter.next();
98                 }
99         }
100         /**
101          * Deletes this element from its compilation unit.
102          * @see MultiOperation
103          */
104         protected void processElement(IJavaElement element) throws JavaModelException {
105                 ICompilationUnit cu = (ICompilationUnit) element;
106         
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;
110         
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];
117                         if (e.exists()) {
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$
123
124                                 int startPosition = node.getStartPosition();
125                                 buffer.replace(startPosition, node.getEndPosition() - startPosition + 1, CharOperation.NO_CHAR);
126                                 delta.removed(e);
127 //                              if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
128 //                                      numberOfImports--;
129 //                                      if (numberOfImports == 0) {
130 //                                              delta.removed(cu.getImportContainer());
131 //                                      }
132 //                              }
133                         }
134                 }
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
138                                 addDelta(delta);
139                                 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
140                         }
141                 }
142         }
143         /**
144          * @see MultiOperation
145          * This method first group the elements by <code>ICompilationUnit</code>,
146          * and then processes the <code>ICompilationUnit</code>.
147          */
148         protected void processElements() throws JavaModelException {
149                 groupElements();
150                 super.processElements();
151         }
152         /**
153          * @see MultiOperation
154          */
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);
163                 }
164         }
165 }