KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > bindings > keys > formatting > NativeKeyFormatter


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.formatting;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.ResourceBundle JavaDoc;
16
17 import org.eclipse.jface.bindings.keys.IKeyLookup;
18 import org.eclipse.jface.bindings.keys.KeyLookupFactory;
19 import org.eclipse.jface.bindings.keys.KeySequence;
20 import org.eclipse.jface.bindings.keys.KeyStroke;
21 import org.eclipse.jface.util.Util;
22 import org.eclipse.swt.SWT;
23
24 /**
25  * <p>
26  * Formats the key sequences and key strokes into the native human-readable
27  * format. This is typically what you would see on the menus for the given
28  * platform and locale.
29  * </p>
30  *
31  * @since 3.1
32  */

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

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

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

44     private final static HashMap JavaDoc CARBON_KEY_LOOK_UP = new HashMap JavaDoc();
45
46     /**
47      * The resource bundle used by <code>format()</code> to translate formal
48      * string representations by locale.
49      */

50     private final static ResourceBundle JavaDoc RESOURCE_BUNDLE;
51
52     /**
53      * The key into the internationalization resource bundle for the delimiter
54      * to use between key strokes (on the Win32 platform).
55      */

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

58     static {
59         RESOURCE_BUNDLE = ResourceBundle.getBundle(NativeKeyFormatter.class
60                 .getName());
61
62         final String JavaDoc carbonBackspace = "\u232B"; //$NON-NLS-1$
63
CARBON_KEY_LOOK_UP.put(IKeyLookup.BS_NAME, carbonBackspace);
64         CARBON_KEY_LOOK_UP.put(IKeyLookup.BACKSPACE_NAME, carbonBackspace);
65         CARBON_KEY_LOOK_UP
66                 .put(IKeyLookup.CR_NAME, "\u21A9"); //$NON-NLS-1$
67
final String JavaDoc carbonDelete = "\u2326"; //$NON-NLS-1$
68
CARBON_KEY_LOOK_UP.put(IKeyLookup.DEL_NAME, carbonDelete);
69         CARBON_KEY_LOOK_UP.put(IKeyLookup.DELETE_NAME, carbonDelete);
70         CARBON_KEY_LOOK_UP.put(IKeyLookup.SPACE_NAME, "\u2423"); //$NON-NLS-1$
71
CARBON_KEY_LOOK_UP.put(IKeyLookup.ALT_NAME, "\u2325"); //$NON-NLS-1$
72
CARBON_KEY_LOOK_UP.put(IKeyLookup.COMMAND_NAME, "\u2318"); //$NON-NLS-1$
73
CARBON_KEY_LOOK_UP.put(IKeyLookup.CTRL_NAME, "\u2303"); //$NON-NLS-1$
74
CARBON_KEY_LOOK_UP.put(IKeyLookup.SHIFT_NAME, "\u21E7"); //$NON-NLS-1$
75
CARBON_KEY_LOOK_UP.put(IKeyLookup.ARROW_DOWN_NAME, "\u2193"); //$NON-NLS-1$
76
CARBON_KEY_LOOK_UP.put(IKeyLookup.ARROW_LEFT_NAME, "\u2190"); //$NON-NLS-1$
77
CARBON_KEY_LOOK_UP.put(IKeyLookup.ARROW_RIGHT_NAME, "\u2192"); //$NON-NLS-1$
78
CARBON_KEY_LOOK_UP.put(IKeyLookup.ARROW_UP_NAME, "\u2191"); //$NON-NLS-1$
79
CARBON_KEY_LOOK_UP.put(IKeyLookup.END_NAME, "\u2198"); //$NON-NLS-1$
80
CARBON_KEY_LOOK_UP.put(IKeyLookup.NUMPAD_ENTER_NAME, "\u2324"); //$NON-NLS-1$
81
CARBON_KEY_LOOK_UP.put(IKeyLookup.HOME_NAME, "\u2196"); //$NON-NLS-1$
82
CARBON_KEY_LOOK_UP.put(IKeyLookup.PAGE_DOWN_NAME, "\u21DF"); //$NON-NLS-1$
83
CARBON_KEY_LOOK_UP.put(IKeyLookup.PAGE_UP_NAME, "\u21DE"); //$NON-NLS-1$
84
}
85
86     /**
87      * Formats an individual key into a human readable format. This uses an
88      * internationalization resource bundle to look up the key. This does the
89      * platform-specific formatting for Carbon.
90      *
91      * @param key
92      * The key to format.
93      * @return The key formatted as a string; should not be <code>null</code>.
94      */

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

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

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

148     protected int[] sortModifierKeys(final int modifierKeys) {
149         final IKeyLookup lookup = KeyLookupFactory.getDefault();
150         final String JavaDoc platform = SWT.getPlatform();
151         final int[] sortedKeys = new int[4];
152         int index = 0;
153
154         if ("win32".equals(platform) || "wpf".equals(platform)) { //$NON-NLS-1$ //$NON-NLS-2$
155
if ((modifierKeys & lookup.getCtrl()) != 0) {
156                 sortedKeys[index++] = lookup.getCtrl();
157             }
158             if ((modifierKeys & lookup.getAlt()) != 0) {
159                 sortedKeys[index++] = lookup.getAlt();
160             }
161             if ((modifierKeys & lookup.getShift()) != 0) {
162                 sortedKeys[index++] = lookup.getShift();
163             }
164
165         } else if ("gtk".equals(platform) || "motif".equals(platform)) { //$NON-NLS-1$ //$NON-NLS-2$
166
if ((modifierKeys & lookup.getShift()) != 0) {
167                 sortedKeys[index++] = lookup.getShift();
168             }
169             if ((modifierKeys & lookup.getCtrl()) != 0) {
170                 sortedKeys[index++] = lookup.getCtrl();
171             }
172             if ((modifierKeys & lookup.getAlt()) != 0) {
173                 sortedKeys[index++] = lookup.getAlt();
174             }
175
176         } else if ("carbon".equals(platform)) { //$NON-NLS-1$
177
if ((modifierKeys & lookup.getShift()) != 0) {
178                 sortedKeys[index++] = lookup.getShift();
179             }
180             if ((modifierKeys & lookup.getCtrl()) != 0) {
181                 sortedKeys[index++] = lookup.getCtrl();
182             }
183             if ((modifierKeys & lookup.getAlt()) != 0) {
184                 sortedKeys[index++] = lookup.getAlt();
185             }
186             if ((modifierKeys & lookup.getCommand()) != 0) {
187                 sortedKeys[index++] = lookup.getCommand();
188             }
189
190         }
191
192         return sortedKeys;
193     }
194 }
195
Popular Tags