Bug #1248155: avoid NPE in parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / phpeditor / EditorUtility.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
12 package net.sourceforge.phpeclipse.phpeditor;
13
14
15 import net.sourceforge.phpdt.core.ICompilationUnit;
16 import net.sourceforge.phpdt.core.IJavaElement;
17 import net.sourceforge.phpdt.core.IJavaProject;
18 import net.sourceforge.phpdt.core.IMember;
19 import net.sourceforge.phpdt.core.IWorkingCopy;
20 import net.sourceforge.phpdt.core.JavaCore;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
23 import net.sourceforge.phpdt.ui.JavaUI;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
25
26 import org.eclipse.core.resources.IFile;
27 import org.eclipse.core.resources.IProject;
28 import org.eclipse.core.resources.IResource;
29 import org.eclipse.jface.action.Action;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.ui.IEditorDescriptor;
32 import org.eclipse.ui.IEditorInput;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IEditorRegistry;
35 import org.eclipse.ui.IFileEditorInput;
36 import org.eclipse.ui.IWorkbenchPage;
37 import org.eclipse.ui.PartInitException;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.ide.IDE;
40 import org.eclipse.ui.part.FileEditorInput;
41 import org.eclipse.ui.texteditor.ITextEditor;
42
43
44 /**
45  * A number of routines for working with JavaElements in editors
46  *
47  * Use 'isOpenInEditor' to test if an element is already open in a editor  
48  * Use 'openInEditor' to force opening an element in a editor
49  * With 'getWorkingCopy' you get the working copy (element in the editor) of an element
50  */
51 public class EditorUtility {
52         
53         
54         public static boolean isEditorInput(Object element, IEditorPart editor) {
55                 if (editor != null) {
56                         try {
57                                 return editor.getEditorInput().equals(getEditorInput(element));
58                         } catch (JavaModelException x) {
59                                 PHPeclipsePlugin.log(x.getStatus());
60                         }
61                 }
62                 return false;
63         }
64         
65         /** 
66          * Tests if a cu is currently shown in an editor
67          * @return the IEditorPart if shown, null if element is not open in an editor
68          */     
69         public static IEditorPart isOpenInEditor(Object inputElement) {
70                 IEditorInput input= null;
71                 
72                 try {
73                         input = getEditorInput(inputElement);
74                 } catch (JavaModelException x) {
75                         PHPeclipsePlugin.log(x.getStatus());
76                 }
77                 
78                 if (input != null) {
79                         IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
80                         if (p != null) {
81                                 return p.findEditor(input);
82                         }
83                 }
84                 
85                 return null;
86         }
87         
88         /**
89          * Opens a Java editor for an element such as <code>IJavaElement</code>, <code>IFile</code>, or <code>IStorage</code>.
90          * The editor is activated by default.
91          * @return the IEditorPart or null if wrong element type or opening failed
92          */
93         public static IEditorPart openInEditor(Object inputElement) throws JavaModelException, PartInitException {
94                 return openInEditor(inputElement, true);
95         }
96                 
97         /**
98          * Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
99          * @return the IEditorPart or null if wrong element type or opening failed
100          */
101         public static IEditorPart openInEditor(Object inputElement, boolean activate) throws JavaModelException, PartInitException {
102                 
103                 if (inputElement instanceof IFile)
104                         return openInEditor((IFile) inputElement, activate);
105                 
106                 IEditorInput input= getEditorInput(inputElement);
107                 if (input instanceof IFileEditorInput) {
108                         IFileEditorInput fileInput= (IFileEditorInput) input;
109                         return openInEditor(fileInput.getFile(), activate);
110                 }
111                 
112                 if (input != null)
113                         return openInEditor(input, getEditorID(input, inputElement), activate);
114                         
115                 return null;
116         }
117         
118         /** 
119          * Selects a Java Element in an editor
120          */     
121         public static void revealInEditor(IEditorPart part, IJavaElement element) {
122                 if (element != null && part instanceof PHPEditor) {
123                         ((PHPEditor) part).setSelection(element);
124                 }
125         }
126         
127         private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
128           if (file != null) {
129             IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
130             if (p != null) {
131               IEditorPart editorPart= IDE.openEditor(p, file, activate);
132               initializeHighlightRange(editorPart);
133               return editorPart;
134             }
135           }
136           return null;
137         }
138
139         private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
140                 if (input != null) {
141                         IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
142                         if (p != null) {
143                                 IEditorPart editorPart= p.openEditor(input, editorID, activate);
144                                 initializeHighlightRange(editorPart);
145                                 return editorPart;
146                         }
147                 }
148                 return null;
149         }
150
151         private static void initializeHighlightRange(IEditorPart editorPart) {
152                 if (editorPart instanceof ITextEditor) {
153                         TogglePresentationAction toggleAction= new TogglePresentationAction();
154                         // Initialize editor
155                         toggleAction.setEditor((ITextEditor)editorPart);
156                         // Reset action
157                         toggleAction.setEditor(null);
158                 }
159         }
160         
161         /**
162          *@deprecated   Made it public again for java debugger UI.
163          */
164         public static String getEditorID(IEditorInput input, Object inputObject) {
165                 IEditorRegistry registry= PlatformUI.getWorkbench().getEditorRegistry();
166                 IEditorDescriptor descriptor= registry.getDefaultEditor(input.getName());
167                 if (descriptor != null)
168                         return descriptor.getId();
169                 return null;
170         }
171         
172         private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException {
173                 while (element != null) {
174                         if (element instanceof IWorkingCopy && ((IWorkingCopy) element).isWorkingCopy()) 
175                                 element= ((IWorkingCopy) element).getOriginalElement();
176                                 
177                         if (element instanceof ICompilationUnit) {
178                                 ICompilationUnit unit= (ICompilationUnit) element;
179                                         IResource resource= unit.getResource();
180                                         if (resource instanceof IFile)
181                                                 return new FileEditorInput((IFile) resource);
182                         }
183                         
184 //                      if (element instanceof IClassFile)
185 //                              return new InternalClassFileEditorInput((IClassFile) element);
186 //                      
187                         element= element.getParent();
188                 }
189                 
190                 return null;
191         }       
192
193         public static IEditorInput getEditorInput(Object input) throws JavaModelException {
194         
195                 if (input instanceof IJavaElement)
196                         return getEditorInput((IJavaElement) input);
197                         
198                 if (input instanceof IFile) 
199                         return new FileEditorInput((IFile) input);
200                 
201 //              if (input instanceof IStorage) 
202 //                      return new JarEntryEditorInput((IStorage)input);
203         
204                 return null;
205         }
206         
207         /**
208          * If the current active editor edits a java element return it, else
209          * return null
210          */
211         public static IJavaElement getActiveEditorJavaInput() {
212                 IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
213                 if (page != null) {
214                         IEditorPart part= page.getActiveEditor();
215                         if (part != null) {
216                                 IEditorInput editorInput= part.getEditorInput();
217                                 if (editorInput != null) {
218                                         return (IJavaElement)editorInput.getAdapter(IJavaElement.class);
219                                 }
220                         }
221                 }
222                 return null;    
223         }
224         
225         /** 
226          * Gets the working copy of an compilation unit opened in an editor
227          * @param part the editor part
228          * @param cu the original compilation unit (or another working copy)
229          * @return the working copy of the compilation unit, or null if not found
230          */     
231         public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
232                 if (cu == null)
233                         return null;
234                 if (cu.isWorkingCopy())
235                         return cu;
236                         
237                 return (ICompilationUnit)cu.findSharedWorkingCopy(JavaUI.getBufferFactory());
238         }
239         
240         /** 
241          * Gets the working copy of an member opened in an editor
242          *
243          * @param member the original member or a member in a working copy
244          * @return the corresponding member in the shared working copy or <code>null</code> if not found
245          */     
246         public static IMember getWorkingCopy(IMember member) throws JavaModelException {
247                 ICompilationUnit cu= member.getCompilationUnit();
248                 if (cu != null) {
249                         ICompilationUnit workingCopy= getWorkingCopy(cu);
250                         if (workingCopy != null) {
251                                 return JavaModelUtil.findMemberInCompilationUnit(workingCopy, member);
252                         }
253                 }
254                 return null;
255         }
256         
257         /**
258          * Returns the compilation unit for the given java element.
259          * @param element the java element whose compilation unit is searched for
260          * @return the compilation unit of the given java element
261          */
262         private static ICompilationUnit getCompilationUnit(IJavaElement element) {
263                 
264                 if (element == null)
265                         return null;
266                         
267                 if (element instanceof IMember)
268                         return ((IMember) element).getCompilationUnit();
269                 
270                 int type= element.getElementType();
271                 if (IJavaElement.COMPILATION_UNIT == type)
272                         return (ICompilationUnit) element;
273                 if (IJavaElement.CLASS_FILE == type)
274                         return null;
275                         
276                 return getCompilationUnit(element.getParent());
277         }
278         
279         /** 
280          * Returns the working copy of the given java element.
281          * @param javaElement the javaElement for which the working copyshould be found
282          * @param reconcile indicates whether the working copy must be reconcile prior to searching it
283          * @return the working copy of the given element or <code>null</code> if none
284          */     
285         public static IJavaElement getWorkingCopy(IJavaElement element, boolean reconcile) throws JavaModelException {
286                 ICompilationUnit unit= getCompilationUnit(element);
287                 if (unit == null)
288                         return null;
289                         
290                 if (unit.isWorkingCopy())
291                         return element;
292                         
293                 ICompilationUnit workingCopy= getWorkingCopy(unit);
294                 if (workingCopy != null) {
295                         if (reconcile) {
296                                 synchronized (workingCopy) {
297                                         workingCopy.reconcile();
298                                         return JavaModelUtil.findInCompilationUnit(workingCopy, element);
299                                 }
300                         } else {
301                                         return JavaModelUtil.findInCompilationUnit(workingCopy, element);
302                         }
303                 }
304                 
305                 return null;
306         }
307
308         /**
309          * Maps the localized modifier name to a code in the same
310          * manner as #findModifier.
311          * 
312          * @return the SWT modifier bit, or <code>0</code> if no match was found
313          * @see findModifier
314          * @since 2.1.1
315          */
316         public static int findLocalizedModifier(String token) {
317                 if (token == null)
318                         return 0;
319                 
320                 if (token.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
321                         return SWT.CTRL;
322                 if (token.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
323                         return SWT.SHIFT;
324                 if (token.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
325                         return SWT.ALT;
326                 if (token.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
327                         return SWT.COMMAND;
328
329                 return 0;
330         }
331
332         /**
333          * Returns the modifier string for the given SWT modifier
334          * modifier bits.
335          * 
336          * @param stateMask     the SWT modifier bits
337          * @return the modifier string
338          * @since 2.1.1
339          */
340         public static String getModifierString(int stateMask) {
341                 String modifierString= ""; //$NON-NLS-1$
342                 if ((stateMask & SWT.CTRL) == SWT.CTRL)
343                         modifierString= appendModifierString(modifierString, SWT.CTRL);
344                 if ((stateMask & SWT.ALT) == SWT.ALT)
345                         modifierString= appendModifierString(modifierString, SWT.ALT);
346                 if ((stateMask & SWT.SHIFT) == SWT.SHIFT)
347                         modifierString= appendModifierString(modifierString, SWT.SHIFT);
348                 if ((stateMask & SWT.COMMAND) == SWT.COMMAND)
349                         modifierString= appendModifierString(modifierString,  SWT.COMMAND);
350                 
351                 return modifierString;
352         }
353
354         /**
355          * Appends to modifier string of the given SWT modifier bit
356          * to the given modifierString.
357          * 
358          * @param modifierString        the modifier string
359          * @param modifier                      an int with SWT modifier bit
360          * @return the concatenated modifier string
361          * @since 2.1.1
362          */
363         private static String appendModifierString(String modifierString, int modifier) {
364                 if (modifierString == null)
365                         modifierString= ""; //$NON-NLS-1$
366                 String newModifierString= Action.findModifierString(modifier);
367                 if (modifierString.length() == 0)
368                         return newModifierString;
369                 return PHPEditorMessages.getFormattedString("EditorUtility.concatModifierStrings", new String[] {modifierString, newModifierString}); //$NON-NLS-1$
370         }
371         
372         /**
373          * Returns the Java project for a given editor input or <code>null</code> if no corresponding
374          * Java project exists.
375          * 
376          * @param input the editor input
377          * @return the corresponding Java project
378          * 
379          * @since 3.0
380          */
381         public static IJavaProject getJavaProject(IEditorInput input) {
382                 IJavaProject jProject= null;
383                 if (input instanceof IFileEditorInput) {
384                         IProject project= ((IFileEditorInput)input).getFile().getProject();
385                         if (project != null) {
386                                 jProject= JavaCore.create(project);
387                                 if (!jProject.exists())
388                                         jProject= null;
389                         }
390                 } 
391 //              else if (input instanceof IClassFileEditorInput) {
392 //                      jProject= ((IClassFileEditorInput)input).getClassFile().getJavaProject();
393 //              }
394                 return jProject;
395         }
396 }