1 11 package org.eclipse.jdt.internal.ui.text.correction; 12 13 import org.eclipse.text.edits.TextEdit; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.core.runtime.IStatus; 17 import org.eclipse.core.runtime.NullProgressMonitor; 18 19 import org.eclipse.swt.graphics.Image; 20 21 import org.eclipse.jface.text.IDocument; 22 23 import org.eclipse.jdt.core.ICompilationUnit; 24 import org.eclipse.jdt.core.dom.CompilationUnit; 25 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 26 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; 27 28 import org.eclipse.jdt.internal.corext.codemanipulation.StubUtility; 29 30 31 import org.eclipse.jdt.internal.ui.JavaUIStatus; 32 33 41 public class ASTRewriteCorrectionProposal extends CUCorrectionProposal { 42 43 private ASTRewrite fRewrite; 44 private ImportRewrite fImportRewrite; 45 46 57 public ASTRewriteCorrectionProposal(String name, ICompilationUnit cu, ASTRewrite rewrite, int relevance, Image image) { 58 super(name, cu, relevance, image); 59 fRewrite= rewrite; 60 } 61 62 65 public ImportRewrite getImportRewrite() { 66 return fImportRewrite; 67 } 68 69 72 public void setImportRewrite(ImportRewrite rewrite) { 73 fImportRewrite= rewrite; 74 } 75 76 79 public ImportRewrite createImportRewrite(CompilationUnit astRoot) { 80 fImportRewrite= StubUtility.createImportRewrite(astRoot, true); 81 return fImportRewrite; 82 } 83 84 85 88 protected void addEdits(IDocument document, TextEdit editRoot) throws CoreException { 89 super.addEdits(document, editRoot); 90 ASTRewrite rewrite= getRewrite(); 91 if (rewrite != null) { 92 try { 93 TextEdit edit= rewrite.rewriteAST(); 94 editRoot.addChild(edit); 95 } catch (IllegalArgumentException e) { 96 throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e)); 97 } 98 } 99 if (fImportRewrite != null) { 100 editRoot.addChild(fImportRewrite.rewriteImports(new NullProgressMonitor())); 101 } 102 } 103 104 111 protected ASTRewrite getRewrite() throws CoreException { 112 if (fRewrite == null) { 113 IStatus status= JavaUIStatus.createError(IStatus.ERROR, "Rewriter not initialized", null); throw new CoreException(status); 115 } 116 return fRewrite; 117 } 118 } 119 | Popular Tags |