KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > keys > NativeKeyFormatter


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.internal.keys;
13
14 import java.util.Comparator JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17
18 import org.eclipse.swt.SWT;
19 import org.eclipse.ui.internal.util.Util;
20 import org.eclipse.ui.keys.CharacterKey;
21 import org.eclipse.ui.keys.Key;
22 import org.eclipse.ui.keys.KeySequence;
23 import org.eclipse.ui.keys.KeyStroke;
24 import org.eclipse.ui.keys.ModifierKey;
25 import org.eclipse.ui.keys.SpecialKey;
26
27 /**
28  * Formats the key sequences and key strokes into the native human-readable
29  * format. This is typically what you would see on the menus for the given
30  * platform and locale.
31  *
32  * @since 3.0
33  */

34 public class NativeKeyFormatter extends AbstractKeyFormatter {
35
36     /**
37      * The key into the internationalization resource bundle for the delimiter
38      * to use between keys (on the Carbon platform).
39      */

40     private final static String JavaDoc CARBON_KEY_DELIMITER_KEY = "CARBON_KEY_DELIMITER"; //$NON-NLS-1$
41

42     /**
43      * A look-up table for the string representations of various carbon keys.
44      */

45     private final static HashMap JavaDoc CARBON_KEY_LOOK_UP = new HashMap JavaDoc();
46
47     /**
48      * A comparator to sort modifier keys in the order that they would be
49      * displayed to a user. This comparator is platform-specific.
50      */

51     private final static Comparator JavaDoc MODIFIER_KEY_COMPARATOR = new NativeModifierKeyComparator();
52
53     /**
54      * The resource bundle used by <code>format()</code> to translate formal
55      * string representations by locale.
56      */

57     private final static ResourceBundle JavaDoc RESOURCE_BUNDLE;
58
59     /**
60      * The key into the internationalization resource bundle for the delimiter
61      * to use between key strokes (on the Win32 platform).
62      */

63     private final static String JavaDoc WIN32_KEY_STROKE_DELIMITER_KEY = "WIN32_KEY_STROKE_DELIMITER"; //$NON-NLS-1$
64

65     static {
66         RESOURCE_BUNDLE = ResourceBundle.getBundle(NativeKeyFormatter.class
67                 .getName());
68
69         CARBON_KEY_LOOK_UP.put(CharacterKey.BS.toString(), "\u232B"); //$NON-NLS-1$
70
CARBON_KEY_LOOK_UP.put(CharacterKey.CR.toString(), "\u21A9"); //$NON-NLS-1$
71
CARBON_KEY_LOOK_UP.put(CharacterKey.DEL.toString(), "\u2326"); //$NON-NLS-1$
72
CARBON_KEY_LOOK_UP.put(CharacterKey.SPACE.toString(), "\u2423"); //$NON-NLS-1$
73
CARBON_KEY_LOOK_UP.put(ModifierKey.ALT.toString(), "\u2325"); //$NON-NLS-1$
74
CARBON_KEY_LOOK_UP.put(ModifierKey.COMMAND.toString(), "\u2318"); //$NON-NLS-1$
75
CARBON_KEY_LOOK_UP.put(ModifierKey.CTRL.toString(), "\u2303"); //$NON-NLS-1$
76
CARBON_KEY_LOOK_UP.put(ModifierKey.SHIFT.toString(), "\u21E7"); //$NON-NLS-1$
77
CARBON_KEY_LOOK_UP.put(SpecialKey.ARROW_DOWN.toString(), "\u2193"); //$NON-NLS-1$
78
CARBON_KEY_LOOK_UP.put(SpecialKey.ARROW_LEFT.toString(), "\u2190"); //$NON-NLS-1$
79
CARBON_KEY_LOOK_UP.put(SpecialKey.ARROW_RIGHT.toString(), "\u2192"); //$NON-NLS-1$
80
CARBON_KEY_LOOK_UP.put(SpecialKey.ARROW_UP.toString(), "\u2191"); //$NON-NLS-1$
81
CARBON_KEY_LOOK_UP.put(SpecialKey.END.toString(), "\u2198"); //$NON-NLS-1$
82
CARBON_KEY_LOOK_UP.put(SpecialKey.NUMPAD_ENTER.toString(), "\u2324"); //$NON-NLS-1$
83
CARBON_KEY_LOOK_UP.put(SpecialKey.HOME.toString(), "\u2196"); //$NON-NLS-1$
84
CARBON_KEY_LOOK_UP.put(SpecialKey.PAGE_DOWN.toString(), "\u21DF"); //$NON-NLS-1$
85
CARBON_KEY_LOOK_UP.put(SpecialKey.PAGE_UP.toString(), "\u21DE"); //$NON-NLS-1$
86
}
87
88     /**
89      * Formats an individual key into a human readable format. This uses an
90      * internationalization resource bundle to look up the key. This does the
91      * platform-specific formatting for Carbon.
92      *
93      * @param key
94      * The key to format; must not be <code>null</code>.
95      * @return The key formatted as a string; should not be <code>null</code>.
96      */

97     public String JavaDoc format(Key key) {
98         String JavaDoc name = key.toString();
99
100         // TODO consider platform-specific resource bundles
101
if ("carbon".equals(SWT.getPlatform())) { //$NON-NLS-1$
102
String JavaDoc formattedName = (String JavaDoc) CARBON_KEY_LOOK_UP.get(name);
103             if (formattedName != null) {
104                 return formattedName;
105             }
106         }
107
108         return super.format(key);
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.eclipse.ui.keys.AbstractKeyFormatter#getKeyDelimiter()
115      */

116     protected String JavaDoc getKeyDelimiter() {
117         // We must do the look up every time, as our locale might change.
118
if ("carbon".equals(SWT.getPlatform())) { //$NON-NLS-1$
119
return Util.translateString(RESOURCE_BUNDLE,
120                     CARBON_KEY_DELIMITER_KEY, Util.ZERO_LENGTH_STRING, false,
121                     false);
122         } else {
123             return Util.translateString(RESOURCE_BUNDLE, KEY_DELIMITER_KEY,
124                     KeyStroke.KEY_DELIMITER, false, false);
125         }
126     }
127
128     /*
129      * (non-Javadoc)
130      *
131      * @see org.eclipse.ui.keys.AbstractKeyFormatter#getKeyStrokeDelimiter()
132      */

133     protected String JavaDoc getKeyStrokeDelimiter() {
134         // We must do the look up every time, as our locale might change.
135
if ("win32".equals(SWT.getPlatform())) { //$NON-NLS-1$
136
return Util.translateString(RESOURCE_BUNDLE,
137                     WIN32_KEY_STROKE_DELIMITER_KEY,
138                     KeySequence.KEY_STROKE_DELIMITER, false, false);
139         } else {
140             return Util.translateString(RESOURCE_BUNDLE,
141                     KEY_STROKE_DELIMITER_KEY, KeySequence.KEY_STROKE_DELIMITER,
142                     false, false);
143         }
144     }
145
146     /*
147      * (non-Javadoc)
148      *
149      * @see org.eclipse.ui.keys.AbstractKeyFormatter#getModifierKeyComparator()
150      */

151     protected Comparator JavaDoc getModifierKeyComparator() {
152         return MODIFIER_KEY_COMPARATOR;
153     }
154 }
155
Popular Tags