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