comparison org.eclipse.jface/src/org/eclipse/jface/bindings/keys/SWTKeySupport.d @ 12:bc29606a740c

Added dwt-addons in original directory structure of eclipse.org
author Frank Benoit <benoit@tionex.de>
date Sat, 14 Mar 2009 18:23:29 +0100
parents
children
comparison
equal deleted inserted replaced
11:43904fec5dca 12:bc29606a740c
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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13
14 module org.eclipse.jface.bindings.keys.SWTKeySupport;
15
16 import org.eclipse.jface.bindings.keys.KeyStroke;
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.jface.bindings.keys.formatting.IKeyFormatter;
22 import org.eclipse.jface.bindings.keys.formatting.NativeKeyFormatter;
23
24 import java.lang.all;
25
26 /**
27 * <p>
28 * A utility class for converting SWT events into key strokes.
29 * </p>
30 *
31 * @since 3.1
32 */
33 public final class SWTKeySupport {
34
35 /**
36 * A formatter that displays key sequences in a style native to the
37 * platform.
38 */
39 private static const IKeyFormatter NATIVE_FORMATTER;
40
41 static this(){
42 NATIVE_FORMATTER = new NativeKeyFormatter();
43 }
44
45 /**
46 * Given an SWT accelerator value, provide the corresponding key stroke.
47 *
48 * @param accelerator
49 * The accelerator to convert; should be a valid SWT accelerator
50 * value.
51 * @return The equivalent key stroke; never <code>null</code>.
52 */
53 public static final KeyStroke convertAcceleratorToKeyStroke(int accelerator) {
54 int modifierKeys = accelerator & SWT.MODIFIER_MASK;
55 int naturalKey;
56 if (accelerator is modifierKeys) {
57 naturalKey = KeyStroke.NO_KEY;
58 } else {
59 naturalKey = accelerator - modifierKeys;
60 }
61
62 return KeyStroke.getInstance(modifierKeys, naturalKey);
63 }
64
65 /**
66 * <p>
67 * Converts the given event into an SWT accelerator value -- considering the
68 * modified character with the shift modifier. This is the third accelerator
69 * value that should be checked when processing incoming key events.
70 * </p>
71 * <p>
72 * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
73 * "Ctrl+Shift+%".
74 * </p>
75 *
76 * @param event
77 * The event to be converted; must not be <code>null</code>.
78 * @return The combination of the state mask and the unmodified character.
79 */
80 public static final int convertEventToModifiedAccelerator(Event event) {
81 int modifiers = event.stateMask & SWT.MODIFIER_MASK;
82 char character = topKey(event);
83 return modifiers + toUpperCase(character);
84 }
85
86 /**
87 * <p>
88 * Converts the given event into an SWT accelerator value -- considering the
89 * unmodified character with all modifier keys. This is the first
90 * accelerator value that should be checked when processing incoming key
91 * events. However, all alphabetic characters are considered as their
92 * uppercase equivalents.
93 * </p>
94 * <p>
95 * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
96 * "Ctrl+Shift+5".
97 * </p>
98 *
99 * @param event
100 * The event to be converted; must not be <code>null</code>.
101 * @return The combination of the state mask and the unmodified character.
102 */
103 public static final int convertEventToUnmodifiedAccelerator(
104 Event event) {
105 return convertEventToUnmodifiedAccelerator(event.stateMask,
106 event.keyCode);
107 }
108
109 /**
110 * <p>
111 * Converts the given state mask and key code into an SWT accelerator value --
112 * considering the unmodified character with all modifier keys. All
113 * alphabetic characters are considered as their uppercase equivalents.
114 * </p>
115 * <p>
116 * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
117 * "Ctrl+Shift+5".
118 * </p>
119 *
120 * @param stateMask
121 * The integer mask of modifiers keys depressed when this was
122 * pressed.
123 * @param keyCode
124 * The key that was pressed, before being modified.
125 * @return The combination of the state mask and the unmodified character.
126 */
127 private static final int convertEventToUnmodifiedAccelerator(
128 int stateMask, int keyCode) {
129 int modifiers = stateMask & SWT.MODIFIER_MASK;
130 int character = keyCode;
131 return modifiers + toUpperCase(character);
132 }
133
134 /**
135 * <p>
136 * Converts the given event into an SWT accelerator value -- considering the
137 * unmodified character with all modifier keys. This is the first
138 * accelerator value that should be checked. However, all alphabetic
139 * characters are considered as their uppercase equivalents.
140 * </p>
141 * <p>
142 * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
143 * "Ctrl+%".
144 * </p>
145 *
146 * @param event
147 * The event to be converted; must not be <code>null</code>.
148 * @return The combination of the state mask and the unmodified character.
149 */
150 public static final int convertEventToUnmodifiedAccelerator(
151 KeyEvent event) {
152 return convertEventToUnmodifiedAccelerator(event.stateMask,
153 event.keyCode);
154 }
155
156 /**
157 * Converts the given event into an SWT accelerator value -- considering the
158 * modified character without the shift modifier. This is the second
159 * accelerator value that should be checked when processing incoming key
160 * events. Key strokes with alphabetic natural keys are run through
161 * <code>convertEventToUnmodifiedAccelerator</code>.
162 *
163 * @param event
164 * The event to be converted; must not be <code>null</code>.
165 * @return The combination of the state mask without shift, and the modified
166 * character.
167 */
168 public static final int convertEventToUnshiftedModifiedAccelerator(
169 Event event) {
170 // Disregard alphabetic key strokes.
171 if (CharacterIsLetter(cast(dchar) event.keyCode)) {
172 return convertEventToUnmodifiedAccelerator(event);
173 }
174
175 int modifiers = event.stateMask & (SWT.MODIFIER_MASK ^ SWT.SHIFT);
176 char character = topKey(event);
177 return modifiers + toUpperCase(character);
178 }
179
180 /**
181 * Given a key stroke, this method provides the equivalent SWT accelerator
182 * value. The functional inverse of
183 * <code>convertAcceleratorToKeyStroke</code>.
184 *
185 * @param keyStroke
186 * The key stroke to convert; must not be <code>null</code>.
187 * @return The SWT accelerator value
188 */
189 public static final int convertKeyStrokeToAccelerator(
190 KeyStroke keyStroke) {
191 return keyStroke.getModifierKeys() + keyStroke.getNaturalKey();
192 }
193
194 /**
195 * Provides an instance of <code>IKeyFormatter</code> appropriate for the
196 * current instance.
197 *
198 * @return an instance of <code>IKeyFormatter</code> appropriate for the
199 * current instance; never <code>null</code>.
200 */
201 public static IKeyFormatter getKeyFormatterForPlatform() {
202 return NATIVE_FORMATTER;
203 }
204
205 /**
206 * Makes sure that a fully-modified character is converted to the normal
207 * form. This means that "Ctrl+" key strokes must reverse the modification
208 * caused by control-escaping. Also, all lower case letters are converted to
209 * uppercase.
210 *
211 * @param event
212 * The event from which the fully-modified character should be
213 * pulled.
214 * @return The modified character, uppercase and without control-escaping.
215 */
216 private static final char topKey(Event event) {
217 char character = event.character;
218 bool ctrlDown = (event.stateMask & SWT.CTRL) !is 0;
219
220 if (ctrlDown && event.character !is event.keyCode
221 && event.character < 0x20
222 && (event.keyCode & SWT.KEYCODE_BIT) is 0) {
223 character += 0x40;
224 }
225
226 return character;
227 }
228
229 /**
230 * Makes the given character uppercase if it is a letter.
231 *
232 * @param keyCode
233 * The character to convert.
234 * @return The uppercase equivalent, if any; otherwise, the character
235 * itself.
236 */
237 private static final int toUpperCase(int keyCode) {
238 // Will this key code be truncated?
239 if (keyCode > 0xFFFF) {
240 return keyCode;
241 }
242
243 // Downcast in safety. Only make characters uppercase.
244 char character = cast(char) keyCode;
245 return CharacterIsLetter(character) ? CharacterToUpper(character)
246 : keyCode;
247 }
248
249 /**
250 * This class should never be instantiated.
251 */
252 protected this() {
253 // This class should never be instantiated.
254 }
255 }