1 11 12 package org.eclipse.jface.bindings.keys; 13 14 import java.util.ArrayList ; 15 import java.util.List ; 16 import java.util.StringTokenizer ; 17 18 import org.eclipse.jface.bindings.TriggerSequence; 19 import org.eclipse.jface.bindings.keys.formatting.KeyFormatterFactory; 20 import org.eclipse.jface.util.Util; 21 22 48 public final class KeySequence extends TriggerSequence implements Comparable { 49 50 53 private final static KeySequence EMPTY_KEY_SEQUENCE = new KeySequence( 54 new KeyStroke[0]); 55 56 61 public final static String KEY_STROKE_DELIMITER = "\u0020"; 63 67 public final static String KEY_STROKE_DELIMITERS = KEY_STROKE_DELIMITER 68 + "\b\r\u007F\u001B\f\n\0\t\u000B"; 70 76 public static final KeySequence getInstance() { 77 return EMPTY_KEY_SEQUENCE; 78 } 79 80 92 public static final KeySequence getInstance(final KeySequence keySequence, 93 final KeyStroke keyStroke) { 94 if (keySequence == null || keyStroke == null) { 95 throw new NullPointerException (); 96 } 97 98 final KeyStroke[] oldKeyStrokes = keySequence.getKeyStrokes(); 99 final int oldKeyStrokeLength = oldKeyStrokes.length; 100 final KeyStroke[] newKeyStrokes = new KeyStroke[oldKeyStrokeLength + 1]; 101 System 102 .arraycopy(oldKeyStrokes, 0, newKeyStrokes, 0, 103 oldKeyStrokeLength); 104 newKeyStrokes[oldKeyStrokeLength] = keyStroke; 105 return new KeySequence(newKeyStrokes); 106 } 107 108 116 public static final KeySequence getInstance(final KeyStroke keyStroke) { 117 return new KeySequence(new KeyStroke[] { keyStroke }); 118 } 119 120 130 public static final KeySequence getInstance(final KeyStroke[] keyStrokes) { 131 return new KeySequence(keyStrokes); 132 } 133 134 144 public static final KeySequence getInstance(final List keyStrokes) { 145 return new KeySequence((KeyStroke[]) keyStrokes 146 .toArray(new KeyStroke[keyStrokes.size()])); 147 } 148 149 160 public static final KeySequence getInstance(final String string) 161 throws ParseException { 162 if (string == null) { 163 throw new NullPointerException (); 164 } 165 166 final List keyStrokes = new ArrayList (); 167 final StringTokenizer stringTokenizer = new StringTokenizer (string, 168 KEY_STROKE_DELIMITERS); 169 170 try { 171 while (stringTokenizer.hasMoreTokens()) { 172 keyStrokes.add(KeyStroke.getInstance(stringTokenizer 173 .nextToken())); 174 } 175 176 final KeyStroke[] keyStrokeArray = (KeyStroke[]) keyStrokes 177 .toArray(new KeyStroke[keyStrokes.size()]); 178 return new KeySequence(keyStrokeArray); 179 } catch (final IllegalArgumentException e) { 180 throw new ParseException( 181 "Could not construct key sequence with these key strokes: " + keyStrokes); 183 } catch (final NullPointerException e) { 184 throw new ParseException( 185 "Could not construct key sequence with these key strokes: " + keyStrokes); 187 } 188 } 189 190 199 protected KeySequence(final KeyStroke[] keyStrokes) { 200 super(keyStrokes); 201 202 for (int i = 0; i < triggers.length - 1; i++) { 203 KeyStroke keyStroke = (KeyStroke) triggers[i]; 204 205 if (!keyStroke.isComplete()) { 206 throw new IllegalArgumentException (); 207 } 208 } 209 } 210 211 216 public final int compareTo(final Object object) { 217 final KeySequence castedObject = (KeySequence) object; 218 return Util.compare(triggers, castedObject.triggers); 219 } 220 221 227 public final String format() { 228 return KeyFormatterFactory.getDefault().format(this); 229 } 230 231 239 public final KeyStroke[] getKeyStrokes() { 240 final int triggerLength = triggers.length; 241 final KeyStroke[] keyStrokes = new KeyStroke[triggerLength]; 242 System.arraycopy(triggers, 0, keyStrokes, 0, triggerLength); 243 return keyStrokes; 244 } 245 246 251 public final TriggerSequence[] getPrefixes() { 252 final int numberOfPrefixes = triggers.length; 253 final TriggerSequence[] prefixes = new TriggerSequence[numberOfPrefixes]; 254 prefixes[0] = KeySequence.getInstance(); 255 for (int i = 0; i < numberOfPrefixes - 1; i++) { 256 final KeyStroke[] prefixKeyStrokes = new KeyStroke[i + 1]; 257 System.arraycopy(triggers, 0, prefixKeyStrokes, 0, i + 1); 258 prefixes[i + 1] = KeySequence.getInstance(prefixKeyStrokes); 259 } 260 261 return prefixes; 262 } 263 264 270 public final boolean isComplete() { 271 final int triggersLength = triggers.length; 272 for (int i = 0; i < triggersLength; i++) { 273 if (!((KeyStroke) triggers[i]).isComplete()) { 274 return false; 275 } 276 } 277 278 return true; 279 } 280 281 288 public final String toString() { 289 return KeyFormatterFactory.getFormalKeyFormatter().format(this); 290 } 291 } 292 | Popular Tags |