A massive organize imports and formatting of the sources using default Eclipse code...
[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 all of their children).
32  * If an element does not exist, it is ignored.
33  * 
34  * <p>
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
38  * should be used.
39  */
40 public class DeleteElementsOperation extends MultiOperation {
41         /**
42          * The elements this operation processes grouped by compilation unit
43          * 
44          * @see processElements(). Keys are compilation units, values are
45          *      <code>IRegion</code>s of elements to be processed in each
46          *      compilation unit.
47          */
48         protected Map fChildrenToRemove;
49
50         /**
51          * The <code>DOMFactory</code> used to manipulate the source code of
52          * <code>ICompilationUnit</code>s.
53          */
54         protected DOMFactory fFactory;
55
56         /**
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.
60          */
61         public DeleteElementsOperation(IJavaElement[] elementsToDelete,
62                         boolean force) {
63                 super(elementsToDelete, force);
64                 fFactory = new DOMFactory();
65         }
66
67         /**
68          * @see MultiOperation
69          */
70         protected String getMainTaskName() {
71                 return Util.bind("operation.deleteElementProgress"); //$NON-NLS-1$
72         }
73
74         /**
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
78          * be processed.
79          */
80         protected void groupElements() throws JavaModelException {
81                 fChildrenToRemove = new HashMap(1);
82                 int uniqueCUs = 0;
83                 for (int i = 0, length = fElementsToProcess.length; i < length; i++) {
84                         IJavaElement e = fElementsToProcess[i];
85                         ICompilationUnit cu = getCompilationUnitFor(e);
86                         if (cu == null) {
87                                 throw new JavaModelException(new JavaModelStatus(
88                                                 JavaModelStatus.READ_ONLY, e));
89                         } else {
90                                 IRegion region = (IRegion) fChildrenToRemove.get(cu);
91                                 if (region == null) {
92                                         region = new Region();
93                                         fChildrenToRemove.put(cu, region);
94                                         uniqueCUs += 1;
95                                 }
96                                 region.add(e);
97                         }
98                 }
99                 fElementsToProcess = new IJavaElement[uniqueCUs];
100                 Iterator iter = fChildrenToRemove.keySet().iterator();
101                 int i = 0;
102                 while (iter.hasNext()) {
103                         fElementsToProcess[i++] = (IJavaElement) iter.next();
104                 }
105         }
106
107         /**
108          * Deletes this element from its compilation unit.
109          * 
110          * @see MultiOperation
111          */
112         protected void processElement(IJavaElement element)
113                         throws JavaModelException {
114                 ICompilationUnit cu = (ICompilationUnit) element;
115
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;
119
120                 IBuffer buffer = cu.getBuffer();
121                 if (buffer == null)
122                         return;
123                 JavaElementDelta delta = new JavaElementDelta(cu);
124                 IJavaElement[] cuElements = ((IRegion) fChildrenToRemove.get(cu))
125                                 .getElements();
126                 for (int i = 0, length = cuElements.length; i < length; i++) {
127                         IJavaElement e = cuElements[i];
128                         if (e.exists()) {
129                                 char[] contents = buffer.getCharacters();
130                                 if (contents == null)
131                                         continue;
132                                 IDOMCompilationUnit cuDOM = fFactory.createCompilationUnit(
133                                                 contents, cu.getElementName());
134                                 DOMNode node = (DOMNode) ((JavaElement) e).findNode(cuDOM);
135                                 if (node == null)
136                                         Assert
137                                                         .isTrue(
138                                                                         false,
139                                                                         "Failed to locate " + e.getElementName() + " in " + cuDOM.getName()); //$NON-NLS-1$//$NON-NLS-2$
140
141                                 int startPosition = node.getStartPosition();
142                                 buffer.replace(startPosition, node.getEndPosition()
143                                                 - startPosition + 1, CharOperation.NO_CHAR);
144                                 delta.removed(e);
145                                 // if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
146                                 // numberOfImports--;
147                                 // if (numberOfImports == 0) {
148                                 // delta.removed(cu.getImportContainer());
149                                 // }
150                                 // }
151                         }
152                 }
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
157                                 addDelta(delta);
158                                 this.setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
159                         }
160                 }
161         }
162
163         /**
164          * @see MultiOperation This method first group the elements by
165          *      <code>ICompilationUnit</code>, and then processes the
166          *      <code>ICompilationUnit</code>.
167          */
168         protected void processElements() throws JavaModelException {
169                 groupElements();
170                 super.processElements();
171         }
172
173         /**
174          * @see MultiOperation
175          */
176         protected void verify(IJavaElement element) throws JavaModelException {
177                 IJavaElement[] children = ((IRegion) fChildrenToRemove.get(element))
178                                 .getElements();
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);
185                 }
186         }
187 }