comparison org.eclipse.jface/src/org/eclipse/jface/bindings/keys/formatting/NativeKeyFormatter.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.formatting.NativeKeyFormatter;
15
16 import org.eclipse.jface.bindings.keys.formatting.AbstractKeyFormatter;
17
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.jface.bindings.keys.IKeyLookup;
21 import org.eclipse.jface.bindings.keys.KeyLookupFactory;
22 import org.eclipse.jface.bindings.keys.KeySequence;
23 import org.eclipse.jface.bindings.keys.KeyStroke;
24 import org.eclipse.jface.util.Util;
25
26 import java.lang.all;
27 import java.util.HashMap;
28 import java.util.ResourceBundle;
29
30 /**
31 * <p>
32 * Formats the key sequences and key strokes into the native human-readable
33 * format. This is typically what you would see on the menus for the given
34 * platform and locale.
35 * </p>
36 *
37 * @since 3.1
38 */
39 public final class NativeKeyFormatter : AbstractKeyFormatter {
40 alias AbstractKeyFormatter.format format;
41
42 /**
43 * The key into the internationalization resource bundle for the delimiter
44 * to use between keys (on the Carbon platform).
45 */
46 private const static String CARBON_KEY_DELIMITER_KEY = "CARBON_KEY_DELIMITER"; //$NON-NLS-1$
47
48 /**
49 * A look-up table for the string representations of various carbon keys.
50 */
51 private const static HashMap CARBON_KEY_LOOK_UP;
52
53 /**
54 * The resource bundle used by <code>format()</code> to translate formal
55 * string representations by locale.
56 */
57 private const static ResourceBundle 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 const static String WIN32_KEY_STROKE_DELIMITER_KEY = "WIN32_KEY_STROKE_DELIMITER"; //$NON-NLS-1$
64
65 static this() {
66 CARBON_KEY_LOOK_UP = new HashMap();
67 RESOURCE_BUNDLE = ResourceBundle.getBundle(
68 getImportData!("org.eclipse.jface.bindings.keys.formatting.NativeKeyFormatter.properties"));
69
70 Object carbonBackspace = stringcast("\u232B"); //$NON-NLS-1$
71 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.BS_NAME), carbonBackspace);
72 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.BACKSPACE_NAME), carbonBackspace);
73 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.CR_NAME), stringcast("\u21A9")); //$NON-NLS-1$
74 Object carbonDelete = stringcast("\u2326"); //$NON-NLS-1$
75 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.DEL_NAME), carbonDelete);
76 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.DELETE_NAME), carbonDelete);
77 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.SPACE_NAME), stringcast("\u2423")); //$NON-NLS-1$
78 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.ALT_NAME), stringcast("\u2325")); //$NON-NLS-1$
79 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.COMMAND_NAME), stringcast("\u2318")); //$NON-NLS-1$
80 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.CTRL_NAME), stringcast("\u2303")); //$NON-NLS-1$
81 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.SHIFT_NAME), stringcast("\u21E7")); //$NON-NLS-1$
82 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.ARROW_DOWN_NAME), stringcast("\u2193")); //$NON-NLS-1$
83 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.ARROW_LEFT_NAME), stringcast("\u2190")); //$NON-NLS-1$
84 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.ARROW_RIGHT_NAME), stringcast("\u2192")); //$NON-NLS-1$
85 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.ARROW_UP_NAME), stringcast("\u2191")); //$NON-NLS-1$
86 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.END_NAME), stringcast("\u2198")); //$NON-NLS-1$
87 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.NUMPAD_ENTER_NAME), stringcast("\u2324")); //$NON-NLS-1$
88 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.HOME_NAME), stringcast("\u2196")); //$NON-NLS-1$
89 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.PAGE_DOWN_NAME), stringcast("\u21DF")); //$NON-NLS-1$
90 CARBON_KEY_LOOK_UP.put(stringcast(IKeyLookup.PAGE_UP_NAME), stringcast("\u21DE")); //$NON-NLS-1$
91 }
92
93 /**
94 * Formats an individual key into a human readable format. This uses an
95 * internationalization resource bundle to look up the key. This does the
96 * platform-specific formatting for Carbon.
97 *
98 * @param key
99 * The key to format.
100 * @return The key formatted as a string; should not be <code>null</code>.
101 */
102 public override final String format(int key) {
103 IKeyLookup lookup = KeyLookupFactory.getDefault();
104 String name = lookup.formalNameLookup(key);
105
106 // TODO consider platform-specific resource bundles
107 if ("carbon".equals(SWT.getPlatform())) { //$NON-NLS-1$
108 String formattedName = stringcast( CARBON_KEY_LOOK_UP.get(name));
109 if (formattedName !is null) {
110 return formattedName;
111 }
112 }
113
114 return super.format(key);
115 }
116
117 /*
118 * (non-Javadoc)
119 *
120 * @see org.eclipse.jface.bindings.keys.AbstractKeyFormatter#getKeyDelimiter()
121 */
122 protected override String getKeyDelimiter() {
123 // We must do the look up every time, as our locale might change.
124 if ("carbon".equals(SWT.getPlatform())) { //$NON-NLS-1$
125 return Util.translateString(RESOURCE_BUNDLE,
126 CARBON_KEY_DELIMITER_KEY, Util.ZERO_LENGTH_STRING);
127 }
128
129 return Util.translateString(RESOURCE_BUNDLE, KEY_DELIMITER_KEY,
130 KeyStroke.KEY_DELIMITER);
131 }
132
133 /*
134 * (non-Javadoc)
135 *
136 * @see org.eclipse.jface.bindings.keys.AbstractKeyFormatter#getKeyStrokeDelimiter()
137 */
138 protected override String getKeyStrokeDelimiter() {
139 // We must do the look up every time, as our locale might change.
140 if ("win32".equals(SWT.getPlatform())) { //$NON-NLS-1$
141 return Util.translateString(RESOURCE_BUNDLE,
142 WIN32_KEY_STROKE_DELIMITER_KEY,
143 KeySequence.KEY_STROKE_DELIMITER);
144 }
145
146 return Util.translateString(RESOURCE_BUNDLE, KEY_STROKE_DELIMITER_KEY,
147 KeySequence.KEY_STROKE_DELIMITER);
148 }
149
150 /*
151 * (non-Javadoc)
152 *
153 * @see org.eclipse.jface.bindings.keys.AbstractKeyFormatter#sortModifierKeys(int)
154 */
155 protected override int[] sortModifierKeys(int modifierKeys) {
156 IKeyLookup lookup = KeyLookupFactory.getDefault();
157 String platform = SWT.getPlatform();
158 int[] sortedKeys = new int[4];
159 int index = 0;
160
161 if ("win32".equals(platform) || "wpf".equals(platform)) { //$NON-NLS-1$ //$NON-NLS-2$
162 if ((modifierKeys & lookup.getCtrl()) !is 0) {
163 sortedKeys[index++] = lookup.getCtrl();
164 }
165 if ((modifierKeys & lookup.getAlt()) !is 0) {
166 sortedKeys[index++] = lookup.getAlt();
167 }
168 if ((modifierKeys & lookup.getShift()) !is 0) {
169 sortedKeys[index++] = lookup.getShift();
170 }
171
172 } else if ("gtk".equals(platform) || "motif".equals(platform)) { //$NON-NLS-1$ //$NON-NLS-2$
173 if ((modifierKeys & lookup.getShift()) !is 0) {
174 sortedKeys[index++] = lookup.getShift();
175 }
176 if ((modifierKeys & lookup.getCtrl()) !is 0) {
177 sortedKeys[index++] = lookup.getCtrl();
178 }
179 if ((modifierKeys & lookup.getAlt()) !is 0) {
180 sortedKeys[index++] = lookup.getAlt();
181 }
182
183 } else if ("carbon".equals(platform)) { //$NON-NLS-1$
184 if ((modifierKeys & lookup.getShift()) !is 0) {
185 sortedKeys[index++] = lookup.getShift();
186 }
187 if ((modifierKeys & lookup.getCtrl()) !is 0) {
188 sortedKeys[index++] = lookup.getCtrl();
189 }
190 if ((modifierKeys & lookup.getAlt()) !is 0) {
191 sortedKeys[index++] = lookup.getAlt();
192 }
193 if ((modifierKeys & lookup.getCommand()) !is 0) {
194 sortedKeys[index++] = lookup.getCommand();
195 }
196
197 }
198
199 return sortedKeys;
200 }
201 }