KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > keys > SWTKeySupport


1 /*******************************************************************************
2  * Copyright (c) 2000, 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
12 package org.eclipse.ui.keys;
13
14 import java.util.Iterator JavaDoc;
15 import java.util.SortedSet JavaDoc;
16 import java.util.TreeSet JavaDoc;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.KeyEvent;
20 import org.eclipse.swt.widgets.Event;
21 import org.eclipse.ui.internal.keys.NativeKeyFormatter;
22
23 /**
24  * A utility class for converting SWT events into key strokes.
25  *
26  * @deprecated Please use org.eclipse.jface.bindings.keys.SWTKeySupport
27  * @since 3.0
28  */

29 public final class SWTKeySupport {
30
31     /**
32      * Given an SWT accelerator value, provide the corresponding key stroke.
33      *
34      * @param accelerator
35      * The accelerator to convert; should be a valid SWT accelerator
36      * value.
37      * @return The equivalent key stroke; never <code>null</code>.
38      */

39     public static KeyStroke convertAcceleratorToKeyStroke(int accelerator) {
40         final SortedSet JavaDoc modifierKeys = new TreeSet JavaDoc();
41         NaturalKey naturalKey = null;
42
43         if ((accelerator & SWT.ALT) != 0) {
44             modifierKeys.add(ModifierKey.ALT);
45         }
46
47         if ((accelerator & SWT.COMMAND) != 0) {
48             modifierKeys.add(ModifierKey.COMMAND);
49         }
50
51         if ((accelerator & SWT.CTRL) != 0) {
52             modifierKeys.add(ModifierKey.CTRL);
53         }
54
55         if ((accelerator & SWT.SHIFT) != 0) {
56             modifierKeys.add(ModifierKey.SHIFT);
57         }
58
59         if (((accelerator & SWT.KEY_MASK) == 0) && (accelerator != 0)) {
60             // There were only accelerators
61
naturalKey = null;
62         } else {
63             // There were other keys.
64
accelerator &= SWT.KEY_MASK;
65
66             switch (accelerator) {
67             case SWT.ARROW_DOWN:
68                 naturalKey = SpecialKey.ARROW_DOWN;
69                 break;
70             case SWT.ARROW_LEFT:
71                 naturalKey = SpecialKey.ARROW_LEFT;
72                 break;
73             case SWT.ARROW_RIGHT:
74                 naturalKey = SpecialKey.ARROW_RIGHT;
75                 break;
76             case SWT.ARROW_UP:
77                 naturalKey = SpecialKey.ARROW_UP;
78                 break;
79             case SWT.BREAK:
80                 naturalKey = SpecialKey.BREAK;
81                 break;
82             case SWT.CAPS_LOCK:
83                 naturalKey = SpecialKey.CAPS_LOCK;
84                 break;
85             case SWT.END:
86                 naturalKey = SpecialKey.END;
87                 break;
88             case SWT.F1:
89                 naturalKey = SpecialKey.F1;
90                 break;
91             case SWT.F10:
92                 naturalKey = SpecialKey.F10;
93                 break;
94             case SWT.F11:
95                 naturalKey = SpecialKey.F11;
96                 break;
97             case SWT.F12:
98                 naturalKey = SpecialKey.F12;
99                 break;
100             case SWT.F2:
101                 naturalKey = SpecialKey.F2;
102                 break;
103             case SWT.F3:
104                 naturalKey = SpecialKey.F3;
105                 break;
106             case SWT.F4:
107                 naturalKey = SpecialKey.F4;
108                 break;
109             case SWT.F5:
110                 naturalKey = SpecialKey.F5;
111                 break;
112             case SWT.F6:
113                 naturalKey = SpecialKey.F6;
114                 break;
115             case SWT.F7:
116                 naturalKey = SpecialKey.F7;
117                 break;
118             case SWT.F8:
119                 naturalKey = SpecialKey.F8;
120                 break;
121             case SWT.F9:
122                 naturalKey = SpecialKey.F9;
123                 break;
124             case SWT.HOME:
125                 naturalKey = SpecialKey.HOME;
126                 break;
127             case SWT.INSERT:
128                 naturalKey = SpecialKey.INSERT;
129                 break;
130             case SWT.KEYPAD_0:
131                 naturalKey = SpecialKey.NUMPAD_0;
132                 break;
133             case SWT.KEYPAD_1:
134                 naturalKey = SpecialKey.NUMPAD_1;
135                 break;
136             case SWT.KEYPAD_2:
137                 naturalKey = SpecialKey.NUMPAD_2;
138                 break;
139             case SWT.KEYPAD_3:
140                 naturalKey = SpecialKey.NUMPAD_3;
141                 break;
142             case SWT.KEYPAD_4:
143                 naturalKey = SpecialKey.NUMPAD_4;
144                 break;
145             case SWT.KEYPAD_5:
146                 naturalKey = SpecialKey.NUMPAD_5;
147                 break;
148             case SWT.KEYPAD_6:
149                 naturalKey = SpecialKey.NUMPAD_6;
150                 break;
151             case SWT.KEYPAD_7:
152                 naturalKey = SpecialKey.NUMPAD_7;
153                 break;
154             case SWT.KEYPAD_8:
155                 naturalKey = SpecialKey.NUMPAD_8;
156                 break;
157             case SWT.KEYPAD_9:
158                 naturalKey = SpecialKey.NUMPAD_9;
159                 break;
160             case SWT.KEYPAD_ADD:
161                 naturalKey = SpecialKey.NUMPAD_ADD;
162                 break;
163             case SWT.KEYPAD_CR:
164                 naturalKey = SpecialKey.NUMPAD_ENTER;
165                 break;
166             case SWT.KEYPAD_DECIMAL:
167                 naturalKey = SpecialKey.NUMPAD_DECIMAL;
168                 break;
169             case SWT.KEYPAD_DIVIDE:
170                 naturalKey = SpecialKey.NUMPAD_DIVIDE;
171                 break;
172             case SWT.KEYPAD_EQUAL:
173                 naturalKey = SpecialKey.NUMPAD_EQUAL;
174                 break;
175             case SWT.KEYPAD_MULTIPLY:
176                 naturalKey = SpecialKey.NUMPAD_MULTIPLY;
177                 break;
178             case SWT.KEYPAD_SUBTRACT:
179                 naturalKey = SpecialKey.NUMPAD_SUBTRACT;
180                 break;
181             case SWT.NUM_LOCK:
182                 naturalKey = SpecialKey.NUM_LOCK;
183                 break;
184             case SWT.PAGE_DOWN:
185                 naturalKey = SpecialKey.PAGE_DOWN;
186                 break;
187             case SWT.PAGE_UP:
188                 naturalKey = SpecialKey.PAGE_UP;
189                 break;
190             case SWT.PAUSE:
191                 naturalKey = SpecialKey.PAUSE;
192                 break;
193             case SWT.PRINT_SCREEN:
194                 naturalKey = SpecialKey.PRINT_SCREEN;
195                 break;
196             case SWT.SCROLL_LOCK:
197                 naturalKey = SpecialKey.SCROLL_LOCK;
198                 break;
199             default:
200                 naturalKey = CharacterKey
201                         .getInstance((char) (accelerator & 0xFFFF));
202             }
203         }
204
205         return KeyStroke.getInstance(modifierKeys, naturalKey);
206     }
207
208     /**
209      * <p>
210      * Converts the given event into an SWT accelerator value -- considering the
211      * modified character with the shift modifier. This is the third accelerator
212      * value that should be checked.
213      * </p>
214      * <p>
215      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
216      * "Ctrl+Shift+%".
217      * </p>
218      *
219      * @param event
220      * The event to be converted; must not be <code>null</code>.
221      * @return The combination of the state mask and the unmodified character.
222      */

223     public static int convertEventToModifiedAccelerator(Event event) {
224         int modifiers = event.stateMask & SWT.MODIFIER_MASK;
225         char character = topKey(event);
226         return modifiers + toUpperCase(character);
227     }
228
229     /**
230      * <p>
231      * Converts the given event into an SWT accelerator value -- considering the
232      * unmodified character with all modifier keys. This is the first
233      * accelerator value that should be checked. However, all alphabetic
234      * characters are considered as their uppercase equivalents.
235      * </p>
236      * <p>
237      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
238      * "Ctrl+Shift+5".
239      * </p>
240      *
241      * @param event
242      * The event to be converted; must not be <code>null</code>.
243      * @return The combination of the state mask and the unmodified character.
244      */

245     public static int convertEventToUnmodifiedAccelerator(Event event) {
246         return convertEventToUnmodifiedAccelerator(event.stateMask,
247                 event.keyCode);
248     }
249
250     /**
251      * <p>
252      * Converts the given state mask and key code into an SWT accelerator value --
253      * considering the unmodified character with all modifier keys. All
254      * alphabetic characters are considered as their uppercase equivalents.
255      * </p>
256      * <p>
257      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
258      * "Ctrl+Shift+5".
259      * </p>
260      *
261      * @param stateMask
262      * The integer mask of modifiers keys depressed when this was
263      * pressed.
264      * @param keyCode
265      * The key that was pressed, before being modified.
266      * @return The combination of the state mask and the unmodified character.
267      */

268     private static int convertEventToUnmodifiedAccelerator(int stateMask,
269             int keyCode) {
270         int modifiers = stateMask & SWT.MODIFIER_MASK;
271         int character = keyCode;
272         return modifiers + toUpperCase(character);
273     }
274
275     /**
276      * <p>
277      * Converts the given event into an SWT accelerator value -- considering the
278      * unmodified character with all modifier keys. This is the first
279      * accelerator value that should be checked. However, all alphabetic
280      * characters are considered as their uppercase equivalents.
281      * </p>
282      * <p>
283      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
284      * "Ctrl+%".
285      * </p>
286      *
287      * @param event
288      * The event to be converted; must not be <code>null</code>.
289      * @return The combination of the state mask and the unmodified character.
290      */

291     public static int convertEventToUnmodifiedAccelerator(KeyEvent event) {
292         return convertEventToUnmodifiedAccelerator(event.stateMask,
293                 event.keyCode);
294     }
295
296     /**
297      * Converts the given event into an SWT accelerator value -- considering
298      * the modified character without the shift modifier. This is the second
299      * accelerator value that should be checked. Key strokes with alphabetic
300      * natural keys are run through <code>convertEventToUnmodifiedAccelerator</code>
301      *
302      * @param event
303      * The event to be converted; must not be <code>null</code>.
304      * @return The combination of the state mask without shift, and the
305      * modified character.
306      */

307     public static int convertEventToUnshiftedModifiedAccelerator(Event event) {
308         // Disregard alphabetic key strokes.
309
if (Character.isLetter((char) event.keyCode)) {
310             return convertEventToUnmodifiedAccelerator(event);
311         }
312
313         int modifiers = event.stateMask & (SWT.MODIFIER_MASK ^ SWT.SHIFT);
314         char character = topKey(event);
315         return modifiers + toUpperCase(character);
316     }
317
318     /**
319      * Given a key stroke, this method provides the equivalent SWT accelerator
320      * value. The functional inverse of <code>convertAcceleratorToKeyStroke</code>.
321      *
322      * @param keyStroke
323      * The key stroke to convert; must not be <code>null</code>.
324      * @return The SWT accelerator value
325      */

326     public static final int convertKeyStrokeToAccelerator(
327             final KeyStroke keyStroke) {
328         int accelerator = 0;
329         final Iterator JavaDoc iterator = keyStroke.getModifierKeys().iterator();
330
331         while (iterator.hasNext()) {
332             final ModifierKey modifierKey = (ModifierKey) iterator.next();
333
334             if (modifierKey == ModifierKey.ALT) {
335                 accelerator |= SWT.ALT;
336             } else if (modifierKey == ModifierKey.COMMAND) {
337                 accelerator |= SWT.COMMAND;
338             } else if (modifierKey == ModifierKey.CTRL) {
339                 accelerator |= SWT.CTRL;
340             } else if (modifierKey == ModifierKey.SHIFT) {
341                 accelerator |= SWT.SHIFT;
342             }
343         }
344
345         final NaturalKey naturalKey = keyStroke.getNaturalKey();
346
347         if (naturalKey instanceof CharacterKey) {
348             accelerator |= ((CharacterKey) naturalKey).getCharacter();
349         } else if (naturalKey instanceof SpecialKey) {
350             final SpecialKey specialKey = (SpecialKey) naturalKey;
351
352             if (specialKey == SpecialKey.ARROW_DOWN) {
353                 accelerator |= SWT.ARROW_DOWN;
354             } else if (specialKey == SpecialKey.ARROW_LEFT) {
355                 accelerator |= SWT.ARROW_LEFT;
356             } else if (specialKey == SpecialKey.ARROW_RIGHT) {
357                 accelerator |= SWT.ARROW_RIGHT;
358             } else if (specialKey == SpecialKey.ARROW_UP) {
359                 accelerator |= SWT.ARROW_UP;
360             } else if (specialKey == SpecialKey.END) {
361                 accelerator |= SWT.END;
362             } else if (specialKey == SpecialKey.F1) {
363                 accelerator |= SWT.F1;
364             } else if (specialKey == SpecialKey.F10) {
365                 accelerator |= SWT.F10;
366             } else if (specialKey == SpecialKey.F11) {
367                 accelerator |= SWT.F11;
368             } else if (specialKey == SpecialKey.F12) {
369                 accelerator |= SWT.F12;
370             } else if (specialKey == SpecialKey.F2) {
371                 accelerator |= SWT.F2;
372             } else if (specialKey == SpecialKey.F3) {
373                 accelerator |= SWT.F3;
374             } else if (specialKey == SpecialKey.F4) {
375                 accelerator |= SWT.F4;
376             } else if (specialKey == SpecialKey.F5) {
377                 accelerator |= SWT.F5;
378             } else if (specialKey == SpecialKey.F6) {
379                 accelerator |= SWT.F6;
380             } else if (specialKey == SpecialKey.F7) {
381                 accelerator |= SWT.F7;
382             } else if (specialKey == SpecialKey.F8) {
383                 accelerator |= SWT.F8;
384             } else if (specialKey == SpecialKey.F9) {
385                 accelerator |= SWT.F9;
386             } else if (specialKey == SpecialKey.HOME) {
387                 accelerator |= SWT.HOME;
388             } else if (specialKey == SpecialKey.INSERT) {
389                 accelerator |= SWT.INSERT;
390             } else if (specialKey == SpecialKey.PAGE_DOWN) {
391                 accelerator |= SWT.PAGE_DOWN;
392             } else if (specialKey == SpecialKey.PAGE_UP) {
393                 accelerator |= SWT.PAGE_UP;
394             }
395         }
396
397         return accelerator;
398     }
399
400     private static final IKeyFormatter NATIVE_FORMATTER = new NativeKeyFormatter();
401
402     /**
403      * Provides an instance of <code>IKeyFormatter</code> appropriate for the
404      * current instance.
405      *
406      * @return an instance of <code>IKeyFormatter</code> appropriate for the
407      * current instance; never <code>null</code>.
408      */

409     public static IKeyFormatter getKeyFormatterForPlatform() {
410         return NATIVE_FORMATTER;
411     }
412
413     /**
414      * Makes sure that a fully-modified character is converted to the normal
415      * form. This means that "Ctrl+" key strokes must reverse the modification
416      * caused by control-escaping. Also, all lower case letters are converted
417      * to uppercase.
418      *
419      * @param event
420      * The event from which the fully-modified character should be
421      * pulled.
422      * @return The modified character, uppercase and without control-escaping.
423      */

424     private static char topKey(Event event) {
425         char character = event.character;
426         boolean ctrlDown = (event.stateMask & SWT.CTRL) != 0;
427
428         if (ctrlDown && event.character != event.keyCode
429                 && event.character < 0x20) {
430             character += 0x40;
431         }
432
433         return character;
434     }
435
436     /**
437      * Makes the given character uppercase if it is a letter.
438      *
439      * @param keyCode
440      * The character to convert.
441      * @return The uppercase equivalent, if any; otherwise, the character
442      * itself.
443      */

444     private static int toUpperCase(int keyCode) {
445         // Will this key code be truncated?
446
if (keyCode > 0xFFFF) {
447             return keyCode;
448         }
449
450         // Downcast in safety. Only make characters uppercase.
451
char character = (char) keyCode;
452         return Character.isLetter(character) ? Character.toUpperCase(character)
453                 : keyCode;
454     }
455
456     /**
457      * This class should never be instantiated.
458      */

459     private SWTKeySupport() {
460         // This class should never be instantiated.
461
}
462 }
463
Popular Tags