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.internal;
011:
012: import java.util.Collection;
013:
014: import org.eclipse.core.expressions.IEvaluationContext;
015: import org.eclipse.jface.viewers.ISelection;
016: import org.eclipse.swt.widgets.Shell;
017: import org.eclipse.ui.IEditorPart;
018: import org.eclipse.ui.ISources;
019: import org.eclipse.ui.IWorkbenchPart;
020: import org.eclipse.ui.IWorkbenchSite;
021: import org.eclipse.ui.IWorkbenchWindow;
022:
023: /**
024: * Some common utilities for working with handlers and IEvaluationContexts in
025: * Platform UI.
026: * <p>
027: * <b>Note</b>: this class should not be instantiated or extended by clients.
028: * </p>
029: * <p>
030: * <strong>PROVISIONAL</strong>. This class or interface has been added as part
031: * of a work in progress. There is a guarantee neither that this API will work
032: * nor that it will remain the same. Please do not use this API without
033: * consulting with the Platform/UI team.
034: * </p>
035: *
036: * @since 3.3
037: */
038: public class InternalHandlerUtil {
039: /**
040: * Extract the variable.
041: *
042: * @param appContext
043: * The application context
044: * @param name
045: * The variable name to extract.
046: * @return The object from the application context, or <code>null</code>
047: * if it could not be found.
048: */
049: public static Object getVariable(Object appContext, String name) {
050: if (appContext instanceof IEvaluationContext) {
051: return ((IEvaluationContext) appContext).getVariable(name);
052: }
053: return null;
054: }
055:
056: /**
057: * Return the active contexts.
058: *
059: * @param appContext
060: * The execution appContext that contains the application context
061: * @return a collection of String contextIds, or <code>null</code>.
062: */
063: public static Collection getActiveContexts(Object appContext) {
064: Object o = getVariable(appContext, ISources.ACTIVE_CONTEXT_NAME);
065: if (o instanceof Collection) {
066: return (Collection) o;
067: }
068: return null;
069: }
070:
071: /**
072: * Return the active shell. Is not necessarily the active workbench window
073: * shell.
074: *
075: * @param appContext
076: * The execution appContext that contains the application context
077: * @return the active shell, or <code>null</code>.
078: */
079: public static Shell getActiveShell(Object appContext) {
080: Object o = getVariable(appContext, ISources.ACTIVE_SHELL_NAME);
081: if (o instanceof Shell) {
082: return (Shell) o;
083: }
084: return null;
085: }
086:
087: /**
088: * Return the active workbench window.
089: *
090: * @param appContext
091: * The execution appContext that contains the application context
092: * @return the active workbench window, or <code>null</code>.
093: */
094: public static IWorkbenchWindow getActiveWorkbenchWindow(
095: Object appContext) {
096: Object o = getVariable(appContext,
097: ISources.ACTIVE_WORKBENCH_WINDOW_NAME);
098: if (o instanceof IWorkbenchWindow) {
099: return (IWorkbenchWindow) o;
100: }
101: return null;
102: }
103:
104: /**
105: * Return the active editor.
106: *
107: * @param appContext
108: * The execution appContext that contains the application context
109: * @return the active editor, or <code>null</code>.
110: */
111: public static IEditorPart getActiveEditor(Object appContext) {
112: Object o = getVariable(appContext, ISources.ACTIVE_EDITOR_NAME);
113: if (o instanceof IEditorPart) {
114: return (IEditorPart) o;
115: }
116: return null;
117: }
118:
119: /**
120: * Return the part id of the active editor.
121: *
122: * @param appContext
123: * The execution appContext that contains the application context
124: * @return the part id of the active editor, or <code>null</code>.
125: */
126: public static String getActiveEditorId(Object appContext) {
127: Object o = getVariable(appContext,
128: ISources.ACTIVE_EDITOR_ID_NAME);
129: if (o instanceof String) {
130: return (String) 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 appContext) {
143: Object 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 getActivePartId(Object appContext) {
158: Object o = getVariable(appContext, ISources.ACTIVE_PART_ID_NAME);
159: if (o instanceof String) {
160: return (String) 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 appContext) {
173: Object 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 appContext) {
188: Object o = getVariable(appContext,
189: ISources.ACTIVE_CURRENT_SELECTION_NAME);
190: if (o instanceof ISelection) {
191: return (ISelection) o;
192: }
193: return null;
194: }
195: }
|