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
43 * selection. The following conversion rules are used:
45 * <li><code>part instanceof PHPEditor</code>: returns a structured
46 * selection using code resolve to convert the editor's text selection.</li>
47 * <li><code>part instanceof IWorkbenchPart</code>: returns the part's
48 * selection if it is a structured selection.</li>
49 * <li><code>default</code>: returns an empty structured selection.</li>
52 public static IStructuredSelection getStructuredSelection(
53 IWorkbenchPart part) throws JavaModelException {
54 if (part instanceof PHPEditor)
55 return new StructuredSelection(codeResolve((PHPEditor) part));
56 ISelectionProvider provider = part.getSite().getSelectionProvider();
57 if (provider != null) {
58 ISelection selection = provider.getSelection();
59 if (selection instanceof IStructuredSelection)
60 return (IStructuredSelection) selection;
62 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
68 * structured 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))
77 // return EMPTY_RESULT;
78 // result[i] = (IJavaElement) element;
82 // return EMPTY_RESULT;
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
95 * selection's length is greater than 0 the methods returns the editor's
98 public static IJavaElement[] codeResolveOrInput(PHPEditor editor)
99 throws JavaModelException {
100 IJavaElement input = getInput(editor);
101 ITextSelection selection = (ITextSelection) editor
102 .getSelectionProvider().getSelection();
103 IJavaElement[] result = codeResolve(input, selection);
104 if (result.length == 0) {
105 result = new IJavaElement[] { input };
110 // public static IJavaElement[] codeResolveOrInputHandled(PHPEditor editor,
111 // Shell shell, String title) {
113 // return codeResolveOrInput(editor);
114 // } catch (JavaModelException e) {
115 // ExceptionHandler.handle(e, shell, title, ActionMessages
116 // .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
122 * Converts the text selection provided by the given editor a Java element
123 * by asking the user if code reolve returned more than one result. If the
124 * selection doesn't cover a Java element and the selection's length is
125 * greater than 0 the methods returns the editor's input element.
127 public static IJavaElement codeResolveOrInput(PHPEditor editor,
128 Shell shell, String title, String message)
129 throws JavaModelException {
130 IJavaElement[] elements = codeResolveOrInput(editor);
131 if (elements == null || elements.length == 0)
133 IJavaElement candidate = elements[0];
134 if (elements.length > 1) {
135 candidate = OpenActionUtil.selectJavaElement(elements, shell,
141 // public static IJavaElement codeResolveOrInputHandled(PHPEditor editor,
142 // Shell shell, String title, String message) {
144 // return codeResolveOrInput(editor, shell, title, message);
145 // } catch (JavaModelException e) {
146 // ExceptionHandler.handle(e, shell, title, ActionMessages
147 // .getString("SelectionConverter.codeResolveOrInput_failed")); //$NON-NLS-1$
152 public static IJavaElement[] codeResolve(PHPEditor editor)
153 throws JavaModelException {
154 return codeResolve(getInput(editor), (ITextSelection) editor
155 .getSelectionProvider().getSelection());
159 * Converts the text selection provided by the given editor a Java element
160 * by asking the user if code reolve returned more than one result. If the
161 * selection doesn't cover a Java element <code>null</code> is returned.
163 // public static IJavaElement codeResolve(PHPEditor editor, Shell shell,
164 // String title, String message) throws JavaModelException {
165 // IJavaElement[] elements = codeResolve(editor);
166 // if (elements == null || elements.length == 0)
168 // IJavaElement candidate = elements[0];
169 // if (elements.length > 1) {
170 // candidate = OpenActionUtil.selectJavaElement(elements, shell,
176 // public static IJavaElement[] codeResolveHandled(PHPEditor editor,
177 // Shell shell, String title) {
179 // return codeResolve(editor);
180 // } catch (JavaModelException e) {
181 // ExceptionHandler.handle(e, shell, title, ActionMessages
182 // .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
187 public static IJavaElement getElementAtOffset(PHPEditor editor)
188 throws JavaModelException {
189 return getElementAtOffset(getInput(editor), (ITextSelection) editor
190 .getSelectionProvider().getSelection());
193 // public static IType getTypeAtOffset(PHPEditor editor)
194 // throws JavaModelException {
195 // IJavaElement element = SelectionConverter.getElementAtOffset(editor);
196 // IType type = (IType) element.getAncestor(IJavaElement.TYPE);
197 // if (type == null) {
198 // ICompilationUnit unit = SelectionConverter
199 // .getInputAsCompilationUnit(editor);
201 // type = unit.findPrimaryType();
206 public static IJavaElement getInput(PHPEditor editor) {
209 IEditorInput input = editor.getEditorInput();
210 // if (input instanceof IClassFileEditorInput)
211 // return ((IClassFileEditorInput)input).getClassFile();
212 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault()
213 .getWorkingCopyManager();
214 return manager.getWorkingCopy(input);
217 // public static ICompilationUnit getInputAsCompilationUnit(PHPEditor editor) {
218 // Object editorInput = SelectionConverter.getInput(editor);
219 // if (editorInput instanceof ICompilationUnit)
220 // return (ICompilationUnit) editorInput;
225 private static IJavaElement[] codeResolve(IJavaElement input,
226 ITextSelection selection) throws JavaModelException {
227 // if (input instanceof ICodeAssist) {
228 // IJavaElement[] elements=
229 // ((ICodeAssist)input).codeSelect(selection.getOffset(),
230 // selection.getLength());
231 // if (elements != null && elements.length > 0)
237 private static IJavaElement getElementAtOffset(IJavaElement input,
238 ITextSelection selection) throws JavaModelException {
239 if (input instanceof ICompilationUnit) {
240 ICompilationUnit cunit = (ICompilationUnit) input;
241 if (cunit.isWorkingCopy()) {
242 synchronized (cunit) {
246 IJavaElement ref = cunit.getElementAt(selection.getOffset());
252 // else if (input instanceof IClassFile) {
254 // ((IClassFile)input).getElementAt(selection.getOffset());