001: /*******************************************************************************
002: * Copyright (c) 2004, 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.commands;
011:
012: import java.util.ArrayList;
013: import java.util.Collections;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Map;
017:
018: import org.eclipse.core.commands.Command;
019: import org.eclipse.core.commands.ExecutionEvent;
020: import org.eclipse.core.commands.ParameterizedCommand;
021: import org.eclipse.jface.bindings.BindingManager;
022: import org.eclipse.jface.bindings.TriggerSequence;
023: import org.eclipse.ui.commands.ExecutionException;
024: import org.eclipse.ui.commands.ICommand;
025: import org.eclipse.ui.commands.ICommandListener;
026: import org.eclipse.ui.commands.NotDefinedException;
027: import org.eclipse.ui.commands.NotHandledException;
028: import org.eclipse.ui.internal.keys.KeySequenceBinding;
029: import org.eclipse.ui.keys.KeySequence;
030:
031: /**
032: * A wrapper around a core command so that it satisfies the deprecated
033: * <code>ICommand</code> interface.
034: *
035: * @since 3.1
036: */
037: final class CommandLegacyWrapper implements ICommand {
038:
039: /**
040: * The supporting binding manager; never <code>null</code>.
041: */
042: private final BindingManager bindingManager;
043:
044: /**
045: * The wrapped command; never <code>null</code>.
046: */
047: private final Command command;
048:
049: /**
050: * A parameterized representation of the command. This is created lazily. If
051: * it has not yet been created, it is <code>null</code>.
052: */
053: private ParameterizedCommand parameterizedCommand;
054:
055: /**
056: * Constructs a new <code>CommandWrapper</code>
057: *
058: * @param command
059: * The command to be wrapped; must not be <code>null</code>.
060: * @param bindingManager
061: * The binding manager to support this wrapper; must not be
062: * <code>null</code>.
063: */
064: CommandLegacyWrapper(final Command command,
065: final BindingManager bindingManager) {
066: if (command == null) {
067: throw new NullPointerException(
068: "The wrapped command cannot be <code>null</code>."); //$NON-NLS-1$
069: }
070:
071: if (bindingManager == null) {
072: throw new NullPointerException(
073: "A binding manager is required to wrap a command"); //$NON-NLS-1$
074: }
075:
076: this .command = command;
077: this .bindingManager = bindingManager;
078: }
079:
080: /*
081: * (non-Javadoc)
082: *
083: * @see org.eclipse.ui.commands.ICommand#addCommandListener(org.eclipse.ui.commands.ICommandListener)
084: */
085:
086: public final void addCommandListener(
087: final ICommandListener commandListener) {
088: command.addCommandListener(new LegacyCommandListenerWrapper(
089: commandListener, bindingManager));
090: }
091:
092: /*
093: * (non-Javadoc)
094: *
095: * @see org.eclipse.ui.commands.ICommand#execute(java.util.Map)
096: */
097: public final Object execute(Map parameterValuesByName)
098: throws ExecutionException, NotHandledException {
099: try {
100: return command
101: .execute(new ExecutionEvent(
102: command,
103: (parameterValuesByName == null) ? Collections.EMPTY_MAP
104: : parameterValuesByName, null, null));
105: } catch (final org.eclipse.core.commands.ExecutionException e) {
106: throw new ExecutionException(e);
107: } catch (final org.eclipse.core.commands.NotHandledException e) {
108: throw new NotHandledException(e);
109: }
110: }
111:
112: /*
113: * (non-Javadoc)
114: *
115: * @see org.eclipse.ui.commands.ICommand#getAttributeValuesByName()
116: */
117: public final Map getAttributeValuesByName() {
118: final Map attributeValues = new HashMap();
119: // avoid using Boolean.valueOf to allow compilation against JCL
120: // Foundation (bug 80053)
121: attributeValues.put(ILegacyAttributeNames.ENABLED, command
122: .isEnabled() ? Boolean.TRUE : Boolean.FALSE);
123: attributeValues.put(ILegacyAttributeNames.HANDLED, command
124: .isHandled() ? Boolean.TRUE : Boolean.FALSE);
125: return attributeValues;
126: }
127:
128: /*
129: * (non-Javadoc)
130: *
131: * @see org.eclipse.ui.commands.ICommand#getCategoryId()
132: */
133: public final String getCategoryId() throws NotDefinedException {
134: try {
135: return command.getCategory().getId();
136: } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
137: throw new NotDefinedException(e);
138: }
139: }
140:
141: /*
142: * (non-Javadoc)
143: *
144: * @see org.eclipse.ui.commands.ICommand#getDescription()
145: */
146: public final String getDescription() throws NotDefinedException {
147: try {
148: return command.getDescription();
149: } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
150: throw new NotDefinedException(e);
151: }
152: }
153:
154: /*
155: * (non-Javadoc)
156: *
157: * @see org.eclipse.ui.commands.ICommand#getId()
158: */
159: public final String getId() {
160: return command.getId();
161: }
162:
163: /*
164: * (non-Javadoc)
165: *
166: * @see org.eclipse.ui.commands.ICommand#getKeySequenceBindings()
167: */
168: public final List getKeySequenceBindings() {
169: final List legacyBindings = new ArrayList();
170: if (parameterizedCommand == null) {
171: parameterizedCommand = new ParameterizedCommand(command,
172: null);
173: }
174: final TriggerSequence[] activeBindings = bindingManager
175: .getActiveBindingsFor(parameterizedCommand);
176: final int activeBindingsCount = activeBindings.length;
177: for (int i = 0; i < activeBindingsCount; i++) {
178: final TriggerSequence triggerSequence = activeBindings[i];
179: if (triggerSequence instanceof org.eclipse.jface.bindings.keys.KeySequence) {
180: legacyBindings
181: .add(new KeySequenceBinding(
182: KeySequence
183: .getInstance((org.eclipse.jface.bindings.keys.KeySequence) triggerSequence),
184: 0));
185: }
186: }
187:
188: return legacyBindings;
189: }
190:
191: /*
192: * (non-Javadoc)
193: *
194: * @see org.eclipse.ui.commands.ICommand#getName()
195: */
196: public final String getName() throws NotDefinedException {
197: try {
198: return command.getName();
199: } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
200: throw new NotDefinedException(e);
201: }
202: }
203:
204: /*
205: * (non-Javadoc)
206: *
207: * @see org.eclipse.ui.commands.ICommand#isDefined()
208: */
209: public final boolean isDefined() {
210: return command.isDefined();
211: }
212:
213: /*
214: * (non-Javadoc)
215: *
216: * @see org.eclipse.ui.commands.ICommand#isHandled()
217: */
218: public final boolean isHandled() {
219: return command.isHandled();
220: }
221:
222: /*
223: * (non-Javadoc)
224: *
225: * @see org.eclipse.ui.commands.ICommand#removeCommandListener(org.eclipse.ui.commands.ICommandListener)
226: */
227: public final void removeCommandListener(
228: final ICommandListener commandListener) {
229: command.removeCommandListener(new LegacyCommandListenerWrapper(
230: commandListener, bindingManager));
231: }
232:
233: /*
234: * (non-Javadoc)
235: *
236: * @see java.lang.Comparable#compareTo(java.lang.Object)
237: */
238: public final int compareTo(final Object o) {
239: return command.compareTo(o);
240: }
241:
242: }
|