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.part.FileEditorInput;
37 import org.eclipse.ui.texteditor.ITextEditor;
41 * A number of routines for working with JavaElements in editors
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
47 public class EditorUtility {
50 public static boolean isEditorInput(Object element, IEditorPart editor) {
53 return editor.getEditorInput().equals(getEditorInput(element));
54 } catch (JavaModelException x) {
55 PHPeclipsePlugin.log(x.getStatus());
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
65 public static IEditorPart isOpenInEditor(Object inputElement) {
66 IEditorInput input= null;
69 input= getEditorInput(inputElement);
70 } catch (JavaModelException x) {
71 PHPeclipsePlugin.log(x.getStatus());
75 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
77 return p.findEditor(input);
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
89 public static IEditorPart openInEditor(Object inputElement) throws JavaModelException, PartInitException {
90 return openInEditor(inputElement, true);
94 * Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
95 * @return the IEditorPart or null if wrong element type or opening failed
97 public static IEditorPart openInEditor(Object inputElement, boolean activate) throws JavaModelException, PartInitException {
99 if (inputElement instanceof IFile)
100 return openInEditor((IFile) inputElement, activate);
102 IEditorInput input= getEditorInput(inputElement);
103 if (input instanceof IFileEditorInput) {
104 IFileEditorInput fileInput= (IFileEditorInput) input;
105 return openInEditor(fileInput.getFile(), activate);
109 return openInEditor(input, getEditorID(input, inputElement), activate);
115 * Selects a Java Element in an editor
117 public static void revealInEditor(IEditorPart part, IJavaElement element) {
118 if (element != null && part instanceof PHPEditor) {
119 ((PHPEditor) part).setSelection(element);
123 private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
125 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
127 IEditorPart editorPart= p.openEditor(file, null, activate);
128 initializeHighlightRange(editorPart);
135 private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
137 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
139 IEditorPart editorPart= p.openEditor(input, editorID, activate);
140 initializeHighlightRange(editorPart);
147 private static void initializeHighlightRange(IEditorPart editorPart) {
148 if (editorPart instanceof ITextEditor) {
149 TogglePresentationAction toggleAction= new TogglePresentationAction();
151 toggleAction.setEditor((ITextEditor)editorPart);
153 toggleAction.setEditor(null);
158 *@deprecated Made it public again for java debugger UI.
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();
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();
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);
180 // if (element instanceof IClassFile)
181 // return new InternalClassFileEditorInput((IClassFile) element);
183 element= element.getParent();
189 public static IEditorInput getEditorInput(Object input) throws JavaModelException {
191 if (input instanceof IJavaElement)
192 return getEditorInput((IJavaElement) input);
194 if (input instanceof IFile)
195 return new FileEditorInput((IFile) input);
197 // if (input instanceof IStorage)
198 // return new JarEntryEditorInput((IStorage)input);
204 * If the current active editor edits a java element return it, else
207 public static IJavaElement getActiveEditorJavaInput() {
208 IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
210 IEditorPart part= page.getActiveEditor();
212 IEditorInput editorInput= part.getEditorInput();
213 if (editorInput != null) {
214 return (IJavaElement)editorInput.getAdapter(IJavaElement.class);
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
227 public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
230 if (cu.isWorkingCopy())
233 return (ICompilationUnit)cu.findSharedWorkingCopy(JavaUI.getBufferFactory());
237 * Gets the working copy of an member opened in an editor
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
242 public static IMember getWorkingCopy(IMember member) throws JavaModelException {
243 ICompilationUnit cu= member.getCompilationUnit();
245 ICompilationUnit workingCopy= getWorkingCopy(cu);
246 if (workingCopy != null) {
247 return JavaModelUtil.findMemberInCompilationUnit(workingCopy, member);
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
258 private static ICompilationUnit getCompilationUnit(IJavaElement element) {
263 if (element instanceof IMember)
264 return ((IMember) element).getCompilationUnit();
266 int type= element.getElementType();
267 if (IJavaElement.COMPILATION_UNIT == type)
268 return (ICompilationUnit) element;
269 if (IJavaElement.CLASS_FILE == type)
272 return getCompilationUnit(element.getParent());
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
281 public static IJavaElement getWorkingCopy(IJavaElement element, boolean reconcile) throws JavaModelException {
282 ICompilationUnit unit= getCompilationUnit(element);
286 if (unit.isWorkingCopy())
289 ICompilationUnit workingCopy= getWorkingCopy(unit);
290 if (workingCopy != null) {
292 synchronized (workingCopy) {
293 workingCopy.reconcile();
294 return JavaModelUtil.findInCompilationUnit(workingCopy, element);
297 return JavaModelUtil.findInCompilationUnit(workingCopy, element);
305 * Maps the localized modifier name to a code in the same
306 * manner as #findModifier.
308 * @return the SWT modifier bit, or <code>0</code> if no match was found
312 public static int findLocalizedModifier(String token) {
316 if (token.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
318 if (token.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
320 if (token.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
322 if (token.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
329 * Returns the modifier string for the given SWT modifier
332 * @param stateMask the SWT modifier bits
333 * @return the modifier string
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);
347 return modifierString;
351 * Appends to modifier string of the given SWT modifier bit
352 * to the given modifierString.
354 * @param modifierString the modifier string
355 * @param modifier an int with SWT modifier bit
356 * @return the concatenated modifier string
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$