KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.contexts;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import org.eclipse.core.commands.contexts.Context;
22 import org.eclipse.core.commands.contexts.IContextManagerListener;
23 import org.eclipse.core.expressions.Expression;
24 import org.eclipse.swt.widgets.Shell;
25 import org.eclipse.ui.ISourceProvider;
26 import org.eclipse.ui.contexts.IContextActivation;
27 import org.eclipse.ui.contexts.IContextService;
28 import org.eclipse.ui.internal.expressions.AndExpression;
29
30 /**
31  * A context service which delegates almost all responsibility to the parent
32  * service.
33  * <p>
34  * This class is not intended for use outside of the
35  * <code>org.eclipse.ui.workbench</code> plug-in.
36  * </p>
37  *
38  * @since 3.2
39  *
40  */

41 public class SlaveContextService implements IContextService {
42
43     /**
44      * The parent context service, which is never <code>null</code>.
45      */

46     protected IContextService fParentService;
47
48     /**
49      * The default expression used when {@link #activateContext(String) } is
50      * called. Contexts contributed that use this expression will only be active
51      * with this service is active.
52      */

53     protected Expression fDefaultExpression;
54
55     /**
56      * Our contexts that are currently active with the parent context service.
57      */

58     protected Set JavaDoc fParentActivations;
59
60     /**
61      * A map of the local activation to the parent activations. If this service
62      * is inactive, then all parent activations are <code>null</code>.
63      * Otherwise, they point to the corresponding activation in the parent
64      * service.
65      */

66     protected Map JavaDoc fLocalActivations;
67
68     /**
69      * A collection of context manager listeners. The listeners are not
70      * activated/deactivated, but they will be removed when this service is
71      * disposed.
72      */

73     private Collection JavaDoc fContextManagerListeners;
74
75     /**
76      * A collection of source providers. The listeners are not
77      * activated/deactivated, but they will be removed when this service is
78      * disposed.
79      */

80     private Collection JavaDoc fSourceProviders;
81
82     /**
83      * A collection of shells registered through this service. The listeners are
84      * not activated/deactivated, but they will be removed when this service is
85      * disposed.
86      */

87     private Collection JavaDoc fRegisteredShells;
88
89     /**
90      * Construct the new slave.
91      *
92      * @param parentService
93      * the parent context service; must not be <code>null</code>.
94      * @param defaultExpression
95      * A default expression to use to determine viability. It's
96      * mainly used for conflict resolution. It can be
97      * <code>null</code>.
98      */

99     public SlaveContextService(IContextService parentService,
100             Expression defaultExpression) {
101         if (parentService == null) {
102             throw new NullPointerException JavaDoc(
103                     "The parent context service must not be null"); //$NON-NLS-1$
104
}
105         fParentService = parentService;
106         fDefaultExpression = defaultExpression;
107         fParentActivations = new HashSet JavaDoc();
108         fLocalActivations = new HashMap JavaDoc();
109         fContextManagerListeners = new ArrayList JavaDoc();
110         fSourceProviders = new ArrayList JavaDoc();
111         fRegisteredShells = new ArrayList JavaDoc();
112     }
113
114     /*
115      * (non-Javadoc)
116      *
117      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String)
118      */

119     public IContextActivation activateContext(String JavaDoc contextId) {
120         ContextActivation activation = new ContextActivation(contextId,
121                 fDefaultExpression, this);
122         return doActivateContext(activation);
123     }
124
125     /*
126      * (non-Javadoc)
127      *
128      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
129      * org.eclipse.core.expressions.Expression)
130      */

131     public IContextActivation activateContext(String JavaDoc contextId,
132             Expression expression) {
133         return activateContext(contextId, expression, false);
134     }
135
136     /*
137      * (non-Javadoc)
138      *
139      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
140      * org.eclipse.core.expressions.Expression, boolean)
141      */

142     public IContextActivation activateContext(String JavaDoc contextId,
143             Expression expression, boolean global) {
144         if (global) {
145             IContextActivation activation = fParentService.activateContext(
146                     contextId, expression, global);
147             fParentActivations.add(activation);
148             return activation;
149         }
150         AndExpression andExpression = null;
151         if (expression instanceof AndExpression) {
152             andExpression = (AndExpression) expression;
153         } else {
154             andExpression = new AndExpression();
155             if (expression!=null) {
156                 andExpression.add(expression);
157             }
158         }
159         if (fDefaultExpression!=null) {
160             andExpression.add(fDefaultExpression);
161         }
162         ContextActivation activation = new ContextActivation(contextId,
163                 andExpression, this);
164         return doActivateContext(activation);
165     }
166
167     /*
168      * (non-Javadoc)
169      *
170      * @see org.eclipse.ui.contexts.IContextService#activateContext(java.lang.String,
171      * org.eclipse.core.expressions.Expression, int)
172      */

173     public IContextActivation activateContext(String JavaDoc contextId,
174             Expression expression, int sourcePriorities) {
175         return activateContext(contextId, expression);
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.eclipse.ui.contexts.IContextService#addContextManagerListener(org.eclipse.core.commands.contexts.IContextManagerListener)
182      */

183     public void addContextManagerListener(IContextManagerListener listener) {
184         if (!fContextManagerListeners.contains(listener)) {
185             fContextManagerListeners.add(listener);
186         }
187         fParentService.addContextManagerListener(listener);
188     }
189
190     /*
191      * (non-Javadoc)
192      *
193      * @see org.eclipse.ui.services.IServiceWithSources#addSourceProvider(org.eclipse.ui.ISourceProvider)
194      */

195     public void addSourceProvider(ISourceProvider provider) {
196         if (!fSourceProviders.contains(provider)) {
197             fSourceProviders.add(provider);
198         }
199         fParentService.addSourceProvider(provider);
200     }
201
202     /*
203      * (non-Javadoc)
204      *
205      * @see org.eclipse.ui.contexts.IContextService#deactivateContext(org.eclipse.ui.contexts.IContextActivation)
206      */

207     public void deactivateContext(IContextActivation activation) {
208         IContextActivation parentActivation = null;
209         if (fLocalActivations.containsKey(activation)) {
210             parentActivation = (IContextActivation) fLocalActivations
211                     .remove(activation);
212         } else {
213             parentActivation = activation;
214         }
215         if (parentActivation != null) {
216             fParentService.deactivateContext(parentActivation);
217             fParentActivations.remove(parentActivation);
218         }
219     }
220
221     /*
222      * (non-Javadoc)
223      *
224      * @see org.eclipse.ui.contexts.IContextService#deactivateContexts(java.util.Collection)
225      */

226     public void deactivateContexts(Collection JavaDoc activations) {
227         Object JavaDoc[] array = activations.toArray();
228         for (int i = 0; i < array.length; i++) {
229             deactivateContext((IContextActivation) array[i]);
230             array[i] = null;
231         }
232     }
233
234     /*
235      * (non-Javadoc)
236      *
237      * @see org.eclipse.ui.services.IDisposable#dispose()
238      */

239     public void dispose() {
240         fParentService.deactivateContexts(fParentActivations);
241         fParentActivations.clear();
242         fLocalActivations.clear();
243         
244         // Remove any "resource", like listeners, that were associated
245
// with this service.
246
if (!fContextManagerListeners.isEmpty()) {
247             Object JavaDoc[] array = fContextManagerListeners.toArray();
248             for (int i = 0; i < array.length; i++) {
249                 removeContextManagerListener((IContextManagerListener) array[i]);
250             }
251             fContextManagerListeners.clear();
252         }
253         if (!fSourceProviders.isEmpty()) {
254             Object JavaDoc[] array = fSourceProviders.toArray();
255             for (int i = 0; i < array.length; i++) {
256                 removeSourceProvider((ISourceProvider) array[i]);
257             }
258             fSourceProviders.clear();
259         }
260         if (!fRegisteredShells.isEmpty()) {
261             Object JavaDoc[] array = fRegisteredShells.toArray();
262             for (int i = 0; i < array.length; i++) {
263                 unregisterShell((Shell) array[i]);
264             }
265             fRegisteredShells.clear();
266         }
267     }
268
269     /**
270      * Activate the context with respect to this slave service.
271      *
272      * @param contextId
273      * the context id
274      * @param expression
275      * the expression to use
276      * @return the activated context
277      */

278     protected IContextActivation doActivateContext(IContextActivation activation) {
279         IContextActivation parentActivation = fParentService.activateContext(
280                 activation.getContextId(), activation.getExpression());
281         fParentActivations.add(parentActivation);
282         fLocalActivations.put(activation, parentActivation);
283         return activation;
284     }
285
286     /*
287      * (non-Javadoc)
288      *
289      * @see org.eclipse.ui.contexts.IContextService#getActiveContextIds()
290      */

291     public Collection JavaDoc getActiveContextIds() {
292         return fParentService.getActiveContextIds();
293     }
294
295     /*
296      * (non-Javadoc)
297      *
298      * @see org.eclipse.ui.contexts.IContextService#getContext(java.lang.String)
299      */

300     public Context getContext(String JavaDoc contextId) {
301         return fParentService.getContext(contextId);
302     }
303
304     /*
305      * (non-Javadoc)
306      *
307      * @see org.eclipse.ui.contexts.IContextService#getDefinedContextIds()
308      */

309     public Collection JavaDoc getDefinedContextIds() {
310         return fParentService.getDefinedContextIds();
311     }
312
313     /*
314      * (non-Javadoc)
315      *
316      * @see org.eclipse.ui.contexts.IContextService#getDefinedContexts()
317      */

318     public Context[] getDefinedContexts() {
319         return fParentService.getDefinedContexts();
320     }
321
322     /*
323      * (non-Javadoc)
324      *
325      * @see org.eclipse.ui.contexts.IContextService#getShellType(org.eclipse.swt.widgets.Shell)
326      */

327     public int getShellType(Shell shell) {
328         return fParentService.getShellType(shell);
329     }
330
331     /*
332      * (non-Javadoc)
333      *
334      * @see org.eclipse.ui.contexts.IContextService#readRegistry()
335      */

336     public void readRegistry() {
337         fParentService.readRegistry();
338     }
339
340     /*
341      * (non-Javadoc)
342      *
343      * @see org.eclipse.ui.contexts.IContextService#registerShell(org.eclipse.swt.widgets.Shell,
344      * int)
345      */

346     public boolean registerShell(Shell shell, int type) {
347         if (!fRegisteredShells.contains(shell)) {
348             fRegisteredShells.add(shell);
349         }
350         return fParentService.registerShell(shell, type);
351     }
352
353     /*
354      * (non-Javadoc)
355      *
356      * @see org.eclipse.ui.contexts.IContextService#removeContextManagerListener(org.eclipse.core.commands.contexts.IContextManagerListener)
357      */

358     public void removeContextManagerListener(IContextManagerListener listener) {
359         fContextManagerListeners.remove(listener);
360         fParentService.removeContextManagerListener(listener);
361     }
362
363     /*
364      * (non-Javadoc)
365      *
366      * @see org.eclipse.ui.services.IServiceWithSources#removeSourceProvider(org.eclipse.ui.ISourceProvider)
367      */

368     public void removeSourceProvider(ISourceProvider provider) {
369         fSourceProviders.remove(provider);
370         fParentService.removeSourceProvider(provider);
371     }
372
373     /*
374      * (non-Javadoc)
375      *
376      * @see org.eclipse.ui.contexts.IContextService#unregisterShell(org.eclipse.swt.widgets.Shell)
377      */

378     public boolean unregisterShell(Shell shell) {
379         fRegisteredShells.remove(shell);
380         return fParentService.unregisterShell(shell);
381     }
382 }
383
Popular Tags