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