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.IJavaElement;
16 import net.sourceforge.phpdt.core.JavaModelException;
17 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.jface.action.Action;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.ui.IEditorDescriptor;
23 import org.eclipse.ui.IEditorInput;
24 import org.eclipse.ui.IEditorPart;
25 import org.eclipse.ui.IEditorRegistry;
26 import org.eclipse.ui.IFileEditorInput;
27 import org.eclipse.ui.IWorkbenchPage;
28 import org.eclipse.ui.PartInitException;
29 import org.eclipse.ui.PlatformUI;
30 import org.eclipse.ui.part.FileEditorInput;
31 import org.eclipse.ui.texteditor.ITextEditor;
35 * A number of routines for working with JavaElements in editors
37 * Use 'isOpenInEditor' to test if an element is already open in a editor
38 * Use 'openInEditor' to force opening an element in a editor
39 * With 'getWorkingCopy' you get the working copy (element in the editor) of an element
41 public class EditorUtility {
44 public static boolean isEditorInput(Object element, IEditorPart editor) {
47 return editor.getEditorInput().equals(getEditorInput(element));
48 // } catch (JavaModelException x) {
49 // JavaPlugin.log(x.getStatus());
56 * Tests if a cu is currently shown in an editor
57 * @return the IEditorPart if shown, null if element is not open in an editor
59 public static IEditorPart isOpenInEditor(Object inputElement) {
60 IEditorInput input= null;
63 input= getEditorInput(inputElement);
64 // } catch (JavaModelException x) {
65 // JavaPlugin.log(x.getStatus());
69 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
71 return p.findEditor(input);
79 * Opens a Java editor for an element such as <code>IJavaElement</code>, <code>IFile</code>, or <code>IStorage</code>.
80 * The editor is activated by default.
81 * @return the IEditorPart or null if wrong element type or opening failed
83 public static IEditorPart openInEditor(Object inputElement) throws JavaModelException, PartInitException {
84 return openInEditor(inputElement, true);
88 * Opens a Java editor for an element (IJavaElement, IFile, IStorage...)
89 * @return the IEditorPart or null if wrong element type or opening failed
91 public static IEditorPart openInEditor(Object inputElement, boolean activate) throws JavaModelException, PartInitException {
93 if (inputElement instanceof IFile)
94 return openInEditor((IFile) inputElement, activate);
96 IEditorInput input= getEditorInput(inputElement);
97 if (input instanceof IFileEditorInput) {
98 IFileEditorInput fileInput= (IFileEditorInput) input;
99 return openInEditor(fileInput.getFile(), activate);
103 return openInEditor(input, getEditorID(input, inputElement), activate);
109 * Selects a Java Element in an editor
111 // public static void revealInEditor(IEditorPart part, IJavaElement element) {
112 // if (element != null && part instanceof PHPEditor) {
113 // ((PHPEditor) part).setSelection(element);
117 private static IEditorPart openInEditor(IFile file, boolean activate) throws PartInitException {
119 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
121 IEditorPart editorPart= p.openEditor(file, null, activate);
122 initializeHighlightRange(editorPart);
129 private static IEditorPart openInEditor(IEditorInput input, String editorID, boolean activate) throws PartInitException {
131 IWorkbenchPage p= PHPeclipsePlugin.getActivePage();
133 IEditorPart editorPart= p.openEditor(input, editorID, activate);
134 initializeHighlightRange(editorPart);
141 private static void initializeHighlightRange(IEditorPart editorPart) {
142 if (editorPart instanceof ITextEditor) {
143 TogglePresentationAction toggleAction= new TogglePresentationAction();
145 toggleAction.setEditor((ITextEditor)editorPart);
147 toggleAction.setEditor(null);
152 *@deprecated Made it public again for java debugger UI.
154 public static String getEditorID(IEditorInput input, Object inputObject) {
155 IEditorRegistry registry= PlatformUI.getWorkbench().getEditorRegistry();
156 IEditorDescriptor descriptor= registry.getDefaultEditor(input.getName());
157 if (descriptor != null)
158 return descriptor.getId();
162 // private static IEditorInput getEditorInput(IJavaElement element) throws JavaModelException {
163 // while (element != null) {
164 // if (element instanceof IWorkingCopy && ((IWorkingCopy) element).isWorkingCopy())
165 // element= ((IWorkingCopy) element).getOriginalElement();
167 // if (element instanceof ICompilationUnit) {
168 // ICompilationUnit unit= (ICompilationUnit) element;
169 // IResource resource= unit.getResource();
170 // if (resource instanceof IFile)
171 // return new FileEditorInput((IFile) resource);
174 // if (element instanceof IClassFile)
175 // return new InternalClassFileEditorInput((IClassFile) element);
177 // element= element.getParent();
183 public static IEditorInput getEditorInput(Object input) {
184 // throws JavaModelException {
186 if (input instanceof IJavaElement)
187 return getEditorInput((IJavaElement) input);
189 if (input instanceof IFile)
190 return new FileEditorInput((IFile) input);
192 // if (input instanceof IStorage)
193 // return new JarEntryEditorInput((IStorage)input);
199 * If the current active editor edits a java element return it, else
202 public static IJavaElement getActiveEditorJavaInput() {
203 IWorkbenchPage page= PHPeclipsePlugin.getActivePage();
205 IEditorPart part= page.getActiveEditor();
207 IEditorInput editorInput= part.getEditorInput();
208 if (editorInput != null) {
209 return (IJavaElement)editorInput.getAdapter(IJavaElement.class);
217 * Gets the working copy of an compilation unit opened in an editor
218 * @param part the editor part
219 * @param cu the original compilation unit (or another working copy)
220 * @return the working copy of the compilation unit, or null if not found
222 // public static ICompilationUnit getWorkingCopy(ICompilationUnit cu) {
225 // if (cu.isWorkingCopy())
228 // return (ICompilationUnit)cu.findSharedWorkingCopy(JavaUI.getBufferFactory());
232 * Gets the working copy of an member opened in an editor
234 * @param member the original member or a member in a working copy
235 * @return the corresponding member in the shared working copy or <code>null</code> if not found
237 // public static IMember getWorkingCopy(IMember member) throws JavaModelException {
238 // ICompilationUnit cu= member.getCompilationUnit();
240 // ICompilationUnit workingCopy= getWorkingCopy(cu);
241 // if (workingCopy != null) {
242 // return JavaModelUtil.findMemberInCompilationUnit(workingCopy, member);
249 * Returns the compilation unit for the given java element.
250 * @param element the java element whose compilation unit is searched for
251 * @return the compilation unit of the given java element
253 // private static ICompilationUnit getCompilationUnit(IJavaElement element) {
255 // if (element == null)
258 // if (element instanceof IMember)
259 // return ((IMember) element).getCompilationUnit();
261 // int type= element.getElementType();
262 // if (IJavaElement.COMPILATION_UNIT == type)
263 // return (ICompilationUnit) element;
264 // if (IJavaElement.CLASS_FILE == type)
267 // return getCompilationUnit(element.getParent());
271 * Returns the working copy of the given java element.
272 * @param javaElement the javaElement for which the working copyshould be found
273 * @param reconcile indicates whether the working copy must be reconcile prior to searching it
274 * @return the working copy of the given element or <code>null</code> if none
276 // public static IJavaElement getWorkingCopy(IJavaElement element, boolean reconcile) throws JavaModelException {
277 // ICompilationUnit unit= getCompilationUnit(element);
281 // if (unit.isWorkingCopy())
284 // ICompilationUnit workingCopy= getWorkingCopy(unit);
285 // if (workingCopy != null) {
287 // synchronized (workingCopy) {
288 // workingCopy.reconcile();
289 // return JavaModelUtil.findInCompilationUnit(workingCopy, element);
292 // return JavaModelUtil.findInCompilationUnit(workingCopy, element);
300 * Maps the localized modifier name to a code in the same
301 * manner as #findModifier.
303 * @return the SWT modifier bit, or <code>0</code> if no match was found
307 public static int findLocalizedModifier(String token) {
311 if (token.equalsIgnoreCase(Action.findModifierString(SWT.CTRL)))
313 if (token.equalsIgnoreCase(Action.findModifierString(SWT.SHIFT)))
315 if (token.equalsIgnoreCase(Action.findModifierString(SWT.ALT)))
317 if (token.equalsIgnoreCase(Action.findModifierString(SWT.COMMAND)))
324 * Returns the modifier string for the given SWT modifier
327 * @param stateMask the SWT modifier bits
328 * @return the modifier string
331 public static String getModifierString(int stateMask) {
332 String modifierString= ""; //$NON-NLS-1$
333 if ((stateMask & SWT.CTRL) == SWT.CTRL)
334 modifierString= appendModifierString(modifierString, SWT.CTRL);
335 if ((stateMask & SWT.ALT) == SWT.ALT)
336 modifierString= appendModifierString(modifierString, SWT.ALT);
337 if ((stateMask & SWT.SHIFT) == SWT.SHIFT)
338 modifierString= appendModifierString(modifierString, SWT.SHIFT);
339 if ((stateMask & SWT.COMMAND) == SWT.COMMAND)
340 modifierString= appendModifierString(modifierString, SWT.COMMAND);
342 return modifierString;
346 * Appends to modifier string of the given SWT modifier bit
347 * to the given modifierString.
349 * @param modifierString the modifier string
350 * @param modifier an int with SWT modifier bit
351 * @return the concatenated modifier string
354 private static String appendModifierString(String modifierString, int modifier) {
355 if (modifierString == null)
356 modifierString= ""; //$NON-NLS-1$
357 String newModifierString= Action.findModifierString(modifier);
358 if (modifierString.length() == 0)
359 return newModifierString;
360 return PHPEditorMessages.getFormattedString("EditorUtility.concatModifierStrings", new String[] {modifierString, newModifierString}); //$NON-NLS-1$