001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.handlers;
011:
012: import java.util.Collection;
013:
014: import org.eclipse.core.commands.ExecutionEvent;
015: import org.eclipse.core.commands.ExecutionException;
016: import org.eclipse.core.expressions.IEvaluationContext;
017: import org.eclipse.jface.viewers.ISelection;
018: import org.eclipse.swt.widgets.Shell;
019: import org.eclipse.ui.IEditorPart;
020: import org.eclipse.ui.ISources;
021: import org.eclipse.ui.IWorkbenchPart;
022: import org.eclipse.ui.IWorkbenchSite;
023: import org.eclipse.ui.IWorkbenchWindow;
024:
025: /**
026: * Some common utilities for working with handlers in Platform UI.
027: * <p>
028: * <b>Note</b>: this class should not be instantiated or extended by clients.
029: * </p>
030: *
031: * @since 3.3
032: */
033: public class HandlerUtil {
034: private static void noVariableFound(ExecutionEvent event,
035: String name) throws ExecutionException {
036: throw new ExecutionException(
037: "No " + name //$NON-NLS-1$
038: + " found while executing " + event.getCommand().getId()); //$NON-NLS-1$
039: }
040:
041: private static void incorrectTypeFound(ExecutionEvent event,
042: String name, Class expectedType, Class wrongType)
043: throws ExecutionException {
044: throw new ExecutionException("Incorrect type for " //$NON-NLS-1$
045: + name
046: + " found while executing " //$NON-NLS-1$
047: + event.getCommand().getId()
048: + ", expected " + expectedType.getName() //$NON-NLS-1$
049: + " found " + wrongType.getName()); //$NON-NLS-1$
050: }
051:
052: /**
053: * Extract the variable.
054: *
055: * @param event
056: * The execution event that contains the application context
057: * @param name
058: * The variable name to extract.
059: * @return The object from the application context, or <code>null</code>
060: * if it could not be found.
061: */
062: public static Object getVariable(ExecutionEvent event, String name) {
063: if (event.getApplicationContext() instanceof IEvaluationContext) {
064: return ((IEvaluationContext) event.getApplicationContext())
065: .getVariable(name);
066: }
067: return null;
068: }
069:
070: /**
071: * Extract the variable.
072: *
073: * @param event
074: * The execution event that contains the application context
075: * @param name
076: * The variable name to extract.
077: * @return The object from the application context. Will not return
078: * <code>null</code>.
079: * @throws ExecutionException
080: * if the variable is not found.
081: */
082: public static Object getVariableChecked(ExecutionEvent event,
083: String name) throws ExecutionException {
084: Object o = getVariable(event, name);
085: if (o == null) {
086: noVariableFound(event, name);
087: }
088: return o;
089: }
090:
091: /**
092: * Return the active contexts.
093: *
094: * @param event
095: * The execution event that contains the application context
096: * @return a collection of String contextIds, or <code>null</code>.
097: */
098: public static Collection getActiveContexts(ExecutionEvent event) {
099: Object o = getVariable(event, ISources.ACTIVE_CONTEXT_NAME);
100: if (o instanceof Collection) {
101: return (Collection) 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 getActiveContextsChecked(
117: ExecutionEvent event) throws ExecutionException {
118: Object o = getVariableChecked(event,
119: ISources.ACTIVE_CONTEXT_NAME);
120: if (!(o instanceof Collection)) {
121: incorrectTypeFound(event, ISources.ACTIVE_CONTEXT_NAME,
122: Collection.class, o.getClass());
123: }
124: return (Collection) o;
125: }
126:
127: /**
128: * Return the active shell. Is not necessarily the active workbench window
129: * shell.
130: *
131: * @param event
132: * The execution event that contains the application context
133: * @return the active shell, or <code>null</code>.
134: */
135: public static Shell getActiveShell(ExecutionEvent event) {
136: Object o = getVariable(event, ISources.ACTIVE_SHELL_NAME);
137: if (o instanceof Shell) {
138: return (Shell) o;
139: }
140: return null;
141: }
142:
143: /**
144: * Return the active shell. Is not necessarily the active workbench window
145: * shell.
146: *
147: * @param event
148: * The execution event that contains the application context
149: * @return the active shell. Will not return <code>null</code>.
150: * @throws ExecutionException
151: * If the active shell variable is not found.
152: */
153: public static Shell getActiveShellChecked(ExecutionEvent event)
154: throws ExecutionException {
155: Object o = getVariableChecked(event, ISources.ACTIVE_SHELL_NAME);
156: if (!(o instanceof Shell)) {
157: incorrectTypeFound(event, ISources.ACTIVE_SHELL_NAME,
158: Shell.class, o.getClass());
159: }
160: return (Shell) o;
161: }
162:
163: /**
164: * Return the active workbench window.
165: *
166: * @param event
167: * The execution event that contains the application context
168: * @return the active workbench window, or <code>null</code>.
169: */
170: public static IWorkbenchWindow getActiveWorkbenchWindow(
171: ExecutionEvent event) {
172: Object o = getVariable(event,
173: ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
174: if (o instanceof IWorkbenchWindow) {
175: return (IWorkbenchWindow) o;
176: }
177: return null;
178: }
179:
180: /**
181: * Return the active workbench window.
182: *
183: * @param event
184: * The execution event that contains the application context
185: * @return the active workbench window. Will not return <code>null</code>.
186: * @throws ExecutionException
187: * If the active workbench window variable is not found.
188: */
189: public static IWorkbenchWindow getActiveWorkbenchWindowChecked(
190: ExecutionEvent event) throws ExecutionException {
191: Object o = getVariableChecked(event,
192: ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
193: if (!(o instanceof IWorkbenchWindow)) {
194: incorrectTypeFound(event,
195: ISources.ACTIVE_WORKBENCH_WINDOW_NAME,
196: IWorkbenchWindow.class, o.getClass());
197: }
198: return (IWorkbenchWindow) o;
199: }
200:
201: /**
202: * Return the active editor.
203: *
204: * @param event
205: * The execution event that contains the application context
206: * @return the active editor, or <code>null</code>.
207: */
208: public static IEditorPart getActiveEditor(ExecutionEvent event) {
209: Object o = getVariable(event, ISources.ACTIVE_EDITOR_NAME);
210: if (o instanceof IEditorPart) {
211: return (IEditorPart) o;
212: }
213: return null;
214: }
215:
216: /**
217: * Return the active editor.
218: *
219: * @param event
220: * The execution event that contains the application context
221: * @return the active editor. Will not return <code>null</code>.
222: * @throws ExecutionException
223: * If the active editor variable is not found.
224: */
225: public static IEditorPart getActiveEditorChecked(
226: ExecutionEvent event) throws ExecutionException {
227: Object o = getVariableChecked(event,
228: ISources.ACTIVE_EDITOR_NAME);
229: if (!(o instanceof IEditorPart)) {
230: incorrectTypeFound(event, ISources.ACTIVE_EDITOR_NAME,
231: IEditorPart.class, o.getClass());
232: }
233: return (IEditorPart) o;
234: }
235:
236: /**
237: * Return the part id of the active editor.
238: *
239: * @param event
240: * The execution event that contains the application context
241: * @return the part id of the active editor, or <code>null</code>.
242: */
243: public static String getActiveEditorId(ExecutionEvent event) {
244: Object o = getVariable(event, ISources.ACTIVE_EDITOR_ID_NAME);
245: if (o instanceof String) {
246: return (String) o;
247: }
248: return null;
249: }
250:
251: /**
252: * Return the part id of the active editor.
253: *
254: * @param event
255: * The execution event that contains the application context
256: * @return the part id of the active editor. Will not return
257: * <code>null</code>.
258: * @throws ExecutionException
259: * If the active editor id variable is not found.
260: */
261: public static String getActiveEditorIdChecked(ExecutionEvent event)
262: throws ExecutionException {
263: Object o = getVariableChecked(event,
264: ISources.ACTIVE_EDITOR_ID_NAME);
265: if (!(o instanceof String)) {
266: incorrectTypeFound(event, ISources.ACTIVE_EDITOR_ID_NAME,
267: String.class, o.getClass());
268: }
269: return (String) o;
270: }
271:
272: /**
273: * Return the active part.
274: *
275: * @param event
276: * The execution event that contains the application context
277: * @return the active part, or <code>null</code>.
278: */
279: public static IWorkbenchPart getActivePart(ExecutionEvent event) {
280: Object o = getVariable(event, ISources.ACTIVE_PART_NAME);
281: if (o instanceof IWorkbenchPart) {
282: return (IWorkbenchPart) o;
283: }
284: return null;
285: }
286:
287: /**
288: * Return the active part.
289: *
290: * @param event
291: * The execution event that contains the application context
292: * @return the active part. Will not return <code>null</code>.
293: * @throws ExecutionException
294: * If the active part variable is not found.
295: */
296: public static IWorkbenchPart getActivePartChecked(
297: ExecutionEvent event) throws ExecutionException {
298: Object o = getVariableChecked(event, ISources.ACTIVE_PART_NAME);
299: if (!(o instanceof IWorkbenchPart)) {
300: incorrectTypeFound(event, ISources.ACTIVE_PART_NAME,
301: IWorkbenchPart.class, o.getClass());
302: }
303: return (IWorkbenchPart) o;
304: }
305:
306: /**
307: * Return the part id of the active part.
308: *
309: * @param event
310: * The execution event that contains the application context
311: * @return the part id of the active part, or <code>null</code>.
312: */
313: public static String getActivePartId(ExecutionEvent event) {
314: Object o = getVariable(event, ISources.ACTIVE_PART_ID_NAME);
315: if (o instanceof String) {
316: return (String) o;
317: }
318: return null;
319: }
320:
321: /**
322: * Return the part id of the active part.
323: *
324: * @param event
325: * The execution event that contains the application context
326: * @return the part id of the active part. Will not return <code>null</code>.
327: * @throws ExecutionException
328: * If the active part id variable is not found.
329: */
330: public static String getActivePartIdChecked(ExecutionEvent event)
331: throws ExecutionException {
332: Object o = getVariableChecked(event,
333: ISources.ACTIVE_PART_ID_NAME);
334: if (!(o instanceof String)) {
335: incorrectTypeFound(event, ISources.ACTIVE_PART_ID_NAME,
336: String.class, o.getClass());
337: }
338: return (String) o;
339: }
340:
341: /**
342: * Return the active part site.
343: *
344: * @param event
345: * The execution event that contains the application context
346: * @return the active part site, or <code>null</code>.
347: */
348: public static IWorkbenchSite getActiveSite(ExecutionEvent event) {
349: Object o = getVariable(event, ISources.ACTIVE_SITE_NAME);
350: if (o instanceof IWorkbenchSite) {
351: return (IWorkbenchSite) o;
352: }
353: return null;
354: }
355:
356: /**
357: * Return the active part site.
358: *
359: * @param event
360: * The execution event that contains the application context
361: * @return the active part site. Will not return <code>null</code>.
362: * @throws ExecutionException
363: * If the active part site variable is not found.
364: */
365: public static IWorkbenchSite getActiveSiteChecked(
366: ExecutionEvent event) throws ExecutionException {
367: Object o = getVariableChecked(event, ISources.ACTIVE_SITE_NAME);
368: if (!(o instanceof IWorkbenchSite)) {
369: incorrectTypeFound(event, ISources.ACTIVE_SITE_NAME,
370: IWorkbenchSite.class, o.getClass());
371: }
372: return (IWorkbenchSite) o;
373: }
374:
375: /**
376: * Return the current selection.
377: *
378: * @param event
379: * The execution event that contains the application context
380: * @return the current selection, or <code>null</code>.
381: */
382: public static ISelection getCurrentSelection(ExecutionEvent event) {
383: Object o = getVariable(event,
384: ISources.ACTIVE_CURRENT_SELECTION_NAME);
385: if (o instanceof ISelection) {
386: return (ISelection) o;
387: }
388: return null;
389: }
390:
391: /**
392: * Return the current selection.
393: *
394: * @param event
395: * The execution event that contains the application context
396: * @return the current selection. Will not return <code>null</code>.
397: * @throws ExecutionException
398: * If the current selection variable is not found.
399: */
400: public static ISelection getCurrentSelectionChecked(
401: ExecutionEvent event) throws ExecutionException {
402: Object o = getVariableChecked(event,
403: ISources.ACTIVE_CURRENT_SELECTION_NAME);
404: if (!(o instanceof ISelection)) {
405: incorrectTypeFound(event,
406: ISources.ACTIVE_CURRENT_SELECTION_NAME,
407: ISelection.class, o.getClass());
408: }
409: return (ISelection) o;
410: }
411:
412: /**
413: * Return the menu IDs that were applied to the registered context
414: * menu. For example, #CompilationUnitEditorContext.
415: *
416: * @param event
417: * The execution event that contains the application context
418: * @return the menu IDs, or <code>null</code>.
419: */
420: public static Collection getActiveMenus(ExecutionEvent event) {
421: Object o = getVariable(event, ISources.ACTIVE_MENU_NAME);
422: if (o instanceof Collection) {
423: return (Collection) o;
424: }
425: return null;
426: }
427:
428: /**
429: * Return the menu IDs that were applied to the registered context
430: * menu. For example, #CompilationUnitEditorContext.
431: *
432: * @param event
433: * The execution event that contains the application context
434: * @return the menu IDs. Will not return <code>null</code>.
435: * @throws ExecutionException
436: * If the active menus variable is not found.
437: */
438: public static Collection getActiveMenusChecked(ExecutionEvent event)
439: throws ExecutionException {
440: Object o = getVariableChecked(event, ISources.ACTIVE_MENU_NAME);
441: if (!(o instanceof Collection)) {
442: incorrectTypeFound(event, ISources.ACTIVE_MENU_NAME,
443: Collection.class, o.getClass());
444: }
445: return (Collection) o;
446: }
447:
448: /**
449: * Return the active menu selection. The active menu is a registered context
450: * menu.
451: *
452: * @param event
453: * The execution event that contains the application context
454: * @return the active menu selection, or <code>null</code>.
455: */
456: public static ISelection getActiveMenuSelection(ExecutionEvent event) {
457: Object o = getVariable(event,
458: ISources.ACTIVE_MENU_SELECTION_NAME);
459: if (o instanceof ISelection) {
460: return (ISelection) o;
461: }
462: return null;
463: }
464:
465: /**
466: * Return the active menu selection. The active menu is a registered context
467: * menu.
468: *
469: * @param event
470: * The execution event that contains the application context
471: * @return the active menu selection. Will not return <code>null</code>.
472: * @throws ExecutionException
473: * If the active menu selection variable is not found.
474: */
475: public static ISelection getActiveMenuSelectionChecked(
476: ExecutionEvent event) throws ExecutionException {
477: Object o = getVariableChecked(event,
478: ISources.ACTIVE_MENU_SELECTION_NAME);
479: if (!(o instanceof ISelection)) {
480: incorrectTypeFound(event,
481: ISources.ACTIVE_MENU_SELECTION_NAME,
482: ISelection.class, o.getClass());
483: }
484: return (ISelection) o;
485: }
486:
487: /**
488: * Return the active menu editor input, if available. The active menu is a
489: * registered context menu.
490: *
491: * @param event
492: * The execution event that contains the application context
493: * @return the active menu editor, or <code>null</code>.
494: */
495: public static ISelection getActiveMenuEditorInput(
496: ExecutionEvent event) {
497: Object o = getVariable(event,
498: ISources.ACTIVE_MENU_EDITOR_INPUT_NAME);
499: if (o instanceof ISelection) {
500: return (ISelection) o;
501: }
502: return null;
503: }
504:
505: /**
506: * Return the active menu editor input. The active menu is a
507: * registered context menu. Some context menus do not include the
508: * editor input which will throw an exception.
509: *
510: * @param event
511: * The execution event that contains the application context
512: * @return the active menu editor input. Will not return <code>null</code>.
513: * @throws ExecutionException
514: * If the active menu editor input variable is not found.
515: */
516: public static ISelection getActiveMenuEditorInputChecked(
517: ExecutionEvent event) throws ExecutionException {
518: Object o = getVariableChecked(event,
519: ISources.ACTIVE_MENU_EDITOR_INPUT_NAME);
520: if (!(o instanceof ISelection)) {
521: incorrectTypeFound(event,
522: ISources.ACTIVE_MENU_EDITOR_INPUT_NAME,
523: ISelection.class, o.getClass());
524: }
525: return (ISelection) o;
526: }
527: }
|