KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > handlers > HandlerUtil


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.handlers;
13
14 import java.util.Collection JavaDoc;
15
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.expressions.IEvaluationContext;
19 import org.eclipse.jface.viewers.ISelection;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.ISources;
23 import org.eclipse.ui.IWorkbenchPart;
24 import org.eclipse.ui.IWorkbenchSite;
25 import org.eclipse.ui.IWorkbenchWindow;
26
27 /**
28  * Some common utilities for working with handlers in Platform UI.
29  * <p>
30  * <b>Note</b>: this class should not be instantiated or extended by clients.
31  * </p>
32  *
33  * @since 3.3
34  */

35 public class HandlerUtil {
36     private static void noVariableFound(ExecutionEvent event, String JavaDoc name)
37             throws ExecutionException {
38         throw new ExecutionException("No " + name //$NON-NLS-1$
39
+ " found while executing " + event.getCommand().getId()); //$NON-NLS-1$
40
}
41
42     private static void incorrectTypeFound(ExecutionEvent event, String JavaDoc name,
43             Class JavaDoc expectedType, Class JavaDoc wrongType) throws ExecutionException {
44         throw new ExecutionException("Incorrect type for " //$NON-NLS-1$
45
+ name
46                 + " found while executing " //$NON-NLS-1$
47
+ event.getCommand().getId()
48                 + ", expected " + expectedType.getName() //$NON-NLS-1$
49
+ " found " + wrongType.getName()); //$NON-NLS-1$
50
}
51
52     /**
53      * Extract the variable.
54      *
55      * @param event
56      * The execution event that contains the application context
57      * @param name
58      * The variable name to extract.
59      * @return The object from the application context, or <code>null</code>
60      * if it could not be found.
61      */

62     public static Object JavaDoc getVariable(ExecutionEvent event, String JavaDoc name) {
63         if (event.getApplicationContext() instanceof IEvaluationContext) {
64             return ((IEvaluationContext) event.getApplicationContext())
65                     .getVariable(name);
66         }
67         return null;
68     }
69
70     /**
71      * Extract the variable.
72      *
73      * @param event
74      * The execution event that contains the application context
75      * @param name
76      * The variable name to extract.
77      * @return The object from the application context. Will not return
78      * <code>null</code>.
79      * @throws ExecutionException
80      * if the variable is not found.
81      */

82     public static Object JavaDoc getVariableChecked(ExecutionEvent event, String JavaDoc name)
83             throws ExecutionException {
84         Object JavaDoc o = getVariable(event, name);
85         if (o == null) {
86             noVariableFound(event, name);
87         }
88         return o;
89     }
90
91     /**
92      * Return the active contexts.
93      *
94      * @param event
95      * The execution event that contains the application context
96      * @return a collection of String contextIds, or <code>null</code>.
97      */

98     public static Collection JavaDoc getActiveContexts(ExecutionEvent event) {
99         Object JavaDoc o = getVariable(event, ISources.ACTIVE_CONTEXT_NAME);
100         if (o instanceof Collection JavaDoc) {
101             return (Collection JavaDoc) o;
102         }
103         return null;
104     }
105
106     /**
107      * Return the active contexts.
108      *
109      * @param event
110      * The execution event that contains the application context
111      * @return a collection of String contextIds. Will not return
112      * <code>null</code>.
113      * @throws ExecutionException
114      * If the context variable is not found.
115      */

116     public static Collection JavaDoc getActiveContextsChecked(ExecutionEvent event)
117             throws ExecutionException {
118         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_CONTEXT_NAME);
119         if (!(o instanceof Collection JavaDoc)) {
120             incorrectTypeFound(event, ISources.ACTIVE_CONTEXT_NAME,
121                     Collection JavaDoc.class, o.getClass());
122         }
123         return (Collection JavaDoc) o;
124     }
125
126     /**
127      * Return the active shell. Is not necessarily the active workbench window
128      * shell.
129      *
130      * @param event
131      * The execution event that contains the application context
132      * @return the active shell, or <code>null</code>.
133      */

134     public static Shell getActiveShell(ExecutionEvent event) {
135         Object JavaDoc o = getVariable(event, ISources.ACTIVE_SHELL_NAME);
136         if (o instanceof Shell) {
137             return (Shell) o;
138         }
139         return null;
140     }
141
142     /**
143      * Return the active shell. Is not necessarily the active workbench window
144      * shell.
145      *
146      * @param event
147      * The execution event that contains the application context
148      * @return the active shell. Will not return <code>null</code>.
149      * @throws ExecutionException
150      * If the active shell variable is not found.
151      */

