1 11 12 package org.eclipse.ui.keys; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.Collections ; 17 import java.util.List ; 18 import java.util.StringTokenizer ; 19 20 import org.eclipse.ui.internal.util.Util; 21 22 49 public final class KeySequence implements Comparable { 50 51 56 public final static String KEY_STROKE_DELIMITER = "\u0020"; 58 61 private final static KeySequence EMPTY_KEY_SEQUENCE = new KeySequence( 62 Collections.EMPTY_LIST); 63 64 67 private final static int HASH_FACTOR = 89; 68 69 72 private final static int HASH_INITIAL = KeySequence.class.getName() 73 .hashCode(); 74 75 79 public final static String KEY_STROKE_DELIMITERS = KEY_STROKE_DELIMITER 80 + "\b\r\u007F\u001B\f\n\0\t\u000B"; 82 88 public static KeySequence getInstance() { 89 return EMPTY_KEY_SEQUENCE; 90 } 91 92 104 public static KeySequence getInstance(KeySequence keySequence, 105 KeyStroke keyStroke) { 106 if (keySequence == null || keyStroke == null) { 107 throw new NullPointerException (); 108 } 109 110 List keyStrokes = new ArrayList (keySequence.getKeyStrokes()); 111 keyStrokes.add(keyStroke); 112 return new KeySequence(keyStrokes); 113 } 114 115 123 public static KeySequence getInstance(KeyStroke keyStroke) { 124 return new KeySequence(Collections.singletonList(keyStroke)); 125 } 126 127 137 public static KeySequence getInstance(KeyStroke[] keyStrokes) { 138 return new KeySequence(Arrays.asList(keyStrokes)); 139 } 140 141 151 public static KeySequence getInstance(List keyStrokes) { 152 return new KeySequence(keyStrokes); 153 } 154 155 164 public static final KeySequence getInstance( 165 final org.eclipse.jface.bindings.keys.KeySequence newKeySequence) { 166 final org.eclipse.jface.bindings.keys.KeyStroke[] newKeyStrokes = newKeySequence 167 .getKeyStrokes(); 168 final int newKeyStrokesCount = newKeyStrokes.length; 169 final List legacyKeyStrokes = new ArrayList (newKeyStrokesCount); 170 171 for (int i = 0; i < newKeyStrokesCount; i++) { 172 final org.eclipse.jface.bindings.keys.KeyStroke newKeyStroke = newKeyStrokes[i]; 173 legacyKeyStrokes.add(SWTKeySupport 174 .convertAcceleratorToKeyStroke(newKeyStroke 175 .getModifierKeys() 176 | newKeyStroke.getNaturalKey())); 177 } 178 179 return new KeySequence(legacyKeyStrokes); 180 } 181 182 193 public static KeySequence getInstance(String string) throws ParseException { 194 if (string == null) { 195 throw new NullPointerException (); 196 } 197 198 List keyStrokes = new ArrayList (); 199 StringTokenizer stringTokenizer = new StringTokenizer (string, 200 KEY_STROKE_DELIMITERS); 201 202 while (stringTokenizer.hasMoreTokens()) { 203 keyStrokes.add(KeyStroke.getInstance(stringTokenizer.nextToken())); 204 } 205 206 try { 207 return new KeySequence(keyStrokes); 208 } catch (Throwable t) { 209 throw new ParseException( 210 "Could not construct key sequence with these key strokes: " + keyStrokes); 212 } 213 } 214 215 221 private transient int hashCode; 222 223 227 private transient boolean hashCodeComputed; 228 229 232 private List keyStrokes; 233 234 243 private KeySequence(List keyStrokes) { 244 this.keyStrokes = Util.safeCopy(keyStrokes, KeyStroke.class); 245 246 for (int i = 0; i < this.keyStrokes.size() - 1; i++) { 247 KeyStroke keyStroke = (KeyStroke) this.keyStrokes.get(i); 248 249 if (!keyStroke.isComplete()) { 250 throw new IllegalArgumentException (); 251 } 252 } 253 } 254 255 258 public int compareTo(Object object) { 259 KeySequence castedObject = (KeySequence) object; 260 int compareTo = Util.compare(keyStrokes, castedObject.keyStrokes); 261 return compareTo; 262 } 263 264 276 public boolean endsWith(KeySequence keySequence, boolean equals) { 277 if (keySequence == null) { 278 throw new NullPointerException (); 279 } 280 281 return Util.endsWith(keyStrokes, keySequence.keyStrokes, equals); 282 } 283 284 287 public boolean equals(Object object) { 288 if (!(object instanceof KeySequence)) { 289 return false; 290 } 291 292 return keyStrokes.equals(((KeySequence) object).keyStrokes); 293 } 294 295 301 public String format() { 302 return KeyFormatterFactory.getDefault().format(this); 303 } 304 305 312 public List getKeyStrokes() { 313 return keyStrokes; 314 } 315 316 319 public int hashCode() { 320 if (!hashCodeComputed) { 321 hashCode = HASH_INITIAL; 322 hashCode = hashCode * HASH_FACTOR + keyStrokes.hashCode(); 323 hashCodeComputed = true; 324 } 325 326 return hashCode; 327 } 328 329 335 public boolean isComplete() { 336 return keyStrokes.isEmpty() 337 || ((KeyStroke) keyStrokes.get(keyStrokes.size() - 1)) 338 .isComplete(); 339 } 340 341 347 public boolean isEmpty() { 348 return keyStrokes.isEmpty(); 349 } 350 351 363 public boolean startsWith(KeySequence keySequence, boolean equals) { 364 if (keySequence == null) { 365 throw new NullPointerException (); 366 } 367 368 return Util.startsWith(keyStrokes, keySequence.keyStrokes, equals); 369 } 370 371 378 public String toString() { 379 return KeyFormatterFactory.getFormalKeyFormatter().format(this); 380 } 381 } 382 | Popular Tags |