KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > actions > SelectionConverter


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.ui.actions;
12
13 import java.util.Iterator;
14
15 import org.eclipse.jdt.core.IClassFile;
16 import org.eclipse.jdt.core.ICodeAssist;
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.IJavaElement;
19 import org.eclipse.jdt.core.ISourceRange;
20 import org.eclipse.jdt.core.ISourceReference;
21 import org.eclipse.jdt.core.IType;
22 import org.eclipse.jdt.core.JavaModelException;
23
24 import org.eclipse.swt.widgets.Shell;
25
26 import org.eclipse.jface.text.ITextSelection;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.ISelectionProvider;
29 import org.eclipse.jface.viewers.IStructuredSelection;
30 import org.eclipse.jface.viewers.StructuredSelection;
31
32 import org.eclipse.ui.IEditorInput;
33 import org.eclipse.ui.IWorkbenchPart;
34
35 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
36 import org.eclipse.jdt.internal.ui.JavaPlugin;
37 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
38 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
39 import org.eclipse.jdt.internal.ui.util.ExceptionHandler;
40
41 import org.eclipse.jdt.ui.IWorkingCopyManager;
42
43 public class SelectionConverter {
44
45     private static final IJavaElement[] EMPTY_RESULT= new IJavaElement[0];
46     
47     private SelectionConverter() {
48         // no instance
49
}
50
51     /**
52      * Converts the selection provided by the given part into a structured selection.
53      * The following conversion rules are used:
54      * <ul>
55      * <li><code>part instanceof JavaEditor</code>: returns a structured selection
56      * using code resolve to convert the editor's text selection.</li>
57      * <li><code>part instanceof IWorkbenchPart</code>: returns the part's selection
58      * if it is a structured selection.</li>
59      * <li><code>default</code>: returns an empty structured selection.</li>
60      * </ul>
61      */

62     public static IStructuredSelection getStructuredSelection(IWorkbenchPart part) throws JavaModelException {
63         if (part instanceof JavaEditor)
64             return new StructuredSelection(codeResolve((JavaEditor)part));
65         ISelectionProvider provider= part.getSite().getSelectionProvider();
66         if (provider != null) {
67             ISelection selection= provider.getSelection();
68             if (selection instanceof IStructuredSelection)
69                 return (IStructuredSelection)selection;
70         }
71         return StructuredSelection.EMPTY;
72     }
73
74     
75     /**
76      * Converts the given structured selection into an array of Java elements.
77      * An empty array is returned if one of the elements stored in the structured
78      * selection is not of tupe <code>IJavaElement</code>
79      */

80     public static IJavaElement[] getElements(IStructuredSelection selection) {
81         if (!selection.isEmpty()) {
82             IJavaElement[] result= new IJavaElement[selection.size()];
83             int i= 0;
84             for (Iterator iter= selection.iterator(); iter.hasNext(); i++) {
85                 Object element= iter.next();
86                 if (!(element instanceof IJavaElement))
87                     return EMPTY_RESULT;
88                 result[i]= (IJavaElement)element;
89             }
90             return result;
91         }
92         return EMPTY_RESULT;
93     }
94
95     public static boolean canOperateOn(JavaEditor editor) {
96         if (editor == null)
97             return false;
98         return getInput(editor) != null;
99         
100     }
101     
102     /**
103      * Converts the text selection provided by the given editor into an array of
104      * Java elements. If the selection doesn't cover a Java element and the selection's
105      * length is greater than 0 the methods returns the editor's input element.
106      */

107     public static IJavaElement[] codeResolveOrInput(JavaEditor editor) throws JavaModelException {
108         IJavaElement input= getInput(editor);
109         ITextSelection selection= (ITextSelection)editor.getSelectionProvider().getSelection();
110         IJavaElement[] result= codeResolve(input, selection);
111         if (result.length == 0) {
112             result= new IJavaElement[] {input};
113         }
114         return result;
115     }
116     
117     public static IJavaElement[] codeResolveOrInputHandled(JavaEditor editor, Shell shell, String title) {
118         try {
119             return codeResolveOrInput(editor);
120         } catch(JavaModelException e) {
121             ExceptionHandler.handle(e, shell, title, ActionMessages.SelectionConverter_codeResolve_failed);
122         }
123         return null;
124     }
125     
126     /**
127      * Converts the text selection provided by the given editor a Java element by
128      * asking the user if code reolve returned more than one result. If the selection
129      * doesn't cover a Java element and the selection's length is greater than 0 the
130      * methods returns the editor's input element.
131      */

132     public static IJavaElement codeResolveOrInput(JavaEditor editor, Shell shell, String title, String message) throws JavaModelException {
133         IJavaElement[] elements= codeResolveOrInput(editor);
134         if (elements == null || elements.length == 0)
135             return null;
136         IJavaElement candidate= elements[0];
137         if (elements.length > 1) {
138             candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message);
139         }
140         return candidate;
141     }
142     
143     public static IJavaElement codeResolveOrInputHandled(JavaEditor editor, Shell shell, String title, String message) {
144         try {
145             return codeResolveOrInput(editor, shell, title, message);
146         } catch (JavaModelException e) {
147             ExceptionHandler.handle(e, shell, title, ActionMessages.SelectionConverter_codeResolveOrInput_failed);
148         }
149         return null;
150     }
151         
152     public static IJavaElement[] codeResolve(JavaEditor editor) throws JavaModelException {
153         return codeResolve(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection());
154     }
155
156     /**
157      * Converts the text selection provided by the given editor a Java element by
158      * asking the user if code reolve returned more than one result. If the selection
159      * doesn't cover a Java element <code>null</code> is returned.
160      */

