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