1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.Map ; 14 15 import org.eclipse.core.resources.IResource; 16 import org.eclipse.core.resources.IWorkspace; 17 import org.eclipse.core.runtime.jobs.ISchedulingRule; 18 import org.eclipse.jdt.core.ICompilationUnit; 19 import org.eclipse.jdt.core.IJavaElement; 20 import org.eclipse.jdt.core.IJavaModelStatus; 21 import org.eclipse.jdt.core.IJavaModelStatusConstants; 22 import org.eclipse.jdt.core.JavaModelException; 23 import org.eclipse.jdt.core.dom.AST; 24 import org.eclipse.jdt.core.dom.ASTNode; 25 import org.eclipse.jdt.core.dom.ASTParser; 26 import org.eclipse.jdt.core.dom.ChildListPropertyDescriptor; 27 import org.eclipse.jdt.core.dom.CompilationUnit; 28 import org.eclipse.jdt.core.dom.StructuralPropertyDescriptor; 29 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 30 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 31 import org.eclipse.jdt.internal.core.util.Util; 32 import org.eclipse.jface.text.BadLocationException; 33 import org.eclipse.jface.text.IDocument; 34 import org.eclipse.text.edits.TextEdit; 35 36 47 public abstract class CreateElementInCUOperation extends JavaModelOperation { 48 51 protected CompilationUnit cuAST; 52 56 protected static final int INSERT_LAST = 1; 57 61 protected static final int INSERT_AFTER = 2; 62 63 67 protected static final int INSERT_BEFORE = 3; 68 72 protected int insertionPolicy = INSERT_LAST; 73 80 protected IJavaElement anchorElement = null; 81 87 protected boolean creationOccurred = true; 88 92 public CreateElementInCUOperation(IJavaElement parentElement) { 93 super(null, new IJavaElement[]{parentElement}); 94 initializeDefaultPosition(); 95 } 96 protected void apply(ASTRewrite rewriter, IDocument document, Map options) throws JavaModelException { 97 TextEdit edits = rewriter.rewriteAST(document, options); 98 try { 99 edits.apply(document); 100 } catch (BadLocationException e) { 101 throw new JavaModelException(e, IJavaModelStatusConstants.INVALID_CONTENTS); 102 } 103 } 104 107 protected void checkCanceled() { 108 if (!isNested) { 109 super.checkCanceled(); 110 } 111 } 112 117 public void createAfter(IJavaElement sibling) { 118 setRelativePosition(sibling, INSERT_AFTER); 119 } 120 125 public void createBefore(IJavaElement sibling) { 126 setRelativePosition(sibling, INSERT_BEFORE); 127 } 128 134 protected void executeOperation() throws JavaModelException { 135 try { 136 beginTask(getMainTaskName(), getMainAmountOfWork()); 137 JavaElementDelta delta = newJavaElementDelta(); 138 ICompilationUnit unit = getCompilationUnit(); 139 generateNewCompilationUnitAST(unit); 140 if (this.creationOccurred) { 141 unit.save(null, false); 143 boolean isWorkingCopy = unit.isWorkingCopy(); 144 if (!isWorkingCopy) 145 setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); 146 worked(1); 147 resultElements = generateResultHandles(); 148 if (!isWorkingCopy && !Util.isExcluded(unit) 150 && unit.getParent().exists()) { 151 for (int i = 0; i < resultElements.length; i++) { 152 delta.added(resultElements[i]); 153 } 154 addDelta(delta); 155 } } 158 } finally { 159 done(); 160 } 161 } 162 163 166 protected abstract StructuralPropertyDescriptor getChildPropertyDescriptor(ASTNode parent); 167 168 171 protected abstract ASTNode generateElementAST(ASTRewrite rewriter, IDocument document, ICompilationUnit cu) throws JavaModelException; 172 175 protected void generateNewCompilationUnitAST(ICompilationUnit cu) throws JavaModelException { 176 this.cuAST = parse(cu); 177 178 AST ast = this.cuAST.getAST(); 179 ASTRewrite rewriter = ASTRewrite.create(ast); 180 IDocument document = getDocument(cu); 181 ASTNode child = generateElementAST(rewriter, document, cu); 182 if (child != null) { 183 ASTNode parent = ((JavaElement) getParentElement()).findNode(this.cuAST); 184 if (parent == null) 185 parent = this.cuAST; 186 insertASTNode(rewriter, parent, child); 187 apply(rewriter, document, cu.getJavaProject().getOptions(true)); 188 } 189 worked(1); 190 } 191 194 protected abstract IJavaElement generateResultHandle(); 195 198 protected IJavaElement[] generateResultHandles() { 199 return new IJavaElement[]{generateResultHandle()}; 200 } 201 204 protected ICompilationUnit getCompilationUnit() { 205 return getCompilationUnitFor(getParentElement()); 206 } 207 211 protected int getMainAmountOfWork(){ 212 return 2; 213 } 214 218 public abstract String getMainTaskName(); 219 220 protected ISchedulingRule getSchedulingRule() { 221 IResource resource = getCompilationUnit().getResource(); 222 IWorkspace workspace = resource.getWorkspace(); 223 return workspace.getRuleFactory().modifyRule(resource); 224 } 225 231 protected void initializeDefaultPosition() { 232 } 235 242 protected void insertASTNode(ASTRewrite rewriter, ASTNode parent, ASTNode child) throws JavaModelException { 243 StructuralPropertyDescriptor propertyDescriptor = getChildPropertyDescriptor(parent); 244 if (propertyDescriptor instanceof ChildListPropertyDescriptor) { 245 ChildListPropertyDescriptor childListPropertyDescriptor = (ChildListPropertyDescriptor) propertyDescriptor; 246 ListRewrite rewrite = rewriter.getListRewrite(parent, childListPropertyDescriptor); 247 switch (this.insertionPolicy) { 248 case INSERT_BEFORE: 249 ASTNode element = ((JavaElement) this.anchorElement).findNode(this.cuAST); 250 if (childListPropertyDescriptor.getElementType().isAssignableFrom(element.getClass())) 251 rewrite.insertBefore(child, element, null); 252 else 253 rewrite.insertLast(child, null); 255 break; 256 case INSERT_AFTER: 257 element = ((JavaElement) this.anchorElement).findNode(this.cuAST); 258 if (childListPropertyDescriptor.getElementType().isAssignableFrom(element.getClass())) 259 rewrite.insertAfter(child, element, null); 260 else 261 rewrite.insertLast(child, null); 263 break; 264 case INSERT_LAST: 265 rewrite.insertLast(child, null); 266 break; 267 } 268 } else { 269 rewriter.set(parent, propertyDescriptor, child, null); 270 } 271 } 272 protected CompilationUnit parse(ICompilationUnit cu) throws JavaModelException { 273 cu.makeConsistent(this.progressMonitor); 275 ASTParser parser = ASTParser.newParser(AST.JLS3); 277 parser.setSource(cu); 278 return (CompilationUnit) parser.createAST(this.progressMonitor); 279 } 280 286 protected void setAlteredName(String newName) { 287 } 289 295 protected void setRelativePosition(IJavaElement sibling, int policy) throws IllegalArgumentException { 296 if (sibling == null) { 297 this.anchorElement = null; 298 this.insertionPolicy = INSERT_LAST; 299 } else { 300 this.anchorElement = sibling; 301 this.insertionPolicy = policy; 302 } 303 } 304 315 public IJavaModelStatus verify() { 316 if (getParentElement() == null) { 317 return new JavaModelStatus(IJavaModelStatusConstants.NO_ELEMENTS_TO_PROCESS); 318 } 319 if (this.anchorElement != null) { 320 IJavaElement domPresentParent = this.anchorElement.getParent(); 321 if (domPresentParent.getElementType() == IJavaElement.IMPORT_CONTAINER) { 322 domPresentParent = domPresentParent.getParent(); 323 } 324 if (!domPresentParent.equals(getParentElement())) { 325 return new JavaModelStatus(IJavaModelStatusConstants.INVALID_SIBLING, this.anchorElement); 326 } 327 } 328 return JavaModelStatus.VERIFIED_OK; 329 } 330 } 331 | Popular Tags |