KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > DeleteElementsOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.core.runtime.Assert;
20 import org.eclipse.core.runtime.jobs.ISchedulingRule;
21 import org.eclipse.jdt.core.ICompilationUnit;
22 import org.eclipse.jdt.core.IJavaElement;
23 import org.eclipse.jdt.core.IJavaModelStatusConstants;
24 import org.eclipse.jdt.core.IRegion;
25 import org.eclipse.jdt.core.JavaModelException;
26 import org.eclipse.jdt.core.dom.AST;
27 import org.eclipse.jdt.core.dom.ASTNode;
28 import org.eclipse.jdt.core.dom.ASTParser;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
31 import org.eclipse.jdt.internal.core.util.Messages;
32 import org.eclipse.jface.text.BadLocationException;
33 import org.eclipse.jface.text.IDocument;
34 import org.eclipse.text.edits.TextEdit;
35
36 /**
37  * This operation deletes a collection of elements (and
38  * all of their children).
39  * If an element does not exist, it is ignored.
40  *
41  * <p>NOTE: This operation only deletes elements contained within leaf resources -
42  * that is, elements within compilation units. To delete a compilation unit or
43  * a package, etc (which have an actual resource), a DeleteResourcesOperation
44  * should be used.
45  */

46 public class DeleteElementsOperation extends MultiOperation {
47     /**
48      * The elements this operation processes grouped by compilation unit
49      * @see #processElements() Keys are compilation units,
50      * values are <code>IRegion</code>s of elements to be processed in each
51      * compilation unit.
52      */

53     protected Map JavaDoc childrenToRemove;
54     /**
55      * The <code>ASTParser</code> used to manipulate the source code of
56      * <code>ICompilationUnit</code>.
57      */

58     protected ASTParser parser;
59     /**
60      * When executed, this operation will delete the given elements. The elements
61      * to delete cannot be <code>null</code> or empty, and must be contained within a
62      * compilation unit.
63      */

64     public DeleteElementsOperation(IJavaElement[] elementsToDelete, boolean force) {
65         super(elementsToDelete, force);
66         initASTParser();
67     }
68     
69     private void deleteElement(IJavaElement elementToRemove, ICompilationUnit cu) throws JavaModelException {
70         // ensure cu is consistent (noop if already consistent)
71
cu.makeConsistent(this.progressMonitor);
72         this.parser.setSource(cu);
73         CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor);
74         ASTNode node = ((JavaElement) elementToRemove).findNode(astCU);
75         if (node == null)
76             Assert.isTrue(false, "Failed to locate " + elementToRemove.getElementName() + " in " + cu.getElementName()); //$NON-NLS-1$//$NON-NLS-2$
77
IDocument document = getDocument(cu);
78         AST ast = astCU.getAST();
79         ASTRewrite rewriter = ASTRewrite.create(ast);
80         rewriter.remove(node, null);
81         TextEdit edits = rewriter.rewriteAST(document, null);
82         try {
83             edits.apply(document);
84         } catch (BadLocationException e) {
85             throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS);
86         }
87     }
88
89     private void initASTParser() {
90         this.parser = ASTParser.newParser(AST.JLS3);
91     }
92
93     /**
94      * @see MultiOperation
95      */

96     protected String JavaDoc getMainTaskName() {
97         return Messages.operation_deleteElementProgress;
98     }
99     protected ISchedulingRule getSchedulingRule() {
100         if (this.elementsToProcess != null && this.elementsToProcess.length == 1) {
101             IResource resource = this.elementsToProcess[0].getResource();
102             if (resource != null)
103                 return ResourcesPlugin.getWorkspace().getRuleFactory().modifyRule(resource);
104         }
105         return super.getSchedulingRule();
106     }
107     /**
108      * Groups the elements to be processed by their compilation unit.
109      * If parent/child combinations are present, children are
110      * discarded (only the parents are processed). Removes any
111      * duplicates specified in elements to be processed.
112      */

113     protected void groupElements() throws JavaModelException {
114         childrenToRemove = new HashMap JavaDoc(1);
115         int uniqueCUs = 0;
116         for (int i = 0, length = elementsToProcess.length; i < length; i++) {
117             IJavaElement e = elementsToProcess[i];
118             ICompilationUnit cu = getCompilationUnitFor(e);
119             if (cu == null) {
120                 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, e));
121             } else {
122                 IRegion region = (IRegion) childrenToRemove.get(cu);
123                 if (region == null) {
124                     region = new Region();
125                     childrenToRemove.put(cu, region);
126                     uniqueCUs += 1;
127                 }
128                 region.add(e);
129             }
130         }
131         elementsToProcess = new IJavaElement[uniqueCUs];
132         Iterator JavaDoc iter = childrenToRemove.keySet().iterator();
133         int i = 0;
134         while (iter.hasNext()) {
135             elementsToProcess[i++] = (IJavaElement) iter.next();
136         }
137     }
138     /**
139      * Deletes this element from its compilation unit.
140      * @see MultiOperation
141      */

142     protected void processElement(IJavaElement element) throws JavaModelException {
143         ICompilationUnit cu = (ICompilationUnit) element;
144     
145         // keep track of the import statements - if all are removed, delete
146
// the import container (and report it in the delta)
147
int numberOfImports = cu.getImports().length;
148     
149         JavaElementDelta delta = new JavaElementDelta(cu);
150         IJavaElement[] cuElements = ((IRegion) childrenToRemove.get(cu)).getElements();
151         for (int i = 0, length = cuElements.length; i < length; i++) {
152             IJavaElement e = cuElements[i];
153             if (e.exists()) {
154                 deleteElement(e, cu);
155                 delta.removed(e);
156                 if (e.getElementType() == IJavaElement.IMPORT_DECLARATION) {
157                     numberOfImports--;
158                     if (numberOfImports == 0) {
159                         delta.removed(cu.getImportContainer());
160                     }
161                 }
162             }
163         }
164         if (delta.getAffectedChildren().length > 0) {
165             cu.save(getSubProgressMonitor(1), force);
166             if (!cu.isWorkingCopy()) { // if unit is working copy, then save will have already fired the delta
167
addDelta(delta);
168                 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE);
169             }
170         }
171     }
172     /**
173      * @see MultiOperation
174      * This method first group the elements by <code>ICompilationUnit</code>,
175      * and then processes the <code>ICompilationUnit</code>.
176      */

177     protected void processElements() throws JavaModelException {
178         groupElements();
179         super.processElements();
180     }
181     /**
182      * @see MultiOperation
183      */

184     protected void verify(IJavaElement element) throws JavaModelException {
185         IJavaElement[] children = ((IRegion) childrenToRemove.get(element)).getElements();
186         for (int i = 0; i < children.length; i++) {
187             IJavaElement child = children[i];
188             if (child.getCorrespondingResource() != null)
189                 error(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, child);
190
191             if (child.isReadOnly())
192                 error(IJavaModelStatusConstants.READ_ONLY, child);
193         }
194     }
195 }
196
Popular Tags