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;
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;
25 import org.eclipse.core.resources.IFile;
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.jface.action.Action;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.ui.IEditorDescriptor;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IEditorRegistry;
34 import org.eclipse.ui.IFileEditorInput;
35 import org.eclipse.ui.IWorkbenchPage;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.PlatformUI;
38 import org.eclipse.ui.ide.IDE;
39 import org.eclipse.ui.part.FileEditorInput;
40 import org.eclipse.ui.texteditor.ITextEditor;
43 * A number of routines for working with JavaElements in editors
45 * Use 'isOpenInEditor' to test if an element is already open in a editor Use
46 * 'openInEditor' to force opening an element in a editor With 'getWorkingCopy'
47 * you get the working copy (element in the editor) of an element
49 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
65 * @return the IEditorPart if shown, null if element is not open in an
68 public static IEditorPart isOpenInEditor(Object inputElement) {
69 IEditorInput input = null;
72 input = getEditorInput(inputElement);
73 } catch (JavaModelException x) {
74 PHPeclipsePlugin.log(x.getStatus());
78 IWorkbenchPage p = PHPeclipsePlugin.getActivePage();
80 return p.findEditor(input);
88 * Opens a Java editor for an element such as <code>IJavaElement</code>,
89 * <code>IFile</code>, or <code>IStorage</code>. The editor is
90 * activated by default.
92 * @return the IEditorPart or null if wrong element type or opening failed
94 public static IEditorPart openInEditor(Object inputElement)
95 throws JavaModelException, PartInitException {
96 return openInEditor(inputElement, true);
100 * Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
102 * @return the IEditorPart or null if wrong element type or opening failed
104 public static IEditorPart openInEditor(Object inputElement, boolean activate)
105 throws JavaModelException, PartInitException {
107 if (inputElement instanceof IFile)
108 return openInEditor((IFile) inputElement, activate);
110 IEditorInput input = getEditorInput(inputElement);
111 if (input instanceof IFileEditorInput) {
112 IFileEditorInput fileInput = (IFileEditorInput) input;
113 return openInEditor(fileInput.getFile(), activate);
117 return openInEditor(input, getEditorID(input, inputElement),
124 * Selects a Java Element in an editor
126 public static void revealInEditor(IEditorPart part, IJavaElement element) {
127 if (element != null && part instanceof PHPEditor) {
128 ((PHPEditor) part).setSelection(element);
132 private static IEditorPart openInEditor(IFile file, boolean activate)
133 throws PartInitException {
135 IWorkbenchPage p = PHPeclipsePlugin.getActivePage();
137 IEditorPart editorPart = IDE.openEditor(p, file, activate);
138 initializeHighlightRange(editorPart);
145 private static IEditorPart openInEditor(IEditorInput input,
146 String editorID, boolean activate) throws PartInitException {
148 IWorkbenchPage p = PHPeclipsePlugin.getActivePage();
150 IEditorPart editorPart = p
151 .openEditor(input, editorID, activate);
152 initializeHighlightRange(editorPart);
159 private static void initializeHighlightRange(IEditorPart editorPart) {
160 if (editorPart instanceof ITextEditor) {
161 TogglePresentationAction toggleAction = new TogglePresentationAction();
163 toggleAction.setEditor((ITextEditor) editorPart);
165 toggleAction.setEditor(null);
170 * @deprecated Made it public again for java debugger UI.
172 public static String getEditorID(IEditorInput input, Object inputObject) {
173 IEditorRegistry registry = PlatformUI.getWorkbench()
174 .getEditorRegistry();
175 IEditorDescriptor descriptor = registry.getDefaultEditor(input
177 if (descriptor != null)
178 return descriptor.getId();
182 private static IEditorInput getEditorInput(IJavaElement element)
183 throws JavaModelException {
184 while (element != null) {
185 if (element instanceof IWorkingCopy
186 && ((IWorkingCopy) element).isWorkingCopy())
187 element = ((IWorkingCopy) element).getOriginalElement();
189 if (element instanceof ICompilationUnit) {
190 ICompilationUnit unit = (ICompilationUnit) element;
191 IResource resource = unit.getResource();
192 if (resource instanceof IFile)
193 return new FileEditorInput((IFile) resource);
196 // if (element instanceof IClassFile)
197 // return new InternalClassFileEditorInput((IClassFile) element);
199 element = element.getParent();
205 public static IEditorInput getEditorInput(Object input)
206 throws JavaModelException {
208 if (input instanceof IJavaElement)
209 return getEditorInput((IJavaElement) input);
211 if (input instanceof IFile)
212 return new FileEditorInput((IFile) input);
214 // if (input instanceof IStorage)
215 // return new JarEntryEditorInput((IStorage)input);
221 * If the current active editor edits a java element return it, else return
224 public static IJavaElement getActiveEditorJavaInput() {
225 IWorkbenchPage page = PHPeclipsePlugin.getActivePage();
227 IEditorPart part = page.getActiveEditor();
229 IEditorInput editorInput = part.getEditorInput();
230 if (editorInput != null) {
231 return (IJavaElement) editorInput
232 .getAdapter(IJavaElement.class);
240 * Gets the working copy of an compilation unit opened in an editor
245 * the original compilation unit (or another working copy)
246 * @return the working copy of the compilation unit, or null if not found
248 public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
251 if (cu.isWorkingCopy())
254 return (ICompilationUnit) cu.findSharedWorkingCopy(JavaUI
255 .getBufferFactory());
259 * Gets the working copy of an member opened in an editor
262 * the original member or a member in a working copy
263 * @return the corresponding member in the shared working copy or
264 * <code>null</code> if not found
266 public static IMember getWorkingCopy(IMember member)
267 throws JavaModelException {
268 ICompilationUnit cu = member.getCompilationUnit();
270 ICompilationUnit workingCopy = getWorkingCopy(cu);
271 if (workingCopy != null) {
272 return JavaModelUtil.findMemberInCompilationUnit(workingCopy,
280 * Returns the compilation unit for the given java element.
283 * the java element whose compilation unit is searched for
284 * @return the compilation unit of the given java element
286 private static ICompilationUnit getCompilationUnit(IJavaElement element) {
291 if (element instanceof IMember)
292 return ((IMember) element).getCompilationUnit();
294 int type = element.getElementType();
295 if (IJavaElement.COMPILATION_UNIT == type)
296 return (ICompilationUnit) element;
297 if (IJavaElement.CLASS_FILE == type)
300 return getCompilationUnit(element.getParent());
304 * Returns the working copy of the given java element.
307 * the javaElement for which the working copyshould be found
309 * indicates whether the working copy must be reconcile prior to
311 * @return the working copy of the given element or <code>null</code> if
314 public static IJavaElement getWorkingCopy(IJavaElement element,
315 boolean reconcile) throws JavaModelException {
316 ICompilationUnit unit = getCompilationUnit(element);
320 if (unit.isWorkingCopy())
323 ICompilationUnit workingCopy = getWorkingCopy(unit);
324 if (workingCopy != null) {
326 synchronized (workingCopy) {
327 workingCopy.reconcile();
328 return JavaModelUtil.findInCompilationUnit(workingCopy,
333 .findInCompilationUnit(workingCopy, element);
341 * Maps the localized modifier name to a code in the same manner as
344 * @return the SWT modifier bit, or <code>0</code> if no match was found
348 public static int findLocalizedModifier(String token) {
352 if (token.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
354 if (token.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
356 if (token.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
358 if (token.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
365 * Returns the modifier string for the given SWT modifier modifier bits.
368 * the SWT modifier bits
369 * @return the modifier string
372 public static String getModifierString(int stateMask) {
373 String modifierString = ""; //$NON-NLS-1$
374 if ((stateMask & SWT.CTRL) == SWT.CTRL)
375 modifierString = appendModifierString(modifierString, SWT.CTRL);
376 if ((stateMask & SWT.ALT) == SWT.ALT)
377 modifierString = appendModifierString(modifierString, SWT.ALT);
378 if ((stateMask & SWT.SHIFT) == SWT.SHIFT)
379 modifierString = appendModifierString(modifierString, SWT.SHIFT);
380 if ((stateMask & SWT.COMMAND) == SWT.COMMAND)
381 modifierString = appendModifierString(modifierString, SWT.COMMAND);
383 return modifierString;
387 * Appends to modifier string of the given SWT modifier bit to the given
390 * @param modifierString
391 * the modifier string
393 * an int with SWT modifier bit
394 * @return the concatenated modifier string
397 private static String appendModifierString(String modifierString,
399 if (modifierString == null)
400 modifierString = ""; //$NON-NLS-1$
401 String newModifierString = Action.findModifierString(modifier);
402 if (modifierString.length() == 0)
403 return newModifierString;
404 return PHPEditorMessages
406 "EditorUtility.concatModifierStrings", new String[] { modifierString, newModifierString }); //$NON-NLS-1$
410 * Returns the Java project for a given editor input or <code>null</code>
411 * if no corresponding Java project exists.
415 * @return the corresponding Java project
419 public static IJavaProject getJavaProject(IEditorInput input) {
420 IJavaProject jProject = null;
421 if (input instanceof IFileEditorInput) {
422 IProject project = ((IFileEditorInput) input).getFile()
424 if (project != null) {
425 jProject = JavaCore.create(project);
426 if (!jProject.exists())
430 // else if (input instanceof IClassFileEditorInput) {
432 // ((IClassFileEditorInput)input).getClassFile().getJavaProject();