KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.ArrayList JavaDoc;
14 import java.util.Collections JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
18
19 import org.eclipse.core.commands.Command;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ParameterizedCommand;
22 import org.eclipse.jface.bindings.BindingManager;
23 import org.eclipse.jface.bindings.TriggerSequence;
24 import org.eclipse.ui.commands.ExecutionException;
25 import org.eclipse.ui.commands.ICommand;
26 import org.eclipse.ui.commands.ICommandListener;
27 import org.eclipse.ui.commands.NotDefinedException;
28 import org.eclipse.ui.commands.NotHandledException;
29 import org.eclipse.ui.internal.keys.KeySequenceBinding;
30 import org.eclipse.ui.keys.KeySequence;
31
32 /**
33  * A wrapper around a core command so that it satisfies the deprecated
34  * <code>ICommand</code> interface.
35  *
36  * @since 3.1
37  */

38 final class CommandWrapper implements ICommand {
39
40     /**
41      * The supporting binding manager; never <code>null</code>.
42      */

43     private final BindingManager bindingManager;
44
45     /**
46      * The wrapped command; never <code>null</code>.
47      */

48     private final Command command;
49
50     /**
51      * A parameterized representation of the command. This is created lazily. If
52      * it has not yet been created, it is <code>null</code>.
53      */

54     private ParameterizedCommand parameterizedCommand;
55
56     /**
57      * Constructs a new <code>CommandWrapper</code>
58      *
59      * @param command
60      * The command to be wrapped; must not be <code>null</code>.
61      * @param bindingManager
62      * The binding manager to support this wrapper; must not be
63      * <code>null</code>.
64      */

65     CommandWrapper(final Command command, final BindingManager bindingManager) {
66         if (command == null) {
67             throw new NullPointerException JavaDoc(
68                     "The wrapped command cannot be <code>null</code>."); //$NON-NLS-1$
69
}
70
71         if (bindingManager == null) {
72             throw new NullPointerException JavaDoc(
73                     "A binding manager is required to wrap a command"); //$NON-NLS-1$
74
}
75
76         this.command = command;
77         this.bindingManager = bindingManager;
78     }
79
80     /*
81      * (non-Javadoc)
82      *
83      * @see org.eclipse.ui.commands.ICommand#addCommandListener(org.eclipse.ui.commands.ICommandListener)
84      */

85
86     public final void addCommandListener(final ICommandListener commandListener) {
87         command.addCommandListener(new CommandListenerWrapper(commandListener,
88                 bindingManager));
89     }
90
91     /*
92      * (non-Javadoc)
93      *
94      * @see org.eclipse.ui.commands.ICommand#execute(java.util.Map)
95      */

96     public final Object JavaDoc execute(Map JavaDoc parameterValuesByName)
97             throws ExecutionException, NotHandledException {
98         try {
99             return command.execute(new ExecutionEvent(
100                     (parameterValuesByName == null) ? Collections.EMPTY_MAP
101                             : parameterValuesByName, null, null));
102         } catch (final org.eclipse.core.commands.ExecutionException e) {
103             throw new ExecutionException(e);
104         } catch (final org.eclipse.core.commands.NotHandledException e) {
105             throw new NotHandledException(e);
106         }
107     }
108
109     /*
110      * (non-Javadoc)
111      *
112      * @see org.eclipse.ui.commands.ICommand#getAttributeValuesByName()
113      */

114     public final Map JavaDoc getAttributeValuesByName() {
115         final Map JavaDoc attributeValues = new HashMap JavaDoc();
116         // avoid using Boolean.valueOf to allow compilation against JCL Foundation (bug 80053)
117
attributeValues.put(ILegacyAttributeNames.ENABLED, command.isEnabled() ? Boolean.TRUE : Boolean.FALSE);
118         attributeValues.put(ILegacyAttributeNames.HANDLED, command.isHandled() ? Boolean.TRUE : Boolean.FALSE);
119         return attributeValues;
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.eclipse.ui.commands.ICommand#getCategoryId()
126      */

127     public final String JavaDoc getCategoryId() throws NotDefinedException {
128         try {
129             return command.getCategory().getId();
130         } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
131             throw new NotDefinedException(e);
132         }
133     }
134
135     /*
136      * (non-Javadoc)
137      *
138      * @see org.eclipse.ui.commands.ICommand#getDescription()
139      */

140     public final String JavaDoc getDescription() throws NotDefinedException {
141         try {
142             return command.getDescription();
143         } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
144             throw new NotDefinedException(e);
145         }
146     }
147
148     /*
149      * (non-Javadoc)
150      *
151      * @see org.eclipse.ui.commands.ICommand#getId()
152      */

153     public final String JavaDoc getId() {
154         return command.getId();
155     }
156
157     /*
158      * (non-Javadoc)
159      *
160      * @see org.eclipse.ui.commands.ICommand#getKeySequenceBindings()
161      */

162     public final List JavaDoc getKeySequenceBindings() {
163         final List JavaDoc legacyBindings = new ArrayList JavaDoc();
164         if (parameterizedCommand == null) {
165             parameterizedCommand = new ParameterizedCommand(command, null);
166         }
167         final TriggerSequence[] activeBindings = bindingManager
168                 .getActiveBindingsFor(parameterizedCommand);
169         final int activeBindingsCount = activeBindings.length;
170         for (int i = 0; i < activeBindingsCount; i++) {
171             final TriggerSequence triggerSequence = activeBindings[i];
172             if (triggerSequence instanceof org.eclipse.jface.bindings.keys.KeySequence) {
173                 legacyBindings
174                         .add(new KeySequenceBinding(
175                                 KeySequence
176                                         .getInstance((org.eclipse.jface.bindings.keys.KeySequence) triggerSequence),
177                                 0));
178             }
179         }
180
181         return legacyBindings;
182     }
183
184     /*
185      * (non-Javadoc)
186      *
187      * @see org.eclipse.ui.commands.ICommand#getName()
188      */

189     public final String JavaDoc getName() throws NotDefinedException {
190         try {
191             return command.getName();
192         } catch (final org.eclipse.core.commands.common.NotDefinedException e) {
193             throw new NotDefinedException(e);
194         }
195     }
196
197     /*
198      * (non-Javadoc)
199      *
200      * @see org.eclipse.ui.commands.ICommand#isDefined()
201      */

202     public final boolean isDefined() {
203         return command.isDefined();
204     }
205
206     /*
207      * (non-Javadoc)
208      *
209      * @see org.eclipse.ui.commands.ICommand#isHandled()
210      */

211     public final boolean isHandled() {
212         return command.isHandled();
213     }
214
215     /*
216      * (non-Javadoc)
217      *
218      * @see org.eclipse.ui.commands.ICommand#removeCommandListener(org.eclipse.ui.commands.ICommandListener)
219      */

220     public final void removeCommandListener(
221             final ICommandListener commandListener) {
222         command.removeCommandListener(new CommandListenerWrapper(
223                 commandListener, bindingManager));
224     }
225
226     /*
227      * (non-Javadoc)
228      *
229      * @see java.lang.Comparable#compareTo(java.lang.Object)
230      */

231     public final int compareTo(final Object JavaDoc o) {
232         return command.compareTo(o);
233     }
234
235 }
236
Popular Tags