KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > text > correction > ASTRewriteCorrectionProposal


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.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 /**
34  * A proposal for quick fixes and quick assists that works on a AST rewriter.
35  * Either a rewriter is directly passed in the constructor or method {@link #getRewrite()}
36  * is overridden to provide the AST rewriter that is evaluated to the document when the
37  * proposal is applied.
38  *
39  * @since 3.2
40  */

41 public class ASTRewriteCorrectionProposal extends CUCorrectionProposal {
42
43     private ASTRewrite fRewrite;
44     private ImportRewrite fImportRewrite;
45
46     /**
47      * Constructs a AST rewrite correction proposal.
48      *
49      * @param name the display name of the proposal.
50      * @param cu the compilation unit that is modified.
51      * @param rewrite the AST rewrite that is invoked when the proposal is applied or
52      * <code>null</code> if {@link #getRewrite()} is overridden.
53      * @param relevance The relevance of this proposal.
54      * @param image The image that is displayed for this proposal or <code>null</code> if no
55      * image is desired.
56      */

57     public ASTRewriteCorrectionProposal(String JavaDoc name, ICompilationUnit cu, ASTRewrite rewrite, int relevance, Image image) {
58         super(name, cu, relevance, image);
59         fRewrite= rewrite;
60     }
61
62     /**
63      * Returns the import rewriter used for this compilation unit. <code>
64      */

65     public ImportRewrite getImportRewrite() {
66         return fImportRewrite;
67     }
68
69     /**
70      * Sets the import rewriter used for this compilation unit.
71      */

72     public void setImportRewrite(ImportRewrite rewrite) {
73         fImportRewrite= rewrite;
74     }
75     
76     /**
77      * Sets the import rewriter used for this compilation unit.
78      */

79     public ImportRewrite createImportRewrite(CompilationUnit astRoot) {
80         fImportRewrite= StubUtility.createImportRewrite(astRoot, true);
81         return fImportRewrite;
82     }
83     
84     
85     /* (non-Javadoc)
86      * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jface.text.IDocument)
87      */

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 JavaDoc 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     /**
105      * Returns the rewriter that has been passed in the constructor. Implementors can override this
106      * method to create the rewriter lazy. This method will only be called once.
107      *
108      * @return returns the rewriter to be used.
109      * @throws CoreException an exception is thrown when the rewriter could not be created.
110      */

111     protected ASTRewrite getRewrite() throws CoreException {
112         if (fRewrite == null) {
113             IStatus status= JavaUIStatus.createError(IStatus.ERROR, "Rewriter not initialized", null); //$NON-NLS-1$
114
throw new CoreException(status);
115         }
116         return fRewrite;
117     }
118 }
119
Popular Tags