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;
24 import org.eclipse.jface.text.ITextSelection;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.ISelectionProvider;
27 import org.eclipse.jface.viewers.IStructuredSelection;
28 import org.eclipse.jface.viewers.StructuredSelection;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.IEditorInput;
31 import org.eclipse.ui.IWorkbenchPart;
33 public class SelectionConverter {
35 private static final IJavaElement[] EMPTY_RESULT= new IJavaElement[0];
37 private SelectionConverter() {
42 * Converts the selection provided by the given part into a structured selection.
43 * The following conversion rules are used:
45 * <li><code>part instanceof PHPEditor</code>: returns a structured selection
46 * using code resolve to convert the editor's text selection.</li>
47 * <li><code>part instanceof IWorkbenchPart</code>: returns the part's selection
48 * if it is a structured selection.</li>
49 * <li><code>default</code>: returns an empty structured selection.</li>
52 public static IStructuredSelection getStructuredSelection(IWorkbenchPart part) throws JavaModelException {
53 if (part instanceof PHPEditor)
54 return new StructuredSelection(codeResolve((PHPEditor)part));
55 ISelectionProvider provider= part.getSite().getSelectionProvider();
56 if (provider != null) {
57 ISelection selection= provider.getSelection();
58 if (selection instanceof IStructuredSelection)
59 return (IStructuredSelection)selection;
61 return StructuredSelection.EMPTY;
66 * Converts the given structured selection into an array of Java elements.
67 * An empty array is returned if one of the elements stored in the structured
68 * selection is not of tupe <code>IJavaElement</code>
70 public static IJavaElement[] getElements(IStructuredSelection selection) {
71 if (!selection.isEmpty()) {
72 IJavaElement[] result= new IJavaElement[selection.size()];
74 for (Iterator iter= selection.iterator(); iter.hasNext(); i++) {
75 Object element= (Object) iter.next();
76 if (!(element instanceof IJavaElement))
78 result[i]= (IJavaElement)element;
85 public static boolean canOperateOn(PHPEditor editor) {
88 return getInput(editor) != null;
93 * Converts the text selection provided by the given editor into an array of
94 * Java elements. If the selection doesn't cover a Java element and the selection's
95 * length is greater than 0 the methods returns the editor's input element.
97 public static IJavaElement[] codeResolveOrInput(PHPEditor editor) throws JavaModelException {
98 IJavaElement input= getInput(editor);
99 ITextSelection selection= (ITextSelection)editor.getSelectionProvider().getSelection();
100 IJavaElement[] result= codeResolve(input, selection);
101 if (result.length == 0) {
102 result= new IJavaElement[] {input};
107 public static IJavaElement[] codeResolveOrInputHandled(PHPEditor editor, Shell shell, String title) {
109 return codeResolveOrInput(editor);
110 } catch(JavaModelException e) {
111 ExceptionHandler.handle(e, shell, title, ActionMessages.getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
117 * Converts the text selection provided by the given editor a Java element by
118 * asking the user if code reolve returned more than one result. If the selection
119 * doesn't cover a Java element and the selection's length is greater than 0 the
120 * methods returns the editor's input element.
122 public static IJavaElement codeResolveOrInput(PHPEditor editor, Shell shell, String title, String message) throws JavaModelException {
123 IJavaElement[] elements= codeResolveOrInput(editor);
124 if (elements == null || elements.length == 0)
126 IJavaElement candidate= elements[0];
127 if (elements.length > 1) {
128 candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message);
133 public static IJavaElement codeResolveOrInputHandled(PHPEditor editor, Shell shell, String title, String message) {
135 return codeResolveOrInput(editor, shell, title, message);
136 } catch (JavaModelException e) {
137 ExceptionHandler.handle(e, shell, title, ActionMessages.getString("SelectionConverter.codeResolveOrInput_failed")); //$NON-NLS-1$
142 public static IJavaElement[] codeResolve(PHPEditor editor) throws JavaModelException {
143 return codeResolve(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection());
147 * Converts the text selection provided by the given editor a Java element by
148 * asking the user if code reolve returned more than one result. If the selection
149 * doesn't cover a Java element <code>null</code> is returned.
151 public static IJavaElement codeResolve(PHPEditor editor, Shell shell, String title, String message) throws JavaModelException {
152 IJavaElement[] elements= codeResolve(editor);
153 if (elements == null || elements.length == 0)
155 IJavaElement candidate= elements[0];
156 if (elements.length > 1) {
157 candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message);
162 public static IJavaElement[] codeResolveHandled(PHPEditor editor, Shell shell, String title) {
164 return codeResolve(editor);
165 } catch (JavaModelException e) {
166 ExceptionHandler.handle(e, shell, title, ActionMessages.getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
171 public static IJavaElement getElementAtOffset(PHPEditor editor) throws JavaModelException {
172 return getElementAtOffset(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection());
175 public static IType getTypeAtOffset(PHPEditor editor) throws JavaModelException {
176 IJavaElement element= SelectionConverter.getElementAtOffset(editor);
177 IType type= (IType)element.getAncestor(IJavaElement.TYPE);
179 ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(editor);
181 type= unit.findPrimaryType();
186 public static IJavaElement getInput(PHPEditor editor) {
189 IEditorInput input= editor.getEditorInput();
190 // if (input instanceof IClassFileEditorInput)
191 // return ((IClassFileEditorInput)input).getClassFile();
192 IWorkingCopyManager manager= PHPeclipsePlugin.getDefault().getWorkingCopyManager();
193 return manager.getWorkingCopy(input);
196 public static ICompilationUnit getInputAsCompilationUnit(PHPEditor editor) {
197 Object editorInput= SelectionConverter.getInput(editor);
198 if (editorInput instanceof ICompilationUnit)
199 return (ICompilationUnit)editorInput;
204 private static IJavaElement[] codeResolve(IJavaElement input, ITextSelection selection) throws JavaModelException {
205 // if (input instanceof ICodeAssist) {
206 // IJavaElement[] elements= ((ICodeAssist)input).codeSelect(selection.getOffset(), selection.getLength());
207 // if (elements != null && elements.length > 0)
213 private static IJavaElement getElementAtOffset(IJavaElement input, ITextSelection selection) throws JavaModelException {
214 if (input instanceof ICompilationUnit) {
215 ICompilationUnit cunit= (ICompilationUnit)input;
216 if (cunit.isWorkingCopy()) {
217 synchronized (cunit) {
221 IJavaElement ref= cunit.getElementAt(selection.getOffset());
227 // else if (input instanceof IClassFile) {
228 // IJavaElement ref= ((IClassFile)input).getElementAt(selection.getOffset());