152     public static Shell getActiveShellChecked(ExecutionEvent event)
153             throws ExecutionException {
154         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_SHELL_NAME);
155         if (!(o instanceof Shell)) {
156             incorrectTypeFound(event, ISources.ACTIVE_SHELL_NAME, Shell.class,
157                     o.getClass());
158         }
159         return (Shell) o;
160     }
161
162     /**
163      * Return the active workbench window.
164      *
165      * @param event
166      * The execution event that contains the application context
167      * @return the active workbench window, or <code>null</code>.
168      */

169     public static IWorkbenchWindow getActiveWorkbenchWindow(ExecutionEvent event) {
170         Object JavaDoc o = getVariable(event, ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
171         if (o instanceof IWorkbenchWindow) {
172             return (IWorkbenchWindow) o;
173         }
174         return null;
175     }
176
177     /**
178      * Return the active workbench window.
179      *
180      * @param event
181      * The execution event that contains the application context
182      * @return the active workbench window. Will not return <code>null</code>.
183      * @throws ExecutionException
184      * If the active workbench window variable is not found.
185      */

186     public static IWorkbenchWindow getActiveWorkbenchWindowChecked(
187             ExecutionEvent event) throws ExecutionException {
188         Object JavaDoc o = getVariableChecked(event,
189                 ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
190         if (!(o instanceof IWorkbenchWindow)) {
191             incorrectTypeFound(event, ISources.ACTIVE_WORKBENCH_WINDOW_NAME,
192                     IWorkbenchWindow.class, o.getClass());
193         }
194         return (IWorkbenchWindow) o;
195     }
196
197     /**
198      * Return the active editor.
199      *
200      * @param event
201      * The execution event that contains the application context
202      * @return the active editor, or <code>null</code>.
203      */

204     public static IEditorPart getActiveEditor(ExecutionEvent event) {
205         Object JavaDoc o = getVariable(event, ISources.ACTIVE_EDITOR_NAME);
206         if (o instanceof IEditorPart) {
207             return (IEditorPart) o;
208         }
209         return null;
210     }
211
212     /**
213      * Return the active editor.
214      *
215      * @param event
216      * The execution event that contains the application context
217      * @return the active editor. Will not return <code>null</code>.
218      * @throws ExecutionException
219      * If the active editor variable is not found.
220      */

221     public static IEditorPart getActiveEditorChecked(ExecutionEvent event)
222             throws ExecutionException {
223         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_EDITOR_NAME);
224         if (!(o instanceof IEditorPart)) {
225             incorrectTypeFound(event, ISources.ACTIVE_EDITOR_NAME,
226                     IEditorPart.class, o.getClass());
227         }
228         return (IEditorPart) o;
229     }
230
231     /**
232      * Return the part id of the active editor.
233      *
234      * @param event
235      * The execution event that contains the application context
236      * @return the part id of the active editor, or <code>null</code>.
237      */

238     public static String JavaDoc getActiveEditorId(ExecutionEvent event) {
239         Object JavaDoc o = getVariable(event, ISources.ACTIVE_EDITOR_ID_NAME);
240         if (o instanceof String JavaDoc) {
241             return (String JavaDoc) o;
242         }
243         return null;
244     }
245
246     /**
247      * Return the part id of the active editor.
248      *
249      * @param event
250      * The execution event that contains the application context
251      * @return the part id of the active editor. Will not return
252      * <code>null</code>.
253      * @throws ExecutionException
254      * If the active editor id variable is not found.
255      */

256     public static String JavaDoc getActiveEditorIdChecked(ExecutionEvent event)
257             throws ExecutionException {
258         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_EDITOR_ID_NAME);
259         if (!(o instanceof String JavaDoc)) {
260             incorrectTypeFound(event, ISources.ACTIVE_EDITOR_ID_NAME,
261                     String JavaDoc.class, o.getClass());
262         }
263         return (String JavaDoc) o;
264     }
265
266     /**
267      * Return the active part.
268      *
269      * @param event
270      * The execution event that contains the application context
271      * @return the active part, or <code>null</code>.
272      */

