KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 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.Collection JavaDoc;
15 import java.util.Collections JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.core.commands.Command;
19 import org.eclipse.core.commands.ExecutionEvent;
20 import org.eclipse.core.commands.ExecutionException;
21 import org.eclipse.core.commands.IHandler;
22 import org.eclipse.core.commands.NotEnabledException;
23 import org.eclipse.core.commands.NotHandledException;
24 import org.eclipse.core.commands.ParameterizedCommand;
25 import org.eclipse.core.commands.common.NotDefinedException;
26 import org.eclipse.core.expressions.Expression;
27 import org.eclipse.core.expressions.IEvaluationContext;
28 import org.eclipse.swt.widgets.Event;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.ISourceProvider;
31 import org.eclipse.ui.ISources;
32 import org.eclipse.ui.commands.ICommandService;
33 import org.eclipse.ui.handlers.IHandlerActivation;
34 import org.eclipse.ui.handlers.IHandlerService;
35 import org.eclipse.ui.internal.misc.Policy;
36 import org.eclipse.ui.internal.services.IEvaluationService;
37
38 /**
39  * <p>
40  * Provides services related to activating and deactivating handlers within the
41  * workbench.
42  * </p>
43  *
44  * @since 3.1
45  */

46 public final class HandlerService implements IHandlerService {
47
48     static {
49         Command.DEBUG_HANDLERS = Policy.DEBUG_HANDLERS_VERBOSE;
50         Command.DEBUG_HANDLERS_COMMAND_ID = Policy.DEBUG_HANDLERS_VERBOSE_COMMAND_ID;
51     }
52
53     /**
54      * The command service for this handler service. This value is never
55      * <code>null</code>.
56      */

57     private final ICommandService commandService;
58
59     /**
60      * The central authority for determining which handler we should use.
61      */

62     private final HandlerAuthority handlerAuthority;
63
64     /**
65      * The class providing persistence for this service.
66      */

67     private final HandlerPersistence handlerPersistence;
68
69     /**
70      * Constructs a new instance of <code>CommandService</code> using a
71      * command manager.
72      *
73      * @param commandService
74      * The command service to use; must not be <code>null</code>.
75      * @param evaluationService
76      * The evaluation service to use; must not be <code>null</code>.
77      */

78     public HandlerService(final ICommandService commandService,
79             final IEvaluationService evaluationService) {
80         if (commandService == null) {
81             throw new NullPointerException JavaDoc(
82                     "A handler service requires a command service"); //$NON-NLS-1$
83
}
84         this.commandService = commandService;
85         this.handlerAuthority = new HandlerAuthority(commandService);
86         this.handlerPersistence = new HandlerPersistence(this,
87                 evaluationService);
88     }
89
90     public final IHandlerActivation activateHandler(
91             final IHandlerActivation childActivation) {
92         final String JavaDoc commandId = childActivation.getCommandId();
93         final IHandler handler = childActivation.getHandler();
94         final Expression expression = childActivation.getExpression();
95         final int depth = childActivation.getDepth() + 1;
96         final IHandlerActivation localActivation = new HandlerActivation(
97                 commandId, handler, expression, depth, this);
98         handlerAuthority.activateHandler(localActivation);
99         return localActivation;
100     }
101
102     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
103             final IHandler handler) {
104         return activateHandler(commandId, handler, null);
105     }
106
107     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
108             final IHandler handler, final Expression expression) {
109         return activateHandler(commandId, handler, expression, false);
110     }
111
112     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
113             final IHandler handler, final Expression expression,
114             final boolean global) {
115         final IHandlerActivation activation = new HandlerActivation(commandId,
116                 handler, expression, IHandlerActivation.ROOT_DEPTH, this);
117         handlerAuthority.activateHandler(activation);
118         return activation;
119     }
120
121     public final IHandlerActivation activateHandler(final String JavaDoc commandId,
122             final IHandler handler, final Expression expression,
123             final int sourcePriority) {
124         return activateHandler(commandId, handler, expression);
125     }
126
127     public final void addSourceProvider(final ISourceProvider provider) {
128         handlerAuthority.addSourceProvider(provider);
129     }
130
131     public final ExecutionEvent createExecutionEvent(final Command command,
132             final Event event) {
133         return new ExecutionEvent(command, null, event, getCurrentState());
134     }
135
136     public ExecutionEvent createExecutionEvent(
137             final ParameterizedCommand command, final Event event) {
138         return new ExecutionEvent(command.getCommand(), command
139                 .getParameterMap(), event, getCurrentState());
140     }
141
142     public final void deactivateHandler(final IHandlerActivation activation) {
143         if (activation.getHandlerService() == this) {
144             handlerAuthority.deactivateHandler(activation);
145         }
146     }
147
148     public final void deactivateHandlers(final Collection JavaDoc activations) {
149         final Iterator JavaDoc activationItr = activations.iterator();
150         while (activationItr.hasNext()) {
151             final IHandlerActivation activation = (IHandlerActivation) activationItr
152                     .next();
153             deactivateHandler(activation);
154         }
155     }
156
157     public final void dispose() {
158         handlerAuthority.dispose();
159         handlerPersistence.dispose();
160     }
161
162     public final Object JavaDoc executeCommand(final ParameterizedCommand command,
163             final Event trigger) throws ExecutionException,
164             NotDefinedException, NotEnabledException, NotHandledException {
165         return command.executeWithChecks(trigger, getCurrentState());
166     }
167
168     public final Object JavaDoc executeCommand(final String JavaDoc commandId,
169             final Event trigger) throws ExecutionException,
170             NotDefinedException, NotEnabledException, NotHandledException {
171         final Command command = commandService.getCommand(commandId);
172         final ExecutionEvent event = new ExecutionEvent(command,
173                 Collections.EMPTY_MAP, trigger, getCurrentState());
174         return command.executeWithChecks(event);
175     }
176
177     public final IEvaluationContext getCurrentState() {
178         return handlerAuthority.getCurrentState();
179     }
180
181     public final void readRegistry() {
182         handlerPersistence.read();
183     }
184
185     public final void removeSourceProvider(final ISourceProvider provider) {
186         handlerAuthority.removeSourceProvider(provider);
187     }
188
189     public final void setHelpContextId(final IHandler handler,
190             final String JavaDoc helpContextId) {
191         commandService.setHelpContextId(handler, helpContextId);
192     }
193
194     /**
195      * <p>
196      * Bug 95792. A mechanism by which the key binding architecture can force an
197      * update of the handlers (based on the active shell) before trying to
198      * execute a command. This mechanism is required for GTK+ only.
199      * </p>
200      * <p>
201      * DO NOT CALL THIS METHOD.
202      * </p>
203      */

