KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > fix > ImportsCleanUp


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.ui.fix;
12
13 import java.util.Map JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17
18 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
19
20 import org.eclipse.jdt.core.ICompilationUnit;
21 import org.eclipse.jdt.core.IJavaProject;
22 import org.eclipse.jdt.core.dom.CompilationUnit;
23
24 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
25 import org.eclipse.jdt.internal.corext.fix.CleanUpConstants;
26 import org.eclipse.jdt.internal.corext.fix.IFix;
27 import org.eclipse.jdt.internal.corext.fix.ImportsFix;
28
29 import org.eclipse.jdt.ui.text.java.IProblemLocation;
30
31 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
32
33 public class ImportsCleanUp extends AbstractCleanUp {
34     
35     private CodeGenerationSettings fCodeGeneratorSettings;
36     private RefactoringStatus fStatus;
37
38     public ImportsCleanUp(Map JavaDoc options) {
39         super(options);
40     }
41     
42     public ImportsCleanUp() {
43         super();
44     }
45
46     /**
47      * {@inheritDoc}
48      */

49     public boolean requireAST(ICompilationUnit unit) throws CoreException {
50         return isEnabled(CleanUpConstants.ORGANIZE_IMPORTS);
51     }
52
53     /**
54      * {@inheritDoc}
55      */

56     public IFix createFix(final CompilationUnit compilationUnit) throws CoreException {
57         return ImportsFix.createCleanUp(compilationUnit, fCodeGeneratorSettings,
58                 isEnabled(CleanUpConstants.ORGANIZE_IMPORTS), fStatus);
59     }
60
61     /**
62      * {@inheritDoc}
63      */

64     public IFix createFix(CompilationUnit compilationUnit, IProblemLocation[] problems) throws CoreException {
65         return null;
66     }
67
68     /**
69      * {@inheritDoc}
70      */

71     public RefactoringStatus checkPreConditions(IJavaProject project, ICompilationUnit[] compilationUnits, IProgressMonitor monitor) throws CoreException {
72         RefactoringStatus result= super.checkPreConditions(project, compilationUnits, monitor);
73         
74         if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) {
75             fCodeGeneratorSettings= JavaPreferencesSettings.getCodeGenerationSettings(project);
76             fStatus= new RefactoringStatus();
77         }
78         
79         return result;
80     }
81     
82     /**
83      * {@inheritDoc}
84      */

85     public RefactoringStatus checkPostConditions(IProgressMonitor monitor) throws CoreException {
86         fCodeGeneratorSettings= null;
87         if (fStatus == null || fStatus.isOK()) {
88             return super.checkPostConditions(monitor);
89         } else {
90             return fStatus;
91         }
92     }
93
94     /**
95      * {@inheritDoc}
96      */

97     public String JavaDoc[] getDescriptions() {
98         if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS))
99             return new String JavaDoc[] {MultiFixMessages.ImportsCleanUp_OrganizeImports_Description};
100             
101         return null;
102     }
103
104     /**
105      * {@inheritDoc}
106      */

107     public String JavaDoc getPreview() {
108         StringBuffer JavaDoc buf= new StringBuffer JavaDoc();
109         
110         if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS)) {
111             buf.append("import org.model.Engine;\n"); //$NON-NLS-1$
112
} else {
113             buf.append("import org.model.*;\n"); //$NON-NLS-1$
114
}
115         
116         return buf.toString();
117     }
118
119     /**
120      * {@inheritDoc}
121      */

122     public Map JavaDoc getRequiredOptions() {
123         return null;
124     }
125
126     /**
127      * {@inheritDoc}
128      */

129     public int maximalNumberOfFixes(CompilationUnit compilationUnit) {
130         return -1;
131     }
132
133     /**
134      * {@inheritDoc}
135      */

136     public boolean needsFreshAST(CompilationUnit compilationUnit) {
137         if (isEnabled(CleanUpConstants.ORGANIZE_IMPORTS))
138             return true;
139         
140         return super.needsFreshAST(compilationUnit);
141     }
142     
143     /**
144      * {@inheritDoc}
145      */

146     public boolean canFix(CompilationUnit compilationUnit, IProblemLocation problem) throws CoreException {
147         return false;
148     }
149
150 }
151
Popular Tags