1) Changed indentation of (single or double) quoted strings to align on the first...
[phpeclipse.git] / net.sourceforge.phpeclipse / 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
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;
32
33 public class SelectionConverter {
34
35         private static final IJavaElement[] EMPTY_RESULT = new IJavaElement[0];
36
37         private SelectionConverter() {
38                 // no instance
39         }
40
41         /**
42          * Converts the selection provided by the given part into a structured
43          * selection. The following conversion rules are used:
44          * <ul>
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>
50          * </ul>
51          */
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;
61                 }
62                 return StructuredSelection.EMPTY;
63         }
64
65         /**
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>
69          */
70 //      public static IJavaElement[] getElements(IStructuredSelection selection) {
71 //              if (!selection.isEmpty()) {
72 //                      IJavaElement[] result = new IJavaElement[selection.size()];
73 //                      int i = 0;
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;
79 //                      }
80 //                      return result;
81 //              }
82 //              return EMPTY_RESULT;
83 //      }
84
85         public static boolean canOperateOn(PHPEditor editor) {
86                 if (editor == null)
87                         return false;
88                 return getInput(editor) != null;
89
90         }
91
92         /**
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
96          * input element.
97          */
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 };
106                 }
107                 return result;
108         }
109
110 //      public static IJavaElement[] codeResolveOrInputHandled(PHPEditor editor,
111 //                      Shell shell, String title) {
112 //              try {
113 //                      return codeResolveOrInput(editor);
114 //              } catch (JavaModelException e) {
115 //                      ExceptionHandler.handle(e, shell, title, ActionMessages
116 //                                      .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
117 //              }
118 //              return null;
119 //      }
120
121         /**
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.
126          */
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)
132                         return null;
133                 IJavaElement candidate = elements[0];
134                 if (elements.length > 1) {
135                         candidate = OpenActionUtil.selectJavaElement(elements, shell,
136                                         title, message);
137                 }
138                 return candidate;
139         }
140
141 //      public static IJavaElement codeResolveOrInputHandled(PHPEditor editor,
142 //                      Shell shell, String title, String message) {
143 //              try {
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$
148 //              }
149 //              return null;
150 //      }
151
152         public static IJavaElement[] codeResolve(PHPEditor editor)
153                         throws JavaModelException {
154                 return codeResolve(getInput(editor), (ITextSelection) editor
155                                 .getSelectionProvider().getSelection());
156         }
157
158         /**
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.
162          */
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)
167 //                      return null;
168 //              IJavaElement candidate = elements[0];
169 //              if (elements.length > 1) {
170 //                      candidate = OpenActionUtil.selectJavaElement(elements, shell,
171 //                                      title, message);
172 //              }
173 //              return candidate;
174 //      }
175
176 //      public static IJavaElement[] codeResolveHandled(PHPEditor editor,
177 //                      Shell shell, String title) {
178 //              try {
179 //                      return codeResolve(editor);
180 //              } catch (JavaModelException e) {
181 //                      ExceptionHandler.handle(e, shell, title, ActionMessages
182 //                                      .getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
183 //              }
184 //              return null;
185 //      }
186
187         public static IJavaElement getElementAtOffset(PHPEditor editor)
188                         throws JavaModelException {
189                 return getElementAtOffset(getInput(editor), (ITextSelection) editor
190                                 .getSelectionProvider().getSelection());
191         }
192
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);
200 //                      if (unit != null)
201 //                              type = unit.findPrimaryType();
202 //              }
203 //              return type;
204 //      }
205
206         public static IJavaElement getInput(PHPEditor editor) {
207                 if (editor == null)
208                         return null;
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);
215         }
216
217 //      public static ICompilationUnit getInputAsCompilationUnit(PHPEditor editor) {
218 //              Object editorInput = SelectionConverter.getInput(editor);
219 //              if (editorInput instanceof ICompilationUnit)
220 //                      return (ICompilationUnit) editorInput;
221 //              else
222 //                      return null;
223 //      }
224
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)
232                 // return elements;
233                 // }
234                 return EMPTY_RESULT;
235         }
236
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) {
243                                         cunit.reconcile();
244                                 }
245                         }
246                         IJavaElement ref = cunit.getElementAt(selection.getOffset());
247                         if (ref == null)
248                                 return input;
249                         else
250                                 return ref;
251                 }
252                 // else if (input instanceof IClassFile) {
253                 // IJavaElement ref=
254                 // ((IClassFile)input).getElementAt(selection.getOffset());
255                 // if (ref == null)
256                 // return input;
257                 // else
258                 // return ref;
259                 // }
260                 return null;
261         }
262 }