KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

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

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

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

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

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

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

206     public final boolean isDefined() {
207         return command.isDefined();
208     }
209
210     /*
211      * (non-Javadoc)
212      *
213      * @see org.eclipse.ui.commands.ICommand#isHandled()
214      */

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

224     public final void removeCommandListener(
225             final ICommandListener commandListener) {
226         command.removeCommandListener(new LegacyCommandListenerWrapper(
227                 commandListener, bindingManager));
228     }
229
230     /*
231      * (non-Javadoc)
232      *
233      * @see java.lang.Comparable#compareTo(java.lang.Object)
234      */

235     public final int compareTo(final Object JavaDoc o) {
236         return command.compareTo(o);
237     }
238
239 }
240
Popular Tags