Code folding for PHPdoc at start of PHP file
[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 selection.
43          * The following conversion rules are used:
44          * <ul>
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>
50          * </ul>
51          */
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;
60                 }
61                 return StructuredSelection.EMPTY;
62         }
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 structured
68          * 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 selection's
95          * length is greater than 0 the methods returns the editor's input element.
96          */
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};
103                 }
104                 return result;
105         }
106         
107         public static IJavaElement[] codeResolveOrInputHandled(PHPEditor editor, Shell shell, String title) {
108                 try {
109                         return codeResolveOrInput(editor);
110                 } catch(JavaModelException e) {
111                         ExceptionHandler.handle(e, shell, title, ActionMessages.getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
112                 }
113                 return null;
114         }
115         
116         /**
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.
121          */
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)
125                         return null;
126                 IJavaElement candidate= elements[0];
127                 if (elements.length > 1) {
128                         candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message);
129                 }
130                 return candidate;
131         }
132         
133         public static IJavaElement codeResolveOrInputHandled(PHPEditor editor, Shell shell, String title, String message) {
134                 try {
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$
138                 }
139                 return null;
140         }
141                 
142         public static IJavaElement[] codeResolve(PHPEditor editor) throws JavaModelException {
143                         return codeResolve(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection());
144         }
145
146         /**
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.
150          */
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)
154                         return null;
155                 IJavaElement candidate= elements[0];
156                 if (elements.length > 1) {
157                         candidate= OpenActionUtil.selectJavaElement(elements, shell, title, message);
158                 }
159                 return candidate;
160         }
161         
162         public static IJavaElement[] codeResolveHandled(PHPEditor editor, Shell shell, String title) {
163                 try {
164                         return codeResolve(editor);
165                 } catch (JavaModelException e) {
166                         ExceptionHandler.handle(e, shell, title, ActionMessages.getString("SelectionConverter.codeResolve_failed")); //$NON-NLS-1$
167                 }
168                 return null;
169         }
170         
171         public static IJavaElement getElementAtOffset(PHPEditor editor) throws JavaModelException {
172                 return getElementAtOffset(getInput(editor), (ITextSelection)editor.getSelectionProvider().getSelection());
173         }
174         
175         public static IType getTypeAtOffset(PHPEditor editor) throws JavaModelException {
176                 IJavaElement element= SelectionConverter.getElementAtOffset(editor);
177                 IType type= (IType)element.getAncestor(IJavaElement.TYPE);
178                 if (type == null) {
179                         ICompilationUnit unit= SelectionConverter.getInputAsCompilationUnit(editor);
180                         if (unit != null)
181                                 type= unit.findPrimaryType();
182                 }
183                 return type;
184         }
185         
186         public static IJavaElement getInput(PHPEditor editor) {
187                 if (editor == null)
188                         return null;
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);                   
194         }
195         
196         public static ICompilationUnit getInputAsCompilationUnit(PHPEditor editor) {
197                 Object editorInput= SelectionConverter.getInput(editor);
198                 if (editorInput instanceof ICompilationUnit)
199                         return (ICompilationUnit)editorInput;
200                 else
201                         return null;
202         }
203
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)
208 //                                      return elements;
209 //                      }
210                         return EMPTY_RESULT;
211         }
212         
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) {
218                                         cunit.reconcile();
219                                 }
220                         }
221                         IJavaElement ref= cunit.getElementAt(selection.getOffset());
222                         if (ref == null)
223                                 return input;
224                         else
225                                 return ref;
226                 } 
227 //              else if (input instanceof IClassFile) {
228 //                      IJavaElement ref= ((IClassFile)input).getElementAt(selection.getOffset());
229 //                      if (ref == null)
230 //                              return input;
231 //                      else
232 //                              return ref;
233 //              }
234                 return null;
235         }
236 }