204     public final void updateShellKludge() {
205         handlerAuthority.updateShellKludge();
206     }
207
208     /**
209      * <p>
210      * Bug 95792. A mechanism by which the key binding architecture can force an
211      * update of the handlers (based on the active shell) before trying to
212      * execute a command. This mechanism is required for GTK+ only.
213      * </p>
214      * <p>
215      * DO NOT CALL THIS METHOD.
216      * </p>
217      *
218      * @param shell
219      * The shell that should be considered active; must not be
220      * <code>null</code>.
221      */

222     public final void updateShellKludge(final Shell shell) {
223         final Shell currentActiveShell = handlerAuthority.getActiveShell();
224         if (currentActiveShell != shell) {
225             handlerAuthority.sourceChanged(ISources.ACTIVE_SHELL,
226                     ISources.ACTIVE_SHELL_NAME, shell);
227         }
228     }
229
230     /**
231      * Currently this is a an internal method to help locate a handler.
232      * <p>
233      * DO NOT CALL THIS METHOD.
234      * </p>
235      *
236      * @param commandId
237      * the command id to check
238      * @param context
239      * the context to use for activations
240      * @since 3.3
241      */

242     public final IHandler findHandler(String JavaDoc commandId,
243             IEvaluationContext context) {
244         return handlerAuthority.findHandler(commandId, context);
245     }
246
247     /**
248      * Normally the context returned from getCurrentState() still tracks the
249      * application state. This method creates a copy and fills it in with the
250      * variables that we know about. Currently it does not fill in the active
251      * selection.
252      * <p>
253      * DO NOT CALL THIS METHOD. It is experimental in 3.3.
254      * </p>
255      *
256      * @return an evaluation context with no parent.
257      * @since 3.3
258      */

259     public final IEvaluationContext getContextSnapshot() {
260         return handlerAuthority.getContextSnapshot();
261     }
262     
263     /**
264      * Normally the context returned from getCurrentState() still tracks the
265      * application state. This method creates a copy and fills it in with all the
266      * variables that we know about.
267      * <p>
268      * DO NOT CALL THIS METHOD. It is experimental in 3.3.
269      * </p>
270      *
271      * @return an evaluation context with no parent.
272      * @since 3.3
273      */

274     public final IEvaluationContext getFullContextSnapshot() {
275         return handlerAuthority.getFullContextSnapshot();
276     }
277
278     /**
279      * Execute the command using the provided context. It takes care of finding
280      * the correct active handler given the context, and executes with that
281      * handler.
282      * <p>
283      * It currently cannot effect the enablement of the handler.
284      * </p>
285      * <p>
286      * DO NOT CALL THIS METHOD. It is experimental in 3.3.
287      * </p>
288      *
289      * @param command
290      * the parameterized command to execute
291      * @param trigger
292      * the SWT event trigger ... can be null
293      * @param context
294      * the evaluation context to run against.
295      * @return
296      * @throws ExecutionException
297      * @throws NotDefinedException
298      * @throws NotEnabledException
299      * @throws NotHandledException
300      * @since 3.3
301      * @see #getContextSnapshot()
302      */

303     public final Object JavaDoc executeCommandInContext(
304             final ParameterizedCommand command, final Event trigger,
305             IEvaluationContext context) throws ExecutionException,
306             NotDefinedException, NotEnabledException, NotHandledException {
307         IHandler oldHandler = command.getCommand().getHandler();
308
309         IHandler handler = findHandler(command.getId(), context);
310         boolean enabled = true;
311         if (handler instanceof HandlerProxy) {
312             enabled = ((HandlerProxy) handler).getProxyEnabled();
313         }
314
315         try {
316             command.getCommand().setHandler(handler);
317             if (handler instanceof HandlerProxy) {
318                 ((HandlerProxy) handler).setEnabledFor(context);
319             }
320
321             return command.executeWithChecks(trigger, context);
322         } finally {
323             if (handler instanceof HandlerProxy) {
324                 ((HandlerProxy) handler).setProxyEnabled(enabled);
325             }
326             command.getCommand().setHandler(oldHandler);
327         }
328     }
329 }
330
Popular Tags