001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 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.contexts;
011:
012: import java.util.Collection;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.commands.contexts.Context;
016: import org.eclipse.core.commands.contexts.ContextManager;
017: import org.eclipse.core.commands.contexts.IContextManagerListener;
018: import org.eclipse.core.expressions.Expression;
019: import org.eclipse.swt.widgets.Shell;
020: import org.eclipse.ui.ISourceProvider;
021: import org.eclipse.ui.ISources;
022: import org.eclipse.ui.contexts.IContextActivation;
023: import org.eclipse.ui.contexts.IContextService;
024:
025: /**
026: * <p>
027: * Provides services related to contexts in the Eclipse workbench. This provides
028: * access to contexts.
029: * </p>
030: *
031: * @since 3.1
032: */
033: public final class ContextService implements IContextService {
034:
035: /**
036: * The central authority for determining which context we should use.
037: */
038: private final ContextAuthority contextAuthority;
039:
040: /**
041: * The context manager that supports this service. This value is never
042: * <code>null</code>.
043: */
044: private final ContextManager contextManager;
045:
046: /**
047: * The persistence class for this context service.
048: */
049: private final ContextPersistence contextPersistence;
050:
051: /**
052: * Constructs a new instance of <code>ContextService</code> using a
053: * context manager.
054: *
055: * @param contextManager
056: * The context manager to use; must not be <code>null</code>.
057: */
058: public ContextService(final ContextManager contextManager) {
059: if (contextManager == null) {
060: throw new NullPointerException(
061: "Cannot create a context service with a null manager"); //$NON-NLS-1$
062: }
063: this .contextManager = contextManager;
064: this .contextAuthority = new ContextAuthority(contextManager,
065: this );
066: this .contextPersistence = new ContextPersistence(contextManager);
067: }
068:
069: /*
070: * (non-Javadoc)
071: *
072: * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String)
073: */
074: public final IContextActivation activateContext(
075: final String contextId) {
076: return activateContext(contextId, null);
077: }
078:
079: /*
080: * (non-Javadoc)
081: *
082: * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
083: * org.eclipse.core.expressions.Expression)
084: */
085: public final IContextActivation activateContext(
086: final String contextId, final Expression expression) {
087: final IContextActivation activation = new ContextActivation(
088: contextId, expression, this );
089: contextAuthority.activateContext(activation);
090: return activation;
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
097: * org.eclipse.core.expressions.Expression, boolean)
098: */
099: public IContextActivation activateContext(String contextId,
100: Expression expression, boolean global) {
101: return activateContext(contextId, expression);
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
108: * org.eclipse.core.expressions.Expression, int)
109: */
110: public final IContextActivation activateContext(
111: final String contextId, final Expression expression,
112: final int sourcePriority) {
113: return activateContext(contextId, expression);
114: }
115:
116: /*
117: * (non-Javadoc)
118: *
119: * @see org.eclipse.ui.contexts.IContextService#addContextManagerListener(org.eclipse.core.commands.contexts.IContextManagerListener)
120: */
121: public final void addContextManagerListener(
122: final IContextManagerListener listener) {
123: contextManager.addContextManagerListener(listener);
124: }
125:
126: /*
127: * (non-Javadoc)
128: *
129: * @see org.eclipse.ui.internal.services.IServiceWithSources#addSourceProvider(org.eclipse.ui.ISourceProvider)
130: */
131: public final void addSourceProvider(final ISourceProvider provider) {
132: contextAuthority.addSourceProvider(provider);
133: }
134:
135: /*
136: * (non-Javadoc)
137: *
138: * @see org.eclipse.ui.contexts.IContextService#deactivateContext(org.eclipse.ui.contexts.IContextActivation)
139: */
140: public final void deactivateContext(
141: final IContextActivation activation) {
142: if (activation.getContextService() == this ) {
143: contextAuthority.deactivateContext(activation);
144: }
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see org.eclipse.ui.contexts.IContextService#deactivateContexts(java.util.Collection)
151: */
152: public final void deactivateContexts(final Collection activations) {
153: final Iterator activationItr = activations.iterator();
154: while (activationItr.hasNext()) {
155: final IContextActivation activation = (IContextActivation) activationItr
156: .next();
157: deactivateContext(activation);
158: }
159: }
160:
161: /*
162: * (non-Javadoc)
163: *
164: * @see org.eclipse.ui.services.IDisposable#dispose()
165: */
166: public final void dispose() {
167: contextPersistence.dispose();
168: contextAuthority.dispose();
169: }
170:
171: /*
172: * (non-Javadoc)
173: *
174: * @see org.eclipse.ui.contexts.IContextService#getActiveContextIds()
175: */
176: public final Collection getActiveContextIds() {
177: return contextManager.getActiveContextIds();
178: }
179:
180: /*
181: * (non-Javadoc)
182: *
183: * @see org.eclipse.ui.contexts.IContextService#getContext(java.lang.String)
184: */
185: public final Context getContext(final String contextId) {
186: return contextManager.getContext(contextId);
187: }
188:
189: /*
190: * (non-Javadoc)
191: *
192: * @see org.eclipse.ui.contexts.IContextService#getDefinedContextIds()
193: */
194: public final Collection getDefinedContextIds() {
195: return contextManager.getDefinedContextIds();
196: }
197:
198: /*
199: * (non-Javadoc)
200: *
201: * @see org.eclipse.ui.contexts.IContextService#getDefinedContexts()
202: */
203: public final Context[] getDefinedContexts() {
204: return contextManager.getDefinedContexts();
205: }
206:
207: /*
208: * (non-Javadoc)
209: *
210: * @see org.eclipse.ui.contexts.IContextService#getShellType(org.eclipse.swt.widgets.Shell)
211: */
212: public final int getShellType(final Shell shell) {
213: return contextAuthority.getShellType(shell);
214: }
215:
216: /*
217: * (non-Javadoc)
218: *
219: * @see org.eclipse.ui.contexts.IContextService#readRegistry()
220: */
221: public final void readRegistry() {
222: contextPersistence.read();
223: }
224:
225: /*
226: * (non-Javadoc)
227: *
228: * @see org.eclipse.ui.contexts.IContextService#registerShell(org.eclipse.swt.widgets.Shell,
229: * int)
230: */
231: public final boolean registerShell(final Shell shell, final int type) {
232: return contextAuthority.registerShell(shell, type);
233: }
234:
235: /*
236: * (non-Javadoc)
237: *
238: * @see org.eclipse.ui.contexts.IContextService#removeContextManagerListener(org.eclipse.core.commands.contexts.IContextManagerListener)
239: */
240: public final void removeContextManagerListener(
241: final IContextManagerListener listener) {
242: contextManager.addContextManagerListener(listener);
243: }
244:
245: /*
246: * (non-Javadoc)
247: *
248: * @see org.eclipse.ui.internal.services.IServiceWithSources#removeSourceProvider(org.eclipse.ui.ISourceProvider)
249: */
250: public final void removeSourceProvider(
251: final ISourceProvider provider) {
252: contextAuthority.removeSourceProvider(provider);
253: }
254:
255: /*
256: * (non-Javadoc)
257: *
258: * @see org.eclipse.ui.contexts.IContextService#unregisterShell(org.eclipse.swt.widgets.Shell)
259: */
260: public final boolean unregisterShell(final Shell shell) {
261: return contextAuthority.unregisterShell(shell);
262: }
263:
264: /**
265: * <p>
266: * Bug 95792. A mechanism by which the key binding architecture can force an
267: * update of the contexts (based on the active shell) before trying to
268: * execute a command. This mechanism is required for GTK+ only.
269: * </p>
270: * <p>
271: * DO NOT CALL THIS METHOD.
272: * </p>
273: */
274: public final void updateShellKludge() {
275: contextAuthority.updateShellKludge();
276: }
277:
278: /**
279: * <p>
280: * Bug 95792. A mechanism by which the key binding architecture can force an
281: * update of the contexts (based on the active shell) before trying to
282: * execute a command. This mechanism is required for GTK+ only.
283: * </p>
284: * <p>
285: * DO NOT CALL THIS METHOD.
286: * </p>
287: *
288: * @param shell
289: * The shell that should be considered active; must not be
290: * <code>null</code>.
291: */
292: public final void updateShellKludge(final Shell shell) {
293: final Shell currentActiveShell = contextAuthority
294: .getActiveShell();
295: if (currentActiveShell != shell) {
296: contextAuthority.sourceChanged(ISources.ACTIVE_SHELL,
297: ISources.ACTIVE_SHELL_NAME, shell);
298: }
299: }
300: }
|