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