1 11 package org.eclipse.jface.bindings.keys; 12 13 import java.util.HashMap ; 14 import java.util.Map ; 15 16 import org.eclipse.jface.util.Util; 17 import org.eclipse.swt.SWT; 18 19 31 public final class SWTKeyLookup implements IKeyLookup { 32 33 37 private final Map modifierKeyTable = new HashMap (); 38 39 43 private final Map nameTable = new HashMap (); 44 45 49 private final Map naturalKeyTable = new HashMap (); 50 51 57 SWTKeyLookup() { 58 final Integer alt = new Integer (SWT.ALT); 59 final Integer command = new Integer (SWT.COMMAND); 60 final Integer ctrl = new Integer (SWT.CTRL); 61 final Integer shift = new Integer (SWT.SHIFT); 62 modifierKeyTable.put(ALT_NAME, alt); 63 nameTable.put(alt, ALT_NAME); 64 modifierKeyTable.put(COMMAND_NAME, command); 65 nameTable.put(command, COMMAND_NAME); 66 modifierKeyTable.put(CTRL_NAME, ctrl); 67 nameTable.put(ctrl, CTRL_NAME); 68 modifierKeyTable.put(SHIFT_NAME, shift); 69 nameTable.put(shift, SHIFT_NAME); 70 modifierKeyTable.put(M1_NAME, 71 "carbon".equals(SWT.getPlatform()) ? command : ctrl); modifierKeyTable.put(M2_NAME, shift); 73 modifierKeyTable.put(M3_NAME, alt); 74 modifierKeyTable.put(M4_NAME, "carbon".equals(SWT.getPlatform()) ? ctrl : command); 76 77 final Integer arrowDown = new Integer (SWT.ARROW_DOWN); 78 naturalKeyTable.put(ARROW_DOWN_NAME, arrowDown); 79 nameTable.put(arrowDown, ARROW_DOWN_NAME); 80 final Integer arrowLeft = new Integer (SWT.ARROW_LEFT); 81 naturalKeyTable.put(ARROW_LEFT_NAME, arrowLeft); 82 nameTable.put(arrowLeft, ARROW_LEFT_NAME); 83 final Integer arrowRight = new Integer (SWT.ARROW_RIGHT); 84 naturalKeyTable.put(ARROW_RIGHT_NAME, arrowRight); 85 nameTable.put(arrowRight, ARROW_RIGHT_NAME); 86 final Integer arrowUp = new Integer (SWT.ARROW_UP); 87 naturalKeyTable.put(ARROW_UP_NAME, arrowUp); 88 nameTable.put(arrowUp, ARROW_UP_NAME); 89 final Integer breakKey = new Integer (SWT.BREAK); 90 naturalKeyTable.put(BREAK_NAME, breakKey); 91 nameTable.put(breakKey, BREAK_NAME); 92 final Integer bs = new Integer (SWT.BS); 93 naturalKeyTable.put(BS_NAME, bs); 94 nameTable.put(bs, BS_NAME); 95 naturalKeyTable.put(BACKSPACE_NAME, bs); 96 final Integer capsLock = new Integer (SWT.CAPS_LOCK); 97 naturalKeyTable.put(CAPS_LOCK_NAME, capsLock); 98 nameTable.put(capsLock, CAPS_LOCK_NAME); 99 final Integer cr = new Integer (SWT.CR); 100 naturalKeyTable.put(CR_NAME, cr); 101 nameTable.put(cr, CR_NAME); 102 naturalKeyTable.put(ENTER_NAME, cr); 103 naturalKeyTable.put(RETURN_NAME, cr); 104 final Integer del = new Integer (SWT.DEL); 105 naturalKeyTable.put(DEL_NAME, del); 106 nameTable.put(del, DEL_NAME); 107 naturalKeyTable.put(DELETE_NAME, del); 108 final Integer end = new Integer (SWT.END); 109 naturalKeyTable.put(END_NAME, end); 110 nameTable.put(end, END_NAME); 111 final Integer esc = new Integer (SWT.ESC); 112 naturalKeyTable.put(ESC_NAME, esc); 113 nameTable.put(esc, ESC_NAME); 114 naturalKeyTable.put(ESCAPE_NAME, esc); 115 final Integer f1 = new Integer (SWT.F1); 116 naturalKeyTable.put(F1_NAME, f1); 117 nameTable.put(f1, F1_NAME); 118 final Integer f2 = new Integer (SWT.F2); 119 naturalKeyTable.put(F2_NAME, new Integer (SWT.F2)); 120 nameTable.put(f2, F2_NAME); 121 final Integer f3 = new Integer (SWT.F3); 122 naturalKeyTable.put(F3_NAME, new Integer (SWT.F3)); 123 nameTable.put(f3, F3_NAME); 124 final Integer f4 = new Integer (SWT.F4); 125 naturalKeyTable.put(F4_NAME, new Integer (SWT.F4)); 126 nameTable.put(f4, F4_NAME); 127 final Integer f5 = new Integer (SWT.F5); 128 naturalKeyTable.put(F5_NAME, new Integer (SWT.F5)); 129 nameTable.put(f5, F5_NAME); 130 final Integer f6 = new Integer (SWT.F6); 131 naturalKeyTable.put(F6_NAME, new Integer (SWT.F6)); 132 nameTable.put(f6, F6_NAME); 133 final Integer f7 = new Integer (SWT.F7); 134 naturalKeyTable.put(F7_NAME, new Integer (SWT.F7)); 135 nameTable.put(f7, F7_NAME); 136 final Integer f8 = new Integer (SWT.F8); 137 naturalKeyTable.put(F8_NAME, new Integer (SWT.F8)); 138 nameTable.put(f8, F8_NAME); 139 final Integer f9 = new Integer (SWT.F9); 140 naturalKeyTable.put(F9_NAME, new Integer (SWT.F9)); 141 nameTable.put(f9, F9_NAME); 142 final Integer f10 = new Integer (SWT.F10); 143 naturalKeyTable.put(F10_NAME, new Integer (SWT.F10)); 144 nameTable.put(f10, F10_NAME); 145 final Integer f11 = new Integer (SWT.F11); 146 naturalKeyTable.put(F11_NAME, new Integer (SWT.F11)); 147 nameTable.put(f11, F11_NAME); 148 final Integer f12 = new Integer (SWT.F12); 149 naturalKeyTable.put(F12_NAME, new Integer (SWT.F12)); 150 nameTable.put(f12, F12_NAME); 151 final Integer f13 = new Integer (SWT.F13); 152 naturalKeyTable.put(F13_NAME, new Integer (SWT.F13)); 153 nameTable.put(f13, F13_NAME); 154 final Integer f14 = new Integer (SWT.F14); 155 naturalKeyTable.put(F14_NAME, new Integer (SWT.F14)); 156 nameTable.put(f14, F14_NAME); 157 final Integer f15 = new Integer (SWT.F15); 158 naturalKeyTable.put(F15_NAME, new Integer (SWT.F15)); 159 nameTable.put(f15, F15_NAME); 160 final Integer ff = new Integer (12); naturalKeyTable.put(FF_NAME, ff); 162 nameTable.put(ff, FF_NAME); 163 final Integer home = new Integer (SWT.HOME); 164 naturalKeyTable.put(HOME_NAME, home); 165 nameTable.put(home, HOME_NAME); 166 final Integer insert = new Integer (SWT.INSERT); 167 naturalKeyTable.put(INSERT_NAME, insert); 168 nameTable.put(insert, INSERT_NAME); 169 final Integer lf = new Integer (SWT.LF); 170 naturalKeyTable.put(LF_NAME, lf); 171 nameTable.put(lf, LF_NAME); 172 final Integer nul = new Integer (SWT.NULL); 173 naturalKeyTable.put(NUL_NAME, nul); 174 nameTable.put(nul, NUL_NAME); 175 final Integer numLock = new Integer (SWT.NUM_LOCK); 176 naturalKeyTable.put(NUM_LOCK_NAME, numLock); 177 nameTable.put(numLock, NUM_LOCK_NAME); 178 final Integer keypad0 = new Integer (SWT.KEYPAD_0); 179 naturalKeyTable.put(NUMPAD_0_NAME, keypad0); 180 nameTable.put(keypad0, NUMPAD_0_NAME); 181 final Integer keypad1 = new Integer (SWT.KEYPAD_1); 182 naturalKeyTable.put(NUMPAD_1_NAME, keypad1); 183 nameTable.put(keypad1, NUMPAD_1_NAME); 184 final Integer keypad2 = new Integer (SWT.KEYPAD_2); 185 naturalKeyTable.put(NUMPAD_2_NAME, keypad2); 186 nameTable.put(keypad2, NUMPAD_2_NAME); 187 final Integer keypad3 = new Integer (SWT.KEYPAD_3); 188 naturalKeyTable.put(NUMPAD_3_NAME, keypad3); 189 nameTable.put(keypad3, NUMPAD_3_NAME); 190 final Integer keypad4 = new Integer (SWT.KEYPAD_4); 191 naturalKeyTable.put(NUMPAD_4_NAME, keypad4); 192 nameTable.put(keypad4, NUMPAD_4_NAME); 193 final Integer keypad5 = new Integer (SWT.KEYPAD_5); 194 naturalKeyTable.put(NUMPAD_5_NAME, keypad5); 195 nameTable.put(keypad5, NUMPAD_5_NAME); 196 final Integer keypad6 = new Integer (SWT.KEYPAD_6); 197 naturalKeyTable.put(NUMPAD_6_NAME, keypad6); 198 nameTable.put(keypad6, NUMPAD_6_NAME); 199 final Integer keypad7 = new Integer (SWT.KEYPAD_7); 200 naturalKeyTable.put(NUMPAD_7_NAME, keypad7); 201 nameTable.put(keypad7, NUMPAD_7_NAME); 202 final Integer keypad8 = new Integer (SWT.KEYPAD_8); 203 naturalKeyTable.put(NUMPAD_8_NAME, keypad8); 204 nameTable.put(keypad8, NUMPAD_8_NAME); 205 final Integer keypad9 = new Integer (SWT.KEYPAD_9); 206 naturalKeyTable.put(NUMPAD_9_NAME, keypad9); 207 nameTable.put(keypad9, NUMPAD_9_NAME); 208 final Integer keypadAdd = new Integer (SWT.KEYPAD_ADD); 209 naturalKeyTable.put(NUMPAD_ADD_NAME, keypadAdd); 210 nameTable.put(keypadAdd, NUMPAD_ADD_NAME); 211 final Integer keypadDecimal = new Integer (SWT.KEYPAD_DECIMAL); 212 naturalKeyTable.put(NUMPAD_DECIMAL_NAME, keypadDecimal); 213 nameTable.put(keypadDecimal, NUMPAD_DECIMAL_NAME); 214 final Integer keypadDivide = new Integer (SWT.KEYPAD_DIVIDE); 215 naturalKeyTable.put(NUMPAD_DIVIDE_NAME, keypadDivide); 216 nameTable.put(keypadDivide, NUMPAD_DIVIDE_NAME); 217 final Integer keypadCr = new Integer (SWT.KEYPAD_CR); 218 naturalKeyTable.put(NUMPAD_ENTER_NAME, keypadCr); 219 nameTable.put(keypadCr, NUMPAD_ENTER_NAME); 220 final Integer keypadEqual = new Integer (SWT.KEYPAD_EQUAL); 221 naturalKeyTable.put(NUMPAD_EQUAL_NAME, keypadEqual); 222 nameTable.put(keypadEqual, NUMPAD_EQUAL_NAME); 223 final Integer keypadMultiply = new Integer (SWT.KEYPAD_MULTIPLY); 224 naturalKeyTable.put(NUMPAD_MULTIPLY_NAME, keypadMultiply); 225 nameTable.put(keypadMultiply, NUMPAD_MULTIPLY_NAME); 226 final Integer keypadSubtract = new Integer (SWT.KEYPAD_SUBTRACT); 227 naturalKeyTable.put(NUMPAD_SUBTRACT_NAME, keypadSubtract); 228 nameTable.put(keypadSubtract, NUMPAD_SUBTRACT_NAME); 229 final Integer pageDown = new Integer (SWT.PAGE_DOWN); 230 naturalKeyTable.put(PAGE_DOWN_NAME, pageDown); 231 nameTable.put(pageDown, PAGE_DOWN_NAME); 232 final Integer pageUp = new Integer (SWT.PAGE_UP); 233 naturalKeyTable.put(PAGE_UP_NAME, pageUp); 234 nameTable.put(pageUp, PAGE_UP_NAME); 235 final Integer pause = new Integer (SWT.PAUSE); 236 naturalKeyTable.put(PAUSE_NAME, pause); 237 nameTable.put(pause, PAUSE_NAME); 238 final Integer printScreen = new Integer (SWT.PRINT_SCREEN); 239 naturalKeyTable.put(PRINT_SCREEN_NAME, printScreen); 240 nameTable.put(printScreen, PRINT_SCREEN_NAME); 241 final Integer scrollLock = new Integer (SWT.SCROLL_LOCK); 242 naturalKeyTable.put(SCROLL_LOCK_NAME, scrollLock); 243 nameTable.put(scrollLock, SCROLL_LOCK_NAME); 244 final Integer space = new Integer (' '); 245 naturalKeyTable.put(SPACE_NAME, space); 246 nameTable.put(space, SPACE_NAME); 247 final Integer tab = new Integer (SWT.TAB); 248 naturalKeyTable.put(TAB_NAME, tab); 249 nameTable.put(tab, TAB_NAME); 250 final Integer vt = new Integer (11); naturalKeyTable.put(VT_NAME, vt); 252 nameTable.put(vt, VT_NAME); 253 } 254 255 261 public final int formalKeyLookup(final String name) { 262 final Object value = naturalKeyTable.get(name); 263 if (value instanceof Integer ) { 264 return ((Integer ) value).intValue(); 265 } 266 267 if (name.length() > 0) { 268 throw new IllegalArgumentException ("Unrecognized formal key name: " + name); 270 } 271 272 return name.charAt(0); 273 } 274 275 281 public final Integer formalKeyLookupInteger(final String name) { 282 final Object value = naturalKeyTable.get(name); 283 if (value instanceof Integer ) { 284 return (Integer ) value; 285 } 286 287 return new Integer (name.charAt(0)); 288 } 289 290 296 public final int formalModifierLookup(final String name) { 297 final Object value = modifierKeyTable.get(name); 298 if (value instanceof Integer ) { 299 return ((Integer ) value).intValue(); 300 } 301 302 return 0; 303 } 304 305 311 public final String formalNameLookup(final int key) { 312 final Integer keyObject = new Integer (key); 313 final Object value = nameTable.get(keyObject); 314 if (value instanceof String ) { 315 return (String ) value; 316 } 317 318 return Util.ZERO_LENGTH_STRING + ((char) key); 319 } 320 321 327 public final int getAlt() { 328 return SWT.ALT; 329 } 330 331 337 public final int getCommand() { 338 return SWT.COMMAND; 339 } 340 341 347 public final int getCtrl() { 348 return SWT.CTRL; 349 } 350 351 357 public final int getShift() { 358 return SWT.SHIFT; 359 } 360 361 367 public final boolean isModifierKey(final int key) { 368 return ((key & SWT.MODIFIER_MASK) != 0); 369 } 370 } 371 | Popular Tags |