1 11 package org.eclipse.jface.bindings; 12 13 import java.io.BufferedWriter ; 14 import java.io.IOException ; 15 import java.io.StringWriter ; 16 17 import org.eclipse.core.commands.ParameterizedCommand; 18 import org.eclipse.jface.util.Util; 19 20 68 public abstract class Binding { 69 70 74 private static final int HASH_CODE_NOT_COMPUTED = -1; 75 76 79 private final static int HASH_FACTOR = 89; 80 81 84 private final static int HASH_INITIAL = Binding.class.getName().hashCode(); 85 86 91 public static final int SYSTEM = 0; 92 93 98 public static final int USER = 1; 99 100 105 private final ParameterizedCommand command; 106 107 112 private final String contextId; 113 114 118 private transient int hashCode = HASH_CODE_NOT_COMPUTED; 119 120 126 private final String locale; 127 128 134 private final String platform; 135 136 140 private final String schemeId; 141 142 147 protected transient String string = null; 148 149 154 private final int type; 155 156 183 protected Binding(final ParameterizedCommand command, 184 final String schemeId, final String contextId, final String locale, 185 final String platform, final String windowManager, final int type) { 186 if (schemeId == null) { 187 throw new NullPointerException ("The scheme cannot be null"); } 189 190 if (contextId == null) { 191 throw new NullPointerException ("The context cannot be null"); } 193 194 if ((type != SYSTEM) && (type != USER)) { 195 throw new IllegalArgumentException ( 196 "The type must be SYSTEM or USER"); } 198 199 this.command = command; 200 this.schemeId = schemeId.intern(); 201 this.contextId = contextId.intern(); 202 this.locale = (locale == null) ? null : locale.intern(); 203 this.platform = (platform == null) ? null : platform.intern(); 204 this.type = type; 205 } 206 207 217 final boolean deletes(final Binding binding) { 218 boolean deletes = true; 219 deletes &= Util.equals(getContextId(), binding.getContextId()); 220 deletes &= Util.equals(getTriggerSequence(), binding 221 .getTriggerSequence()); 222 if (getLocale() != null) { 223 deletes &= !Util.equals(getLocale(), binding.getLocale()); 224 } 225 if (getPlatform() != null) { 226 deletes &= !Util.equals(getPlatform(), binding.getPlatform()); 227 } 228 deletes &= (binding.getType() == SYSTEM); 229 deletes &= Util.equals(getParameterizedCommand(), null); 230 231 return deletes; 232 } 233 234 243 public final boolean equals(final Object object) { 244 if (this == object) { 245 return true; 246 247 } 248 if (!(object instanceof Binding)) { 249 return false; 250 } 251 252 final Binding binding = (Binding) object; 253 if (!Util.equals(getParameterizedCommand(), binding 254 .getParameterizedCommand())) { 255 return false; 256 } 257 if (!Util.equals(getContextId(), binding.getContextId())) { 258 return false; 259 } 260 if (!Util.equals(getTriggerSequence(), binding.getTriggerSequence())) { 261 return false; 262 } 263 if (!Util.equals(getLocale(), binding.getLocale())) { 264 return false; 265 } 266 if (!Util.equals(getPlatform(), binding.getPlatform())) { 267 return false; 268 } 269 if (!Util.equals(getSchemeId(), binding.getSchemeId())) { 270 return false; 271 } 272 return (getType() != binding.getType()); 273 } 274 275 282 public final ParameterizedCommand getParameterizedCommand() { 283 return command; 284 } 285 286 291 public final String getContextId() { 292 return contextId; 293 } 294 295 303 public final String getLocale() { 304 return locale; 305 } 306 307 314 public final String getPlatform() { 315 return platform; 316 } 317 318 323 public final String getSchemeId() { 324 return schemeId; 325 } 326 327 335 public abstract TriggerSequence getTriggerSequence(); 336 337 344 public final int getType() { 345 return type; 346 } 347 348 354 public final int hashCode() { 355 if (hashCode == HASH_CODE_NOT_COMPUTED) { 356 hashCode = HASH_INITIAL; 357 hashCode = hashCode * HASH_FACTOR 358 + Util.hashCode(getParameterizedCommand()); 359 hashCode = hashCode * HASH_FACTOR + Util.hashCode(getContextId()); 360 hashCode = hashCode * HASH_FACTOR 361 + Util.hashCode(getTriggerSequence()); 362 hashCode = hashCode * HASH_FACTOR + Util.hashCode(getLocale()); 363 hashCode = hashCode * HASH_FACTOR + Util.hashCode(getPlatform()); 364 hashCode = hashCode * HASH_FACTOR + Util.hashCode(getSchemeId()); 365 hashCode = hashCode * HASH_FACTOR + Util.hashCode(getType()); 366 if (hashCode == HASH_CODE_NOT_COMPUTED) { 367 hashCode++; 368 } 369 } 370 371 return hashCode; 372 } 373 374 381 public String toString() { 382 if (string == null) { 383 384 final StringWriter sw = new StringWriter (); 385 final BufferedWriter stringBuffer = new BufferedWriter (sw); 386 try { 387 stringBuffer.write("Binding("); stringBuffer.write(getTriggerSequence().toString()); 389 stringBuffer.write(','); 390 stringBuffer.newLine(); 391 stringBuffer.write('\t'); 392 stringBuffer.write(command==null?"":command.toString()); stringBuffer.write(','); 394 stringBuffer.newLine(); 395 stringBuffer.write('\t'); 396 stringBuffer.write(schemeId); 397 stringBuffer.write(','); 398 stringBuffer.newLine(); 399 stringBuffer.write('\t'); 400 stringBuffer.write(contextId); 401 stringBuffer.write(','); 402 stringBuffer.write(locale==null?"":locale); stringBuffer.write(','); 404 stringBuffer.write(platform==null?"":platform); stringBuffer.write(','); 406 stringBuffer.write((type == SYSTEM) ? "system" : "user"); stringBuffer.write(')'); 408 stringBuffer.flush(); 409 } catch (IOException e) { 410 } 412 string = sw.toString(); 413 } 414 415 return string; 416 } 417 } 418 | Popular Tags |