KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > fix > AbstractFix


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.corext.fix;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.text.edits.TextEditGroup;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IStatus;
21
22 import org.eclipse.ltk.core.refactoring.CategorizedTextEditGroup;
23 import org.eclipse.ltk.core.refactoring.GroupCategory;
24 import org.eclipse.ltk.core.refactoring.GroupCategorySet;
25 import org.eclipse.ltk.core.refactoring.TextChange;
26
27 import org.eclipse.jdt.core.ICompilationUnit;
28 import org.eclipse.jdt.core.dom.ASTNode;
29 import org.eclipse.jdt.core.dom.CompilationUnit;
30 import org.eclipse.jdt.core.dom.ITypeBinding;
31 import org.eclipse.jdt.core.dom.Type;
32 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite;
33 import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext;
34
35 import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
36 import org.eclipse.jdt.internal.corext.refactoring.changes.CompilationUnitChange;
37 import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite;
38
39 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo;
40
41
42 public abstract class AbstractFix implements IFix {
43     
44     public static abstract class AbstractFixRewriteOperation implements IFixRewriteOperation {
45         
46         protected Type importType(final ITypeBinding toImport, final ASTNode accessor, ImportRewrite imports, final CompilationUnit compilationUnit) {
47             ImportRewriteContext importContext= new ContextSensitiveImportRewriteContext(compilationUnit, accessor.getStartPosition(), imports);
48             return imports.addImport(toImport, compilationUnit.getAST(), importContext);
49         }
50         
51         protected TextEditGroup createTextEditGroup(String JavaDoc label) {
52             if (label.length() > 0){
53                 return new CategorizedTextEditGroup(label, new GroupCategorySet(new GroupCategory(label, label, label)));
54             } else {
55                 return new TextEditGroup(label);
56             }
57         }
58
59         public String JavaDoc getAdditionalInfo() {
60             return null;
61         }
62     }
63     
64     private final String JavaDoc fName;
65     private final ICompilationUnit fCompilationUnit;
66     private final IFixRewriteOperation[] fFixRewrites;
67     private final CompilationUnit fUnit;
68     private IStatus fStatus;
69     
70     protected AbstractFix(String JavaDoc name, CompilationUnit compilationUnit, IFixRewriteOperation[] fixRewriteOperations) {
71         fName= name;
72         fCompilationUnit= (ICompilationUnit)compilationUnit.getJavaElement();
73         fFixRewrites= fixRewriteOperations;
74         fUnit= compilationUnit;
75         fStatus= StatusInfo.OK_STATUS;
76     }
77
78     /* (non-Javadoc)
79      * @see org.eclipse.jdt.internal.corext.fix.IFix#getName()
80      */

81     public String JavaDoc getDescription() {
82         return fName;
83     }
84
85     /* (non-Javadoc)
86      * @see org.eclipse.jdt.internal.corext.fix.IFix#getCompilationUnit()
87      */

88     public ICompilationUnit getCompilationUnit() {
89         return fCompilationUnit;
90     }
91     
92     /* (non-Javadoc)
93      * @see org.eclipse.jdt.internal.corext.fix.IFix#createChange()
94      */

95     public TextChange createChange() throws CoreException {
96         if (fFixRewrites == null || fFixRewrites.length == 0)
97             return null;
98
99         CompilationUnitRewrite cuRewrite= new CompilationUnitRewrite(getCompilationUnit(), fUnit);
100
101         List JavaDoc/*<TextEditGroup>*/ groups= new ArrayList JavaDoc();
102         
103         for (int i= 0; i < fFixRewrites.length; i++) {
104             fFixRewrites[i].rewriteAST(cuRewrite, groups);
105         }
106         
107         CompilationUnitChange result= cuRewrite.createChange(getDescription(), true, null);
108         if (result == null)
109             return null;
110         
111         for (Iterator JavaDoc iter= groups.iterator(); iter.hasNext();) {
112             TextEditGroup group= (TextEditGroup)iter.next();
113             result.addTextEditGroup(group);
114         }
115         return result;
116     }
117     
118     public String JavaDoc getAdditionalInfo(){
119         StringBuffer JavaDoc sb= new StringBuffer JavaDoc();
120         for (int i = 0; i < fFixRewrites.length; i++) {
121             IFixRewriteOperation frw = fFixRewrites[i];
122             if (frw instanceof AbstractFixRewriteOperation) {
123                 AbstractFixRewriteOperation fro= (AbstractFixRewriteOperation) frw;
124                 String JavaDoc info= fro.getAdditionalInfo();
125                 if (info != null)
126                     sb.append(info);
127             }
128         }
129         if (sb.length() > 0) {
130             return sb.toString();
131         }
132         return null;
133     }
134     
135     /* (non-Javadoc)
136      * @see org.eclipse.jdt.internal.corext.fix.IFix#getStatus()
137      */

138     public IStatus getStatus() {
139         return fStatus;
140     }
141     
142     public void setStatus(IStatus status) {
143         fStatus= status;
144     }
145 }
146
Popular Tags