KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2007 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.jface.bindings.keys;
13
14 import org.eclipse.jface.bindings.keys.formatting.IKeyFormatter;
15 import org.eclipse.jface.bindings.keys.formatting.NativeKeyFormatter;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.events.KeyEvent;
18 import org.eclipse.swt.widgets.Event;
19
20 /**
21  * <p>
22  * A utility class for converting SWT events into key strokes.
23  * </p>
24  *
25  * @since 3.1
26  */

27 public final class SWTKeySupport {
28
29     /**
30      * A formatter that displays key sequences in a style native to the
31      * platform.
32      */

33     private static final IKeyFormatter NATIVE_FORMATTER = new NativeKeyFormatter();
34
35     /**
36      * Given an SWT accelerator value, provide the corresponding key stroke.
37      *
38      * @param accelerator
39      * The accelerator to convert; should be a valid SWT accelerator
40      * value.
41      * @return The equivalent key stroke; never <code>null</code>.
42      */

43     public static final KeyStroke convertAcceleratorToKeyStroke(int accelerator) {
44         final int modifierKeys = accelerator & SWT.MODIFIER_MASK;
45         final int naturalKey;
46         if (accelerator == modifierKeys) {
47             naturalKey = KeyStroke.NO_KEY;
48         } else {
49             naturalKey = accelerator - modifierKeys;
50         }
51         
52         return KeyStroke.getInstance(modifierKeys, naturalKey);
53     }
54
55     /**
56      * <p>
57      * Converts the given event into an SWT accelerator value -- considering the
58      * modified character with the shift modifier. This is the third accelerator
59      * value that should be checked when processing incoming key events.
60      * </p>
61      * <p>
62      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
63      * "Ctrl+Shift+%".
64      * </p>
65      *
66      * @param event
67      * The event to be converted; must not be <code>null</code>.
68      * @return The combination of the state mask and the unmodified character.
69      */

70     public static final int convertEventToModifiedAccelerator(final Event event) {
71         int modifiers = event.stateMask & SWT.MODIFIER_MASK;
72         char character = topKey(event);
73         return modifiers + toUpperCase(character);
74     }
75
76     /**
77      * <p>
78      * Converts the given event into an SWT accelerator value -- considering the
79      * unmodified character with all modifier keys. This is the first
80      * accelerator value that should be checked when processing incoming key
81      * events. However, all alphabetic characters are considered as their
82      * uppercase equivalents.
83      * </p>
84      * <p>
85      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
86      * "Ctrl+Shift+5".
87      * </p>
88      *
89      * @param event
90      * The event to be converted; must not be <code>null</code>.
91      * @return The combination of the state mask and the unmodified character.
92      */

93     public static final int convertEventToUnmodifiedAccelerator(
94             final Event event) {
95         return convertEventToUnmodifiedAccelerator(event.stateMask,
96                 event.keyCode);
97     }
98
99     /**
100      * <p>
101      * Converts the given state mask and key code into an SWT accelerator value --
102      * considering the unmodified character with all modifier keys. All
103      * alphabetic characters are considered as their uppercase equivalents.
104      * </p>
105      * <p>
106      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
107      * "Ctrl+Shift+5".
108      * </p>
109      *
110      * @param stateMask
111      * The integer mask of modifiers keys depressed when this was
112      * pressed.
113      * @param keyCode
114      * The key that was pressed, before being modified.
115      * @return The combination of the state mask and the unmodified character.
116      */

117     private static final int convertEventToUnmodifiedAccelerator(
118             final int stateMask, final int keyCode) {
119         int modifiers = stateMask & SWT.MODIFIER_MASK;
120         int character = keyCode;
121         return modifiers + toUpperCase(character);
122     }
123
124     /**
125      * <p>
126      * Converts the given event into an SWT accelerator value -- considering the
127      * unmodified character with all modifier keys. This is the first
128      * accelerator value that should be checked. However, all alphabetic
129      * characters are considered as their uppercase equivalents.
130      * </p>
131      * <p>
132      * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
133      * "Ctrl+%".
134      * </p>
135      *
136      * @param event
137      * The event to be converted; must not be <code>null</code>.
138      * @return The combination of the state mask and the unmodified character.
139      */

140     public static final int convertEventToUnmodifiedAccelerator(
141             final KeyEvent event) {
142         return convertEventToUnmodifiedAccelerator(event.stateMask,
143                 event.keyCode);
144     }
145
146     /**
147      * Converts the given event into an SWT accelerator value -- considering the
148      * modified character without the shift modifier. This is the second
149      * accelerator value that should be checked when processing incoming key
150      * events. Key strokes with alphabetic natural keys are run through
151      * <code>convertEventToUnmodifiedAccelerator</code>.
152      *
153      * @param event
154      * The event to be converted; must not be <code>null</code>.
155      * @return The combination of the state mask without shift, and the modified
156      * character.
157      */

158     public static final int convertEventToUnshiftedModifiedAccelerator(
159             final Event event) {
160         // Disregard alphabetic key strokes.
161
if (Character.isLetter((char) event.keyCode)) {
162             return convertEventToUnmodifiedAccelerator(event);
163         }
164
165         int modifiers = event.stateMask & (SWT.MODIFIER_MASK ^ SWT.SHIFT);
166         char character = topKey(event);
167         return modifiers + toUpperCase(character);
168     }
169
170     /**
171      * Given a key stroke, this method provides the equivalent SWT accelerator
172      * value. The functional inverse of
173      * <code>convertAcceleratorToKeyStroke</code>.
174      *
175      * @param keyStroke
176      * The key stroke to convert; must not be <code>null</code>.
177      * @return The SWT accelerator value
178      */

179     public static final int convertKeyStrokeToAccelerator(
180             final KeyStroke keyStroke) {
181         return keyStroke.getModifierKeys() + keyStroke.getNaturalKey();
182     }
183
184     /**
185      * Provides an instance of <code>IKeyFormatter</code> appropriate for the
186      * current instance.
187      *
188      * @return an instance of <code>IKeyFormatter</code> appropriate for the
189      * current instance; never <code>null</code>.
190      */

191     public static IKeyFormatter getKeyFormatterForPlatform() {
192         return NATIVE_FORMATTER;
193     }
194
195     /**
196      * Makes sure that a fully-modified character is converted to the normal
197      * form. This means that "Ctrl+" key strokes must reverse the modification
198      * caused by control-escaping. Also, all lower case letters are converted to
199      * uppercase.
200      *
201      * @param event
202      * The event from which the fully-modified character should be
203      * pulled.
204      * @return The modified character, uppercase and without control-escaping.
205      */

206     private static final char topKey(final Event event) {
207         char character = event.character;
208         boolean ctrlDown = (event.stateMask & SWT.CTRL) != 0;
209
210         if (ctrlDown && event.character != event.keyCode
211                 && event.character < 0x20
212                 && (event.keyCode & SWT.KEYCODE_BIT) == 0) {
213             character += 0x40;
214         }
215
216         return character;
217     }
218
219     /**
220      * Makes the given character uppercase if it is a letter.
221      *
222      * @param keyCode
223      * The character to convert.
224      * @return The uppercase equivalent, if any; otherwise, the character
225      * itself.
226      */

227     private static final int toUpperCase(int keyCode) {
228         // Will this key code be truncated?
229
if (keyCode > 0xFFFF) {
230             return keyCode;
231         }
232
233         // Downcast in safety. Only make characters uppercase.
234
final char character = (char) keyCode;
235         return Character.isLetter(character) ? Character.toUpperCase(character)
236                 : keyCode;
237     }
238
239     /**
240      * This class should never be instantiated.
241      */

242     protected SWTKeySupport() {
243         // This class should never be instantiated.
244
}
245 }
246
Popular Tags