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