001: /*******************************************************************************
002: * Copyright (c) 2000, 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.ui.keys;
011:
012: import java.util.Iterator;
013: import java.util.SortedSet;
014: import java.util.TreeSet;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.swt.events.KeyEvent;
018: import org.eclipse.swt.widgets.Event;
019: import org.eclipse.ui.internal.keys.NativeKeyFormatter;
020:
021: /**
022: * A utility class for converting SWT events into key strokes.
023: *
024: * @deprecated Please use org.eclipse.jface.bindings.keys.SWTKeySupport
025: * @since 3.0
026: */
027: public final class SWTKeySupport {
028:
029: /**
030: * Given an SWT accelerator value, provide the corresponding key stroke.
031: *
032: * @param accelerator
033: * The accelerator to convert; should be a valid SWT accelerator
034: * value.
035: * @return The equivalent key stroke; never <code>null</code>.
036: */
037: public static KeyStroke convertAcceleratorToKeyStroke(
038: int accelerator) {
039: final SortedSet modifierKeys = new TreeSet();
040: NaturalKey naturalKey = null;
041:
042: if ((accelerator & SWT.ALT) != 0) {
043: modifierKeys.add(ModifierKey.ALT);
044: }
045:
046: if ((accelerator & SWT.COMMAND) != 0) {
047: modifierKeys.add(ModifierKey.COMMAND);
048: }
049:
050: if ((accelerator & SWT.CTRL) != 0) {
051: modifierKeys.add(ModifierKey.CTRL);
052: }
053:
054: if ((accelerator & SWT.SHIFT) != 0) {
055: modifierKeys.add(ModifierKey.SHIFT);
056: }
057:
058: if (((accelerator & SWT.KEY_MASK) == 0) && (accelerator != 0)) {
059: // There were only accelerators
060: naturalKey = null;
061: } else {
062: // There were other keys.
063: accelerator &= SWT.KEY_MASK;
064:
065: switch (accelerator) {
066: case SWT.ARROW_DOWN:
067: naturalKey = SpecialKey.ARROW_DOWN;
068: break;
069: case SWT.ARROW_LEFT:
070: naturalKey = SpecialKey.ARROW_LEFT;
071: break;
072: case SWT.ARROW_RIGHT:
073: naturalKey = SpecialKey.ARROW_RIGHT;
074: break;
075: case SWT.ARROW_UP:
076: naturalKey = SpecialKey.ARROW_UP;
077: break;
078: case SWT.BREAK:
079: naturalKey = SpecialKey.BREAK;
080: break;
081: case SWT.CAPS_LOCK:
082: naturalKey = SpecialKey.CAPS_LOCK;
083: break;
084: case SWT.END:
085: naturalKey = SpecialKey.END;
086: break;
087: case SWT.F1:
088: naturalKey = SpecialKey.F1;
089: break;
090: case SWT.F10:
091: naturalKey = SpecialKey.F10;
092: break;
093: case SWT.F11:
094: naturalKey = SpecialKey.F11;
095: break;
096: case SWT.F12:
097: naturalKey = SpecialKey.F12;
098: break;
099: case SWT.F2:
100: naturalKey = SpecialKey.F2;
101: break;
102: case SWT.F3:
103: naturalKey = SpecialKey.F3;
104: break;
105: case SWT.F4:
106: naturalKey = SpecialKey.F4;
107: break;
108: case SWT.F5:
109: naturalKey = SpecialKey.F5;
110: break;
111: case SWT.F6:
112: naturalKey = SpecialKey.F6;
113: break;
114: case SWT.F7:
115: naturalKey = SpecialKey.F7;
116: break;
117: case SWT.F8:
118: naturalKey = SpecialKey.F8;
119: break;
120: case SWT.F9:
121: naturalKey = SpecialKey.F9;
122: break;
123: case SWT.HOME:
124: naturalKey = SpecialKey.HOME;
125: break;
126: case SWT.INSERT:
127: naturalKey = SpecialKey.INSERT;
128: break;
129: case SWT.KEYPAD_0:
130: naturalKey = SpecialKey.NUMPAD_0;
131: break;
132: case SWT.KEYPAD_1:
133: naturalKey = SpecialKey.NUMPAD_1;
134: break;
135: case SWT.KEYPAD_2:
136: naturalKey = SpecialKey.NUMPAD_2;
137: break;
138: case SWT.KEYPAD_3:
139: naturalKey = SpecialKey.NUMPAD_3;
140: break;
141: case SWT.KEYPAD_4:
142: naturalKey = SpecialKey.NUMPAD_4;
143: break;
144: case SWT.KEYPAD_5:
145: naturalKey = SpecialKey.NUMPAD_5;
146: break;
147: case SWT.KEYPAD_6:
148: naturalKey = SpecialKey.NUMPAD_6;
149: break;
150: case SWT.KEYPAD_7:
151: naturalKey = SpecialKey.NUMPAD_7;
152: break;
153: case SWT.KEYPAD_8:
154: naturalKey = SpecialKey.NUMPAD_8;
155: break;
156: case SWT.KEYPAD_9:
157: naturalKey = SpecialKey.NUMPAD_9;
158: break;
159: case SWT.KEYPAD_ADD:
160: naturalKey = SpecialKey.NUMPAD_ADD;
161: break;
162: case SWT.KEYPAD_CR:
163: naturalKey = SpecialKey.NUMPAD_ENTER;
164: break;
165: case SWT.KEYPAD_DECIMAL:
166: naturalKey = SpecialKey.NUMPAD_DECIMAL;
167: break;
168: case SWT.KEYPAD_DIVIDE:
169: naturalKey = SpecialKey.NUMPAD_DIVIDE;
170: break;
171: case SWT.KEYPAD_EQUAL:
172: naturalKey = SpecialKey.NUMPAD_EQUAL;
173: break;
174: case SWT.KEYPAD_MULTIPLY:
175: naturalKey = SpecialKey.NUMPAD_MULTIPLY;
176: break;
177: case SWT.KEYPAD_SUBTRACT:
178: naturalKey = SpecialKey.NUMPAD_SUBTRACT;
179: break;
180: case SWT.NUM_LOCK:
181: naturalKey = SpecialKey.NUM_LOCK;
182: break;
183: case SWT.PAGE_DOWN:
184: naturalKey = SpecialKey.PAGE_DOWN;
185: break;
186: case SWT.PAGE_UP:
187: naturalKey = SpecialKey.PAGE_UP;
188: break;
189: case SWT.PAUSE:
190: naturalKey = SpecialKey.PAUSE;
191: break;
192: case SWT.PRINT_SCREEN:
193: naturalKey = SpecialKey.PRINT_SCREEN;
194: break;
195: case SWT.SCROLL_LOCK:
196: naturalKey = SpecialKey.SCROLL_LOCK;
197: break;
198: default:
199: naturalKey = CharacterKey
200: .getInstance((char) (accelerator & 0xFFFF));
201: }
202: }
203:
204: return KeyStroke.getInstance(modifierKeys, naturalKey);
205: }
206:
207: /**
208: * <p>
209: * Converts the given event into an SWT accelerator value -- considering the
210: * modified character with the shift modifier. This is the third accelerator
211: * value that should be checked.
212: * </p>
213: * <p>
214: * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
215: * "Ctrl+Shift+%".
216: * </p>
217: *
218: * @param event
219: * The event to be converted; must not be <code>null</code>.
220: * @return The combination of the state mask and the unmodified character.
221: */
222: public static int convertEventToModifiedAccelerator(Event event) {
223: int modifiers = event.stateMask & SWT.MODIFIER_MASK;
224: char character = topKey(event);
225: return modifiers + toUpperCase(character);
226: }
227:
228: /**
229: * <p>
230: * Converts the given event into an SWT accelerator value -- considering the
231: * unmodified character with all modifier keys. This is the first
232: * accelerator value that should be checked. However, all alphabetic
233: * characters are considered as their uppercase equivalents.
234: * </p>
235: * <p>
236: * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
237: * "Ctrl+Shift+5".
238: * </p>
239: *
240: * @param event
241: * The event to be converted; must not be <code>null</code>.
242: * @return The combination of the state mask and the unmodified character.
243: */
244: public static int convertEventToUnmodifiedAccelerator(Event event) {
245: return convertEventToUnmodifiedAccelerator(event.stateMask,
246: event.keyCode);
247: }
248:
249: /**
250: * <p>
251: * Converts the given state mask and key code into an SWT accelerator value --
252: * considering the unmodified character with all modifier keys. All
253: * alphabetic characters are considered as their uppercase equivalents.
254: * </p>
255: * <p>
256: * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
257: * "Ctrl+Shift+5".
258: * </p>
259: *
260: * @param stateMask
261: * The integer mask of modifiers keys depressed when this was
262: * pressed.
263: * @param keyCode
264: * The key that was pressed, before being modified.
265: * @return The combination of the state mask and the unmodified character.
266: */
267: private static int convertEventToUnmodifiedAccelerator(
268: int stateMask, int keyCode) {
269: int modifiers = stateMask & SWT.MODIFIER_MASK;
270: int character = keyCode;
271: return modifiers + toUpperCase(character);
272: }
273:
274: /**
275: * <p>
276: * Converts the given event into an SWT accelerator value -- considering the
277: * unmodified character with all modifier keys. This is the first
278: * accelerator value that should be checked. However, all alphabetic
279: * characters are considered as their uppercase equivalents.
280: * </p>
281: * <p>
282: * For example, on a standard US keyboard, "Ctrl+Shift+5" would be viewed as
283: * "Ctrl+%".
284: * </p>
285: *
286: * @param event
287: * The event to be converted; must not be <code>null</code>.
288: * @return The combination of the state mask and the unmodified character.
289: */
290: public static int convertEventToUnmodifiedAccelerator(KeyEvent event) {
291: return convertEventToUnmodifiedAccelerator(event.stateMask,
292: event.keyCode);
293: }
294:
295: /**
296: * Converts the given event into an SWT accelerator value -- considering
297: * the modified character without the shift modifier. This is the second
298: * accelerator value that should be checked. Key strokes with alphabetic
299: * natural keys are run through <code>convertEventToUnmodifiedAccelerator</code>
300: *
301: * @param event
302: * The event to be converted; must not be <code>null</code>.
303: * @return The combination of the state mask without shift, and the
304: * modified character.
305: */
306: public static int convertEventToUnshiftedModifiedAccelerator(
307: 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
314: & (SWT.MODIFIER_MASK ^ SWT.SHIFT);
315: char character = topKey(event);
316: return modifiers + toUpperCase(character);
317: }
318:
319: /**
320: * Given a key stroke, this method provides the equivalent SWT accelerator
321: * value. The functional inverse of <code>convertAcceleratorToKeyStroke</code>.
322: *
323: * @param keyStroke
324: * The key stroke to convert; must not be <code>null</code>.
325: * @return The SWT accelerator value
326: */
327: public static final int convertKeyStrokeToAccelerator(
328: final KeyStroke keyStroke) {
329: int accelerator = 0;
330: final Iterator iterator = keyStroke.getModifierKeys()
331: .iterator();
332:
333: while (iterator.hasNext()) {
334: final ModifierKey modifierKey = (ModifierKey) iterator
335: .next();
336:
337: if (modifierKey == ModifierKey.ALT) {
338: accelerator |= SWT.ALT;
339: } else if (modifierKey == ModifierKey.COMMAND) {
340: accelerator |= SWT.COMMAND;
341: } else if (modifierKey == ModifierKey.CTRL) {
342: accelerator |= SWT.CTRL;
343: } else if (modifierKey == ModifierKey.SHIFT) {
344: accelerator |= SWT.SHIFT;
345: }
346: }
347:
348: final NaturalKey naturalKey = keyStroke.getNaturalKey();
349:
350: if (naturalKey instanceof CharacterKey) {
351: accelerator |= ((CharacterKey) naturalKey).getCharacter();
352: } else if (naturalKey instanceof SpecialKey) {
353: final SpecialKey specialKey = (SpecialKey) naturalKey;
354:
355: if (specialKey == SpecialKey.ARROW_DOWN) {
356: accelerator |= SWT.ARROW_DOWN;
357: } else if (specialKey == SpecialKey.ARROW_LEFT) {
358: accelerator |= SWT.ARROW_LEFT;
359: } else if (specialKey == SpecialKey.ARROW_RIGHT) {
360: accelerator |= SWT.ARROW_RIGHT;
361: } else if (specialKey == SpecialKey.ARROW_UP) {
362: accelerator |= SWT.ARROW_UP;
363: } else if (specialKey == SpecialKey.END) {
364: accelerator |= SWT.END;
365: } else if (specialKey == SpecialKey.F1) {
366: accelerator |= SWT.F1;
367: } else if (specialKey == SpecialKey.F10) {
368: accelerator |= SWT.F10;
369: } else if (specialKey == SpecialKey.F11) {
370: accelerator |= SWT.F11;
371: } else if (specialKey == SpecialKey.F12) {
372: accelerator |= SWT.F12;
373: } else if (specialKey == SpecialKey.F2) {
374: accelerator |= SWT.F2;
375: } else if (specialKey == SpecialKey.F3) {
376: accelerator |= SWT.F3;
377: } else if (specialKey == SpecialKey.F4) {
378: accelerator |= SWT.F4;
379: } else if (specialKey == SpecialKey.F5) {
380: accelerator |= SWT.F5;
381: } else if (specialKey == SpecialKey.F6) {
382: accelerator |= SWT.F6;
383: } else if (specialKey == SpecialKey.F7) {
384: accelerator |= SWT.F7;
385: } else if (specialKey == SpecialKey.F8) {
386: accelerator |= SWT.F8;
387: } else if (specialKey == SpecialKey.F9) {
388: accelerator |= SWT.F9;
389: } else if (specialKey == SpecialKey.HOME) {
390: accelerator |= SWT.HOME;
391: } else if (specialKey == SpecialKey.INSERT) {
392: accelerator |= SWT.INSERT;
393: } else if (specialKey == SpecialKey.PAGE_DOWN) {
394: accelerator |= SWT.PAGE_DOWN;
395: } else if (specialKey == SpecialKey.PAGE_UP) {
396: accelerator |= SWT.PAGE_UP;
397: }
398: }
399:
400: return accelerator;
401: }
402:
403: private static final IKeyFormatter NATIVE_FORMATTER = new NativeKeyFormatter();
404:
405: /**
406: * Provides an instance of <code>IKeyFormatter</code> appropriate for the
407: * current instance.
408: *
409: * @return an instance of <code>IKeyFormatter</code> appropriate for the
410: * current instance; never <code>null</code>.
411: */
412: public static IKeyFormatter getKeyFormatterForPlatform() {
413: return NATIVE_FORMATTER;
414: }
415:
416: /**
417: * Makes sure that a fully-modified character is converted to the normal
418: * form. This means that "Ctrl+" key strokes must reverse the modification
419: * caused by control-escaping. Also, all lower case letters are converted
420: * to uppercase.
421: *
422: * @param event
423: * The event from which the fully-modified character should be
424: * pulled.
425: * @return The modified character, uppercase and without control-escaping.
426: */
427: private static char topKey(Event event) {
428: char character = event.character;
429: boolean ctrlDown = (event.stateMask & SWT.CTRL) != 0;
430:
431: if (ctrlDown && event.character != event.keyCode
432: && event.character < 0x20) {
433: character += 0x40;
434: }
435:
436: return character;
437: }
438:
439: /**
440: * Makes the given character uppercase if it is a letter.
441: *
442: * @param keyCode
443: * The character to convert.
444: * @return The uppercase equivalent, if any; otherwise, the character
445: * itself.
446: */
447: private static int toUpperCase(int keyCode) {
448: // Will this key code be truncated?
449: if (keyCode > 0xFFFF) {
450: return keyCode;
451: }
452:
453: // Downcast in safety. Only make characters uppercase.
454: char character = (char) keyCode;
455: return Character.isLetter(character) ? Character
456: .toUpperCase(character) : keyCode;
457: }
458:
459: /**
460: * This class should never be instantiated.
461: */
462: private SWTKeySupport() {
463: // This class should never be instantiated.
464: }
465: }
|