KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > infoviews > TextSelectionConverter


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.infoviews;
12
13 import org.eclipse.jface.text.ITextSelection;
14
15 import org.eclipse.ui.IEditorInput;
16
17 import org.eclipse.jdt.core.IClassFile;
18 import org.eclipse.jdt.core.ICodeAssist;
19 import org.eclipse.jdt.core.ICompilationUnit;
20 import org.eclipse.jdt.core.IJavaElement;
21 import org.eclipse.jdt.core.JavaModelException;
22
23 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
24
25 import org.eclipse.jdt.ui.IWorkingCopyManager;
26
27 import org.eclipse.jdt.internal.ui.JavaPlugin;
28 import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
29 import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;
30
31 /**
32  * Helper class to convert text selections to Java elements.
33  *
34  * @since 3.0
35  */

36 class TextSelectionConverter {
37
38     /** Empty result. */
39     private static final IJavaElement[] EMPTY_RESULT= new IJavaElement[0];
40
41     /** Prevent instance creation. */
42     private TextSelectionConverter() {
43     }
44
45     /**
46      * Finds and returns the Java elements for the given editor selection.
47      *
48      * @param editor the Java editor
49      * @param selection the text selection
50      * @return the Java elements for the given editor selection
51      * @throws JavaModelException
52      */

53     public static IJavaElement[] codeResolve(JavaEditor editor, ITextSelection selection) throws JavaModelException {
54         return codeResolve(getInput(editor), selection);
55     }
56
57     /**
58      * Finds and returns the Java element that contains the
59      * text selection in the given editor.
60      *
61      * @param editor the Java editor
62      * @param selection the text selection
63      * @return the Java elements for the given editor selection
64      * @throws JavaModelException
65      */

66     public static IJavaElement getElementAtOffset(JavaEditor editor, ITextSelection selection) throws JavaModelException {
67         return getElementAtOffset(getInput(editor), selection);
68     }
69
70     //-------------------- Helper methods --------------------
71

72     private static IJavaElement getInput(JavaEditor editor) {
73         if (editor == null)
74             return null;
75         IEditorInput input= editor.getEditorInput();
76         if (input instanceof IClassFileEditorInput)
77             return ((IClassFileEditorInput)input).getClassFile();
78         IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();
79         return manager.getWorkingCopy(input);
80     }
81
82     private static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException {
83             if (input instanceof ICodeAssist) {
84                 if (input instanceof ICompilationUnit) {
85                     ICompilationUnit cunit= (ICompilationUnit)input;
86                     if (cunit.isWorkingCopy())
87                         JavaModelUtil.reconcile(cunit);
88                 }
89                 IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength());
90                 if (elements != null && elements.length > 0)
91                     return elements;
92             }
93             return EMPTY_RESULT;
94     }
95
96     private static IJavaElement getElementAtOffset(IJavaElement input, ITextSelection selection) throws JavaModelException {
97         if (input instanceof ICompilationUnit) {
98             ICompilationUnit cunit= (ICompilationUnit)input;
99             if (cunit.isWorkingCopy())
100                 JavaModelUtil.reconcile(cunit);
101             IJavaElement ref= cunit.getElementAt(selection.getOffset());
102             if (ref == null)
103                 return input;
104             else
105                 return ref;
106         } else if (input instanceof IClassFile) {
107             IJavaElement ref= ((IClassFile)input).getElementAt(selection.getOffset());
108             if (ref == null)
109                 return input;
110             else
111                 return ref;
112         }
113         return null;
114     }
115 }
116
Popular Tags