1 11 package org.eclipse.ui.internal.commands; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 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 categoryId; 35 36 private List commandListeners; 37 38 private Set commandsWithListeners; 39 40 private boolean defined; 41 42 private String description; 43 44 private IHandler handler; 45 46 private transient int hashCode; 47 48 private transient boolean hashCodeComputed; 49 50 private String id; 51 52 private List keySequenceBindings; 53 54 private IKeySequenceBinding[] keySequenceBindingsAsArray; 55 56 private String name; 57 58 private transient String string; 59 60 Command(Set commandsWithListeners, String id) { 61 if (commandsWithListeners == null || id == null) 62 throw new NullPointerException (); 63 this.commandsWithListeners = commandsWithListeners; 64 this.id = id; 65 } 66 67 public void addCommandListener(ICommandListener commandListener) { 68 if (commandListener == null) throw new NullPointerException (); 69 if (commandListeners == null) commandListeners = new ArrayList (); 70 if (!commandListeners.contains(commandListener)) 71 commandListeners.add(commandListener); 72 commandsWithListeners.add(this); 73 } 74 75 public int compareTo(Object 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 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 execute(Map parameterValuesByName) throws ExecutionException, 112 NotHandledException { 113 IHandler handler = this.handler; 114 115 if (MutableCommandManager.DEBUG_COMMAND_EXECUTION) { 117 System.out.print("KEYS >>> executing "); if (handler == null) { 119 System.out.print("no handler"); } else { 121 System.out.print("'"); System.out.print(handler.getClass().getName()); 123 System.out.print("' ("); System.out.print(handler.hashCode()); 125 System.out.print(")"); } 127 System.out.println(); 128 } 129 130 if (handler != null) 132 return handler.execute(parameterValuesByName); 133 else { 134 throw new NotHandledException("There is no handler to execute."); } 136 } 137 138 void fireCommandChanged(CommandEvent commandEvent) { 139 if (commandEvent == null) throw new NullPointerException (); 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 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."); } 154 155 public String getCategoryId() throws NotDefinedException { 156 if (!defined) 157 throw new NotDefinedException( 158 "Cannot get category identifier from an undefined command."); return categoryId; 160 } 161 162 public String getDescription() throws NotDefinedException { 163 if (!defined) 164 throw new NotDefinedException( 165 "Cannot get a description from an undefined command."); return description; 167 } 168 169 public String getId() { 170 return id; 171 } 172 173 public List getKeySequenceBindings() { 174 return keySequenceBindings; 175 } 176 177 public String getName() throws NotDefinedException { 178 if (!defined) 179 throw new NotDefinedException( 180 "Cannot get the name from an undefined command."); 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 attributeValuesByName = handler.getAttributeValuesByName(); 207 if (attributeValuesByName.containsKey("handled") && !Boolean.TRUE.equals(attributeValuesByName.get("handled"))) return false; 210 else 211 return true; 212 } 213 214 public void removeCommandListener(ICommandListener commandListener) { 215 if (commandListener == null) throw new NullPointerException (); 216 if (commandListeners != null) { 217 commandListeners.remove(commandListener); 218 if (commandListeners.isEmpty()) commandsWithListeners.remove(this); 219 } 220 } 221 222 boolean setCategoryId(String 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 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 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 + "' has changed to "); if (handler == null) { 269 System.out.println("no handler"); } else { 271 System.out.print("'"); System.out.print(handler); 273 System.out.println("' as its handler"); } 275 } 276 277 return true; 278 } 279 return false; 280 } 281 282 boolean setKeySequenceBindings(List 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 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 toString() { 309 if (string == null) { 310 final StringBuffer stringBuffer = new StringBuffer (); 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 |