1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Map ; 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 46 public class DeleteElementsOperation extends MultiOperation { 47 53 protected Map childrenToRemove; 54 58 protected ASTParser parser; 59 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 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()); 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 96 protected String 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 113 protected void groupElements() throws JavaModelException { 114 childrenToRemove = new HashMap (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 iter = childrenToRemove.keySet().iterator(); 133 int i = 0; 134 while (iter.hasNext()) { 135 elementsToProcess[i++] = (IJavaElement) iter.next(); 136 } 137 } 138 142 protected void processElement(IJavaElement element) throws JavaModelException { 143 ICompilationUnit cu = (ICompilationUnit) element; 144 145 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()) { addDelta(delta); 168 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 169 } 170 } 171 } 172 177 protected void processElements() throws JavaModelException { 178 groupElements(); 179 super.processElements(); 180 } 181 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 |