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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
12 package net.sourceforge.phpeclipse.phpeditor;
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;
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;
42 * A number of routines for working with JavaElements in editors
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
48 public class EditorUtility {
51 public static boolean isEditorInput(Object element, IEditorPart editor) {
54 return editor.getEditorInput().equals(getEditorInput(element));
55 } catch (JavaModelException x) {
56 PHPeclipsePlugin.log(x.getStatus());
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
66 public static IEditorPart isOpenInEditor(Object inputElement) {
67 IEditorInput input= null;
70 input = getEditorInput(inputElement);
71 } catch (JavaModelException x) {
72 PHPeclipsePlugin.log(x.getStatus());
76 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
78 return p.findEditor(input);
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
90 public static IEditorPart openInEditor(Object inputElement) throws JavaModelException, PartInitException {
91 return openInEditor(inputElement, true);
95 * Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
96 * @return the IEditorPart or null if wrong element type or opening failed
98 public static IEditorPart openInEditor(Object inputElement, boolean activate) throws JavaModelException, PartInitException {
100 if (inputElement instanceof IFile)
101 return openInEditor((IFile) inputElement, activate);
103 IEditorInput input= getEditorInput(inputElement);
104 if (input instanceof IFileEditorInput) {
105 IFileEditorInput fileInput= (IFileEditorInput) input;
106 return openInEditor(fileInput.getFile(), activate);
110 return openInEditor(input, getEditorID(input, inputElement), activate);
116 * Selects a Java Element in an editor
118 public static void revealInEditor(IEditorPart part, IJavaElement element) {
119 if (element != null && part instanceof PHPEditor) {
120 ((PHPEditor) part).setSelection(element);
124 private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
126 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
128 IEditorPart editorPart= IDE.openEditor(p, file, activate);
129 initializeHighlightRange(editorPart);
136 private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
138 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
140 IEditorPart editorPart= p.openEditor(input, editorID, activate);
141 initializeHighlightRange(editorPart);
148 private static void initializeHighlightRange(IEditorPart editorPart) {
149 if (editorPart instanceof ITextEditor) {
150 TogglePresentationAction toggleAction= new TogglePresentationAction();
152 toggleAction.setEditor((ITextEditor)editorPart);
154 toggleAction.setEditor(null);
159 *@deprecated Made it public again for java debugger UI.
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();
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();
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);
181 // if (element instanceof IClassFile)
182 // return new InternalClassFileEditorInput((IClassFile) element);
184 element= element.getParent();
190 public static IEditorInput getEditorInput(Object input) throws JavaModelException {
192 if (input instanceof IJavaElement)
193 return getEditorInput((IJavaElement) input);
195 if (input instanceof IFile)
196 return new FileEditorInput((IFile) input);
198 // if (input instanceof IStorage)
199 // return new JarEntryEditorInput((IStorage)input);
205 * If the current active editor edits a java element return it, else
208 public static IJavaElement getActiveEditorJavaInput() {
209 IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
211 IEditorPart part= page.getActiveEditor();
213 IEditorInput editorInput= part.getEditorInput();
214 if (editorInput != null) {
215 return (IJavaElement)editorInput.getAdapter(IJavaElement.class);
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
228 public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
231 if (cu.isWorkingCopy())
234 return (ICompilationUnit)cu.findSharedWorkingCopy(JavaUI.getBufferFactory());
238 * Gets the working copy of an member opened in an editor
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
243 public static IMember getWorkingCopy(IMember member) throws JavaModelException {
244 ICompilationUnit cu= member.getCompilationUnit();
246 ICompilationUnit workingCopy= getWorkingCopy(cu);
247 if (workingCopy != null) {
248 return JavaModelUtil.findMemberInCompilationUnit(workingCopy, member);
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
259 private static ICompilationUnit getCompilationUnit(IJavaElement element) {
264 if (element instanceof IMember)
265 return ((IMember) element).getCompilationUnit();
267 int type= element.getElementType();
268 if (IJavaElement.COMPILATION_UNIT == type)
269 return (ICompilationUnit) element;
270 if (IJavaElement.CLASS_FILE == type)
273 return getCompilationUnit(element.getParent());
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
282 public static IJavaElement getWorkingCopy(IJavaElement element, boolean reconcile) throws JavaModelException {
283 ICompilationUnit unit= getCompilationUnit(element);
287 if (unit.isWorkingCopy())
290 ICompilationUnit workingCopy= getWorkingCopy(unit);
291 if (workingCopy != null) {
293 synchronized (workingCopy) {
294 workingCopy.reconcile();
295 return JavaModelUtil.findInCompilationUnit(workingCopy, element);
298 return JavaModelUtil.findInCompilationUnit(workingCopy, element);
306 * Maps the localized modifier name to a code in the same
307 * manner as #findModifier.
309 * @return the SWT modifier bit, or <code>0</code> if no match was found
313 public static int findLocalizedModifier(String token) {
317 if (token.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
319 if (token.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
321 if (token.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
323 if (token.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
330 * Returns the modifier string for the given SWT modifier
333 * @param stateMask the SWT modifier bits
334 * @return the modifier string
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);
348 return modifierString;
352 * Appends to modifier string of the given SWT modifier bit
353 * to the given modifierString.
355 * @param modifierString the modifier string
356 * @param modifier an int with SWT modifier bit
357 * @return the concatenated modifier string
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$