161     public static IJavaElement codeResolve(JavaEditor editor, Shell shell, String title, String message) throws JavaModelException {
162         IJavaElement[] elements= codeResolve(editor);
163         if (elements == null || elements.length == 0)
164             return null;
165         IJavaElement candidate= elements[0];
166         if (elements.length > 1) {
167             candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message);
168         }
169         return candidate;
170     }
171     
172     public static IJavaElement[] codeResolveHandled(JavaEditor editor, Shell shell, String title) {
173         try {
174             return codeResolve(editor);
175         } catch (JavaModelException e) {
176             ExceptionHandler.handle(e, shell, title, ActionMessages.SelectionConverter_codeResolve_failed);
177         }
178         return null;
179     }
180     
181     public static IJavaElement getElementAtOffset(JavaEditor editor) throws JavaModelException {
182         return getElementAtOffset(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection());
183     }
184     
185     public static IType getTypeAtOffset(JavaEditor editor) throws JavaModelException {
186         IJavaElement element= SelectionConverter.getElementAtOffset(editor);
187         IType type= (IType)element.getAncestor(IJavaElement.TYPE);
188         if (type == null) {
189             ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(editor);
190             if (unit != null)
191                 type= unit.findPrimaryType();
192         }
193         return type;
194     }
195     
196     public static IJavaElement getInput(JavaEditor editor) {
197         if (editor == null)
198             return null;
199         IEditorInput input= editor.getEditorInput();
200         if (input instanceof IClassFileEditorInput)
201             return ((IClassFileEditorInput)input).getClassFile();
202         IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
203         return manager.getWorkingCopy(input);
204     }
205     
206     public static ICompilationUnit getInputAsCompilationUnit(JavaEditor editor) {
207         Object editorInput= SelectionConverter.getInput(editor);
208         if (editorInput instanceof ICompilationUnit)
209             return (ICompilationUnit)editorInput;
210         else
211             return null;
212     }
213
214     public static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException {
215             if (input instanceof ICodeAssist) {
216                 if (input instanceof ICompilationUnit) {
217                     JavaModelUtil.reconcile((ICompilationUnit) input);
218                 }
219                 IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength());
220                 if (elements != null && elements.length > 0)
221                     return elements;
222             }
223             return EMPTY_RESULT;
224     }
225     
226     public static IJavaElement getElementAtOffset(IJavaElement input, ITextSelection selection) throws JavaModelException {
227         if (input instanceof ICompilationUnit) {
228             ICompilationUnit cunit= (ICompilationUnit) input;
229             JavaModelUtil.reconcile(cunit);
230             IJavaElement ref= cunit.getElementAt(selection.getOffset());
231             if (ref == null)
232                 return input;
233             else
234                 return ref;
235         } else if (input instanceof IClassFile) {
236             IJavaElement ref= ((IClassFile)input).getElementAt(selection.getOffset());
237             if (ref == null)
238                 return input;
239             else
240                 return ref;
241         }
242         return null;
243     }
244     
245 // public static IJavaElement[] resolveSelectedElements(IJavaElement input, ITextSelection selection) throws JavaModelException {
246
// IJavaElement enclosing= resolveEnclosingElement(input, selection);
247
// if (enclosing == null)
248
// return EMPTY_RESULT;
249
// if (!(enclosing instanceof ISourceReference))
250
// return EMPTY_RESULT;
251
// ISourceRange sr= ((ISourceReference)enclosing).getSourceRange();
252
// if (selection.getOffset() == sr.getOffset() && selection.getLength() == sr.getLength())
253
// return new IJavaElement[] {enclosing};
254
// }
255

256     public static IJavaElement resolveEnclosingElement(JavaEditor editor, ITextSelection selection) throws JavaModelException {
257         return resolveEnclosingElement(getInput(editor), selection);
258     }
259     
260     public static IJavaElement resolveEnclosingElement(IJavaElement input, ITextSelection selection) throws JavaModelException {
261         IJavaElement atOffset= null;
262         if (input instanceof ICompilationUnit) {
263             ICompilationUnit cunit= (ICompilationUnit)input;
264             JavaModelUtil.reconcile(cunit);
265             atOffset= cunit.getElementAt(selection.getOffset());
266         } else if (input instanceof IClassFile) {
267             IClassFile cfile= (IClassFile)input;
268             atOffset= cfile.getElementAt(selection.getOffset());
269         } else {
270             return null;
271         }
272         if (atOffset == null) {
273             return input;
274         } else {
275             int selectionEnd= selection.getOffset() + selection.getLength();
276             IJavaElement result= atOffset;
277             if (atOffset instanceof ISourceReference) {
278                 ISourceRange range= ((ISourceReference)atOffset).getSourceRange();
279                 while (range.getOffset() + range.getLength() < selectionEnd) {
280                     result= result.getParent();
281                     if (! (result instanceof ISourceReference)) {
282                         result= input;
283                         break;
284                     }
285                     range= ((ISourceReference)result).getSourceRange();
286                 }
287             }
288             return result;
289         }
290     }
291 }
292
Popular Tags