KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-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.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 import org.eclipse.ui.commands.CommandEvent;
19 import org.eclipse.ui.commands.ExecutionException;
20 import org.eclipse.ui.commands.ICommand;
21 import org.eclipse.ui.commands.ICommandListener;
22 import org.eclipse.ui.commands.IHandler;
23 import org.eclipse.ui.commands.IKeySequenceBinding;
24 import org.eclipse.ui.commands.NotDefinedException;
25 import org.eclipse.ui.commands.NotHandledException;
26 import org.eclipse.ui.internal.util.Util;
27
28 final class Command implements ICommand {
29
30     private final static int HASH_FACTOR = 89;
31
32     private final static int HASH_INITIAL = Command.class.getName().hashCode();
33
34     private String JavaDoc categoryId;
35
36     private List JavaDoc commandListeners;
37
38     private Set JavaDoc commandsWithListeners;
39
40     private boolean defined;
41
42     private String JavaDoc description;
43
44     private IHandler handler;
45
46     private transient int hashCode;
47
48     private transient boolean hashCodeComputed;
49
50     private String JavaDoc id;
51
52     private List JavaDoc keySequenceBindings;
53
54     private IKeySequenceBinding[] keySequenceBindingsAsArray;
55
56     private String JavaDoc name;
57
58     private transient String JavaDoc string;
59
60     Command(Set JavaDoc commandsWithListeners, String JavaDoc id) {
61         if (commandsWithListeners == null || id == null)
62                 throw new NullPointerException JavaDoc();
63         this.commandsWithListeners = commandsWithListeners;
64         this.id = id;
65     }
66
67     public void addCommandListener(ICommandListener commandListener) {
68         if (commandListener == null) throw new NullPointerException JavaDoc();
69         if (commandListeners == null) commandListeners = new ArrayList JavaDoc();
70         if (!commandListeners.contains(commandListener))
71                 commandListeners.add(commandListener);
72         commandsWithListeners.add(this);
73     }
74
75     public int compareTo(Object JavaDoc object) {
76         Command castedObject = (Command) object;
77         int compareTo = Util.compare(categoryId, castedObject.categoryId);
78         if (compareTo == 0) {
79             compareTo = Util.compare(defined, castedObject.defined);
80             if (compareTo == 0) {
81                 compareTo = Util.compare(description, castedObject.description);
82                 if (compareTo == 0) {
83                     compareTo = Util.compare(handler, castedObject.handler);
84                     if (compareTo == 0) {
85                         compareTo = Util.compare(id, castedObject.id);
86                         if (compareTo == 0) {
87                             compareTo = Util.compare(name, castedObject.name);
88                         }
89                     }
90                 }
91             }
92         }
93         return compareTo;
94     }
95
96     public boolean equals(Object JavaDoc object) {
97         if (!(object instanceof Command)) return false;
98         Command castedObject = (Command) object;
99         boolean equals = true;
100         equals &= Util.equals(categoryId, castedObject.categoryId);
101         equals &= Util.equals(defined, castedObject.defined);
102         equals &= Util.equals(description, castedObject.description);
103         equals &= Util.equals(handler, castedObject.handler);
104         equals &= Util.equals(id, castedObject.id);
105         equals &= Util.equals(keySequenceBindings,
106                 castedObject.keySequenceBindings);
107         equals &= Util.equals(name, castedObject.name);
108         return equals;
109     }
110
111     public Object JavaDoc execute(Map JavaDoc parameterValuesByName) throws ExecutionException,
112             NotHandledException {
113         IHandler handler = this.handler;
114         
115         // Debugging output
116
if (MutableCommandManager.DEBUG_COMMAND_EXECUTION) {
117             System.out.print("KEYS >>> executing "); //$NON-NLS-1$
118
if (handler == null) {
119                 System.out.print("no handler"); //$NON-NLS-1$
120
} else {
121                 System.out.print("'"); //$NON-NLS-1$
122
System.out.print(handler.getClass().getName());
123                 System.out.print("' ("); //$NON-NLS-1$
124
System.out.print(handler.hashCode());
125                 System.out.print(")"); //$NON-NLS-1$
126
}
127             System.out.println();
128         }
129         
130         // Perform the execution, if there is a handler.
131
if (handler != null)
132             return handler.execute(parameterValuesByName);
133         else {
134             throw new NotHandledException("There is no handler to execute."); //$NON-NLS-1$
135
}
136     }
137
138     void fireCommandChanged(CommandEvent commandEvent) {
139         if (commandEvent == null) throw new NullPointerException JavaDoc();
140         if (commandListeners != null)
141                 for (int i = 0; i < commandListeners.size(); i++)
142                     ((ICommandListener) commandListeners.get(i))
143                             .commandChanged(commandEvent);
144     }
145
146     public Map JavaDoc getAttributeValuesByName() throws NotHandledException {
147         IHandler handler = this.handler;
148         if (handler != null)
149             return handler.getAttributeValuesByName();
150         else
151             throw new NotHandledException(
152                     "There is no handler from which to retrieve attributes."); //$NON-NLS-1$
153
}
154
155     public String JavaDoc getCategoryId() throws NotDefinedException {
156         if (!defined)
157                 throw new NotDefinedException(
158                         "Cannot get category identifier from an undefined command."); //$NON-NLS-1$
159
return categoryId;
160     }
161
162     public String JavaDoc getDescription() throws NotDefinedException {
163         if (!defined)
164                 throw new NotDefinedException(
165                         "Cannot get a description from an undefined command."); //$NON-NLS-1$
166
return description;
167     }
168
169     public String JavaDoc getId() {
170         return id;
171     }
172
173     public List JavaDoc getKeySequenceBindings() {
174         return keySequenceBindings;
175     }
176
177     public String JavaDoc getName() throws NotDefinedException {
178         if (!defined)
179                 throw new NotDefinedException(
180                         "Cannot get the name from an undefined command."); //$NON-NLS-1$
181
return name;
182     }
183
184     public int hashCode() {
185         if (!hashCodeComputed) {
186             hashCode = HASH_INITIAL;
187             hashCode = hashCode * HASH_FACTOR + Util.hashCode(categoryId);
188             hashCode = hashCode * HASH_FACTOR + Util.hashCode(defined);
189             hashCode = hashCode * HASH_FACTOR + Util.hashCode(description);
190             hashCode = hashCode * HASH_FACTOR + Util.hashCode(handler);
191             hashCode = hashCode * HASH_FACTOR + Util.hashCode(id);
192             hashCode = hashCode * HASH_FACTOR
193                     + Util.hashCode(keySequenceBindings);
194             hashCode = hashCode * HASH_FACTOR + Util.hashCode(name);
195             hashCodeComputed = true;
196         }
197         return hashCode;
198     }
199
200     public boolean isDefined() {
201         return defined;
202     }
203
204     public boolean isHandled() {
205         if (handler == null) return false;
206         Map JavaDoc attributeValuesByName = handler.getAttributeValuesByName();
207         if (attributeValuesByName.containsKey("handled") //$NON-NLS-1$
208
&& !Boolean.TRUE.equals(attributeValuesByName.get("handled"))) //$NON-NLS-1$
209
return false;
210         else
211             return true;
212     }
213
214     public void removeCommandListener(ICommandListener commandListener) {
215         if (commandListener == null) throw new NullPointerException JavaDoc();
216         if (commandListeners != null) {
217             commandListeners.remove(commandListener);
218             if (commandListeners.isEmpty()) commandsWithListeners.remove(this);
219         }
220     }
221
222     boolean setCategoryId(String JavaDoc categoryId) {
223         if (!Util.equals(categoryId, this.categoryId)) {
224             this.categoryId = categoryId;
225             hashCodeComputed = false;
226             hashCode = 0;
227             string = null;
228             return true;
229         }
230         return false;
231     }
232
233     boolean setDefined(boolean defined) {
234         if (defined != this.defined) {
235             this.defined = defined;
236             hashCodeComputed = false;
237             hashCode = 0;
238             string = null;
239             return true;
240         }
241         return false;
242     }
243
244     boolean setDescription(String JavaDoc description) {
245         if (!Util.equals(description, this.description)) {
246             this.description = description;
247             hashCodeComputed = false;
248             hashCode = 0;
249             string = null;
250             return true;
251         }
252         return false;
253     }
254
255     boolean setHandler(IHandler handler) {
256         if (handler != this.handler) {
257             this.handler = handler;
258             hashCodeComputed = false;
259             hashCode = 0;
260             string = null;
261             
262             // Debugging output
263
if ((MutableCommandManager.DEBUG_HANDLERS)
264                     && ((MutableCommandManager.DEBUG_HANDLERS_COMMAND_ID == null) || (MutableCommandManager.DEBUG_HANDLERS_COMMAND_ID
265                             .equals(id)))) {
266                 System.out.print("HANDLERS >>> Command('" + id //$NON-NLS-1$
267
+ "' has changed to "); //$NON-NLS-1$
268
if (handler == null) {
269                     System.out.println("no handler"); //$NON-NLS-1$
270
} else {
271                     System.out.print("'"); //$NON-NLS-1$
272
System.out.print(handler);
273                     System.out.println("' as its handler"); //$NON-NLS-1$
274
}
275             }
276             
277             return true;
278         }
279         return false;
280     }
281
282     boolean setKeySequenceBindings(List JavaDoc keySequenceBindings) {
283         keySequenceBindings = Util.safeCopy(keySequenceBindings,
284                 IKeySequenceBinding.class);
285         if (!Util.equals(keySequenceBindings, this.keySequenceBindings)) {
286             this.keySequenceBindings = keySequenceBindings;
287             this.keySequenceBindingsAsArray = (IKeySequenceBinding[]) this.keySequenceBindings
288                     .toArray(new IKeySequenceBinding[this.keySequenceBindings.size()]);
289             hashCodeComputed = false;
290             hashCode = 0;
291             string = null;
292             return true;
293         }
294         return false;
295     }
296
297     boolean setName(String JavaDoc name) {
298         if (!Util.equals(name, this.name)) {
299             this.name = name;
300             hashCodeComputed = false;
301             hashCode = 0;
302             string = null;
303             return true;
304         }
305         return false;
306     }
307
308     public String JavaDoc toString() {
309         if (string == null) {
310             final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
311             stringBuffer.append('[');
312             stringBuffer.append(categoryId);
313             stringBuffer.append(',');
314             stringBuffer.append(defined);
315             stringBuffer.append(',');
316             stringBuffer.append(description);
317             stringBuffer.append(',');
318             stringBuffer.append(handler);
319             stringBuffer.append(',');
320             stringBuffer.append(id);
321             stringBuffer.append(',');
322             stringBuffer.append(keySequenceBindings);
323             stringBuffer.append(',');
324             stringBuffer.append(name);
325             stringBuffer.append(']');
326             string = stringBuffer.toString();
327         }
328         return string;
329     }
330 }
331
Popular Tags