KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > InternalHandlerUtil


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  ******************************************************************************/

11
12 package org.eclipse.ui.internal;
13
14 import java.util.Collection JavaDoc;
15
16 import org.eclipse.core.expressions.IEvaluationContext;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.swt.widgets.Shell;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.ISources;
21 import org.eclipse.ui.IWorkbenchPart;
22 import org.eclipse.ui.IWorkbenchSite;
23 import org.eclipse.ui.IWorkbenchWindow;
24
25 /**
26  * Some common utilities for working with handlers and IEvaluationContexts in
27  * Platform UI.
28  * <p>
29  * <b>Note</b>: this class should not be instantiated or extended by clients.
30  * </p>
31  * <p>
32  * <strong>PROVISIONAL</strong>. This class or interface has been added as part
33  * of a work in progress. There is a guarantee neither that this API will work
34  * nor that it will remain the same. Please do not use this API without
35  * consulting with the Platform/UI team.
36  * </p>
37  *
38  * @since 3.3
39  */

40 public class InternalHandlerUtil {
41     /**
42      * Extract the variable.
43      *
44      * @param appContext
45      * The application context
46      * @param name
47      * The variable name to extract.
48      * @return The object from the application context, or <code>null</code>
49      * if it could not be found.
50      */

51     public static Object JavaDoc getVariable(Object JavaDoc appContext, String JavaDoc name) {
52         if (appContext instanceof IEvaluationContext) {
53             return ((IEvaluationContext) appContext).getVariable(name);
54         }
55         return null;
56     }
57
58     /**
59      * Return the active contexts.
60      *
61      * @param appContext
62      * The execution appContext that contains the application context
63      * @return a collection of String contextIds, or <code>null</code>.
64      */

65     public static Collection JavaDoc getActiveContexts(Object JavaDoc appContext) {
66         Object JavaDoc o = getVariable(appContext, ISources.ACTIVE_CONTEXT_NAME);
67         if (o instanceof Collection JavaDoc) {
68             return (Collection JavaDoc) o;
69         }
70         return null;
71     }
72
73     /**
74      * Return the active shell. Is not necessarily the active workbench window
75      * shell.
76      *
77      * @param appContext
78      * The execution appContext that contains the application context
79      * @return the active shell, or <code>null</code>.
80      */

81     public static Shell getActiveShell(Object JavaDoc appContext) {
82         Object JavaDoc o = getVariable(appContext, ISources.ACTIVE_SHELL_NAME);
83         if (o instanceof Shell) {
84             return (Shell) o;
85         }
86         return null;
87     }
88
89     /**
90      * Return the active workbench window.
91      *
92      * @param appContext
93      * The execution appContext that contains the application context
94      * @return the active workbench window, or <code>null</code>.
95      */

96     public static IWorkbenchWindow getActiveWorkbenchWindow(Object JavaDoc appContext) {
97         Object JavaDoc o = getVariable(appContext,
98                 ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
99         if (o instanceof IWorkbenchWindow) {
100             return (IWorkbenchWindow) o;
101         }
102         return null;
103     }
104
105     /**
106      * Return the active editor.
107      *
108      * @param appContext
109      * The execution appContext that contains the application context
110      * @return the active editor, or <code>null</code>.
111      */

112     public static IEditorPart getActiveEditor(Object JavaDoc appContext) {
113         Object JavaDoc o = getVariable(appContext, ISources.ACTIVE_EDITOR_NAME);
114         if (o instanceof IEditorPart) {
115             return (IEditorPart) o;
116         }
117         return null;
118     }
119
120     /**
121      * Return the part id of the active editor.
122      *
123      * @param appContext
124      * The execution appContext that contains the application context
125      * @return the part id of the active editor, or <code>null</code>.
126      */

127     public static String JavaDoc getActiveEditorId(Object JavaDoc appContext) {
128         Object JavaDoc o = getVariable(appContext, ISources.ACTIVE_EDITOR_ID_NAME);
129         if (o instanceof String JavaDoc) {
130             return (String JavaDoc) o;
131         }
132         return null;
133     }
134
135     /**
136      * Return the active part.
137      *
138      * @param appContext
139      * The execution appContext that contains the application context
140      * @return the active part, or <code>null</code>.
141      */

142     public static IWorkbenchPart getActivePart(Object JavaDoc appContext) {
143         Object JavaDoc o = getVariable(appContext, ISources.ACTIVE_PART_NAME);
144         if (o instanceof IWorkbenchPart) {
145             return (IWorkbenchPart) o;
146         }
147         return null;
148     }
149
150     /**
151      * Return the part id of the active part.
152      *
153      * @param appContext
154      * The execution appContext that contains the application context
155      * @return the part id of the active part, or <code>null</code>.
156      */

157     public static String JavaDoc getActivePartId(Object JavaDoc appContext) {
158         Object JavaDoc o = getVariable(appContext, ISources.ACTIVE_PART_ID_NAME);
159         if (o instanceof String JavaDoc) {
160             return (String JavaDoc) o;
161         }
162         return null;
163     }
164
165     /**
166      * Return the active part site.
167      *
168      * @param appContext
169      * The execution appContext that contains the application context
170      * @return the active part site, or <code>null</code>.
171      */

172     public static IWorkbenchSite getActiveSite(Object JavaDoc appContext) {
173         Object JavaDoc o = getVariable(appContext, ISources.ACTIVE_SITE_NAME);
174         if (o instanceof IWorkbenchSite) {
175             return (IWorkbenchSite) o;
176         }
177         return null;
178     }
179
180     /**
181      * Return the current selection.
182      *
183      * @param appContext
184      * The execution appContext that contains the application context
185      * @return the current selection, or <code>null</code>.
186      */

187     public static ISelection getCurrentSelection(Object JavaDoc appContext) {
188         Object JavaDoc o = getVariable(appContext,
189                 ISources.ACTIVE_CURRENT_SELECTION_NAME);
190         if (o instanceof ISelection) {
191             return (ISelection) o;
192         }
193         return null;
194     }
195 }
196
Popular Tags