001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.internal.keys;
011:
012: import java.util.Comparator;
013: import java.util.HashMap;
014: import java.util.ResourceBundle;
015:
016: import org.eclipse.swt.SWT;
017: import org.eclipse.ui.internal.util.Util;
018: import org.eclipse.ui.keys.CharacterKey;
019: import org.eclipse.ui.keys.Key;
020: import org.eclipse.ui.keys.KeySequence;
021: import org.eclipse.ui.keys.KeyStroke;
022: import org.eclipse.ui.keys.ModifierKey;
023: import org.eclipse.ui.keys.SpecialKey;
024:
025: /**
026: * Formats the key sequences and key strokes into the native human-readable
027: * format. This is typically what you would see on the menus for the given
028: * platform and locale.
029: *
030: * @since 3.0
031: */
032: public class NativeKeyFormatter extends AbstractKeyFormatter {
033:
034: /**
035: * The key into the internationalization resource bundle for the delimiter
036: * to use between keys (on the Carbon platform).
037: */
038: private final static String CARBON_KEY_DELIMITER_KEY = "CARBON_KEY_DELIMITER"; //$NON-NLS-1$
039:
040: /**
041: * A look-up table for the string representations of various carbon keys.
042: */
043: private final static HashMap CARBON_KEY_LOOK_UP = new HashMap();
044:
045: /**
046: * A comparator to sort modifier keys in the order that they would be
047: * displayed to a user. This comparator is platform-specific.
048: */
049: private final static Comparator MODIFIER_KEY_COMPARATOR = new NativeModifierKeyComparator();
050:
051: /**
052: * The resource bundle used by <code>format()</code> to translate formal
053: * string representations by locale.
054: */
055: private final static ResourceBundle RESOURCE_BUNDLE;
056:
057: /**
058: * The key into the internationalization resource bundle for the delimiter
059: * to use between key strokes (on the Win32 platform).
060: */
061: private final static String WIN32_KEY_STROKE_DELIMITER_KEY = "WIN32_KEY_STROKE_DELIMITER"; //$NON-NLS-1$
062:
063: static {
064: RESOURCE_BUNDLE = ResourceBundle
065: .getBundle(NativeKeyFormatter.class.getName());
066:
067: CARBON_KEY_LOOK_UP.put(CharacterKey.BS.toString(), "\u232B"); //$NON-NLS-1$
068: CARBON_KEY_LOOK_UP.put(CharacterKey.CR.toString(), "\u21A9"); //$NON-NLS-1$
069: CARBON_KEY_LOOK_UP.put(CharacterKey.DEL.toString(), "\u2326"); //$NON-NLS-1$
070: CARBON_KEY_LOOK_UP.put(CharacterKey.SPACE.toString(), "\u2423"); //$NON-NLS-1$
071: CARBON_KEY_LOOK_UP.put(ModifierKey.ALT.toString(), "\u2325"); //$NON-NLS-1$
072: CARBON_KEY_LOOK_UP
073: .put(ModifierKey.COMMAND.toString(), "\u2318"); //$NON-NLS-1$
074: CARBON_KEY_LOOK_UP.put(ModifierKey.CTRL.toString(), "\u2303"); //$NON-NLS-1$
075: CARBON_KEY_LOOK_UP.put(ModifierKey.SHIFT.toString(), "\u21E7"); //$NON-NLS-1$
076: CARBON_KEY_LOOK_UP.put(SpecialKey.ARROW_DOWN.toString(),
077: "\u2193"); //$NON-NLS-1$
078: CARBON_KEY_LOOK_UP.put(SpecialKey.ARROW_LEFT.toString(),
079: "\u2190"); //$NON-NLS-1$
080: CARBON_KEY_LOOK_UP.put(SpecialKey.ARROW_RIGHT.toString(),
081: "\u2192"); //$NON-NLS-1$
082: CARBON_KEY_LOOK_UP
083: .put(SpecialKey.ARROW_UP.toString(), "\u2191"); //$NON-NLS-1$
084: CARBON_KEY_LOOK_UP.put(SpecialKey.END.toString(), "\u2198"); //$NON-NLS-1$
085: CARBON_KEY_LOOK_UP.put(SpecialKey.NUMPAD_ENTER.toString(),
086: "\u2324"); //$NON-NLS-1$
087: CARBON_KEY_LOOK_UP.put(SpecialKey.HOME.toString(), "\u2196"); //$NON-NLS-1$
088: CARBON_KEY_LOOK_UP.put(SpecialKey.PAGE_DOWN.toString(),
089: "\u21DF"); //$NON-NLS-1$
090: CARBON_KEY_LOOK_UP.put(SpecialKey.PAGE_UP.toString(), "\u21DE"); //$NON-NLS-1$
091: }
092:
093: /**
094: * Formats an individual key into a human readable format. This uses an
095: * internationalization resource bundle to look up the key. This does the
096: * platform-specific formatting for Carbon.
097: *
098: * @param key
099: * The key to format; must not be <code>null</code>.
100: * @return The key formatted as a string; should not be <code>null</code>.
101: */
102: public String format(Key key) {
103: String name = key.toString();
104:
105: // TODO consider platform-specific resource bundles
106: if ("carbon".equals(SWT.getPlatform())) { //$NON-NLS-1$
107: String formattedName = (String) CARBON_KEY_LOOK_UP
108: .get(name);
109: if (formattedName != null) {
110: return formattedName;
111: }
112: }
113:
114: return super .format(key);
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see org.eclipse.ui.keys.AbstractKeyFormatter#getKeyDelimiter()
121: */
122: protected 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: false, false);
128: } else {
129: return Util.translateString(RESOURCE_BUNDLE,
130: KEY_DELIMITER_KEY, KeyStroke.KEY_DELIMITER, false,
131: false);
132: }
133: }
134:
135: /*
136: * (non-Javadoc)
137: *
138: * @see org.eclipse.ui.keys.AbstractKeyFormatter#getKeyStrokeDelimiter()
139: */
140: protected String getKeyStrokeDelimiter() {
141: // We must do the look up every time, as our locale might change.
142: if ("win32".equals(SWT.getPlatform())) { //$NON-NLS-1$
143: return Util.translateString(RESOURCE_BUNDLE,
144: WIN32_KEY_STROKE_DELIMITER_KEY,
145: KeySequence.KEY_STROKE_DELIMITER, false, false);
146: } else {
147: return Util.translateString(RESOURCE_BUNDLE,
148: KEY_STROKE_DELIMITER_KEY,
149: KeySequence.KEY_STROKE_DELIMITER, false, false);
150: }
151: }
152:
153: /*
154: * (non-Javadoc)
155: *
156: * @see org.eclipse.ui.keys.AbstractKeyFormatter#getModifierKeyComparator()
157: */
158: protected Comparator getModifierKeyComparator() {
159: return MODIFIER_KEY_COMPARATOR;
160: }
161: }
|