273     public static IWorkbenchPart getActivePart(ExecutionEvent event) {
274         Object JavaDoc o = getVariable(event, ISources.ACTIVE_PART_NAME);
275         if (o instanceof IWorkbenchPart) {
276             return (IWorkbenchPart) o;
277         }
278         return null;
279     }
280
281     /**
282      * Return the active part.
283      *
284      * @param event
285      * The execution event that contains the application context
286      * @return the active part. Will not return <code>null</code>.
287      * @throws ExecutionException
288      * If the active part variable is not found.
289      */

290     public static IWorkbenchPart getActivePartChecked(ExecutionEvent event)
291             throws ExecutionException {
292         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_PART_NAME);
293         if (!(o instanceof IWorkbenchPart)) {
294             incorrectTypeFound(event, ISources.ACTIVE_PART_NAME,
295                     IWorkbenchPart.class, o.getClass());
296         }
297         return (IWorkbenchPart) o;
298     }
299
300     /**
301      * Return the part id of the active part.
302      *
303      * @param event
304      * The execution event that contains the application context
305      * @return the part id of the active part, or <code>null</code>.
306      */

307     public static String JavaDoc getActivePartId(ExecutionEvent event) {
308         Object JavaDoc o = getVariable(event, ISources.ACTIVE_PART_ID_NAME);
309         if (o instanceof String JavaDoc) {
310             return (String JavaDoc) o;
311         }
312         return null;
313     }
314
315     /**
316      * Return the part id of the active part.
317      *
318      * @param event
319      * The execution event that contains the application context
320      * @return the part id of the active part. Will not return <code>null</code>.
321      * @throws ExecutionException
322      * If the active part id variable is not found.
323      */

324     public static String JavaDoc getActivePartIdChecked(ExecutionEvent event)
325             throws ExecutionException {
326         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_PART_ID_NAME);
327         if (!(o instanceof String JavaDoc)) {
328             incorrectTypeFound(event, ISources.ACTIVE_PART_ID_NAME,
329                     String JavaDoc.class, o.getClass());
330         }
331         return (String JavaDoc) o;
332     }
333
334     /**
335      * Return the active part site.
336      *
337      * @param event
338      * The execution event that contains the application context
339      * @return the active part site, or <code>null</code>.
340      */

341     public static IWorkbenchSite getActiveSite(ExecutionEvent event) {
342         Object JavaDoc o = getVariable(event, ISources.ACTIVE_SITE_NAME);
343         if (o instanceof IWorkbenchSite) {
344             return (IWorkbenchSite) o;
345         }
346         return null;
347     }
348
349     /**
350      * Return the active part site.
351      *
352      * @param event
353      * The execution event that contains the application context
354      * @return the active part site. Will not return <code>null</code>.
355      * @throws ExecutionException
356      * If the active part site variable is not found.
357      */

358     public static IWorkbenchSite getActiveSiteChecked(ExecutionEvent event)
359             throws ExecutionException {
360         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_SITE_NAME);
361         if (!(o instanceof IWorkbenchSite)) {
362             incorrectTypeFound(event, ISources.ACTIVE_SITE_NAME,
363                     IWorkbenchSite.class, o.getClass());
364         }
365         return (IWorkbenchSite) o;
366     }
367
368     /**
369      * Return the current selection.
370      *
371      * @param event
372      * The execution event that contains the application context
373      * @return the current selection, or <code>null</code>.
374      */

375     public static ISelection getCurrentSelection(ExecutionEvent event) {
376         Object JavaDoc o = getVariable(event, ISources.ACTIVE_CURRENT_SELECTION_NAME);
377         if (o instanceof ISelection) {
378             return (ISelection) o;
379         }
380         return null;
381     }
382
383     /**
384      * Return the current selection.
385      *
386      * @param event
387      * The execution event that contains the application context
388      * @return the current selection. Will not return <code>null</code>.
389      * @throws ExecutionException
390      * If the current selection variable is not found.
391      */

