1 11 12 package org.eclipse.jface.bindings.keys; 13 14 import java.util.StringTokenizer ; 15 16 import org.eclipse.jface.bindings.Trigger; 17 import org.eclipse.jface.bindings.keys.formatting.KeyFormatterFactory; 18 import org.eclipse.jface.util.Util; 19 20 47 public final class KeyStroke extends Trigger implements Comparable { 48 49 54 public static final String KEY_DELIMITER = "\u002B"; 56 60 public static final String KEY_DELIMITERS = KEY_DELIMITER; 61 62 65 public static final int NO_KEY = 0; 66 67 78 public static final KeyStroke getInstance(final int naturalKey) { 79 return new KeyStroke(NO_KEY, naturalKey); 80 } 81 82 97 public static final KeyStroke getInstance(final int modifierKeys, 98 final int naturalKey) { 99 return new KeyStroke(modifierKeys, naturalKey); 100 } 101 102 113 public static final KeyStroke getInstance(final String string) 114 throws ParseException { 115 if (string == null) { 116 throw new NullPointerException ("Cannot parse a null string"); } 118 119 final IKeyLookup lookup = KeyLookupFactory.getDefault(); 120 int modifierKeys = NO_KEY; 121 int naturalKey = NO_KEY; 122 final StringTokenizer stringTokenizer = new StringTokenizer (string, 123 KEY_DELIMITERS, true); 124 int i = 0; 125 126 while (stringTokenizer.hasMoreTokens()) { 127 String token = stringTokenizer.nextToken(); 128 129 if (i % 2 == 0) { 130 if (stringTokenizer.hasMoreTokens()) { 131 token = token.toUpperCase(); 132 final int modifierKey = lookup.formalModifierLookup(token); 133 if (modifierKey == NO_KEY) { 134 throw new ParseException( 135 "Cannot create key stroke with duplicate or non-existent modifier key: " + token); 137 } 138 139 modifierKeys |= modifierKey; 140 141 } else if (token.length() == 1) { 142 naturalKey = token.charAt(0); 143 144 } else { 145 token = token.toUpperCase(); 146 naturalKey = lookup.formalKeyLookup(token); 147 } 148 } 149 150 i++; 151 } 152 153 return new KeyStroke(modifierKeys, naturalKey); 154 } 155 156 160 private final int modifierKeys; 161 162 166 private final int naturalKey; 167 168 182 private KeyStroke(final int modifierKeys, final int naturalKey) { 183 this.modifierKeys = modifierKeys; 184 this.naturalKey = naturalKey; 185 } 186 187 192 public final int compareTo(final Object object) { 193 final KeyStroke keyStroke = (KeyStroke) object; 194 int compareTo = Util.compare(modifierKeys, keyStroke.modifierKeys); 195 196 if (compareTo == 0) { 197 compareTo = Util.compare(naturalKey, keyStroke.naturalKey); 198 } 199 200 return compareTo; 201 } 202 203 208 public final boolean equals(final Object object) { 209 if (!(object instanceof KeyStroke)) { 210 return false; 211 } 212 213 final KeyStroke keyStroke = (KeyStroke) object; 214 if (modifierKeys != keyStroke.modifierKeys) { 215 return false; 216 } 217 218 return (naturalKey == keyStroke.naturalKey); 219 } 220 221 227 public final String format() { 228 return KeyFormatterFactory.getDefault().format(this); 229 } 230 231 237 public final int getModifierKeys() { 238 return modifierKeys; 239 } 240 241 248 public final int getNaturalKey() { 249 return naturalKey; 250 } 251 252 257 public final int hashCode() { 258 return modifierKeys << 4 + naturalKey; 259 } 260 261 267 public final boolean isComplete() { 268 return (naturalKey != NO_KEY); 269 } 270 271 278 public final String toString() { 279 return KeyFormatterFactory.getFormalKeyFormatter().format(this); 280 } 281 } 282 | Popular Tags |