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