KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > loskutov > bco > ui > EclipseUtils


1 /*****************************************************************************************
2  * Copyright (c) 2004 Andrei Loskutov. All rights reserved. This program and the
3  * accompanying materials are made available under the terms of the BSD License which
4  * accompanies this distribution, and is available at
5  * http://www.opensource.org/licenses/bsd-license.php Contributor: Andrei Loskutov -
6  * initial API and implementation
7  ****************************************************************************************/

8
9 package de.loskutov.bco.ui;
10
11 import org.eclipse.jdt.core.IJavaElement;
12 import org.eclipse.jface.text.ITextSelection;
13 import org.eclipse.jface.viewers.ISelection;
14 import org.eclipse.jface.viewers.ISelectionProvider;
15 import org.eclipse.ui.IEditorInput;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.IWorkbenchPage;
18 import org.eclipse.ui.IWorkbenchWindow;
19 import org.eclipse.ui.texteditor.ITextEditor;
20
21 import de.loskutov.bco.BytecodeOutlinePlugin;
22
23 /**
24  * @author Andrei
25  */

26 public class EclipseUtils {
27
28     /**
29      * don't call me ;)
30      */

31     private EclipseUtils() {
32         super();
33     }
34
35     /**
36      * @return current active editor in workbench
37      */

38     public static IEditorPart getActiveEditor() {
39         IWorkbenchWindow window = BytecodeOutlinePlugin.getDefault()
40             .getWorkbench().getActiveWorkbenchWindow();
41         if (window != null) {
42             IWorkbenchPage page = window.getActivePage();
43             if (page != null) {
44                 return page.getActiveEditor();
45             }
46         }
47         return null;
48     }
49
50     /**
51      * @param part
52      * @return editor input as IJavaElement
53      */

54     public static IJavaElement getJavaInput(IEditorPart part) {
55         IEditorInput editorInput = part.getEditorInput();
56         if (editorInput != null) {
57             IJavaElement input = (IJavaElement) editorInput
58                 .getAdapter(IJavaElement.class);
59             return input;
60         }
61         return null;
62     }
63
64     /**
65      * @param editor
66      * @param offset
67      * @param length
68      */

69     public static void selectInEditor(ITextEditor editor, int offset, int length) {
70         IEditorPart active = getActiveEditor();
71         if (active != editor) {
72             editor.getSite().getPage().activate(editor);
73         }
74         editor.selectAndReveal(offset, length);
75     }
76
77     /**
78      * @param selectionProvider
79      * @return TextSelection or null, if provider does not provide TextSelection's
80      */

81     public static ITextSelection getSelection(
82         ISelectionProvider selectionProvider) {
83         ISelection selection = selectionProvider.getSelection();
84         if (selection instanceof ITextSelection) {
85             return (ITextSelection) selection;
86         }
87         return null;
88     }
89
90     /**
91      * @param resource
92      * @return full package name in default java notation (with dots)
93      */

94     public static String JavaDoc getJavaPackageName(IJavaElement resource) {
95         String JavaDoc name = resource == null
96             ? null : resource.getElementName(); //$NON-NLS-1$
97
if (name == null) {
98             return ""; //$NON-NLS-1$
99
}
100         int type = resource.getElementType();
101         if (type == IJavaElement.PACKAGE_FRAGMENT
102             || type == IJavaElement.PACKAGE_FRAGMENT_ROOT) {
103             return name;
104         }
105         IJavaElement ancestor = resource
106             .getAncestor(IJavaElement.PACKAGE_FRAGMENT);
107         if (ancestor != null) {
108             return ancestor.getElementName();
109         }
110         return ""; //$NON-NLS-1$
111
}
112
113 }
Popular Tags