392     public static ISelection getCurrentSelectionChecked(ExecutionEvent event)
393             throws ExecutionException {
394         Object JavaDoc o = getVariableChecked(event,
395                 ISources.ACTIVE_CURRENT_SELECTION_NAME);
396         if (!(o instanceof ISelection)) {
397             incorrectTypeFound(event, ISources.ACTIVE_CURRENT_SELECTION_NAME,
398                     ISelection.class, o.getClass());
399         }
400         return (ISelection) o;
401     }
402
403     /**
404      * Return the menu IDs that were applied to the registered context
405      * menu. For example, #CompilationUnitEditorContext.
406      *
407      * @param event
408      * The execution event that contains the application context
409      * @return the menu IDs, or <code>null</code>.
410      */

411     public static Collection JavaDoc getActiveMenus(ExecutionEvent event) {
412         Object JavaDoc o = getVariable(event, ISources.ACTIVE_MENU_NAME);
413         if (o instanceof Collection JavaDoc) {
414             return (Collection JavaDoc) o;
415         }
416         return null;
417     }
418
419     /**
420      * Return the menu IDs that were applied to the registered context
421      * menu. For example, #CompilationUnitEditorContext.
422      *
423      * @param event
424      * The execution event that contains the application context
425      * @return the menu IDs. Will not return <code>null</code>.
426      * @throws ExecutionException
427      * If the active menus variable is not found.
428      */

429     public static Collection JavaDoc getActiveMenusChecked(ExecutionEvent event)
430             throws ExecutionException {
431         Object JavaDoc o = getVariableChecked(event, ISources.ACTIVE_MENU_NAME);
432         if (!(o instanceof Collection JavaDoc)) {
433             incorrectTypeFound(event, ISources.ACTIVE_MENU_NAME,
434                     Collection JavaDoc.class, o.getClass());
435         }
436         return (Collection JavaDoc) o;
437     }
438
439     /**
440      * Return the active menu selection. The active menu is a registered context
441      * menu.
442      *
443      * @param event
444      * The execution event that contains the application context
445      * @return the active menu selection, or <code>null</code>.
446      */

447     public static ISelection getActiveMenuSelection(ExecutionEvent event) {
448         Object JavaDoc o = getVariable(event, ISources.ACTIVE_MENU_SELECTION_NAME);
449         if (o instanceof ISelection) {
450             return (ISelection) o;
451         }
452         return null;
453     }
454
455     /**
456      * Return the active menu selection. The active menu is a registered context
457      * menu.
458      *
459      * @param event
460      * The execution event that contains the application context
461      * @return the active menu selection. Will not return <code>null</code>.
462      * @throws ExecutionException
463      * If the active menu selection variable is not found.
464      */

465     public static ISelection getActiveMenuSelectionChecked(ExecutionEvent event)
466             throws ExecutionException {
467         Object JavaDoc o = getVariableChecked(event,
468                 ISources.ACTIVE_MENU_SELECTION_NAME);
469         if (!(o instanceof ISelection)) {
470             incorrectTypeFound(event, ISources.ACTIVE_MENU_SELECTION_NAME,
471                     ISelection.class, o.getClass());
472         }
473         return (ISelection) o;
474     }
475
476     /**
477      * Return the active menu editor input, if available. The active menu is a
478      * registered context menu.
479      *
480      * @param event
481      * The execution event that contains the application context
482      * @return the active menu editor, or <code>null</code>.
483      */

484     public static ISelection getActiveMenuEditorInput(ExecutionEvent event) {
485         Object JavaDoc o = getVariable(event, ISources.ACTIVE_MENU_EDITOR_INPUT_NAME);
486         if (o instanceof ISelection) {
487             return (ISelection) o;
488         }
489         return null;
490     }
491
492     /**
493      * Return the active menu editor input. The active menu is a
494      * registered context menu. Some context menus do not include the
495      * editor input which will throw an exception.
496      *
497      * @param event
498      * The execution event that contains the application context
499      * @return the active menu editor input. Will not return <code>null</code>.
500      * @throws ExecutionException
501      * If the active menu editor input variable is not found.
502      */

503     public static ISelection getActiveMenuEditorInputChecked(
504             ExecutionEvent event) throws ExecutionException {
505         Object JavaDoc o = getVariableChecked(event,
506                 ISources.ACTIVE_MENU_EDITOR_INPUT_NAME);
507         if (!(o instanceof ISelection)) {
508             incorrectTypeFound(event, ISources.ACTIVE_MENU_EDITOR_INPUT_NAME,
509                     ISelection.class, o.getClass());
510         }
511         return (ISelection) o;
512     }
513 }
514
Popular Tags