001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.bindings.keys;
011:
012: import java.util.HashMap;
013: import java.util.Map;
014:
015: import org.eclipse.jface.util.Util;
016: import org.eclipse.swt.SWT;
017:
018: /**
019: * <p>
020: * A look-up table for the formal grammar for keys, and the integer values they
021: * represent. This look-up table is hard-coded to use SWT representations. By
022: * replacing this class (and
023: * {@link org.eclipse.jface.bindings.keys.SWTKeySupport}), you can remove the
024: * dependency on SWT.
025: * </p>
026: *
027: * @since 3.1
028: * @see org.eclipse.jface.bindings.keys.KeyLookupFactory
029: */
030: public final class SWTKeyLookup implements IKeyLookup {
031:
032: /**
033: * The look-up table for modifier keys. This is a map of formal name (<code>String</code>)
034: * to integer value (<code>Integer</code>).
035: */
036: private final Map modifierKeyTable = new HashMap();
037:
038: /**
039: * The look-up table for formal names. This is a map of integer value (<code>Integer</code>)
040: * to formal name (<code>String</code>).
041: */
042: private final Map nameTable = new HashMap();
043:
044: /**
045: * The look-up table for natural keys. This is a map of formal name (<code>String</code>)
046: * to integer value (<code>Integer</code>).
047: */
048: private final Map naturalKeyTable = new HashMap();
049:
050: /**
051: * Constructs a new look-up class. This should only be done by the look-up
052: * factory.
053: *
054: * @see KeyLookupFactory
055: */
056: SWTKeyLookup() {
057: final Integer alt = new Integer(SWT.ALT);
058: final Integer command = new Integer(SWT.COMMAND);
059: final Integer ctrl = new Integer(SWT.CTRL);
060: final Integer shift = new Integer(SWT.SHIFT);
061: modifierKeyTable.put(ALT_NAME, alt);
062: nameTable.put(alt, ALT_NAME);
063: modifierKeyTable.put(COMMAND_NAME, command);
064: nameTable.put(command, COMMAND_NAME);
065: modifierKeyTable.put(CTRL_NAME, ctrl);
066: nameTable.put(ctrl, CTRL_NAME);
067: modifierKeyTable.put(SHIFT_NAME, shift);
068: nameTable.put(shift, SHIFT_NAME);
069: modifierKeyTable.put(M1_NAME,
070: "carbon".equals(SWT.getPlatform()) ? command : ctrl); //$NON-NLS-1$
071: modifierKeyTable.put(M2_NAME, shift);
072: modifierKeyTable.put(M3_NAME, alt);
073: modifierKeyTable.put(M4_NAME,
074: "carbon".equals(SWT.getPlatform()) ? ctrl //$NON-NLS-1$
075: : command);
076:
077: final Integer arrowDown = new Integer(SWT.ARROW_DOWN);
078: naturalKeyTable.put(ARROW_DOWN_NAME, arrowDown);
079: nameTable.put(arrowDown, ARROW_DOWN_NAME);
080: final Integer arrowLeft = new Integer(SWT.ARROW_LEFT);
081: naturalKeyTable.put(ARROW_LEFT_NAME, arrowLeft);
082: nameTable.put(arrowLeft, ARROW_LEFT_NAME);
083: final Integer arrowRight = new Integer(SWT.ARROW_RIGHT);
084: naturalKeyTable.put(ARROW_RIGHT_NAME, arrowRight);
085: nameTable.put(arrowRight, ARROW_RIGHT_NAME);
086: final Integer arrowUp = new Integer(SWT.ARROW_UP);
087: naturalKeyTable.put(ARROW_UP_NAME, arrowUp);
088: nameTable.put(arrowUp, ARROW_UP_NAME);
089: final Integer breakKey = new Integer(SWT.BREAK);
090: naturalKeyTable.put(BREAK_NAME, breakKey);
091: nameTable.put(breakKey, BREAK_NAME);
092: final Integer bs = new Integer(SWT.BS);
093: naturalKeyTable.put(BS_NAME, bs);
094: nameTable.put(bs, BS_NAME);
095: naturalKeyTable.put(BACKSPACE_NAME, bs);
096: final Integer capsLock = new Integer(SWT.CAPS_LOCK);
097: naturalKeyTable.put(CAPS_LOCK_NAME, capsLock);
098: nameTable.put(capsLock, CAPS_LOCK_NAME);
099: 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); // ASCII 0x0C
161: 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); // ASCII 0x0B
251: naturalKeyTable.put(VT_NAME, vt);
252: nameTable.put(vt, VT_NAME);
253: }
254:
255: /*
256: * (non-Javadoc)
257: *
258: * @see org.eclipse.jface.bindings.keys.IKeyLookup#formalKeyLookup(java.lang.String)
259: *
260: */
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(
269: "Unrecognized formal key name: " //$NON-NLS-1$
270: + name);
271: }
272:
273: return name.charAt(0);
274: }
275:
276: /*
277: * (non-Javadoc)
278: *
279: * @see org.eclipse.jface.bindings.keys.IKeyLookup#formalKeyLookupInteger(java.lang.String)
280: *
281: */
282: public final Integer formalKeyLookupInteger(final String name) {
283: final Object value = naturalKeyTable.get(name);
284: if (value instanceof Integer) {
285: return (Integer) value;
286: }
287:
288: return new Integer(name.charAt(0));
289: }
290:
291: /*
292: * (non-Javadoc)
293: *
294: * @see org.eclipse.jface.bindings.keys.IKeyLookup#formalModifierLookup(java.lang.String)
295: *
296: */
297: public final int formalModifierLookup(final String name) {
298: final Object value = modifierKeyTable.get(name);
299: if (value instanceof Integer) {
300: return ((Integer) value).intValue();
301: }
302:
303: return 0;
304: }
305:
306: /*
307: * (non-Javadoc)
308: *
309: * @see org.eclipse.jface.bindings.keys.IKeyLookup#formalNameLookup(int)
310: *
311: */
312: public final String formalNameLookup(final int key) {
313: final Integer keyObject = new Integer(key);
314: final Object value = nameTable.get(keyObject);
315: if (value instanceof String) {
316: return (String) value;
317: }
318:
319: return Util.ZERO_LENGTH_STRING + ((char) key);
320: }
321:
322: /*
323: * (non-Javadoc)
324: *
325: * @see org.eclipse.jface.bindings.keys.IKeyLookup#getAlt()
326: *
327: */
328: public final int getAlt() {
329: return SWT.ALT;
330: }
331:
332: /*
333: * (non-Javadoc)
334: *
335: * @see org.eclipse.jface.bindings.keys.IKeyLookup#getCommand()
336: *
337: */
338: public final int getCommand() {
339: return SWT.COMMAND;
340: }
341:
342: /*
343: * (non-Javadoc)
344: *
345: * @see org.eclipse.jface.bindings.keys.IKeyLookup#getCtrl()
346: *
347: */
348: public final int getCtrl() {
349: return SWT.CTRL;
350: }
351:
352: /*
353: * (non-Javadoc)
354: *
355: * @see org.eclipse.jface.bindings.keys.IKeyLookup#getShift()
356: *
357: */
358: public final int getShift() {
359: return SWT.SHIFT;
360: }
361:
362: /*
363: * (non-Javadoc)
364: *
365: * @see org.eclipse.jface.bindings.keys.IKeyLookup#isModifierKey(int)
366: *
367: */
368: public final boolean isModifierKey(final int key) {
369: return ((key & SWT.MODIFIER_MASK) != 0);
370: }
371: }
|