Fix hover functionality.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpdt / internal / ui / actions / SelectionConverter.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
12
13 import java.util.Iterator;
14
15 import net.sourceforge.phpdt.core.ICompilationUnit;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IType;
18 import net.sourceforge.phpdt.core.JavaModelException;
19 import net.sourceforge.phpdt.internal.ui.util.ExceptionHandler;
20 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
21 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
22 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
23 import net.sourceforge.phpeclipse.ui.WebUI;
24
25 import org.eclipse.jface.text.ITextSelection;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.jface.viewers.IStructuredSelection;
29 import org.eclipse.jface.viewers.StructuredSelection;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.IWorkbenchPart;
33
34 public class SelectionConverter {
35
36         private static final IJavaElement[] EMPTY_RESULT = new IJavaElement[0];
37
38         private SelectionConverter() {
39                 // no instance
40         }
41
42         /**
43          * Converts the selection provided by the given part into a structured
44          * selection. The following conversion rules are used:
45          * <ul>
46          * <li><code>part instanceof PHPEditor</code>: returns a structured
47          * selection using code resolve to convert the editor's text selection.</li>
48          * <li><code>part instanceof IWorkbenchPart</code>: returns the part's
49          * selection if it is a structured selection.</li>
50          * <li><code>default</code>: returns an empty structured selection.</li>
51          * </ul>
52          */
53         public static IStructuredSelection getStructuredSelection(
54                         IWorkbenchPart part) throws JavaModelException {
55                 if (part instanceof PHPEditor)
56                         return new StructuredSelection(codeResolve((PHPEditor) part));
57                 ISelectionProvider provider = part.getSite().getSelectionProvider();
58                 if (provider != null) {
59                         ISelection selection = provider.getSelection();
60                         if (selection instanceof IStructuredSelection)
61                                 return (IStructuredSelection) selection;
62                 }
63                 return StructuredSelection.EMPTY;
64         }
65
66         /**
67          * Converts the given structured selection into an array of Java elements.
68          * An empty array is returned if one of the elements stored in the
69          * structured selection is not of tupe <code>IJavaElement</code>
70          */
71         public static IJavaElement[] getElements(IStructuredSelection selection) {
72                 if (!selection.isEmpty()) {
73                         IJavaElement[] result = new IJavaElement[selection.size()];
74                         int i = 0;
75                         for (Iterator iter = selection.iterator(); iter.hasNext(); i++) {
76                                 Object element = (Object) iter.next();
77                                 if (!(element instanceof IJavaElement))
78                                         return EMPTY_RESULT;
79                                 result[i] = (IJavaElement) element;
80                         }
81                         return result;
82                 }
83                 return EMPTY_RESULT;
84         }
85
86         public static boolean canOperateOn(PHPEditor editor) {
87                 if (editor == null)
88                         return false;
89                 return getInput(editor) != null;
90
91         }
92
93         /**
94          * Converts the text selection provided by the given editor into an array of
95          * Java elements. If the selection doesn't cover a Java element and the
96          * selection's length is greater than 0 the methods returns the editor's
97          * input element.
98          */
99         public static IJavaElement[] codeResolveOrInput(PHPEditor editor)
100                         throws JavaModelException {
101                 IJavaElement input = getInput(editor);
102                 ITextSelection selection = (ITextSelection) editor
103                                 .getSelectionProvider().getSelection();
104                 IJavaElement[] result = codeResolve(input, selection);
105                 if (result.length == 0) {
106                         result = new IJavaElement[] { input };
107                 }
108                 return result;
109         }
110
111         public static IJavaElement[] codeResolveOrInputHandled(PHPEditor editor,
112                         Shell shell, String title) {
113                 try {
114                         return codeResolveOrInput(editor);
115                 } catch (JavaModelException e) {
116                         ExceptionHandler.handle(e, shell, title, ActionMessages
117                                         .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
118                 }
119                 return null;
120         }
121
122         /**
123          * Converts the text selection provided by the given editor a Java element
124          * by asking the user if code reolve returned more than one result. If the
125          * selection doesn't cover a Java element and the selection's length is
126          * greater than 0 the methods returns the editor's input element.
127          */
128         public static IJavaElement codeResolveOrInput(PHPEditor editor,
129                         Shell shell, String title, String message)
130                         throws JavaModelException {
131                 IJavaElement[] elements = codeResolveOrInput(editor);
132                 if (elements == null || elements.length == 0)
133                         return null;
134                 IJavaElement candidate = elements[0];
135                 if (elements.length > 1) {
136                         candidate = OpenActionUtil.selectJavaElement(elements, shell,
137                                         title, message);
138                 }
139                 return candidate;
140         }
141
142         public static IJavaElement codeResolveOrInputHandled(PHPEditor editor,
143                         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
148                                         .getString("SelectionConverter.codeResolveOrInput_failed")); //$NON-NLS-1$
149                 }
150                 return null;
151         }
152
153         public static IJavaElement[] codeResolve(PHPEditor editor)
154                         throws JavaModelException {
155                 return codeResolve(getInput(editor), (ITextSelection) editor
156                                 .getSelectionProvider().getSelection());
157         }
158
159         /**
160          * Converts the text selection provided by the given editor a Java element
161          * by asking the user if code reolve returned more than one result. If the
162          * selection doesn't cover a Java element <code>null</code> is returned.
163          */
164         public static IJavaElement codeResolve(PHPEditor editor, Shell shell,
165                         String title, String message) throws JavaModelException {
166                 IJavaElement[] elements = codeResolve(editor);
167                 if (elements == null || elements.length == 0)
168                         return null;
169                 IJavaElement candidate = elements[0];
170                 if (elements.length > 1) {
171                         candidate = OpenActionUtil.selectJavaElement(elements, shell,
172                                         title, message);
173                 }
174                 return candidate;
175         }
176
177         public static IJavaElement[] codeResolveHandled(PHPEditor editor,
178                         Shell shell, String title) {
179                 try {
180                         return codeResolve(editor);
181                 } catch (JavaModelException e) {
182                         ExceptionHandler.handle(e, shell, title, ActionMessages
183                                         .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
184                 }
185                 return null;
186         }
187
188         public static IJavaElement getElementAtOffset(PHPEditor editor)
189                         throws JavaModelException {
190                 return getElementAtOffset(getInput(editor), (ITextSelection) editor
191                                 .getSelectionProvider().getSelection());
192         }
193
194         public static IType getTypeAtOffset(PHPEditor editor)
195                         throws JavaModelException {
196                 IJavaElement element = SelectionConverter.getElementAtOffset(editor);
197                 IType type = (IType) element.getAncestor(IJavaElement.TYPE);
198                 if (type == null) {
199                         ICompilationUnit unit = SelectionConverter
200                                         .getInputAsCompilationUnit(editor);
201                         if (unit != null)
202                                 type = unit.findPrimaryType();
203                 }
204                 return type;
205         }
206
207         public static IJavaElement getInput(PHPEditor editor) {
208                 if (editor == null)
209                         return null;
210                 IEditorInput input = editor.getEditorInput();
211                 // if (input instanceof IClassFileEditorInput)
212                 // return ((IClassFileEditorInput)input).getClassFile();
213                 IWorkingCopyManager manager = WebUI.getDefault()
214                                 .getWorkingCopyManager();
215                 return manager.getWorkingCopy(input);
216         }
217
218         public static ICompilationUnit getInputAsCompilationUnit(PHPEditor editor) {
219                 Object editorInput = SelectionConverter.getInput(editor);
220                 if (editorInput instanceof ICompilationUnit)
221                         return (ICompilationUnit) editorInput;
222                 else
223                         return null;
224         }
225
226         private static IJavaElement[] codeResolve(IJavaElement input,
227                         ITextSelection selection) throws JavaModelException {
228                 // if (input instanceof ICodeAssist) {
229                 // IJavaElement[] elements=
230                 // ((ICodeAssist)input).codeSelect(selection.getOffset(),
231                 // selection.getLength());
232                 // if (elements != null && elements.length > 0)
233                 // return elements;
234                 // }
235                 return EMPTY_RESULT;
236         }
237
238         private static IJavaElement getElementAtOffset(IJavaElement input,
239                         ITextSelection selection) throws JavaModelException {
240                 if (input instanceof ICompilationUnit) {
241                         ICompilationUnit cunit = (ICompilationUnit) input;
242                         if (cunit.isWorkingCopy()) {
243                                 synchronized (cunit) {
244                                         cunit.reconcile();
245                                 }
246                         }
247                         IJavaElement ref = cunit.getElementAt(selection.getOffset());
248                         if (ref == null)
249                                 return input;
250                         else
251                                 return ref;
252                 }
253                 // else if (input instanceof IClassFile) {
254                 // IJavaElement ref=
255                 // ((IClassFile)input).getElementAt(selection.getOffset());
256                 // if (ref == null)
257                 // return input;
258                 // else
259                 // return ref;
260                 // }
261                 return null;
262         }
263 }