KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > action > LegacyActionTools


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
12 package org.eclipse.jface.action;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.StringTokenizer JavaDoc;
17
18 import org.eclipse.jface.resource.JFaceResources;
19 import org.eclipse.swt.SWT;
20
21 /**
22  * <p>
23  * Some static utility methods for handling labels on actions. This includes
24  * mnemonics and accelerators.
25  * </p>
26  * <p>
27  * Clients may neither instantiate this class nor extend.
28  * </p>
29  *
30  * @since 3.2
31  */

32 public final class LegacyActionTools {
33
34     /**
35      * Table of key codes (key type: <code>String</code>, value type:
36      * <code>Integer</code>); <code>null</code> if not yet initialized.
37      *
38      * @see #findKeyCode
39      */

40     private static Map JavaDoc keyCodes = null;
41
42     /**
43      * Table of string representations of keys (key type: <code>Integer</code>,
44      * value type: <code>String</code>); <code>null</code>> if not yet
45      * initialized.
46      *
47      * @see #findKeyString
48      */

49     private static Map JavaDoc keyStrings = null;
50
51     /**
52      * The localized uppercase version of ALT
53      */

54     private static String JavaDoc localizedAlt;
55
56     /**
57      * The localized uppercase version of COMMAND
58      */

59     private static String JavaDoc localizedCommand;
60
61     /**
62      * The localized uppercase version of CTRL
63      */

64     private static String JavaDoc localizedCtrl;
65
66     /**
67      * Table of key codes (key type: <code>String</code>, value type:
68      * <code>Integer</code>); <code>null</code> if not yet initialized. The
69      * key is the localalized name of the key as it appears in menus.
70      *
71      * @see #findLocalizedKeyCode
72      */

73     private static Map JavaDoc localizedKeyCodes = null;
74
75     /**
76      * The localized uppercase version of SHIFT
77      */

78     private static String JavaDoc localizedShift;
79
80     /**
81      * The constant to use if there is no mnemonic for this location.
82      */

83     public static final char MNEMONIC_NONE = 0;
84
85     /**
86      * Converts an accelerator key code to a string representation.
87      *
88      * @param keyCode
89      * the key code to be translated
90      * @return a string representation of the key code
91      */

92     public static final String JavaDoc convertAccelerator(final int keyCode) {
93         String JavaDoc modifier = getModifierString(keyCode);
94         String JavaDoc fullKey;
95         if (modifier.equals("")) { //$NON-NLS-1$
96
fullKey = findKeyString(keyCode);
97         } else {
98             fullKey = modifier + "+" + findKeyString(keyCode); //$NON-NLS-1$
99
}
100         return fullKey;
101     }
102
103     /**
104      * Parses the given accelerator text, and converts it to an accelerator key
105      * code.
106      *
107      * @param acceleratorText
108      * the accelerator text
109      * @return the SWT key code, or 0 if there is no accelerator
110      */

111     public static final int convertAccelerator(final String JavaDoc acceleratorText) {
112         int accelerator = 0;
113         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(acceleratorText, "+"); //$NON-NLS-1$
114

115         int keyCode = -1;
116
117         boolean hasMoreTokens = stok.hasMoreTokens();
118         while (hasMoreTokens) {
119             String JavaDoc token = stok.nextToken();
120             hasMoreTokens = stok.hasMoreTokens();
121             // Every token except the last must be one of the modifiers
122
// Ctrl, Shift, Alt, or Command
123
if (hasMoreTokens) {
124                 int modifier = findModifier(token);
125                 if (modifier != 0) {
126                     accelerator |= modifier;
127                 } else { // Leave if there are none
128
return 0;
129                 }
130             } else {
131                 keyCode = findKeyCode(token);
132             }
133         }
134         if (keyCode != -1) {
135             accelerator |= keyCode;
136         }
137         return accelerator;
138     }
139
140     /**
141      * Parses the given accelerator text, and converts it to an accelerator key
142      * code.
143      *
144      * Support for localized modifiers is for backwards compatibility with 1.0.
145      * Use setAccelerator(int) to set accelerators programatically or the
146      * <code>accelerator</code> tag in action definitions in plugin.xml.
147      *
148      * @param acceleratorText
149      * the accelerator text localized to the current locale
150      * @return the SWT key code, or 0 if there is no accelerator
151      */

152     static final int convertLocalizedAccelerator(final String JavaDoc acceleratorText) {
153         int accelerator = 0;
154         StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(acceleratorText, "+"); //$NON-NLS-1$
155

156         int keyCode = -1;
157
158         boolean hasMoreTokens = stok.hasMoreTokens();
159         while (hasMoreTokens) {
160             String JavaDoc token = stok.nextToken();
161             hasMoreTokens = stok.hasMoreTokens();
162             // Every token except the last must be one of the modifiers
163
// Ctrl, Shift, Alt, or Command
164
if (hasMoreTokens) {
165                 int modifier = findLocalizedModifier(token);
166                 if (modifier != 0) {
167                     accelerator |= modifier;
168                 } else { // Leave if there are none
169
return 0;
170                 }
171             } else {
172                 keyCode = findLocalizedKeyCode(token);
173             }
174         }
175         if (keyCode != -1) {
176             accelerator |= keyCode;
177         }
178         return accelerator;
179     }
180
181     /**
182      * Extracts the accelerator text from the given text. Returns
183      * <code>null</code> if there is no accelerator text, and the empty string
184      * if there is no text after the accelerator delimeter (tab or '@').
185      *
186      * @param text
187      * the text for the action; may be <code>null</code>.
188      * @return the accelerator text, or <code>null</code>
189      */

190     public static final String JavaDoc extractAcceleratorText(final String JavaDoc text) {
191         if (text == null) {
192             return null;
193         }
194
195         int index = text.lastIndexOf('\t');
196         if (index == -1) {
197             index = text.lastIndexOf('@');
198         }
199         if (index >= 0) {
200             return text.substring(index + 1);
201         }
202         return null;
203     }
204
205     /**
206      * Extracts the mnemonic text from the given string.
207      *
208      * @param text
209      * The text from which the mnemonic should be extracted; may be
210      * <code>null</code>
211      * @return The text of the mnemonic; will be {@link #MNEMONIC_NONE} if there
212      * is no mnemonic;
213      */

214     public static final char extractMnemonic(final String JavaDoc text) {
215         if (text == null) {
216             return MNEMONIC_NONE;
217         }
218
219         int index = text.indexOf('&');
220         if (index == -1) {
221             return MNEMONIC_NONE;
222         }
223
224         final int textLength = text.length();
225
226         // Ignore '&' at the end of the string.
227
if (index == textLength - 1) {
228             return MNEMONIC_NONE;
229         }
230
231         // Ignore two consecutive ampersands.
232
while (text.charAt(index + 1) == '&') {
233             index = text.indexOf('&', ++index);
234             if (index == textLength - 1) {
235                 return MNEMONIC_NONE;
236             }
237         }
238
239         return text.charAt(index + 1);
240     }
241
242     /**
243      * Maps a standard keyboard key name to an SWT key code. Key names are
244      * converted to upper case before comparison. If the key name is a single
245      * letter, for example "S", its character code is returned.
246      * <p>
247      * The following key names are known (case is ignored):
248      * <ul>
249      * <li><code>"BACKSPACE"</code></li>
250      * <li><code>"TAB"</code></li>
251      * <li><code>"RETURN"</code></li>
252      * <li><code>"ENTER"</code></li>
253      * <li><code>"ESC"</code></li>
254      * <li><code>"ESCAPE"</code></li>
255      * <li><code>"DELETE"</code></li>
256      * <li><code>"SPACE"</code></li>
257      * <li><code>"ARROW_UP"</code>, <code>"ARROW_DOWN"</code>,
258      * <code>"ARROW_LEFT"</code>, and <code>"ARROW_RIGHT"</code></li>
259      * <li><code>"PAGE_UP"</code> and <code>"PAGE_DOWN"</code></li>
260      * <li><code>"HOME"</code></li>
261      * <li><code>"END"</code></li>
262      * <li><code>"INSERT"</code></li>
263      * <li><code>"F1"</code>, <code>"F2"</code> through <code>"F12"</code></li>
264      * </ul>
265      * </p>
266      *
267      * @param token
268      * the key name
269      * @return the SWT key code, <code>-1</code> if no match was found
270      * @see SWT
271      */

272     public static final int findKeyCode(String JavaDoc token) {
273         if (keyCodes == null) {
274             initKeyCodes();
275         }
276         token = token.toUpperCase();
277         Integer JavaDoc i = (Integer JavaDoc) keyCodes.get(token);
278         if (i != null) {
279             return i.intValue();
280         }
281         if (token.length() == 1) {
282             return token.charAt(0);
283         }
284         return -1;
285     }
286
287     /**
288      * Maps an SWT key code to a standard keyboard key name. The key code is
289      * stripped of modifiers (SWT.CTRL, SWT.ALT, SWT.SHIFT, and SWT.COMMAND). If
290      * the key code is not an SWT code (for example if it a key code for the key
291      * 'S'), a string containing a character representation of the key code is
292      * returned.
293      *
294      * @param keyCode
295      * the key code to be translated
296      * @return the string representation of the key code
297      * @see SWT
298      * @since 2.0
299      */

300     public static final String JavaDoc findKeyString(final int keyCode) {
301         if (keyStrings == null) {
302             initKeyStrings();
303         }
304         int i = keyCode & ~(SWT.CTRL | SWT.ALT | SWT.SHIFT | SWT.COMMAND);
305         Integer JavaDoc integer = new Integer JavaDoc(i);
306         String JavaDoc result = (String JavaDoc) keyStrings.get(integer);
307         if (result != null) {
308             return result;
309         }
310         result = new String JavaDoc(new char[] { (char) i });
311         return result;
312     }
313
314     /**
315      * Find the supplied code for a localized key. As #findKeyCode but localized
316      * to the current locale.
317      *
318      * Support for localized modifiers is for backwards compatibility with 1.0.
319      * Use setAccelerator(int) to set accelerators programatically or the
320      * <code>accelerator</code> tag in action definitions in plugin.xml.
321      *
322      * @param token
323      * the localized key name
324      * @return the SWT key code, <code>-1</code> if no match was found
325      * @see #findKeyCode
326      */

327     private static final int findLocalizedKeyCode(String JavaDoc token) {
328         if (localizedKeyCodes == null) {
329             initLocalizedKeyCodes();
330         }
331         token = token.toUpperCase();
332         Integer JavaDoc i = (Integer JavaDoc) localizedKeyCodes.get(token);
333         if (i != null) {
334             return i.intValue();
335         }
336         if (token.length() == 1) {
337             return token.charAt(0);
338         }
339         return -1;
340     }
341
342     /**
343      * Maps the localized modifier names to a code in the same manner as
344      * #findModifier.
345      *
346      * Support for localized modifiers is for backwards compatibility with 1.0.
347      * Use setAccelerator(int) to set accelerators programatically or the
348      * <code>accelerator</code> tag in action definitions in plugin.xml.
349      *
350      * @see #findModifier
351      */

352     private static final int findLocalizedModifier(String JavaDoc token) {
353         if (localizedCtrl == null) {
354             initLocalizedModifiers();
355         }
356
357         token = token.toUpperCase();
358         if (token.equals(localizedCtrl)) {
359             return SWT.CTRL;
360         }
361         if (token.equals(localizedShift)) {
362             return SWT.SHIFT;
363         }
364         if (token.equals(localizedAlt)) {
365             return SWT.ALT;
366         }
367         if (token.equals(localizedCommand)) {
368             return SWT.COMMAND;
369         }
370         return 0;
371     }
372
373     /**
374      * Maps standard keyboard modifier key names to the corresponding SWT
375      * modifier bit. The following modifier key names are recognized (case is
376      * ignored): <code>"CTRL"</code>, <code>"SHIFT"</code>,
377      * <code>"ALT"</code>, and <code>"COMMAND"</code>. The given modifier
378      * key name is converted to upper case before comparison.
379      *
380      * @param token
381      * the modifier key name
382      * @return the SWT modifier bit, or <code>0</code> if no match was found
383      * @see SWT
384      */

385     public static final int findModifier(String JavaDoc token) {
386         token = token.toUpperCase();
387         if (token.equals("CTRL")) { //$NON-NLS-1$
388
return SWT.CTRL;
389         }
390         if (token.equals("SHIFT")) { //$NON-NLS-1$
391
return SWT.SHIFT;
392         }
393         if (token.equals("ALT")) { //$NON-NLS-1$
394
return SWT.ALT;
395         }
396         if (token.equals("COMMAND")) { //$NON-NLS-1$
397
return SWT.COMMAND;
398         }
399         return 0;
400     }
401
402     /**
403      * Returns a string representation of an SWT modifier bit (SWT.CTRL,
404      * SWT.ALT, SWT.SHIFT, and SWT.COMMAND). Returns <code>null</code> if the
405      * key code is not an SWT modifier bit.
406      *
407      * @param keyCode
408      * the SWT modifier bit to be translated
409      * @return the string representation of the SWT modifier bit, or
410      * <code>null</code> if the key code was not an SWT modifier bit
411      * @see SWT
412      */

413     public static final String JavaDoc findModifierString(final int keyCode) {
414         if (keyCode == SWT.CTRL) {
415             return JFaceResources.getString("Ctrl"); //$NON-NLS-1$
416
}
417         if (keyCode == SWT.ALT) {
418             return JFaceResources.getString("Alt"); //$NON-NLS-1$
419
}
420         if (keyCode == SWT.SHIFT) {
421             return JFaceResources.getString("Shift"); //$NON-NLS-1$
422
}
423         if (keyCode == SWT.COMMAND) {
424             return JFaceResources.getString("Command"); //$NON-NLS-1$
425
}
426         return null;
427     }
428
429     /**
430      * Returns the string representation of the modifiers (Ctrl, Alt, Shift,
431      * Command) of the key event.
432      *
433      * @param keyCode
434      * The key code for which the modifier string is desired.
435      * @return The string representation of the key code; never
436      * <code>null</code>.
437      */

438     private static String JavaDoc getModifierString(int keyCode) {
439         String JavaDoc modString = ""; //$NON-NLS-1$
440

441         if ((keyCode & SWT.CTRL) != 0) {
442             modString = findModifierString(keyCode & SWT.CTRL);
443         }
444
445         if ((keyCode & SWT.ALT) != 0) {
446             if (modString.equals("")) { //$NON-NLS-1$
447
modString = findModifierString(keyCode & SWT.ALT);
448             } else {
449                 modString = modString
450                         + "+" + findModifierString(keyCode & SWT.ALT); //$NON-NLS-1$
451
}
452         }
453
454         if ((keyCode & SWT.SHIFT) != 0) {
455             if (modString.equals("")) { //$NON-NLS-1$
456
modString = findModifierString(keyCode & SWT.SHIFT);
457             } else {
458                 modString = modString
459                         + "+" + findModifierString(keyCode & SWT.SHIFT); //$NON-NLS-1$
460
}
461         }
462
463         if ((keyCode & SWT.COMMAND) != 0) {
464             if (modString.equals("")) { //$NON-NLS-1$
465
modString = findModifierString(keyCode & SWT.COMMAND);
466             } else {
467                 modString = modString
468                         + "+" + findModifierString(keyCode & SWT.COMMAND); //$NON-NLS-1$
469
}
470         }
471
472         return modString;
473     }
474
475     /**
476      * Initializes the internal key code table.
477      */

478     private static final void initKeyCodes() {
479         keyCodes = new HashMap JavaDoc(40);
480
481         keyCodes.put("BACKSPACE", new Integer JavaDoc(8)); //$NON-NLS-1$
482
keyCodes.put("TAB", new Integer JavaDoc(9)); //$NON-NLS-1$
483
keyCodes.put("RETURN", new Integer JavaDoc(13)); //$NON-NLS-1$
484
keyCodes.put("ENTER", new Integer JavaDoc(13)); //$NON-NLS-1$
485
keyCodes.put("ESCAPE", new Integer JavaDoc(27)); //$NON-NLS-1$
486
keyCodes.put("ESC", new Integer JavaDoc(27)); //$NON-NLS-1$
487
keyCodes.put("DELETE", new Integer JavaDoc(127)); //$NON-NLS-1$
488

489         keyCodes.put("SPACE", new Integer JavaDoc(' ')); //$NON-NLS-1$
490
keyCodes.put("ARROW_UP", new Integer JavaDoc(SWT.ARROW_UP)); //$NON-NLS-1$
491
keyCodes.put("ARROW_DOWN", new Integer JavaDoc(SWT.ARROW_DOWN)); //$NON-NLS-1$
492
keyCodes.put("ARROW_LEFT", new Integer JavaDoc(SWT.ARROW_LEFT)); //$NON-NLS-1$
493
keyCodes.put("ARROW_RIGHT", new Integer JavaDoc(SWT.ARROW_RIGHT)); //$NON-NLS-1$
494
keyCodes.put("PAGE_UP", new Integer JavaDoc(SWT.PAGE_UP)); //$NON-NLS-1$
495
keyCodes.put("PAGE_DOWN", new Integer JavaDoc(SWT.PAGE_DOWN)); //$NON-NLS-1$
496
keyCodes.put("HOME", new Integer JavaDoc(SWT.HOME)); //$NON-NLS-1$
497
keyCodes.put("END", new Integer JavaDoc(SWT.END)); //$NON-NLS-1$
498
keyCodes.put("INSERT", new Integer JavaDoc(SWT.INSERT)); //$NON-NLS-1$
499
keyCodes.put("F1", new Integer JavaDoc(SWT.F1)); //$NON-NLS-1$
500
keyCodes.put("F2", new Integer JavaDoc(SWT.F2)); //$NON-NLS-1$
501
keyCodes.put("F3", new Integer JavaDoc(SWT.F3)); //$NON-NLS-1$
502
keyCodes.put("F4", new Integer JavaDoc(SWT.F4)); //$NON-NLS-1$
503
keyCodes.put("F5", new Integer JavaDoc(SWT.F5)); //$NON-NLS-1$
504
keyCodes.put("F6", new Integer JavaDoc(SWT.F6)); //$NON-NLS-1$
505
keyCodes.put("F7", new Integer JavaDoc(SWT.F7)); //$NON-NLS-1$
506
keyCodes.put("F8", new Integer JavaDoc(SWT.F8)); //$NON-NLS-1$
507
keyCodes.put("F9", new Integer JavaDoc(SWT.F9)); //$NON-NLS-1$
508
keyCodes.put("F10", new Integer JavaDoc(SWT.F10)); //$NON-NLS-1$
509
keyCodes.put("F11", new Integer JavaDoc(SWT.F11)); //$NON-NLS-1$
510
keyCodes.put("F12", new Integer JavaDoc(SWT.F12)); //$NON-NLS-1$
511
}
512
513     /**
514      * Initializes the internal key string table.
515      */

516     private static void initKeyStrings() {
517         keyStrings = new HashMap JavaDoc(40);
518
519         keyStrings.put(new Integer JavaDoc(8), JFaceResources.getString("Backspace")); //$NON-NLS-1$
520
keyStrings.put(new Integer JavaDoc(9), JFaceResources.getString("Tab")); //$NON-NLS-1$
521
keyStrings.put(new Integer JavaDoc(13), JFaceResources.getString("Return")); //$NON-NLS-1$
522
keyStrings.put(new Integer JavaDoc(13), JFaceResources.getString("Enter")); //$NON-NLS-1$
523
keyStrings.put(new Integer JavaDoc(27), JFaceResources.getString("Escape")); //$NON-NLS-1$
524
keyStrings.put(new Integer JavaDoc(27), JFaceResources.getString("Esc")); //$NON-NLS-1$
525
keyStrings.put(new Integer JavaDoc(127), JFaceResources.getString("Delete")); //$NON-NLS-1$
526

527         keyStrings.put(new Integer JavaDoc(' '), JFaceResources.getString("Space")); //$NON-NLS-1$
528

529         keyStrings.put(new Integer JavaDoc(SWT.ARROW_UP), JFaceResources
530                 .getString("Arrow_Up")); //$NON-NLS-1$
531
keyStrings.put(new Integer JavaDoc(SWT.ARROW_DOWN), JFaceResources
532                 .getString("Arrow_Down")); //$NON-NLS-1$
533
keyStrings.put(new Integer JavaDoc(SWT.ARROW_LEFT), JFaceResources
534                 .getString("Arrow_Left")); //$NON-NLS-1$
535
keyStrings.put(new Integer JavaDoc(SWT.ARROW_RIGHT), JFaceResources
536                 .getString("Arrow_Right")); //$NON-NLS-1$
537
keyStrings.put(new Integer JavaDoc(SWT.PAGE_UP), JFaceResources
538                 .getString("Page_Up")); //$NON-NLS-1$
539
keyStrings.put(new Integer JavaDoc(SWT.PAGE_DOWN), JFaceResources
540                 .getString("Page_Down")); //$NON-NLS-1$
541
keyStrings.put(new Integer JavaDoc(SWT.HOME), JFaceResources.getString("Home")); //$NON-NLS-1$
542
keyStrings.put(new Integer JavaDoc(SWT.END), JFaceResources.getString("End")); //$NON-NLS-1$
543
keyStrings.put(new Integer JavaDoc(SWT.INSERT), JFaceResources
544                 .getString("Insert")); //$NON-NLS-1$
545
keyStrings.put(new Integer JavaDoc(SWT.F1), JFaceResources.getString("F1")); //$NON-NLS-1$
546
keyStrings.put(new Integer JavaDoc(SWT.F2), JFaceResources.getString("F2")); //$NON-NLS-1$
547
keyStrings.put(new Integer JavaDoc(SWT.F3), JFaceResources.getString("F3")); //$NON-NLS-1$
548
keyStrings.put(new Integer JavaDoc(SWT.F4), JFaceResources.getString("F4")); //$NON-NLS-1$
549
keyStrings.put(new Integer JavaDoc(SWT.F5), JFaceResources.getString("F5")); //$NON-NLS-1$
550
keyStrings.put(new Integer JavaDoc(SWT.F6), JFaceResources.getString("F6")); //$NON-NLS-1$
551
keyStrings.put(new Integer JavaDoc(SWT.F7), JFaceResources.getString("F7")); //$NON-NLS-1$
552
keyStrings.put(new Integer JavaDoc(SWT.F8), JFaceResources.getString("F8")); //$NON-NLS-1$
553
keyStrings.put(new Integer JavaDoc(SWT.F9), JFaceResources.getString("F9")); //$NON-NLS-1$
554
keyStrings.put(new Integer JavaDoc(SWT.F10), JFaceResources.getString("F10")); //$NON-NLS-1$
555
keyStrings.put(new Integer JavaDoc(SWT.F11), JFaceResources.getString("F11")); //$NON-NLS-1$
556
keyStrings.put(new Integer JavaDoc(SWT.F12), JFaceResources.getString("F12")); //$NON-NLS-1$
557
}
558
559     /**
560      * Initializes the localized internal key code table.
561      */

562     private static void initLocalizedKeyCodes() {
563         localizedKeyCodes = new HashMap JavaDoc(40);
564
565         localizedKeyCodes.put(JFaceResources
566                 .getString("Backspace").toUpperCase(), new Integer JavaDoc(8)); //$NON-NLS-1$
567
localizedKeyCodes.put(
568                 JFaceResources.getString("Tab").toUpperCase(), new Integer JavaDoc(9)); //$NON-NLS-1$
569
localizedKeyCodes
570                 .put(
571                         JFaceResources.getString("Return").toUpperCase(), new Integer JavaDoc(13)); //$NON-NLS-1$
572
localizedKeyCodes
573                 .put(
574                         JFaceResources.getString("Enter").toUpperCase(), new Integer JavaDoc(13)); //$NON-NLS-1$
575
localizedKeyCodes
576                 .put(
577                         JFaceResources.getString("Escape").toUpperCase(), new Integer JavaDoc(27)); //$NON-NLS-1$
578
localizedKeyCodes.put(
579                 JFaceResources.getString("Esc").toUpperCase(), new Integer JavaDoc(27)); //$NON-NLS-1$
580
localizedKeyCodes
581                 .put(
582                         JFaceResources.getString("Delete").toUpperCase(), new Integer JavaDoc(127)); //$NON-NLS-1$
583

584         localizedKeyCodes
585                 .put(
586                         JFaceResources.getString("Space").toUpperCase(), new Integer JavaDoc(' ')); //$NON-NLS-1$
587

588         localizedKeyCodes
589                 .put(
590                         JFaceResources.getString("Arrow_Up").toUpperCase(), new Integer JavaDoc(SWT.ARROW_UP)); //$NON-NLS-1$
591
localizedKeyCodes
592                 .put(
593                         JFaceResources.getString("Arrow_Down").toUpperCase(), new Integer JavaDoc(SWT.ARROW_DOWN)); //$NON-NLS-1$
594
localizedKeyCodes
595                 .put(
596                         JFaceResources.getString("Arrow_Left").toUpperCase(), new Integer JavaDoc(SWT.ARROW_LEFT)); //$NON-NLS-1$
597
localizedKeyCodes
598                 .put(
599                         JFaceResources.getString("Arrow_Right").toUpperCase(), new Integer JavaDoc(SWT.ARROW_RIGHT)); //$NON-NLS-1$
600
localizedKeyCodes
601                 .put(
602                         JFaceResources.getString("Page_Up").toUpperCase(), new Integer JavaDoc(SWT.PAGE_UP)); //$NON-NLS-1$
603
localizedKeyCodes
604                 .put(
605                         JFaceResources.getString("Page_Down").toUpperCase(), new Integer JavaDoc(SWT.PAGE_DOWN)); //$NON-NLS-1$
606
localizedKeyCodes
607                 .put(
608                         JFaceResources.getString("Home").toUpperCase(), new Integer JavaDoc(SWT.HOME)); //$NON-NLS-1$
609
localizedKeyCodes
610                 .put(
611                         JFaceResources.getString("End").toUpperCase(), new Integer JavaDoc(SWT.END)); //$NON-NLS-1$
612
localizedKeyCodes
613                 .put(
614                         JFaceResources.getString("Insert").toUpperCase(), new Integer JavaDoc(SWT.INSERT)); //$NON-NLS-1$
615
localizedKeyCodes
616                 .put(
617                         JFaceResources.getString("F1").toUpperCase(), new Integer JavaDoc(SWT.F1)); //$NON-NLS-1$
618
localizedKeyCodes
619                 .put(
620                         JFaceResources.getString("F2").toUpperCase(), new Integer JavaDoc(SWT.F2)); //$NON-NLS-1$
621
localizedKeyCodes
622                 .put(
623                         JFaceResources.getString("F3").toUpperCase(), new Integer JavaDoc(SWT.F3)); //$NON-NLS-1$
624
localizedKeyCodes
625                 .put(
626                         JFaceResources.getString("F4").toUpperCase(), new Integer JavaDoc(SWT.F4)); //$NON-NLS-1$
627
localizedKeyCodes
628                 .put(
629                         JFaceResources.getString("F5").toUpperCase(), new Integer JavaDoc(SWT.F5)); //$NON-NLS-1$
630
localizedKeyCodes
631                 .put(
632                         JFaceResources.getString("F6").toUpperCase(), new Integer JavaDoc(SWT.F6)); //$NON-NLS-1$
633
localizedKeyCodes
634                 .put(
635                         JFaceResources.getString("F7").toUpperCase(), new Integer JavaDoc(SWT.F7)); //$NON-NLS-1$
636
localizedKeyCodes
637                 .put(
638                         JFaceResources.getString("F8").toUpperCase(), new Integer JavaDoc(SWT.F8)); //$NON-NLS-1$
639
localizedKeyCodes
640                 .put(
641                         JFaceResources.getString("F9").toUpperCase(), new Integer JavaDoc(SWT.F9)); //$NON-NLS-1$
642
localizedKeyCodes
643                 .put(
644                         JFaceResources.getString("F10").toUpperCase(), new Integer JavaDoc(SWT.F10)); //$NON-NLS-1$
645
localizedKeyCodes
646                 .put(
647                         JFaceResources.getString("F11").toUpperCase(), new Integer JavaDoc(SWT.F11)); //$NON-NLS-1$
648
localizedKeyCodes
649                 .put(
650                         JFaceResources.getString("F12").toUpperCase(), new Integer JavaDoc(SWT.F12)); //$NON-NLS-1$
651
}
652
653     /**
654      * Initialize the list of localized modifiers
655      */

656     private static void initLocalizedModifiers() {
657         localizedCtrl = JFaceResources.getString("Ctrl").toUpperCase(); //$NON-NLS-1$
658
localizedShift = JFaceResources.getString("Shift").toUpperCase(); //$NON-NLS-1$
659
localizedAlt = JFaceResources.getString("Alt").toUpperCase(); //$NON-NLS-1$
660
localizedCommand = JFaceResources.getString("Command").toUpperCase(); //$NON-NLS-1$
661
}
662
663     /**
664      * Convenience method for removing any optional accelerator text from the
665      * given string. The accelerator text appears at the end of the text, and is
666      * separated from the main part by a single tab character <code>'\t'</code>.
667      *
668      * @param text
669      * the text
670      * @return the text sans accelerator
671      */

672     public static final String JavaDoc removeAcceleratorText(final String JavaDoc text) {
673         int index = text.lastIndexOf('\t');
674         if (index == -1) {
675             index = text.lastIndexOf('@');
676         }
677         if (index >= 0) {
678             return text.substring(0, index);
679         }
680         return text;
681     }
682
683     /**
684      * Convenience method for removing any mnemonics from the given string. For
685      * example, <code>removeMnemonics("&Open")</code> will return
686      * <code>"Open"</code>.
687      *
688      * @param text
689      * the text
690      * @return the text sans mnemonics
691      */

692     public static final String JavaDoc removeMnemonics(final String JavaDoc text) {
693         int index = text.indexOf('&');
694         if (index == -1) {
695             return text;
696         }
697         int len = text.length();
698         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(len);
699         int lastIndex = 0;
700         while (index != -1) {
701             // ignore & at the end
702
if (index == len - 1) {
703                 break;
704             }
705             // handle the && case
706
if (text.charAt(index + 1) == '&') {
707                 ++index;
708             }
709
710             // DBCS languages use "(&X)" format
711
if (index > 0 && text.charAt(index - 1) == '('
712                     && text.length() >= index + 3
713                     && text.charAt(index + 2) == ')') {
714                 sb.append(text.substring(lastIndex, index - 1));
715                 index += 3;
716             } else {
717                 sb.append(text.substring(lastIndex, index));
718                 // skip the &
719
++index;
720             }
721
722             lastIndex = index;
723             index = text.indexOf('&', index);
724         }
725         if (lastIndex < len) {
726             sb.append(text.substring(lastIndex, len));
727         }
728         return sb.toString();
729     }
730
731     /**
732      * This class cannot be instantiated.
733      */

734     private LegacyActionTools() {
735         // Does nothing
736
}
737
738 }
739
Popular Tags