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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.actions;
13 import java.util.Iterator;
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;
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;
34 public class SelectionConverter {
36 private static final IJavaElement[] EMPTY_RESULT = new IJavaElement[0];
38 private SelectionConverter() {
43 * Converts the selection provided by the given part into a structured
44 * selection. The following conversion rules are used:
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>
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;
63 return StructuredSelection.EMPTY;
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>
71 public static IJavaElement[] getElements(IStructuredSelection selection) {
72 if (!selection.isEmpty()) {
73 IJavaElement[] result = new IJavaElement[selection.size()];
75 for (Iterator iter = selection.iterator(); iter.hasNext(); i++) {
76 Object element = (Object) iter.next();
77 if (!(element instanceof IJavaElement))
79 result[i] = (IJavaElement) element;
86 public static boolean canOperateOn(PHPEditor editor) {
89 return getInput(editor) != null;
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
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 };
111 public static IJavaElement[] codeResolveOrInputHandled(PHPEditor editor,
112 Shell shell, String title) {
114 return codeResolveOrInput(editor);
115 } catch (JavaModelException e) {
116 ExceptionHandler.handle(e, shell, title, ActionMessages
117 .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
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.
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)
134 IJavaElement candidate = elements[0];
135 if (elements.length > 1) {
136 candidate = OpenActionUtil.selectJavaElement(elements, shell,
142 public static IJavaElement codeResolveOrInputHandled(PHPEditor editor,
143 Shell shell, String title, String message) {
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$
153 public static IJavaElement[] codeResolve(PHPEditor editor)
154 throws JavaModelException {
155 return codeResolve(getInput(editor), (ITextSelection) editor
156 .getSelectionProvider().getSelection());
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.
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)
169 IJavaElement candidate = elements[0];
170 if (elements.length > 1) {
171 candidate = OpenActionUtil.selectJavaElement(elements, shell,
177 public static IJavaElement[] codeResolveHandled(PHPEditor editor,
178 Shell shell, String title) {
180 return codeResolve(editor);
181 } catch (JavaModelException e) {
182 ExceptionHandler.handle(e, shell, title, ActionMessages
183 .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
188 public static IJavaElement getElementAtOffset(PHPEditor editor)
189 throws JavaModelException {
190 return getElementAtOffset(getInput(editor), (ITextSelection) editor
191 .getSelectionProvider().getSelection());
194 public static IType getTypeAtOffset(PHPEditor editor)
195 throws JavaModelException {
196 IJavaElement element = SelectionConverter.getElementAtOffset(editor);
197 IType type = (IType) element.getAncestor(IJavaElement.TYPE);
199 ICompilationUnit unit = SelectionConverter
200 .getInputAsCompilationUnit(editor);
202 type = unit.findPrimaryType();
207 public static IJavaElement getInput(PHPEditor editor) {
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);
218 public static ICompilationUnit getInputAsCompilationUnit(PHPEditor editor) {
219 Object editorInput = SelectionConverter.getInput(editor);
220 if (editorInput instanceof ICompilationUnit)
221 return (ICompilationUnit) editorInput;
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)
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) {
247 IJavaElement ref = cunit.getElementAt(selection.getOffset());
253 // else if (input instanceof IClassFile) {
255 // ((IClassFile)input).getElementAt(selection.getOffset());