KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > commands > WorkbenchCommandSupport


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 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.commands;
12
13 import java.util.Collection JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.commands.CommandManager;
19 import org.eclipse.core.commands.contexts.ContextManager;
20 import org.eclipse.jface.bindings.BindingManager;
21 import org.eclipse.ui.ISources;
22 import org.eclipse.ui.LegacyHandlerSubmissionExpression;
23 import org.eclipse.ui.commands.HandlerSubmission;
24 import org.eclipse.ui.commands.ICommandManager;
25 import org.eclipse.ui.commands.IWorkbenchCommandSupport;
26 import org.eclipse.ui.commands.Priority;
27 import org.eclipse.ui.handlers.IHandlerActivation;
28 import org.eclipse.ui.handlers.IHandlerService;
29 import org.eclipse.ui.internal.handlers.LegacyHandlerWrapper;
30
31 /**
32  * Provides command support in terms of the workbench.
33  *
34  * @since 3.0
35  */

36 public class WorkbenchCommandSupport implements IWorkbenchCommandSupport {
37
38     /**
39      * The map of activations that have been given to the handler service (<code>IHandlerActivation</code>),
40      * indexed by the submissions (<code>HandlerSubmission</code>). This map
41      * should be <code>null</code> if there are no such activations.
42      */

43     private Map JavaDoc activationsBySubmission = null;
44
45     /**
46      * The mutable command manager that should be notified of changes to the
47      * list of active handlers. This value is never <code>null</code>.
48      */

49     private final CommandManagerLegacyWrapper commandManagerWrapper;
50
51     /**
52      * The handler service for the workbench. This value is never
53      * <code>null</code>.
54      */

55     private final IHandlerService handlerService;
56
57     /**
58      * Constructs a new instance of <code>WorkbenchCommandSupport</code>
59      *
60      * @param bindingManager
61      * The binding manager providing support for the command manager;
62      * must not be <code>null</code>.
63      * @param commandManager
64      * The command manager for the workbench; must not be
65      * <code>null</code>.
66      * @param contextManager
67      * The context manager providing support for the command manager
68      * and binding manager; must not be <code>null</code>.
69      * @param handlerService
70      * The handler service for the workbench; must not be
71      * <code>null</code>.
72      */

73     public WorkbenchCommandSupport(final BindingManager bindingManager,
74             final CommandManager commandManager,
75             final ContextManager contextManager,
76             final IHandlerService handlerService) {
77         if (handlerService == null) {
78             throw new NullPointerException JavaDoc("The handler service cannot be null"); //$NON-NLS-1$
79
}
80
81         this.handlerService = handlerService;
82
83         commandManagerWrapper = CommandManagerFactory.getCommandManagerWrapper(
84                 bindingManager, commandManager, contextManager);
85
86         // Initialize the old key formatter settings.
87
org.eclipse.ui.keys.KeyFormatterFactory
88                 .setDefault(org.eclipse.ui.keys.SWTKeySupport
89                         .getKeyFormatterForPlatform());
90     }
91
92     public final void addHandlerSubmission(
93             final HandlerSubmission handlerSubmission) {
94         /*
95          * Create the source priorities based on the conditions mentioned in the
96          * submission.
97          */

98         int sourcePriorities = 0;
99         if (handlerSubmission.getActivePartId() != null) {
100             sourcePriorities |= ISources.ACTIVE_PART_ID;
101         }
102         if (handlerSubmission.getActiveShell() != null) {
103             sourcePriorities |= (ISources.ACTIVE_SHELL | ISources.ACTIVE_WORKBENCH_WINDOW);
104         }
105         if (handlerSubmission.getActiveWorkbenchPartSite() != null) {
106             sourcePriorities |= ISources.ACTIVE_SITE;
107         }
108         if (handlerSubmission.getPriority() == Priority.LEGACY) {
109             sourcePriorities |= ISources.LEGACY_LEGACY;
110         } else if (handlerSubmission.getPriority() == Priority.LOW) {
111             sourcePriorities |= ISources.LEGACY_LOW;
112         } else if (handlerSubmission.getPriority() == Priority.MEDIUM) {
113             sourcePriorities |= ISources.LEGACY_MEDIUM;
114         }
115
116         final IHandlerActivation activation = handlerService.activateHandler(
117                 handlerSubmission.getCommandId(), new LegacyHandlerWrapper(
118                         handlerSubmission.getHandler()),
119                 new LegacyHandlerSubmissionExpression(handlerSubmission
120                         .getActivePartId(), handlerSubmission.getActiveShell(),
121                         handlerSubmission.getActiveWorkbenchPartSite()));
122         if (activationsBySubmission == null) {
123             activationsBySubmission = new HashMap JavaDoc();
124         }
125         activationsBySubmission.put(handlerSubmission, activation);
126     }
127
128     public final void addHandlerSubmissions(final Collection JavaDoc handlerSubmissions) {
129         final Iterator JavaDoc submissionItr = handlerSubmissions.iterator();
130         while (submissionItr.hasNext()) {
131             addHandlerSubmission((HandlerSubmission) submissionItr.next());
132         }
133     }
134
135     public ICommandManager getCommandManager() {
136         return commandManagerWrapper;
137     }
138
139     public final void removeHandlerSubmission(
140             final HandlerSubmission handlerSubmission) {
141         if (activationsBySubmission == null) {
142             return;
143         }
144
145         final Object JavaDoc value = activationsBySubmission.remove(handlerSubmission);
146         if (value instanceof IHandlerActivation) {
147             final IHandlerActivation activation = (IHandlerActivation) value;
148             handlerService.deactivateHandler(activation);
149         }
150     }
151
152     public final void removeHandlerSubmissions(
153             final Collection JavaDoc handlerSubmissions) {
154         final Iterator JavaDoc submissionItr = handlerSubmissions.iterator();
155         while (submissionItr.hasNext()) {
156             removeHandlerSubmission((HandlerSubmission) submissionItr.next());
157         }
158     }
159 }
160
Popular Tags