1 11 12 package org.eclipse.ui.keys; 13 14 import java.util.Arrays ; 15 import java.util.Collections ; 16 import java.util.Set ; 17 import java.util.SortedSet ; 18 import java.util.StringTokenizer ; 19 import java.util.TreeSet ; 20 21 import org.eclipse.ui.internal.util.Util; 22 23 53 public final class KeyStroke implements Comparable { 54 55 60 public final static String KEY_DELIMITER = "\u002B"; 62 65 private final static int HASH_FACTOR = 89; 66 67 70 private final static int HASH_INITIAL = KeyStroke.class.getName() 71 .hashCode(); 72 73 77 public final static String KEY_DELIMITERS = KEY_DELIMITER; 78 79 89 public static KeyStroke getInstance(ModifierKey modifierKey, 90 NaturalKey naturalKey) { 91 if (modifierKey == null) { 92 throw new NullPointerException (); 93 } 94 95 return new KeyStroke( 96 new TreeSet (Collections.singletonList(modifierKey)), naturalKey); 97 } 98 99 111 public static KeyStroke getInstance(ModifierKey[] modifierKeys, 112 NaturalKey naturalKey) { 113 Util.assertInstance(modifierKeys, ModifierKey.class); 114 return new KeyStroke(new TreeSet (Arrays.asList(modifierKeys)), 115 naturalKey); 116 } 117 118 126 public static KeyStroke getInstance(NaturalKey naturalKey) { 127 return new KeyStroke(Util.EMPTY_SORTED_SET, naturalKey); 128 } 129 130 142 public static KeyStroke getInstance(SortedSet modifierKeys, 143 NaturalKey naturalKey) { 144 return new KeyStroke(modifierKeys, naturalKey); 145 } 146 147 158 public static KeyStroke getInstance(String string) throws ParseException { 159 if (string == null) { 160 throw new NullPointerException (); 161 } 162 163 SortedSet modifierKeys = new TreeSet (); 164 NaturalKey naturalKey = null; 165 StringTokenizer stringTokenizer = new StringTokenizer (string, 166 KEY_DELIMITERS, true); 167 int i = 0; 168 169 while (stringTokenizer.hasMoreTokens()) { 170 String token = stringTokenizer.nextToken(); 171 172 if (i % 2 == 0) { 173 if (stringTokenizer.hasMoreTokens()) { 174 token = token.toUpperCase(); 175 ModifierKey modifierKey = (ModifierKey) ModifierKey.modifierKeysByName 176 .get(token); 177 178 if (modifierKey == null || !modifierKeys.add(modifierKey)) { 179 throw new ParseException( 180 "Cannot create key stroke with duplicate or non-existent modifier key: " + token); 182 } 183 } else if (token.length() == 1) { 184 naturalKey = CharacterKey.getInstance(token.charAt(0)); 185 break; 186 } else { 187 token = token.toUpperCase(); 188 naturalKey = (NaturalKey) CharacterKey.characterKeysByName 189 .get(token); 190 191 if (naturalKey == null) { 192 naturalKey = (NaturalKey) SpecialKey.specialKeysByName 193 .get(token); 194 } 195 196 if (naturalKey == null) { 197 throw new ParseException( 198 "Cannot create key stroke with invalid natural key: " + token); 200 } 201 } 202 } 203 204 i++; 205 } 206 207 try { 208 return new KeyStroke(modifierKeys, naturalKey); 209 } catch (Throwable t) { 210 throw new ParseException("Cannot create key stroke with " + modifierKeys + " and " + naturalKey); } 213 } 214 215 221 private transient int hashCode; 222 223 227 private transient boolean hashCodeComputed; 228 229 232 private SortedSet modifierKeys; 233 234 238 private transient ModifierKey[] modifierKeysAsArray; 239 240 243 private NaturalKey naturalKey; 244 245 256 private KeyStroke(SortedSet modifierKeys, NaturalKey naturalKey) { 257 this.modifierKeys = Util.safeCopy(modifierKeys, ModifierKey.class); 258 this.naturalKey = naturalKey; 259 this.modifierKeysAsArray = (ModifierKey[]) this.modifierKeys 260 .toArray(new ModifierKey[this.modifierKeys.size()]); 261 } 262 263 266 public int compareTo(Object object) { 267 KeyStroke castedObject = (KeyStroke) object; 268 int compareTo = Util.compare(modifierKeysAsArray, 269 castedObject.modifierKeysAsArray); 270 271 if (compareTo == 0) { 272 compareTo = Util.compare(naturalKey, castedObject.naturalKey); 273 } 274 275 return compareTo; 276 } 277 278 281 public boolean equals(Object object) { 282 if (!(object instanceof KeyStroke)) { 283 return false; 284 } 285 286 KeyStroke castedObject = (KeyStroke) object; 287 288 if (!modifierKeys.equals(castedObject.modifierKeys)) { 289 return false; 290 } 291 return Util.equals(naturalKey, castedObject.naturalKey); 292 } 293 294 300 public String format() { 301 return KeyFormatterFactory.getDefault().format(this); 302 } 303 304 311 public Set getModifierKeys() { 312 return Collections.unmodifiableSet(modifierKeys); 313 } 314 315 320 public NaturalKey getNaturalKey() { 321 return naturalKey; 322 } 323 324 327 public int hashCode() { 328 if (!hashCodeComputed) { 329 hashCode = HASH_INITIAL; 330 hashCode = hashCode * HASH_FACTOR + modifierKeys.hashCode(); 331 hashCode = hashCode * HASH_FACTOR + Util.hashCode(naturalKey); 332 hashCodeComputed = true; 333 } 334 335 return hashCode; 336 } 337 338 344 public boolean isComplete() { 345 return naturalKey != null; 346 } 347 348 355 public String toString() { 356 return KeyFormatterFactory.getFormalKeyFormatter().format(this); 357 } 358 } 359 | Popular Tags |