KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > contexts > ContextService


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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 package org.eclipse.ui.internal.contexts;
12
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15
16 import org.eclipse.core.commands.contexts.Context;
17 import org.eclipse.core.commands.contexts.ContextManager;
18 import org.eclipse.core.commands.contexts.IContextManagerListener;
19 import org.eclipse.core.expressions.Expression;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.ISourceProvider;
22 import org.eclipse.ui.ISources;
23 import org.eclipse.ui.contexts.IContextActivation;
24 import org.eclipse.ui.contexts.IContextService;
25
26 /**
27  * <p>
28  * Provides services related to contexts in the Eclipse workbench. This provides
29  * access to contexts.
30  * </p>
31  *
32  * @since 3.1
33  */

34 public final class ContextService implements IContextService {
35
36     /**
37      * The central authority for determining which context we should use.
38      */

39     private final ContextAuthority contextAuthority;
40
41     /**
42      * The context manager that supports this service. This value is never
43      * <code>null</code>.
44      */

45     private final ContextManager contextManager;
46
47     /**
48      * The persistence class for this context service.
49      */

50     private final ContextPersistence contextPersistence;
51
52     /**
53      * Constructs a new instance of <code>ContextService</code> using a
54      * context manager.
55      *
56      * @param contextManager
57      * The context manager to use; must not be <code>null</code>.
58      */

59     public ContextService(final ContextManager contextManager) {
60         if (contextManager == null) {
61             throw new NullPointerException JavaDoc(
62                     "Cannot create a context service with a null manager"); //$NON-NLS-1$
63
}
64         this.contextManager = contextManager;
65         this.contextAuthority = new ContextAuthority(contextManager, this);
66         this.contextPersistence = new ContextPersistence(contextManager);
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String)
73      */

74     public final IContextActivation activateContext(final String JavaDoc contextId) {
75         return activateContext(contextId, null);
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
82      * org.eclipse.core.expressions.Expression)
83      */

84     public final IContextActivation activateContext(final String JavaDoc contextId,
85             final Expression expression) {
86         final IContextActivation activation = new ContextActivation(contextId,
87                 expression, this);
88         contextAuthority.activateContext(activation);
89         return activation;
90     }
91
92     /*
93      * (non-Javadoc)
94      *
95      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
96      * org.eclipse.core.expressions.Expression, boolean)
97      */

98     public IContextActivation activateContext(String JavaDoc contextId,
99             Expression expression, boolean global) {
100         return activateContext(contextId, expression);
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
107      * org.eclipse.core.expressions.Expression, int)
108      */

109     public final IContextActivation activateContext(final String JavaDoc contextId,
110             final Expression expression, final int sourcePriority) {
111         return activateContext(contextId, expression);
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see org.eclipse.ui.contexts.IContextService#addContextManagerListener(org.eclipse.core.commands.contexts.IContextManagerListener)
118      */

119     public final void addContextManagerListener(
120             final IContextManagerListener listener) {
121         contextManager.addContextManagerListener(listener);
122     }
123
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.eclipse.ui.internal.services.IServiceWithSources#addSourceProvider(org.eclipse.ui.ISourceProvider)
128      */

129     public final void addSourceProvider(final ISourceProvider provider) {
130         contextAuthority.addSourceProvider(provider);
131     }
132
133     /*
134      * (non-Javadoc)
135      *
136      * @see org.eclipse.ui.contexts.IContextService#deactivateContext(org.eclipse.ui.contexts.IContextActivation)
137      */

138     public final void deactivateContext(final IContextActivation activation) {
139         if (activation.getContextService() == this) {
140             contextAuthority.deactivateContext(activation);
141         }
142     }
143
144     /*
145      * (non-Javadoc)
146      *
147      * @see org.eclipse.ui.contexts.IContextService#deactivateContexts(java.util.Collection)
148      */

149     public final void deactivateContexts(final Collection JavaDoc activations) {
150         final Iterator JavaDoc activationItr = activations.iterator();
151         while (activationItr.hasNext()) {
152             final IContextActivation activation = (IContextActivation) activationItr
153                     .next();
154             deactivateContext(activation);
155         }
156     }
157
158     /*
159      * (non-Javadoc)
160      *
161      * @see org.eclipse.ui.services.IDisposable#dispose()
162      */

163     public final void dispose() {
164         contextPersistence.dispose();
165         contextAuthority.dispose();
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.eclipse.ui.contexts.IContextService#getActiveContextIds()
172      */

173     public final Collection JavaDoc getActiveContextIds() {
174         return contextManager.getActiveContextIds();
175     }
176
177     /*
178      * (non-Javadoc)
179      *
180      * @see org.eclipse.ui.contexts.IContextService#getContext(java.lang.String)
181      */

182     public final Context getContext(final String JavaDoc contextId) {
183         return contextManager.getContext(contextId);
184     }
185
186     /*
187      * (non-Javadoc)
188      *
189      * @see org.eclipse.ui.contexts.IContextService#getDefinedContextIds()
190      */

191     public final Collection JavaDoc getDefinedContextIds() {
192         return contextManager.getDefinedContextIds();
193     }
194
195     /*
196      * (non-Javadoc)
197      *
198      * @see org.eclipse.ui.contexts.IContextService#getDefinedContexts()
199      */

200     public final Context[] getDefinedContexts() {
201         return contextManager.getDefinedContexts();
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see org.eclipse.ui.contexts.IContextService#getShellType(org.eclipse.swt.widgets.Shell)
208      */

209     public final int getShellType(final Shell shell) {
210         return contextAuthority.getShellType(shell);
211     }
212
213     /*
214      * (non-Javadoc)
215      *
216      * @see org.eclipse.ui.contexts.IContextService#readRegistry()
217      */

218     public final void readRegistry() {
219         contextPersistence.read();
220     }
221
222     /*
223      * (non-Javadoc)
224      *
225      * @see org.eclipse.ui.contexts.IContextService#registerShell(org.eclipse.swt.widgets.Shell,
226      * int)
227      */

228     public final boolean registerShell(final Shell shell, final int type) {
229         return contextAuthority.registerShell(shell, type);
230     }
231
232     /*
233      * (non-Javadoc)
234      *
235      * @see org.eclipse.ui.contexts.IContextService#removeContextManagerListener(org.eclipse.core.commands.contexts.IContextManagerListener)
236      */

237     public final void removeContextManagerListener(
238             final IContextManagerListener listener) {
239         contextManager.addContextManagerListener(listener);
240     }
241
242     /*
243      * (non-Javadoc)
244      *
245      * @see org.eclipse.ui.internal.services.IServiceWithSources#removeSourceProvider(org.eclipse.ui.ISourceProvider)
246      */

247     public final void removeSourceProvider(final ISourceProvider provider) {
248         contextAuthority.removeSourceProvider(provider);
249     }
250
251     /*
252      * (non-Javadoc)
253      *
254      * @see org.eclipse.ui.contexts.IContextService#unregisterShell(org.eclipse.swt.widgets.Shell)
255      */

256     public final boolean unregisterShell(final Shell shell) {
257         return contextAuthority.unregisterShell(shell);
258     }
259
260     /**
261      * <p>
262      * Bug 95792. A mechanism by which the key binding architecture can force an
263      * update of the contexts (based on the active shell) before trying to
264      * execute a command. This mechanism is required for GTK+ only.
265      * </p>
266      * <p>
267      * DO NOT CALL THIS METHOD.
268      * </p>
269      */

270     public final void updateShellKludge() {
271         contextAuthority.updateShellKludge();
272     }
273
274     /**
275      * <p>
276      * Bug 95792. A mechanism by which the key binding architecture can force an
277      * update of the contexts (based on the active shell) before trying to
278      * execute a command. This mechanism is required for GTK+ only.
279      * </p>
280      * <p>
281      * DO NOT CALL THIS METHOD.
282      * </p>
283      *
284      * @param shell
285      * The shell that should be considered active; must not be
286      * <code>null</code>.
287      */

288     public final void updateShellKludge(final Shell shell) {
289         final Shell currentActiveShell = contextAuthority.getActiveShell();
290         if (currentActiveShell != shell) {
291             contextAuthority.sourceChanged(ISources.ACTIVE_SHELL,
292                     ISources.ACTIVE_SHELL_NAME, shell);
293         }
294     }
295 }
296
Popular Tags