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.IJavaProject;
18 import net.sourceforge.phpdt.core.IMember;
19 import net.sourceforge.phpdt.core.IWorkingCopy;
20 import net.sourceforge.phpdt.core.JavaCore;
21 import net.sourceforge.phpdt.core.JavaModelException;
22 import net.sourceforge.phpdt.internal.corext.util.JavaModelUtil;
23 import net.sourceforge.phpdt.ui.JavaUI;
24 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
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;
45 * A number of routines for working with JavaElements in editors
47 * Use 'isOpenInEditor' to test if an element is already open in a editor
48 * Use 'openInEditor' to force opening an element in a editor
49 * With 'getWorkingCopy' you get the working copy (element in the editor) of an element
51 public class EditorUtility {
54 public static boolean isEditorInput(Object element, IEditorPart editor) {
57 return editor.getEditorInput().equals(getEditorInput(element));
58 } catch (JavaModelException x) {
59 PHPeclipsePlugin.log(x.getStatus());
66 * Tests if a cu is currently shown in an editor
67 * @return the IEditorPart if shown, null if element is not open in an editor
69 public static IEditorPart isOpenInEditor(Object inputElement) {
70 IEditorInput input= null;
73 input = getEditorInput(inputElement);
74 } catch (JavaModelException x) {
75 PHPeclipsePlugin.log(x.getStatus());
79 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
81 return p.findEditor(input);
89 * Opens a Java editor for an element such as <code>IJavaElement</code>, <code>IFile</code>, or <code>IStorage</code>.
90 * The editor is activated by default.
91 * @return the IEditorPart or null if wrong element type or opening failed
93 public static IEditorPart openInEditor(Object inputElement) throws JavaModelException, PartInitException {
94 return openInEditor(inputElement, true);
98 * Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
99 * @return the IEditorPart or null if wrong element type or opening failed
101 public static IEditorPart openInEditor(Object inputElement, boolean activate) throws JavaModelException, PartInitException {
103 if (inputElement instanceof IFile)
104 return openInEditor((IFile) inputElement, activate);
106 IEditorInput input= getEditorInput(inputElement);
107 if (input instanceof IFileEditorInput) {
108 IFileEditorInput fileInput= (IFileEditorInput) input;
109 return openInEditor(fileInput.getFile(), activate);
113 return openInEditor(input, getEditorID(input, inputElement), activate);
119 * Selects a Java Element in an editor
121 public static void revealInEditor(IEditorPart part, IJavaElement element) {
122 if (element != null && part instanceof PHPEditor) {
123 ((PHPEditor) part).setSelection(element);
127 private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
129 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
131 IEditorPart editorPart= IDE.openEditor(p, file, activate);
132 initializeHighlightRange(editorPart);
139 private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
141 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
143 IEditorPart editorPart= p.openEditor(input, editorID, activate);
144 initializeHighlightRange(editorPart);
151 private static void initializeHighlightRange(IEditorPart editorPart) {
152 if (editorPart instanceof ITextEditor) {
153 TogglePresentationAction toggleAction= new TogglePresentationAction();
155 toggleAction.setEditor((ITextEditor)editorPart);
157 toggleAction.setEditor(null);
162 *@deprecated Made it public again for java debugger UI.
164 public static String getEditorID(IEditorInput input, Object inputObject) {
165 IEditorRegistry registry= PlatformUI.getWorkbench().getEditorRegistry();
166 IEditorDescriptor descriptor= registry.getDefaultEditor(input.getName());
167 if (descriptor != null)
168 return descriptor.getId();
172 private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException {
173 while (element != null) {
174 if (element instanceof IWorkingCopy && ((IWorkingCopy) element).isWorkingCopy())
175 element= ((IWorkingCopy) element).getOriginalElement();
177 if (element instanceof ICompilationUnit) {
178 ICompilationUnit unit= (ICompilationUnit) element;
179 IResource resource= unit.getResource();
180 if (resource instanceof IFile)
181 return new FileEditorInput((IFile) resource);
184 // if (element instanceof IClassFile)
185 // return new InternalClassFileEditorInput((IClassFile) element);
187 element= element.getParent();
193 public static IEditorInput getEditorInput(Object input) throws JavaModelException {
195 if (input instanceof IJavaElement)
196 return getEditorInput((IJavaElement) input);
198 if (input instanceof IFile)
199 return new FileEditorInput((IFile) input);
201 // if (input instanceof IStorage)
202 // return new JarEntryEditorInput((IStorage)input);
208 * If the current active editor edits a java element return it, else
211 public static IJavaElement getActiveEditorJavaInput() {
212 IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
214 IEditorPart part= page.getActiveEditor();
216 IEditorInput editorInput= part.getEditorInput();
217 if (editorInput != null) {
218 return (IJavaElement)editorInput.getAdapter(IJavaElement.class);
226 * Gets the working copy of an compilation unit opened in an editor
227 * @param part the editor part
228 * @param cu the original compilation unit (or another working copy)
229 * @return the working copy of the compilation unit, or null if not found
231 public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
234 if (cu.isWorkingCopy())
237 return (ICompilationUnit)cu.findSharedWorkingCopy(JavaUI.getBufferFactory());
241 * Gets the working copy of an member opened in an editor
243 * @param member the original member or a member in a working copy
244 * @return the corresponding member in the shared working copy or <code>null</code> if not found
246 public static IMember getWorkingCopy(IMember member) throws JavaModelException {
247 ICompilationUnit cu= member.getCompilationUnit();
249 ICompilationUnit workingCopy= getWorkingCopy(cu);
250 if (workingCopy != null) {
251 return JavaModelUtil.findMemberInCompilationUnit(workingCopy, member);
258 * Returns the compilation unit for the given java element.
259 * @param element the java element whose compilation unit is searched for
260 * @return the compilation unit of the given java element
262 private static ICompilationUnit getCompilationUnit(IJavaElement element) {
267 if (element instanceof IMember)
268 return ((IMember) element).getCompilationUnit();
270 int type= element.getElementType();
271 if (IJavaElement.COMPILATION_UNIT == type)
272 return (ICompilationUnit) element;
273 if (IJavaElement.CLASS_FILE == type)
276 return getCompilationUnit(element.getParent());
280 * Returns the working copy of the given java element.
281 * @param javaElement the javaElement for which the working copyshould be found
282 * @param reconcile indicates whether the working copy must be reconcile prior to searching it
283 * @return the working copy of the given element or <code>null</code> if none
285 public static IJavaElement getWorkingCopy(IJavaElement element, boolean reconcile) throws JavaModelException {
286 ICompilationUnit unit= getCompilationUnit(element);
290 if (unit.isWorkingCopy())
293 ICompilationUnit workingCopy= getWorkingCopy(unit);
294 if (workingCopy != null) {
296 synchronized (workingCopy) {
297 workingCopy.reconcile();
298 return JavaModelUtil.findInCompilationUnit(workingCopy, element);
301 return JavaModelUtil.findInCompilationUnit(workingCopy, element);
309 * Maps the localized modifier name to a code in the same
310 * manner as #findModifier.
312 * @return the SWT modifier bit, or <code>0</code> if no match was found
316 public static int findLocalizedModifier(String token) {
320 if (token.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
322 if (token.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
324 if (token.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
326 if (token.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
333 * Returns the modifier string for the given SWT modifier
336 * @param stateMask the SWT modifier bits
337 * @return the modifier string
340 public static String getModifierString(int stateMask) {
341 String modifierString= ""; //$NON-NLS-1$
342 if ((stateMask & SWT.CTRL) == SWT.CTRL)
343 modifierString= appendModifierString(modifierString, SWT.CTRL);
344 if ((stateMask & SWT.ALT) == SWT.ALT)
345 modifierString= appendModifierString(modifierString, SWT.ALT);
346 if ((stateMask & SWT.SHIFT) == SWT.SHIFT)
347 modifierString= appendModifierString(modifierString, SWT.SHIFT);
348 if ((stateMask & SWT.COMMAND) == SWT.COMMAND)
349 modifierString= appendModifierString(modifierString, SWT.COMMAND);
351 return modifierString;
355 * Appends to modifier string of the given SWT modifier bit
356 * to the given modifierString.
358 * @param modifierString the modifier string
359 * @param modifier an int with SWT modifier bit
360 * @return the concatenated modifier string
363 private static String appendModifierString(String modifierString, int modifier) {
364 if (modifierString == null)
365 modifierString= ""; //$NON-NLS-1$
366 String newModifierString= Action.findModifierString(modifier);
367 if (modifierString.length() == 0)
368 return newModifierString;
369 return PHPEditorMessages.getFormattedString("EditorUtility.concatModifierStrings", new String[] {modifierString, newModifierString}); //$NON-NLS-1$
373 * Returns the Java project for a given editor input or <code>null</code> if no corresponding
374 * Java project exists.
376 * @param input the editor input
377 * @return the corresponding Java project
381 public static IJavaProject getJavaProject(IEditorInput input) {
382 IJavaProject jProject= null;
383 if (input instanceof IFileEditorInput) {
384 IProject project= ((IFileEditorInput)input).getFile().getProject();
385 if (project != null) {
386 jProject= JavaCore.create(project);
387 if (!jProject.exists())
391 // else if (input instanceof IClassFileEditorInput) {
392 // jProject= ((IClassFileEditorInput)input).getClassFile().getJavaProject();