KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > bindings > keys > SWTKeyLookup


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.bindings.keys;
12
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import org.eclipse.jface.util.Util;
17 import org.eclipse.swt.SWT;
18
19 /**
20  * <p>
21  * A look-up table for the formal grammar for keys, and the integer values they
22  * represent. This look-up table is hard-coded to use SWT representations. By
23  * replacing this class (and
24  * {@link org.eclipse.jface.bindings.keys.SWTKeySupport}), you can remove the
25  * dependency on SWT.
26  * </p>
27  *
28  * @since 3.1
29  * @see org.eclipse.jface.bindings.keys.KeyLookupFactory
30  */

31 public final class SWTKeyLookup implements IKeyLookup {
32
33     /**
34      * The look-up table for modifier keys. This is a map of formal name (<code>String</code>)
35      * to integer value (<code>Integer</code>).
36      */

37     private final Map JavaDoc modifierKeyTable = new HashMap JavaDoc();
38
39     /**
40      * The look-up table for formal names. This is a map of integer value (<code>Integer</code>)
41      * to formal name (<code>String</code>).
42      */

43     private final Map JavaDoc nameTable = new HashMap JavaDoc();
44
45     /**
46      * The look-up table for natural keys. This is a map of formal name (<code>String</code>)
47      * to integer value (<code>Integer</code>).
48      */

49     private final Map JavaDoc naturalKeyTable = new HashMap JavaDoc();
50
51     /**
52      * Constructs a new look-up class. This should only be done by the look-up
53      * factory.
54      *
55      * @see KeyLookupFactory
56      */

57     SWTKeyLookup() {
58         final Integer JavaDoc alt = new Integer JavaDoc(SWT.ALT);
59         final Integer JavaDoc command = new Integer JavaDoc(SWT.COMMAND);
60         final Integer JavaDoc ctrl = new Integer JavaDoc(SWT.CTRL);
61         final Integer JavaDoc shift = new Integer JavaDoc(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); //$NON-NLS-1$
72
modifierKeyTable.put(M2_NAME, shift);
73         modifierKeyTable.put(M3_NAME, alt);
74         modifierKeyTable.put(M4_NAME, "carbon".equals(SWT.getPlatform()) ? ctrl //$NON-NLS-1$
75
: command);
76
77         final Integer JavaDoc arrowDown = new Integer JavaDoc(SWT.ARROW_DOWN);
78         naturalKeyTable.put(ARROW_DOWN_NAME, arrowDown);
79         nameTable.put(arrowDown, ARROW_DOWN_NAME);
80         final Integer JavaDoc arrowLeft = new Integer JavaDoc(SWT.ARROW_LEFT);
81         naturalKeyTable.put(ARROW_LEFT_NAME, arrowLeft);
82         nameTable.put(arrowLeft, ARROW_LEFT_NAME);
83         final Integer JavaDoc arrowRight = new Integer JavaDoc(SWT.ARROW_RIGHT);
84         naturalKeyTable.put(ARROW_RIGHT_NAME, arrowRight);
85         nameTable.put(arrowRight, ARROW_RIGHT_NAME);
86         final Integer JavaDoc arrowUp = new Integer JavaDoc(SWT.ARROW_UP);
87         naturalKeyTable.put(ARROW_UP_NAME, arrowUp);
88         nameTable.put(arrowUp, ARROW_UP_NAME);
89         final Integer JavaDoc breakKey = new Integer JavaDoc(SWT.BREAK);
90         naturalKeyTable.put(BREAK_NAME, breakKey);
91         nameTable.put(breakKey, BREAK_NAME);
92         final Integer JavaDoc bs = new Integer JavaDoc(SWT.BS);
93         naturalKeyTable.put(BS_NAME, bs);
94         nameTable.put(bs, BS_NAME);
95         naturalKeyTable.put(BACKSPACE_NAME, bs);
96         final Integer JavaDoc capsLock = new Integer JavaDoc(SWT.CAPS_LOCK);
97         naturalKeyTable.put(CAPS_LOCK_NAME, capsLock);
98         nameTable.put(capsLock, CAPS_LOCK_NAME);
99         final Integer JavaDoc cr = new Integer JavaDoc(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 JavaDoc del = new Integer JavaDoc(SWT.DEL);
105         naturalKeyTable.put(DEL_NAME, del);
106         nameTable.put(del, DEL_NAME);
107         naturalKeyTable.put(DELETE_NAME, del);
108         final Integer JavaDoc end = new Integer JavaDoc(SWT.END);
109         naturalKeyTable.put(END_NAME, end);
110         nameTable.put(end, END_NAME);
111         final Integer JavaDoc esc = new Integer JavaDoc(SWT.ESC);
112         naturalKeyTable.put(ESC_NAME, esc);
113         nameTable.put(esc, ESC_NAME);
114         naturalKeyTable.put(ESCAPE_NAME, esc);
115         final Integer JavaDoc f1 = new Integer JavaDoc(SWT.F1);
116         naturalKeyTable.put(F1_NAME, f1);
117         nameTable.put(f1, F1_NAME);
118         final Integer JavaDoc f2 = new Integer JavaDoc(SWT.F2);
119         naturalKeyTable.put(F2_NAME, new Integer JavaDoc(SWT.F2));
120         nameTable.put(f2, F2_NAME);
121         final Integer JavaDoc f3 = new Integer JavaDoc(SWT.F3);
122         naturalKeyTable.put(F3_NAME, new Integer JavaDoc(SWT.F3));
123         nameTable.put(f3, F3_NAME);
124         final Integer JavaDoc f4 = new Integer JavaDoc(SWT.F4);
125         naturalKeyTable.put(F4_NAME, new Integer JavaDoc(SWT.F4));
126         nameTable.put(f4, F4_NAME);
127         final Integer JavaDoc f5 = new Integer JavaDoc(SWT.F5);
128         naturalKeyTable.put(F5_NAME, new Integer JavaDoc(SWT.F5));
129         nameTable.put(f5, F5_NAME);
130         final Integer JavaDoc f6 = new Integer JavaDoc(SWT.F6);
131         naturalKeyTable.put(F6_NAME, new Integer JavaDoc(SWT.F6));
132         nameTable.put(f6, F6_NAME);
133         final Integer JavaDoc f7 = new Integer JavaDoc(SWT.F7);
134         naturalKeyTable.put(F7_NAME, new Integer JavaDoc(SWT.F7));
135         nameTable.put(f7, F7_NAME);
136         final Integer JavaDoc f8 = new Integer JavaDoc(SWT.F8);
137         naturalKeyTable.put(F8_NAME, new Integer JavaDoc(SWT.F8));
138         nameTable.put(f8, F8_NAME);
139         final Integer JavaDoc f9 = new Integer JavaDoc(SWT.F9);
140         naturalKeyTable.put(F9_NAME, new Integer JavaDoc(SWT.F9));
141         nameTable.put(f9, F9_NAME);
142         final Integer JavaDoc f10 = new Integer JavaDoc(SWT.F10);
143         naturalKeyTable.put(F10_NAME, new Integer JavaDoc(SWT.F10));
144         nameTable.put(f10, F10_NAME);
145         final Integer JavaDoc f11 = new Integer JavaDoc(SWT.F11);
146         naturalKeyTable.put(F11_NAME, new Integer JavaDoc(SWT.F11));
147         nameTable.put(f11, F11_NAME);
148         final Integer JavaDoc f12 = new Integer JavaDoc(SWT.F12);
149         naturalKeyTable.put(F12_NAME, new Integer JavaDoc(SWT.F12));
150         nameTable.put(f12, F12_NAME);
151         final Integer JavaDoc f13 = new Integer JavaDoc(SWT.F13);
152         naturalKeyTable.put(F13_NAME, new Integer JavaDoc(SWT.F13));
153         nameTable.put(f13, F13_NAME);
154         final Integer JavaDoc f14 = new Integer JavaDoc(SWT.F14);
155         naturalKeyTable.put(F14_NAME, new Integer JavaDoc(SWT.F14));
156         nameTable.put(f14, F14_NAME);
157         final Integer JavaDoc f15 = new Integer JavaDoc(SWT.F15);
158         naturalKeyTable.put(F15_NAME, new Integer JavaDoc(SWT.F15));
159         nameTable.put(f15, F15_NAME);
160         final Integer JavaDoc ff = new Integer JavaDoc(12); // ASCII 0x0C
161
naturalKeyTable.put(FF_NAME, ff);
162         nameTable.put(ff, FF_NAME);
163         final Integer JavaDoc home = new Integer JavaDoc(SWT.HOME);
164         naturalKeyTable.put(HOME_NAME, home);
165         nameTable.put(home, HOME_NAME);
166         final Integer JavaDoc insert = new Integer JavaDoc(SWT.INSERT);
167         naturalKeyTable.put(INSERT_NAME, insert);
168         nameTable.put(insert, INSERT_NAME);
169         final Integer JavaDoc lf = new Integer JavaDoc(SWT.LF);
170         naturalKeyTable.put(LF_NAME, lf);
171         nameTable.put(lf, LF_NAME);
172         final Integer JavaDoc nul = new Integer JavaDoc(SWT.NULL);
173         naturalKeyTable.put(NUL_NAME, nul);
174         nameTable.put(nul, NUL_NAME);
175         final Integer JavaDoc numLock = new Integer JavaDoc(SWT.NUM_LOCK);
176         naturalKeyTable.put(NUM_LOCK_NAME, numLock);
177         nameTable.put(numLock, NUM_LOCK_NAME);
178         final Integer JavaDoc keypad0 = new Integer JavaDoc(SWT.KEYPAD_0);
179         naturalKeyTable.put(NUMPAD_0_NAME, keypad0);
180         nameTable.put(keypad0, NUMPAD_0_NAME);
181         final Integer JavaDoc keypad1 = new Integer JavaDoc(SWT.KEYPAD_1);
182         naturalKeyTable.put(NUMPAD_1_NAME, keypad1);
183         nameTable.put(keypad1, NUMPAD_1_NAME);
184         final Integer JavaDoc keypad2 = new Integer JavaDoc(SWT.KEYPAD_2);
185         naturalKeyTable.put(NUMPAD_2_NAME, keypad2);
186         nameTable.put(keypad2, NUMPAD_2_NAME);
187         final Integer JavaDoc keypad3 = new Integer JavaDoc(SWT.KEYPAD_3);
188         naturalKeyTable.put(NUMPAD_3_NAME, keypad3);
189         nameTable.put(keypad3, NUMPAD_3_NAME);
190         final Integer JavaDoc keypad4 = new Integer JavaDoc(SWT.KEYPAD_4);
191         naturalKeyTable.put(NUMPAD_4_NAME, keypad4);
192         nameTable.put(keypad4, NUMPAD_4_NAME);
193         final Integer JavaDoc keypad5 = new Integer JavaDoc(SWT.KEYPAD_5);
194         naturalKeyTable.put(NUMPAD_5_NAME, keypad5);
195         nameTable.put(keypad5, NUMPAD_5_NAME);
196         final Integer JavaDoc keypad6 = new Integer JavaDoc(SWT.KEYPAD_6);
197         naturalKeyTable.put(NUMPAD_6_NAME, keypad6);
198         nameTable.put(keypad6, NUMPAD_6_NAME);
199         final Integer JavaDoc keypad7 = new Integer JavaDoc(SWT.KEYPAD_7);
200         naturalKeyTable.put(NUMPAD_7_NAME, keypad7);
201         nameTable.put(keypad7, NUMPAD_7_NAME);
202         final Integer JavaDoc keypad8 = new Integer JavaDoc(SWT.KEYPAD_8);
203         naturalKeyTable.put(NUMPAD_8_NAME, keypad8);
204         nameTable.put(keypad8, NUMPAD_8_NAME);
205         final Integer JavaDoc keypad9 = new Integer JavaDoc(SWT.KEYPAD_9);
206         naturalKeyTable.put(NUMPAD_9_NAME, keypad9);
207         nameTable.put(keypad9, NUMPAD_9_NAME);
208         final Integer JavaDoc keypadAdd = new Integer JavaDoc(SWT.KEYPAD_ADD);
209         naturalKeyTable.put(NUMPAD_ADD_NAME, keypadAdd);
210         nameTable.put(keypadAdd, NUMPAD_ADD_NAME);
211         final Integer JavaDoc keypadDecimal = new Integer JavaDoc(SWT.KEYPAD_DECIMAL);
212         naturalKeyTable.put(NUMPAD_DECIMAL_NAME, keypadDecimal);
213         nameTable.put(keypadDecimal, NUMPAD_DECIMAL_NAME);
214         final Integer JavaDoc keypadDivide = new Integer JavaDoc(SWT.KEYPAD_DIVIDE);
215         naturalKeyTable.put(NUMPAD_DIVIDE_NAME, keypadDivide);
216         nameTable.put(keypadDivide, NUMPAD_DIVIDE_NAME);
217         final Integer JavaDoc keypadCr = new Integer JavaDoc(SWT.KEYPAD_CR);
218         naturalKeyTable.put(NUMPAD_ENTER_NAME, keypadCr);
219         nameTable.put(keypadCr, NUMPAD_ENTER_NAME);
220         final Integer JavaDoc keypadEqual = new Integer JavaDoc(SWT.KEYPAD_EQUAL);
221         naturalKeyTable.put(NUMPAD_EQUAL_NAME, keypadEqual);
222         nameTable.put(keypadEqual, NUMPAD_EQUAL_NAME);
223         final Integer JavaDoc keypadMultiply = new Integer JavaDoc(SWT.KEYPAD_MULTIPLY);
224         naturalKeyTable.put(NUMPAD_MULTIPLY_NAME, keypadMultiply);
225         nameTable.put(keypadMultiply, NUMPAD_MULTIPLY_NAME);
226         final Integer JavaDoc keypadSubtract = new Integer JavaDoc(SWT.KEYPAD_SUBTRACT);
227         naturalKeyTable.put(NUMPAD_SUBTRACT_NAME, keypadSubtract);
228         nameTable.put(keypadSubtract, NUMPAD_SUBTRACT_NAME);
229         final Integer JavaDoc pageDown = new Integer JavaDoc(SWT.PAGE_DOWN);
230         naturalKeyTable.put(PAGE_DOWN_NAME, pageDown);
231         nameTable.put(pageDown, PAGE_DOWN_NAME);
232         final Integer JavaDoc pageUp = new Integer JavaDoc(SWT.PAGE_UP);
233         naturalKeyTable.put(PAGE_UP_NAME, pageUp);
234         nameTable.put(pageUp, PAGE_UP_NAME);
235         final Integer JavaDoc pause = new Integer JavaDoc(SWT.PAUSE);
236         naturalKeyTable.put(PAUSE_NAME, pause);
237         nameTable.put(pause, PAUSE_NAME);
238         final Integer JavaDoc printScreen = new Integer JavaDoc(SWT.PRINT_SCREEN);
239         naturalKeyTable.put(PRINT_SCREEN_NAME, printScreen);
240         nameTable.put(printScreen, PRINT_SCREEN_NAME);
241         final Integer JavaDoc scrollLock = new Integer JavaDoc(SWT.SCROLL_LOCK);
242         naturalKeyTable.put(SCROLL_LOCK_NAME, scrollLock);
243         nameTable.put(scrollLock, SCROLL_LOCK_NAME);
244         final Integer JavaDoc space = new Integer JavaDoc(' ');
245         naturalKeyTable.put(SPACE_NAME, space);
246         nameTable.put(space, SPACE_NAME);
247         final Integer JavaDoc tab = new Integer JavaDoc(SWT.TAB);
248         naturalKeyTable.put(TAB_NAME, tab);
249         nameTable.put(tab, TAB_NAME);
250         final Integer JavaDoc vt = new Integer JavaDoc(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 JavaDoc name) {
262         final Object JavaDoc value = naturalKeyTable.get(name);
263         if (value instanceof Integer JavaDoc) {
264             return ((Integer JavaDoc) value).intValue();
265         }
266         
267         if (name.length() > 0) {
268             throw new IllegalArgumentException JavaDoc("Unrecognized formal key name: " //$NON-NLS-1$
269
+ name);
270         }
271
272         return name.charAt(0);
273     }
274
275     /*
276      * (non-Javadoc)
277      *
278      * @see org.eclipse.jface.bindings.keys.IKeyLookup#formalKeyLookupInteger(java.lang.String)
279      *
280      */

281     public final Integer JavaDoc formalKeyLookupInteger(final String JavaDoc name) {
282         final Object JavaDoc value = naturalKeyTable.get(name);
283         if (value instanceof Integer JavaDoc) {
284             return (Integer JavaDoc) value;
285         }
286
287         return new Integer JavaDoc(name.charAt(0));
288     }
289
290     /*
291      * (non-Javadoc)
292      *
293      * @see org.eclipse.jface.bindings.keys.IKeyLookup#formalModifierLookup(java.lang.String)
294      *
295      */

296     public final int formalModifierLookup(final String JavaDoc name) {
297         final Object JavaDoc value = modifierKeyTable.get(name);
298         if (value instanceof Integer JavaDoc) {
299             return ((Integer JavaDoc) value).intValue();
300         }
301
302         return 0;
303     }
304
305     /*
306      * (non-Javadoc)
307      *
308      * @see org.eclipse.jface.bindings.keys.IKeyLookup#formalNameLookup(int)
309      *
310      */

311     public final String JavaDoc formalNameLookup(final int key) {
312         final Integer JavaDoc keyObject = new Integer JavaDoc(key);
313         final Object JavaDoc value = nameTable.get(keyObject);
314         if (value instanceof String JavaDoc) {
315             return (String JavaDoc) value;
316         }
317
318         return Util.ZERO_LENGTH_STRING + ((char) key);
319     }
320
321     /*
322      * (non-Javadoc)
323      *
324      * @see org.eclipse.jface.bindings.keys.IKeyLookup#getAlt()
325      *
326      */

327     public final int getAlt() {
328         return SWT.ALT;
329     }
330
331     /*
332      * (non-Javadoc)
333      *
334      * @see org.eclipse.jface.bindings.keys.IKeyLookup#getCommand()
335      *
336      */

337     public final int getCommand() {
338         return SWT.COMMAND;
339     }
340
341     /*
342      * (non-Javadoc)
343      *
344      * @see org.eclipse.jface.bindings.keys.IKeyLookup#getCtrl()
345      *
346      */

347     public final int getCtrl() {
348         return SWT.CTRL;
349     }
350
351     /*
352      * (non-Javadoc)
353      *
354      * @see org.eclipse.jface.bindings.keys.IKeyLookup#getShift()
355      *
356      */

357     public final int getShift() {
358         return SWT.SHIFT;
359     }
360
361     /*
362      * (non-Javadoc)
363      *
364      * @see org.eclipse.jface.bindings.keys.IKeyLookup#isModifierKey(int)
365      *
366      */

367     public final boolean isModifierKey(final int key) {
368         return ((key & SWT.MODIFIER_MASK) != 0);
369     }
370 }
371
Popular Tags