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;
24 import net.sourceforge.phpeclipse.ui.WebUI;
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;
44 * A number of routines for working with JavaElements in editors
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
50 public class EditorUtility {
52 // public static boolean isEditorInput(Object element, IEditorPart editor) {
53 // if (editor != null) {
55 // return editor.getEditorInput().equals(getEditorInput(element));
56 // } catch (JavaModelException x) {
57 // PHPeclipsePlugin.log(x.getStatus());
64 * Tests if a cu is currently shown in an editor
66 * @return the IEditorPart if shown, null if element is not open in an
69 // public static IEditorPart isOpenInEditor(Object inputElement) {
70 // IEditorInput input = null;
73 // input = getEditorInput(inputElement);
74 // } catch (JavaModelException x) {
75 // PHPeclipsePlugin.log(x.getStatus());
78 // if (input != null) {
79 // IWorkbenchPage p = WebUI.getActivePage();
81 // return p.findEditor(input);
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.
93 * @return the IEditorPart or null if wrong element type or opening failed
95 public static IEditorPart openInEditor(Object inputElement)
96 throws JavaModelException, PartInitException {
97 return openInEditor(inputElement, true);
101 * Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
103 * @return the IEditorPart or null if wrong element type or opening failed
105 public static IEditorPart openInEditor(Object inputElement, boolean activate)
106 throws JavaModelException, PartInitException {
108 if (inputElement instanceof IFile)
109 return openInEditor((IFile) inputElement, activate);
111 IEditorInput input = getEditorInput(inputElement);
112 if (input instanceof IFileEditorInput) {
113 IFileEditorInput fileInput = (IFileEditorInput) input;
114 return openInEditor(fileInput.getFile(), activate);
118 return openInEditor(input, getEditorID(input, inputElement),
125 * Selects a Java Element in an editor
127 public static void revealInEditor(IEditorPart part, IJavaElement element) {
128 if (element != null && part instanceof PHPEditor) {
129 ((PHPEditor) part).setSelection(element);
133 private static IEditorPart openInEditor(IFile file, boolean activate)
134 throws PartInitException {
136 IWorkbenchPage p = WebUI.getActivePage();
138 IEditorPart editorPart = IDE.openEditor(p, file, activate);
139 initializeHighlightRange(editorPart);
146 private static IEditorPart openInEditor(IEditorInput input,
147 String editorID, boolean activate) throws PartInitException {
149 IWorkbenchPage p = WebUI.getActivePage();
151 IEditorPart editorPart = p
152 .openEditor(input, editorID, activate);
153 initializeHighlightRange(editorPart);
160 private static void initializeHighlightRange(IEditorPart editorPart) {
161 if (editorPart instanceof ITextEditor) {
162 TogglePresentationAction toggleAction = new TogglePresentationAction();
164 toggleAction.setEditor((ITextEditor) editorPart);
166 toggleAction.setEditor(null);
171 * @deprecated Made it public again for java debugger UI.
173 public static String getEditorID(IEditorInput input, Object inputObject) {
174 IEditorRegistry registry = PlatformUI.getWorkbench()
175 .getEditorRegistry();
176 IEditorDescriptor descriptor = registry.getDefaultEditor(input
178 if (descriptor != null)
179 return descriptor.getId();
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();
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);
197 // if (element instanceof IClassFile)
198 // return new InternalClassFileEditorInput((IClassFile) element);
200 element = element.getParent();
206 public static IEditorInput getEditorInput(Object input)
207 throws JavaModelException {
209 if (input instanceof IJavaElement)
210 return getEditorInput((IJavaElement) input);
212 if (input instanceof IFile)
213 return new FileEditorInput((IFile) input);
215 // if (input instanceof IStorage)
216 // return new JarEntryEditorInput((IStorage)input);
222 * If the current active editor edits a java element return it, else return
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);
241 * Gets the working copy of an compilation unit opened in an editor
246 * the original compilation unit (or another working copy)
247 * @return the working copy of the compilation unit, or null if not found
249 public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
252 if (cu.isWorkingCopy())
255 return (ICompilationUnit) cu.findSharedWorkingCopy(JavaUI
256 .getBufferFactory());
260 * Gets the working copy of an member opened in an editor
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
267 // public static IMember getWorkingCopy(IMember member)
268 // throws JavaModelException {
269 // ICompilationUnit cu = member.getCompilationUnit();
271 // ICompilationUnit workingCopy = getWorkingCopy(cu);
272 // if (workingCopy != null) {
273 // return JavaModelUtil.findMemberInCompilationUnit(workingCopy,
281 * Returns the compilation unit for the given java element.
284 * the java element whose compilation unit is searched for
285 * @return the compilation unit of the given java element
287 private static ICompilationUnit getCompilationUnit(IJavaElement element) {
292 if (element instanceof IMember)
293 return ((IMember) element).getCompilationUnit();
295 int type = element.getElementType();
296 if (IJavaElement.COMPILATION_UNIT == type)
297 return (ICompilationUnit) element;
298 if (IJavaElement.CLASS_FILE == type)
301 return getCompilationUnit(element.getParent());
305 * Returns the working copy of the given java element.
308 * the javaElement for which the working copyshould be found
310 * indicates whether the working copy must be reconcile prior to
312 * @return the working copy of the given element or <code>null</code> if
315 public static IJavaElement getWorkingCopy(IJavaElement element,
316 boolean reconcile) throws JavaModelException {
317 ICompilationUnit unit = getCompilationUnit(element);
321 if (unit.isWorkingCopy())
324 ICompilationUnit workingCopy = getWorkingCopy(unit);
325 if (workingCopy != null) {
327 synchronized (workingCopy) {
328 workingCopy.reconcile();
329 return JavaModelUtil.findInCompilationUnit(workingCopy,
334 .findInCompilationUnit(workingCopy, element);
342 * Maps the localized modifier name to a code in the same manner as
345 * @return the SWT modifier bit, or <code>0</code> if no match was found
349 public static int findLocalizedModifier(String token) {
353 if (token.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
355 if (token.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
357 if (token.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
359 if (token.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
366 * Returns the modifier string for the given SWT modifier modifier bits.
369 * the SWT modifier bits
370 * @return the modifier string
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);
384 return modifierString;
388 * Appends to modifier string of the given SWT modifier bit to the given
391 * @param modifierString
392 * the modifier string
394 * an int with SWT modifier bit
395 * @return the concatenated modifier string
398 private static String appendModifierString(String modifierString,
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
407 "EditorUtility.concatModifierStrings", new String[] { modifierString, newModifierString }); //$NON-NLS-1$
411 * Returns the Java project for a given editor input or <code>null</code>
412 * if no corresponding Java project exists.
416 * @return the corresponding Java project
420 public static IJavaProject getJavaProject(IEditorInput input) {
421 IJavaProject jProject = null;
422 if (input instanceof IFileEditorInput) {
423 IProject project = ((IFileEditorInput) input).getFile()
425 if (project != null) {
426 jProject = JavaCore.create(project);
427 if (!jProject.exists())
431 // else if (input instanceof IClassFileEditorInput) {
433 // ((IClassFileEditorInput)input).getClassFile().getJavaProject();