KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > codemanipulation > ImportRewrite


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.codemanipulation;
12
13 import org.eclipse.text.edits.TextEdit;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21
22 import org.eclipse.jdt.core.ICompilationUnit;
23 import org.eclipse.jdt.core.dom.AST;
24 import org.eclipse.jdt.core.dom.IBinding;
25 import org.eclipse.jdt.core.dom.ITypeBinding;
26 import org.eclipse.jdt.core.dom.Type;
27
28 import org.eclipse.jdt.internal.corext.Assert;
29
30 import org.eclipse.jdt.internal.ui.JavaUIStatus;
31 import org.eclipse.jdt.internal.ui.preferences.JavaPreferencesSettings;
32
33 /**
34  * A rewriter for imports that considers the organize import
35  * settings.
36  */

37 public final class ImportRewrite {
38     
39     private ImportsStructure fImportsStructure;
40     
41     private ImportRewrite(ICompilationUnit cu, String JavaDoc[] preferenceOrder, int importThreshold) throws CoreException {
42         Assert.isNotNull(cu);
43         Assert.isNotNull(preferenceOrder);
44         fImportsStructure= new ImportsStructure(cu, preferenceOrder, importThreshold, true);
45     }
46     
47     /**
48      * Creates a import rewriter with the settings as configured in the preferences
49      * @param cunit The compilation unit that contains the imports to change.
50      * @throws CoreException
51      */

52     public ImportRewrite(ICompilationUnit cunit) throws CoreException {
53         this(cunit, JavaPreferencesSettings.getImportOrderPreference(cunit.getJavaProject()), JavaPreferencesSettings.getImportNumberThreshold(cunit.getJavaProject()));
54     }
55     
56     /**
57      * @deprecated Use #createEdit(IDocument, IProgressMonitor) instead
58      */

59     public final TextEdit createEdit(IDocument document) throws CoreException {
60         return createEdit(document, null);
61     }
62     
63     public final TextEdit createEdit(IDocument document, IProgressMonitor monitor) throws CoreException {
64         try {
65             return fImportsStructure.getResultingEdits(document, monitor);
66         } catch (BadLocationException e) {
67             throw new CoreException(JavaUIStatus.createError(IStatus.ERROR, e));
68         }
69     }
70             
71     public ICompilationUnit getCompilationUnit() {
72         return fImportsStructure.getCompilationUnit();
73     }
74     
75     /**
76      * @see ImportsStructure#setFilterImplicitImports(boolean)
77      */

78     public void setFilterImplicitImports(boolean filterImplicitImports) {
79         fImportsStructure.setFilterImplicitImports(filterImplicitImports);
80     }
81     
82     /**
83      * @see ImportsStructure#setFindAmbiguousImports(boolean)
84      */

85     public void setFindAmbiguosImports(boolean findAmbiguosImports) {
86         fImportsStructure.setFindAmbiguousImports(findAmbiguosImports);
87     }
88     
89     /**
90      * Adds a new import declaration that is sorted in the structure using
91      * a best match algorithm. If an import already exists, the import is
92      * not added. This method does not correctly handle type parameters nor type variables.
93      * @param qualifiedTypeName The fully qualified name of the type to import
94      * @return Returns the simple type name that can be used in the code or the
95      * fully qualified type name if an import conflict prevented the import.
96      * The type name can contain dimensions.
97      */

98     public String JavaDoc addImport(String JavaDoc qualifiedTypeName) {
99         return fImportsStructure.addImport(qualifiedTypeName);
100     }
101     
102     /**
103      * Adds a new static import declaration that is sorted in the structure using
104      * a best match algorithm.
105      * @param qualifiedName The name of the static member type
106      * @param selector The name of the static member
107      * @return Returns the simple type name that can be used in the code or the
108      * fully qualified type name if an import conflict prevented the import.
109      * The type name can contain dimensions.
110      */

111     public String JavaDoc addStaticImport(String JavaDoc qualifiedName, String JavaDoc selector, boolean isField) {
112         return fImportsStructure.addStaticImport(qualifiedName, selector, isField);
113     }
114     
115     /**
116      * Adds a new static import declaration that is sorted in the structure using
117      * a best match algorithm.
118      * @param binding The binding of the member to be added
119      * @return Returns the simple name that can be used in the code or the
120      * fully qualified type name if an import conflict prevented the import.
121      */

122     public String JavaDoc addStaticImport(IBinding binding) {
123         return fImportsStructure.addStaticImport(binding);
124     }
125     
126     /**
127      * Adds a new import declaration that is sorted in the structure using
128      * a best match algorithm. If an import already exists, the import is
129      * not added. The type binding can be an array binding, type variable or wildcard.
130      * If the binding is a generic type, the type parameters are ignored. For parametrized types, also the type
131      * arguments are processed and imports added if necessary.
132      * @param binding The type binding of the type to be added
133      * @return Returns the unqualified type if the import could be added or a fully qualified type if
134      * an import conflict prevented the import. The returned string represents a type to which the type binding can
135      * be assigned or casted to. Anonymous types inside type arguments are normalized to their base type, wildcard
136      * of wildcards are ignored.
137      */

138     public String JavaDoc addImport(ITypeBinding binding) {
139         return fImportsStructure.addImport(binding);
140     }
141     
142     
143     /**
144      * Adds a new import declaration that is sorted in the structure using
145      * a best match algorithm. If an import already exists, the import is
146      * not added. The type binding can also be an array binding, type variable or wildcard.
147      * If the binding is a generic type, the type parameters are ignored. For parametrized types, also the type
148      * arguments are processed and imports added if necessary.
149      * @param binding The type binding of the type to be added
150      * @param ast The ast to create the node for
151      * @return Returns the unqualified type if the import could be added or a fully qualified type name if
152      * an import conflict prevented the import. The returned type node represents a type to which the type binding can
153      * be assigned or casted to. Anonymous types inside type arguments are normalized to their base type, wildcard of
154      * wildcards are ignored.
155      */

156     public Type addImport(ITypeBinding binding, AST ast) {
157         return fImportsStructure.addImport(binding, ast);
158     }
159     
160     /**
161      * Adds a new import declaration that is sorted in the structure using
162      * a best match algorithm. If an import already exists, the import is
163      * not added. The type signature can be an array binding, type variable or wildcard.
164      * If the type is a generic type, the type parameters are ignored. For parametrized types, also the type
165      * arguments are processed and imports added if necessary.
166      * @param typeSig The type name in signature notations (See {@link org.eclipse.jdt.core.Signature}).
167      * @param ast The ast to create the node for
168      * @return Returns the unqualified type if the import could be added or a fully qualified type name if
169      * an import conflict prevented the import. The returned type node represents a type to which the type binding can
170      * be assigned or casted to. Anonymous types inside type arguments are normalized to their base type, wildcard of
171      * wildcards are ignored.
172      */

173     public Type addImportFromSignature(String JavaDoc typeSig, AST ast) {
174         return fImportsStructure.addImportFromSignature(typeSig, ast);
175     }
176     
177     /**
178      * Looks if there already is single import for the given name.
179      * @param simpleName The simple name to find
180      * @return Returns the qualified import name or <code>null</code>.
181      */

182     public String JavaDoc findImport(String JavaDoc simpleName) {
183         return fImportsStructure.findImport(simpleName);
184     }
185     
186
187     /**
188      * Removes an import declaration if it exists. Does not touch on-demand imports.
189      * @param binding The type binding of the type to be removed as import
190      * @return Returns true if an import for the given type existed.
191      */

192     public boolean removeImport(ITypeBinding binding) {
193         return fImportsStructure.removeImport(binding);
194     }
195     
196     /**
197      * Removes an import declaration for a type or an on-demand import.
198      * @param qualifiedTypeName The qualified name the type to be removed as import
199      * @return Returns true if an import for the given type existed.
200      */

201     public boolean removeImport(String JavaDoc qualifiedTypeName) {
202         return fImportsStructure.removeImport(qualifiedTypeName);
203     }
204     
205     /**
206      * Removes a static import declaration for a static member or a static on-demand import.
207      * @param qualifiedName The qualified name the static member to be removed as import
208      * @return Returns true if an import for the given type existed.
209      */

210     public boolean removeStaticImport(String JavaDoc qualifiedName) {
211         return fImportsStructure.removeStaticImport(qualifiedName);
212     }
213     
214     /**
215      * Returns <code>true</code> if the import edit will not change the import
216      * container; otherwise <code>false</code> is returned.
217      *
218      * @return <code>true</code> if the import edit will not change the import
219      * container; otherwise <code>false</code> is returned
220      */

221     public boolean isEmpty() {
222         return !fImportsStructure.hasChanges();
223     }
224
225     public String JavaDoc[] getCreatedImports() {
226         return fImportsStructure.getCreatedImports();
227     }
228     
229     public String JavaDoc[] getCreatedStaticImports() {
230         return fImportsStructure.getCreatedStaticImports();
231     }
232 }
233
Popular Tags