KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > base > JavaStatusContext


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.refactoring.base;
12
13 import org.eclipse.jdt.core.IClassFile;
14 import org.eclipse.jdt.core.ICompilationUnit;
15 import org.eclipse.jdt.core.IImportDeclaration;
16 import org.eclipse.jdt.core.IMember;
17 import org.eclipse.jdt.core.IMethod;
18 import org.eclipse.jdt.core.ISourceRange;
19 import org.eclipse.jdt.core.ITypeRoot;
20 import org.eclipse.jdt.core.JavaModelException;
21 import org.eclipse.jdt.core.dom.ASTNode;
22 import org.eclipse.jdt.core.dom.IMethodBinding;
23
24 import org.eclipse.jdt.internal.corext.SourceRange;
25 import org.eclipse.jdt.internal.corext.dom.Selection;
26 import org.eclipse.ltk.core.refactoring.RefactoringStatusContext;
27
28 /**
29  * A Java element context that can be used to annotate a </code>RefactoringStatusEntry<code>
30  * with detailed information about an error detected in an <code>IJavaElement</code>.
31  */

32 public abstract class JavaStatusContext extends RefactoringStatusContext {
33
34     private static class MemberSourceContext extends JavaStatusContext {
35         private IMember fMember;
36         private MemberSourceContext(IMember member) {
37             fMember= member;
38         }
39         public boolean isBinary() {
40             return fMember.isBinary();
41         }
42         public ICompilationUnit getCompilationUnit() {
43             return fMember.getCompilationUnit();
44         }
45         public IClassFile getClassFile() {
46             return fMember.getClassFile();
47         }
48         public ISourceRange getSourceRange() {
49             try {
50                 return fMember.getSourceRange();
51             } catch (JavaModelException e) {
52                 return new SourceRange(0,0);
53             }
54         }
55     }
56     
57     private static class ImportDeclarationSourceContext extends JavaStatusContext {
58         private IImportDeclaration fImportDeclartion;
59         private ImportDeclarationSourceContext(IImportDeclaration declaration) {
60             fImportDeclartion= declaration;
61         }
62         public boolean isBinary() {
63             return false;
64         }
65         public ICompilationUnit getCompilationUnit() {
66             return (ICompilationUnit)fImportDeclartion.getParent().getParent();
67         }
68         public IClassFile getClassFile() {
69             return null;
70         }
71         public ISourceRange getSourceRange() {
72             try {
73                 return fImportDeclartion.getSourceRange();
74             } catch (JavaModelException e) {
75                 return new SourceRange(0,0);
76             }
77         }
78     }
79     
80     private static class CompilationUnitSourceContext extends JavaStatusContext {
81         private ICompilationUnit fCUnit;
82         private ISourceRange fSourceRange;
83         private CompilationUnitSourceContext(ICompilationUnit cunit, ISourceRange range) {
84             fCUnit= cunit;
85             fSourceRange= range;
86             if (fSourceRange == null)
87                 fSourceRange= new SourceRange(0,0);
88         }
89         public boolean isBinary() {
90             return false;
91         }
92         public ICompilationUnit getCompilationUnit() {
93             return fCUnit;
94         }
95         public IClassFile getClassFile() {
96             return null;
97         }
98         public ISourceRange getSourceRange() {
99             return fSourceRange;
100         }
101         public String JavaDoc toString() {
102             return getSourceRange() + " in " + super.toString(); //$NON-NLS-1$
103
}
104     }
105
106     private static class ClassFileSourceContext extends JavaStatusContext {
107         private IClassFile fClassFile;
108         private ISourceRange fSourceRange;
109         private ClassFileSourceContext(IClassFile classFile, ISourceRange range) {
110             fClassFile= classFile;
111             fSourceRange= range;
112             if (fSourceRange == null)
113                 fSourceRange= new SourceRange(0,0);
114         }
115         public boolean isBinary() {
116             return true;
117         }
118         public ICompilationUnit getCompilationUnit() {
119             return null;
120         }
121         public IClassFile getClassFile() {
122             return fClassFile;
123         }
124         public ISourceRange getSourceRange() {
125             return fSourceRange;
126         }
127         public String JavaDoc toString() {
128             return getSourceRange() + " in " + super.toString(); //$NON-NLS-1$
129
}
130     }
131     
132     /**
133      * Creates an status entry context for the given member
134      *
135      * @param member the java member for which the context is supposed
136      * to be created
137      * @return the status entry context or <code>null</code> if the
138      * context cannot be created
139      */

140     public static RefactoringStatusContext create(IMember member) {
141         if (member == null || !member.exists())
142             return null;
143         return new MemberSourceContext(member);
144     }
145     
146     /**
147      * Creates an status entry context for the given import declaration
148      *
149      * @param declaration the import declaration for which the context is
150      * supposed to be created
151      * @return the status entry context or <code>null</code> if the
152      * context cannot be created
153      */

154     public static RefactoringStatusContext create(IImportDeclaration declaration) {
155         if (declaration == null || !declaration.exists())
156             return null;
157         return new ImportDeclarationSourceContext(declaration);
158     }
159     
160     /**
161      * Creates an status entry context for the given method binding
162      *
163      * @param method the method binding for which the context is supposed to be created
164      * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
165      * context cannot be created
166      */

167     public static RefactoringStatusContext create(IMethodBinding method) {
168         return create((IMethod) method.getJavaElement());
169     }
170
171     /**
172      * Creates an status entry context for the given type root.
173      *
174      * @param typeRoot the type root containing the error
175      * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
176      * context cannot be created
177      */

178     public static RefactoringStatusContext create(ITypeRoot typeRoot) {
179         return create(typeRoot, (ISourceRange)null);
180     }
181
182     /**
183      * Creates an status entry context for the given type root and source range.
184      *
185      * @param typeRoot the type root containing the error
186      * @param range the source range that has caused the error or
187      * <code>null</code> if the source range is unknown
188      * @return the status entry context or <code>null</code> if the
189      * context cannot be created
190      */

191     public static RefactoringStatusContext create(ITypeRoot typeRoot, ISourceRange range) {
192         if (typeRoot instanceof ICompilationUnit)
193             return new CompilationUnitSourceContext((ICompilationUnit) typeRoot, range);
194         else if (typeRoot instanceof IClassFile)
195             return new ClassFileSourceContext((IClassFile) typeRoot, range);
196         else
197             return null;
198     }
199
200     /**
201      * Creates an status entry context for the given type root and AST node.
202      *
203      * @param typeRoot the type root containing the error
204      * @param node an astNode denoting the source range that has caused the error
205      *
206      * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
207      * context cannot be created
208      */

209     public static RefactoringStatusContext create(ITypeRoot typeRoot, ASTNode node) {
210         ISourceRange range= null;
211         if (node != null)
212             range= new SourceRange(node.getStartPosition(), node.getLength());
213         return create(typeRoot, range);
214     }
215
216     /**
217      * Creates an status entry context for the given type root and selection.
218      *
219      * @param typeRoot the type root containing the error
220      * @param selection a selection denoting the source range that has caused the error
221      *
222      * @return the status entry context or <code>Context.NULL_CONTEXT</code> if the
223      * context cannot be created
224      */

225     public static RefactoringStatusContext create(ITypeRoot typeRoot, Selection selection) {
226         ISourceRange range= null;
227         if (selection != null)
228             range= new SourceRange(selection.getOffset(), selection.getLength());
229         return create(typeRoot, range);
230     }
231
232     /**
233      * Returns whether this context is for a class file.
234      *
235      * @return <code>true</code> if from a class file, and <code>false</code> if
236      * from a compilation unit
237      */

238     public abstract boolean isBinary();
239     
240     /**
241      * Returns the compilation unit this context is working on. Returns <code>null</code>
242      * if the context is a binary context.
243      *
244      * @return the compilation unit
245      */

246     public abstract ICompilationUnit getCompilationUnit();
247     
248     /**
249      * Returns the class file this context is working on. Returns <code>null</code>
250      * if the context is not a binary context.
251      *
252      * @return the class file
253      */

254     public abstract IClassFile getClassFile();
255     
256     /**
257      * Returns the source range associated with this element.
258      *
259      * @return the source range
260      */

261     public abstract ISourceRange getSourceRange();
262     
263     /* (non-Javadoc)
264      * Method declared on Context.
265      */

266     public Object JavaDoc getCorrespondingElement() {
267         if (isBinary())
268             return getClassFile();
269         else
270             return getCompilationUnit();
271     }
272 }
273
274
Popular Tags