001: /*******************************************************************************
002: * Copyright (c) 2003, 2005 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.commands;
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.CommandManager;
018: import org.eclipse.core.commands.contexts.ContextManager;
019: import org.eclipse.jface.bindings.BindingManager;
020: import org.eclipse.ui.ISources;
021: import org.eclipse.ui.LegacyHandlerSubmissionExpression;
022: import org.eclipse.ui.commands.HandlerSubmission;
023: import org.eclipse.ui.commands.ICommandManager;
024: import org.eclipse.ui.commands.IWorkbenchCommandSupport;
025: import org.eclipse.ui.commands.Priority;
026: import org.eclipse.ui.handlers.IHandlerActivation;
027: import org.eclipse.ui.handlers.IHandlerService;
028: import org.eclipse.ui.internal.handlers.LegacyHandlerWrapper;
029:
030: /**
031: * Provides command support in terms of the workbench.
032: *
033: * @since 3.0
034: */
035: public class WorkbenchCommandSupport implements
036: IWorkbenchCommandSupport {
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 mutable command manager that should be notified of changes to the
047: * list of active handlers. This value is never <code>null</code>.
048: */
049: private final CommandManagerLegacyWrapper commandManagerWrapper;
050:
051: /**
052: * The handler service for the workbench. This value is never
053: * <code>null</code>.
054: */
055: private final IHandlerService handlerService;
056:
057: /**
058: * Constructs a new instance of <code>WorkbenchCommandSupport</code>
059: *
060: * @param bindingManager
061: * The binding manager providing support for the command manager;
062: * must not be <code>null</code>.
063: * @param commandManager
064: * The command manager for the workbench; must not be
065: * <code>null</code>.
066: * @param contextManager
067: * The context manager providing support for the command manager
068: * and binding manager; must not be <code>null</code>.
069: * @param handlerService
070: * The handler service for the workbench; must not be
071: * <code>null</code>.
072: */
073: public WorkbenchCommandSupport(final BindingManager bindingManager,
074: final CommandManager commandManager,
075: final ContextManager contextManager,
076: final IHandlerService handlerService) {
077: if (handlerService == null) {
078: throw new NullPointerException(
079: "The handler service cannot be null"); //$NON-NLS-1$
080: }
081:
082: this .handlerService = handlerService;
083:
084: commandManagerWrapper = CommandManagerFactory
085: .getCommandManagerWrapper(bindingManager,
086: commandManager, contextManager);
087:
088: // Initialize the old key formatter settings.
089: org.eclipse.ui.keys.KeyFormatterFactory
090: .setDefault(org.eclipse.ui.keys.SWTKeySupport
091: .getKeyFormatterForPlatform());
092: }
093:
094: public final void addHandlerSubmission(
095: final HandlerSubmission handlerSubmission) {
096: /*
097: * Create the source priorities based on the conditions mentioned in the
098: * submission.
099: */
100: int sourcePriorities = 0;
101: if (handlerSubmission.getActivePartId() != null) {
102: sourcePriorities |= ISources.ACTIVE_PART_ID;
103: }
104: if (handlerSubmission.getActiveShell() != null) {
105: sourcePriorities |= (ISources.ACTIVE_SHELL | ISources.ACTIVE_WORKBENCH_WINDOW);
106: }
107: if (handlerSubmission.getActiveWorkbenchPartSite() != null) {
108: sourcePriorities |= ISources.ACTIVE_SITE;
109: }
110: if (handlerSubmission.getPriority() == Priority.LEGACY) {
111: sourcePriorities |= ISources.LEGACY_LEGACY;
112: } else if (handlerSubmission.getPriority() == Priority.LOW) {
113: sourcePriorities |= ISources.LEGACY_LOW;
114: } else if (handlerSubmission.getPriority() == Priority.MEDIUM) {
115: sourcePriorities |= ISources.LEGACY_MEDIUM;
116: }
117:
118: final IHandlerActivation activation = handlerService
119: .activateHandler(handlerSubmission.getCommandId(),
120: new LegacyHandlerWrapper(handlerSubmission
121: .getHandler()),
122: new LegacyHandlerSubmissionExpression(
123: handlerSubmission.getActivePartId(),
124: handlerSubmission.getActiveShell(),
125: handlerSubmission
126: .getActiveWorkbenchPartSite()));
127: if (activationsBySubmission == null) {
128: activationsBySubmission = new HashMap();
129: }
130: activationsBySubmission.put(handlerSubmission, activation);
131: }
132:
133: public final void addHandlerSubmissions(
134: final Collection handlerSubmissions) {
135: final Iterator submissionItr = handlerSubmissions.iterator();
136: while (submissionItr.hasNext()) {
137: addHandlerSubmission((HandlerSubmission) submissionItr
138: .next());
139: }
140: }
141:
142: public ICommandManager getCommandManager() {
143: return commandManagerWrapper;
144: }
145:
146: public final void removeHandlerSubmission(
147: final HandlerSubmission handlerSubmission) {
148: if (activationsBySubmission == null) {
149: return;
150: }
151:
152: final Object value = activationsBySubmission
153: .remove(handlerSubmission);
154: if (value instanceof IHandlerActivation) {
155: final IHandlerActivation activation = (IHandlerActivation) value;
156: handlerService.deactivateHandler(activation);
157: }
158: }
159:
160: public final void removeHandlerSubmissions(
161: final Collection handlerSubmissions) {
162: final Iterator submissionItr = handlerSubmissions.iterator();
163: while (submissionItr.hasNext()) {
164: removeHandlerSubmission((HandlerSubmission) submissionItr
165: .next());
166: }
167: }
168: }
|