KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.commands.contexts.ContextManager;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.ISources;
21 import org.eclipse.ui.LegacyHandlerSubmissionExpression;
22 import org.eclipse.ui.contexts.EnabledSubmission;
23 import org.eclipse.ui.contexts.IContextActivation;
24 import org.eclipse.ui.contexts.IContextManager;
25 import org.eclipse.ui.contexts.IContextService;
26 import org.eclipse.ui.contexts.IWorkbenchContextSupport;
27 import org.eclipse.ui.internal.Workbench;
28 import org.eclipse.ui.keys.IBindingService;
29
30 /**
31  * Provides support for contexts within the workbench -- including key bindings,
32  * and some default contexts for shell types.
33  *
34  * @since 3.0
35  */

36 public class WorkbenchContextSupport implements IWorkbenchContextSupport {
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 binding service for the workbench. This value is never
47      * <code>null</code>.
48      */

49     private IBindingService bindingService;
50
51     /**
52      * The context service for the workbench. This value is never
53      * <code>null</code>.
54      */

55     private IContextService contextService;
56
57     /**
58      * The legacy context manager supported by this application.
59      */

60     private ContextManagerLegacyWrapper contextManagerWrapper;
61
62     /**
63      * The workbench for which context support is being provided. This value
64      * must not be <code>null</code>.
65      */

66     private final Workbench workbench;
67
68     /**
69      * Constructs a new instance of <code>WorkbenchCommandSupport</code>.
70      * This attaches the key binding support, and adds a global shell activation
71      * filter.
72      *
73      * @param workbenchToSupport
74      * The workbench that needs to be supported by this instance;
75      * must not be <code>null</code>.
76      * @param contextManager
77      * The context manager to be wrappered; must not be
78      * <code>null</code>.
79      */

80     public WorkbenchContextSupport(final Workbench workbenchToSupport,
81             final ContextManager contextManager) {
82         workbench = workbenchToSupport;
83         contextService = (IContextService) workbench.getService(IContextService.class);
84         bindingService = (IBindingService) workbench.getService(IBindingService.class);
85         contextManagerWrapper = ContextManagerFactory
86                 .getContextManagerWrapper(contextManager);
87     }
88
89     public final void addEnabledSubmission(
90             final EnabledSubmission enabledSubmission) {
91         /*
92          * Create the source priorities based on the conditions mentioned in the
93          * submission.
94          */

95         int sourcePriorities = 0;
96         if (enabledSubmission.getActivePartId() != null) {
97             sourcePriorities |= ISources.ACTIVE_PART_ID;
98         }
99         if (enabledSubmission.getActiveShell() != null) {
100             sourcePriorities |= (ISources.ACTIVE_SHELL | ISources.ACTIVE_WORKBENCH_WINDOW);
101         }
102         if (enabledSubmission.getActiveWorkbenchPartSite() != null) {
103             sourcePriorities |= ISources.ACTIVE_SITE;
104         }
105
106         final IContextActivation activation = contextService.activateContext(
107                 enabledSubmission.getContextId(),
108                 new LegacyHandlerSubmissionExpression(enabledSubmission
109                         .getActivePartId(), enabledSubmission.getActiveShell(),
110                         enabledSubmission.getActiveWorkbenchPartSite()));
111         if (activationsBySubmission == null) {
112             activationsBySubmission = new HashMap JavaDoc();
113         }
114         activationsBySubmission.put(enabledSubmission, activation);
115     }
116
117     public final void addEnabledSubmissions(final Collection JavaDoc enabledSubmissions) {
118         final Iterator JavaDoc submissionItr = enabledSubmissions.iterator();
119         while (submissionItr.hasNext()) {
120             addEnabledSubmission((EnabledSubmission) submissionItr.next());
121         }
122     }
123
124     public final IContextManager getContextManager() {
125         return contextManagerWrapper;
126     }
127
128     public final int getShellType(Shell shell) {
129         return contextService.getShellType(shell);
130     }
131
132     public final boolean isKeyFilterEnabled() {
133         return bindingService.isKeyFilterEnabled();
134     }
135
136     public final void openKeyAssistDialog() {
137         bindingService.openKeyAssistDialog();
138     }
139
140     public final boolean registerShell(final Shell shell, final int type) {
141         return contextService.registerShell(shell, type);
142     }
143
144     public final void removeEnabledSubmission(
145             final EnabledSubmission enabledSubmission) {
146         if (activationsBySubmission == null) {
147             return;
148         }
149
150         final Object JavaDoc value = activationsBySubmission.remove(enabledSubmission);
151         if (value instanceof IContextActivation) {
152             final IContextActivation activation = (IContextActivation) value;
153             contextService.deactivateContext(activation);
154         }
155     }
156
157     public final void removeEnabledSubmissions(
158             final Collection JavaDoc enabledSubmissions) {
159         final Iterator JavaDoc submissionItr = enabledSubmissions.iterator();
160         while (submissionItr.hasNext()) {
161             removeEnabledSubmission((EnabledSubmission) submissionItr.next());
162         }
163     }
164
165     public final void setKeyFilterEnabled(final boolean enabled) {
166         bindingService.setKeyFilterEnabled(enabled);
167     }
168
169     public final boolean unregisterShell(final Shell shell) {
170         return contextService.unregisterShell(shell);
171     }
172 }
173
Popular Tags