KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > handlers > SlaveHandlerService


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.handlers;
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.Command;
22 import org.eclipse.core.commands.ExecutionEvent;
23 import org.eclipse.core.commands.ExecutionException;
24 import org.eclipse.core.commands.IHandler;
25 import org.eclipse.core.commands.NotEnabledException;
26 import org.eclipse.core.commands.NotHandledException;
27 import org.eclipse.core.commands.ParameterizedCommand;
28 import org.eclipse.core.commands.common.NotDefinedException;
29 import org.eclipse.core.expressions.Expression;
30 import org.eclipse.core.expressions.IEvaluationContext;
31 import org.eclipse.swt.widgets.Event;
32 import org.eclipse.ui.ISourceProvider;
33 import org.eclipse.ui.handlers.IHandlerActivation;
34 import org.eclipse.ui.handlers.IHandlerService;
35 import org.eclipse.ui.internal.expressions.AndExpression;
36
37 /**
38  * A handler service which delegates almost all responsibility to the parent
39  * service. It is only responsible for disposing of locally activated handlers
40  * when it is disposed.
41  * <p>
42  * This class is not intended for use outside of the
43  * <code>org.eclipse.ui.workbench</code> plug-in.
44  * </p>
45  *
46  * @since 3.2
47  */

48 public class SlaveHandlerService implements IHandlerService {
49
50     /**
51      * The default expression to use when
52      * {@link #activateHandler(String, IHandler)} is called. Handlers
53      * contributed using that method will only be active when this service is
54      * active. However, this expression will be used for conflict resolution.
55      */

56     protected final Expression defaultExpression;
57
58     /**
59      * A collection of source providers. The listeners are not
60      * activated/deactivated, but they will be removed when this service is
61      * disposed.
62      */

63     private Collection JavaDoc fSourceProviders = new ArrayList JavaDoc();
64
65     /**
66      * A map of the local activation to the parent activations. If this service
67      * is inactive, then all parent activations are <code>null</code>.
68      * Otherwise, they point to the corresponding activation in the parent
69      * service.
70      */

71     protected final Map JavaDoc localActivationsToParentActivations = new HashMap JavaDoc();
72
73     /**
74      * The parent handler service to which all requests are ultimately routed.
75      * This value is never <code>null</code>.
76      */

77     protected final IHandlerService parent;
78
79     /**
80      * The activations registered with the parent handler service. This value is
81      * never <code>null</code>.
82      */

83     protected final Set JavaDoc parentActivations = new HashSet JavaDoc();
84
85     /**
86      * Constructs a new instance.
87      *
88      * @param parentHandlerService
89      * The parent handler service for this slave; must not be
90      * <code>null</code>.
91      * @param defaultExpression
92      * The default expression to use if none is provided. This is
93      * primarily used for conflict resolution. This value may be
94      * <code>null</code>.
95      */

96     public SlaveHandlerService(final IHandlerService parentHandlerService,
97             final Expression defaultExpression) {
98         if (parentHandlerService == null) {
99             throw new NullPointerException JavaDoc(
100                     "The parent handler service cannot be null"); //$NON-NLS-1$
101
}
102
103         this.defaultExpression = defaultExpression;
104         this.parent = parentHandlerService;
105     }
106
107     public final IHandlerActivation activateHandler(
108             final IHandlerActivation childActivation) {
109         final String JavaDoc commandId = childActivation.getCommandId();
110         final IHandler handler = childActivation.getHandler();
111         final Expression childExpression = childActivation.getExpression();
112         final AndExpression expression;
113         if (childExpression instanceof AndExpression) {
114             expression = (AndExpression) childExpression;
115         } else {
116             expression = new AndExpression();
117             if (childExpression != null) {
118                 expression.add(childExpression);
119             }
120         }
121         if (defaultExpression != null) {
122             expression.add(defaultExpression);
123         }
124         final int depth = childActivation.getDepth() + 1;
125         final IHandlerActivation localActivation = new HandlerActivation(
126                 commandId, handler, expression, depth, this);
127
128         return doActivation(localActivation);
129     }
130
131     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
132             final IHandler handler) {
133         final IHandlerActivation localActivation = new HandlerActivation(
134                 commandId, handler, defaultExpression,
135                 IHandlerActivation.ROOT_DEPTH, this);
136         return doActivation(localActivation);
137     }
138
139     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
140             final IHandler handler, final Expression expression) {
141         return activateHandler(commandId, handler, expression, false);
142     }
143
144     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
145             final IHandler handler, final Expression expression,
146             final boolean global) {
147         if (global) {
148             final IHandlerActivation activation = parent.activateHandler(
149                     commandId, handler, expression, global);
150             parentActivations.add(activation);
151             return activation;
152         }
153
154         final AndExpression andExpression;
155         if (expression instanceof AndExpression) {
156             andExpression = (AndExpression) expression;
157         } else {
158             andExpression = new AndExpression();
159             if (expression != null) {
160                 andExpression.add(expression);
161             }
162         }
163         if (defaultExpression != null) {
164             andExpression.add(defaultExpression);
165         }
166         final IHandlerActivation localActivation = new HandlerActivation(
167                 commandId, handler, andExpression,
168                 IHandlerActivation.ROOT_DEPTH, this);
169         return doActivation(localActivation);
170     }
171
172     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
173             final IHandler handler, final Expression expression,
174             final int sourcePriorities) {
175         return activateHandler(commandId, handler, expression);
176     }
177
178     public final void addSourceProvider(final ISourceProvider provider) {
179         if (!fSourceProviders.contains(provider)) {
180             fSourceProviders.add(provider);
181         }
182         parent.addSourceProvider(provider);
183     }
184
185     public final ExecutionEvent createExecutionEvent(final Command command,
186             final Event event) {
187         return parent.createExecutionEvent(command, event);
188     }
189
190     public final ExecutionEvent createExecutionEvent(
191             final ParameterizedCommand command, final Event event) {
192         return parent.createExecutionEvent(command, event);
193     }
194
195     public final void deactivateHandler(final IHandlerActivation activation) {
196         final IHandlerActivation parentActivation;
197         if (localActivationsToParentActivations.containsKey(activation)) {
198             parentActivation = (IHandlerActivation) localActivationsToParentActivations
199                     .remove(activation);
200         } else {
201             parentActivation = activation;
202         }
203
204         if (parentActivation != null) {
205             parent.deactivateHandler(parentActivation);
206             parentActivations.remove(parentActivation);
207         }
208     }
209
210     public final void deactivateHandlers(final Collection JavaDoc activations) {
211         Object JavaDoc[] array = activations.toArray();
212         for (int i = 0; i < array.length; i++) {
213             deactivateHandler((IHandlerActivation) array[i]);
214             array[i] = null;
215         }
216     }
217
218     public final void dispose() {
219         parent.deactivateHandlers(parentActivations);
220         parentActivations.clear();
221         localActivationsToParentActivations.clear();
222
223         // Remove any "resource", like listeners, that were associated
224
// with this service.
225
if (!fSourceProviders.isEmpty()) {
226             Object JavaDoc[] array = fSourceProviders.toArray();
227             for (int i = 0; i < array.length; i++) {
228                 removeSourceProvider((ISourceProvider) array[i]);
229             }
230             fSourceProviders.clear();
231         }
232     }
233
234     protected IHandlerActivation doActivation(
235             final IHandlerActivation localActivation) {
236         final IHandlerActivation parentActivation;
237         parentActivation = parent.activateHandler(localActivation);
238         parentActivations.add(parentActivation);
239         localActivationsToParentActivations.put(localActivation,
240                 parentActivation);
241         return localActivation;
242     }
243
244     public final Object JavaDoc executeCommand(final ParameterizedCommand command,
245             final Event event) throws ExecutionException, NotDefinedException,
246             NotEnabledException, NotHandledException {
247         return parent.executeCommand(command, event);
248     }
249
250     public final Object JavaDoc executeCommand(final String JavaDoc commandId, final Event event)
251             throws ExecutionException, NotDefinedException,
252             NotEnabledException, NotHandledException {
253         return parent.executeCommand(commandId, event);
254     }
255
256     public final IEvaluationContext getCurrentState() {
257         return parent.getCurrentState();
258     }
259
260     public final void readRegistry() {
261         parent.readRegistry();
262     }
263
264     public final void removeSourceProvider(final ISourceProvider provider) {
265         fSourceProviders.remove(provider);
266         parent.removeSourceProvider(provider);
267     }
268
269     public final void setHelpContextId(final IHandler handler,
270             final String JavaDoc helpContextId) {
271         parent.setHelpContextId(handler, helpContextId);
272     }
273
274 }
275
276
Popular Tags