001: /*******************************************************************************
002: * Copyright (c) 2003, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.contexts;
011:
012: import java.util.Collection;
013: import java.util.HashMap;
014: import java.util.Iterator;
015: import java.util.Map;
016:
017: import org.eclipse.core.commands.contexts.ContextManager;
018: import org.eclipse.swt.widgets.Shell;
019: import org.eclipse.ui.ISources;
020: import org.eclipse.ui.LegacyHandlerSubmissionExpression;
021: import org.eclipse.ui.contexts.EnabledSubmission;
022: import org.eclipse.ui.contexts.IContextActivation;
023: import org.eclipse.ui.contexts.IContextManager;
024: import org.eclipse.ui.contexts.IContextService;
025: import org.eclipse.ui.contexts.IWorkbenchContextSupport;
026: import org.eclipse.ui.internal.Workbench;
027: import org.eclipse.ui.keys.IBindingService;
028:
029: /**
030: * Provides support for contexts within the workbench -- including key bindings,
031: * and some default contexts for shell types.
032: *
033: * @since 3.0
034: */
035: public class WorkbenchContextSupport implements
036: IWorkbenchContextSupport {
037:
038: /**
039: * The map of activations that have been given to the handler service (<code>IHandlerActivation</code>),
040: * indexed by the submissions (<code>HandlerSubmission</code>). This map
041: * should be <code>null</code> if there are no such activations.
042: */
043: private Map activationsBySubmission = null;
044:
045: /**
046: * The binding service for the workbench. This value is never
047: * <code>null</code>.
048: */
049: private IBindingService bindingService;
050:
051: /**
052: * The context service for the workbench. This value is never
053: * <code>null</code>.
054: */
055: private IContextService contextService;
056:
057: /**
058: * The legacy context manager supported by this application.
059: */
060: private ContextManagerLegacyWrapper contextManagerWrapper;
061:
062: /**
063: * The workbench for which context support is being provided. This value
064: * must not be <code>null</code>.
065: */
066: private final Workbench workbench;
067:
068: /**
069: * Constructs a new instance of <code>WorkbenchCommandSupport</code>.
070: * This attaches the key binding support, and adds a global shell activation
071: * filter.
072: *
073: * @param workbenchToSupport
074: * The workbench that needs to be supported by this instance;
075: * must not be <code>null</code>.
076: * @param contextManager
077: * The context manager to be wrappered; must not be
078: * <code>null</code>.
079: */
080: public WorkbenchContextSupport(final Workbench workbenchToSupport,
081: final ContextManager contextManager) {
082: workbench = workbenchToSupport;
083: contextService = (IContextService) workbench
084: .getService(IContextService.class);
085: bindingService = (IBindingService) workbench
086: .getService(IBindingService.class);
087: contextManagerWrapper = ContextManagerFactory
088: .getContextManagerWrapper(contextManager);
089: }
090:
091: public final void addEnabledSubmission(
092: final EnabledSubmission enabledSubmission) {
093: /*
094: * Create the source priorities based on the conditions mentioned in the
095: * submission.
096: */
097: int sourcePriorities = 0;
098: if (enabledSubmission.getActivePartId() != null) {
099: sourcePriorities |= ISources.ACTIVE_PART_ID;
100: }
101: if (enabledSubmission.getActiveShell() != null) {
102: sourcePriorities |= (ISources.ACTIVE_SHELL | ISources.ACTIVE_WORKBENCH_WINDOW);
103: }
104: if (enabledSubmission.getActiveWorkbenchPartSite() != null) {
105: sourcePriorities |= ISources.ACTIVE_SITE;
106: }
107:
108: final IContextActivation activation = contextService
109: .activateContext(enabledSubmission.getContextId(),
110: new LegacyHandlerSubmissionExpression(
111: enabledSubmission.getActivePartId(),
112: enabledSubmission.getActiveShell(),
113: enabledSubmission
114: .getActiveWorkbenchPartSite()));
115: if (activationsBySubmission == null) {
116: activationsBySubmission = new HashMap();
117: }
118: activationsBySubmission.put(enabledSubmission, activation);
119: }
120:
121: public final void addEnabledSubmissions(
122: final Collection enabledSubmissions) {
123: final Iterator submissionItr = enabledSubmissions.iterator();
124: while (submissionItr.hasNext()) {
125: addEnabledSubmission((EnabledSubmission) submissionItr
126: .next());
127: }
128: }
129:
130: public final IContextManager getContextManager() {
131: return contextManagerWrapper;
132: }
133:
134: public final int getShellType(Shell shell) {
135: return contextService.getShellType(shell);
136: }
137:
138: public final boolean isKeyFilterEnabled() {
139: return bindingService.isKeyFilterEnabled();
140: }
141:
142: public final void openKeyAssistDialog() {
143: bindingService.openKeyAssistDialog();
144: }
145:
146: public final boolean registerShell(final Shell shell, final int type) {
147: return contextService.registerShell(shell, type);
148: }
149:
150: public final void removeEnabledSubmission(
151: final EnabledSubmission enabledSubmission) {
152: if (activationsBySubmission == null) {
153: return;
154: }
155:
156: final Object value = activationsBySubmission
157: .remove(enabledSubmission);
158: if (value instanceof IContextActivation) {
159: final IContextActivation activation = (IContextActivation) value;
160: contextService.deactivateContext(activation);
161: }
162: }
163:
164: public final void removeEnabledSubmissions(
165: final Collection enabledSubmissions) {
166: final Iterator submissionItr = enabledSubmissions.iterator();
167: while (submissionItr.hasNext()) {
168: removeEnabledSubmission((EnabledSubmission) submissionItr
169: .next());
170: }
171: }
172:
173: public final void setKeyFilterEnabled(final boolean enabled) {
174: bindingService.setKeyFilterEnabled(enabled);
175: }
176:
177: public final boolean unregisterShell(final Shell shell) {
178: return contextService.unregisterShell(shell);
179: }
180: }
|