001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.Iterator;
014: import java.util.ResourceBundle;
015: import java.util.SortedSet;
016: import java.util.TreeSet;
017:
018: import org.eclipse.ui.internal.util.Util;
019: import org.eclipse.ui.keys.IKeyFormatter;
020: import org.eclipse.ui.keys.Key;
021: import org.eclipse.ui.keys.KeySequence;
022: import org.eclipse.ui.keys.KeyStroke;
023: import org.eclipse.ui.keys.ModifierKey;
024: import org.eclipse.ui.keys.NaturalKey;
025:
026: /**
027: * An abstract implementation of a key formatter that provides a lot of common
028: * key formatting functionality. It is recommended that those people
029: * implementing their own key formatters subclass from here, rather than
030: * implementing <code>KeyFormatter</code> directly.
031: *
032: * @since 3.0
033: */
034: public abstract class AbstractKeyFormatter implements IKeyFormatter {
035:
036: /**
037: * The key for the delimiter between keys. This is used in the
038: * internationalization bundles.
039: */
040: protected final static String KEY_DELIMITER_KEY = "KEY_DELIMITER"; //$NON-NLS-1$
041:
042: /**
043: * The key for the delimiter between key strokes. This is used in the
044: * internationalization bundles.
045: */
046: protected final static String KEY_STROKE_DELIMITER_KEY = "KEY_STROKE_DELIMITER"; //$NON-NLS-1$
047:
048: /**
049: * The bundle in which to look up the internationalized text for all of the
050: * individual keys in the system. This is the platform-agnostic version of
051: * the internationalized strings. Some platforms (namely Carbon) provide
052: * special Unicode characters and glyphs for some keys.
053: */
054: private final static ResourceBundle RESOURCE_BUNDLE = ResourceBundle
055: .getBundle(AbstractKeyFormatter.class.getName());
056:
057: /*
058: * (non-Javadoc)
059: *
060: * @see org.eclipse.ui.keys.KeyFormatter#format(org.eclipse.ui.keys.KeySequence)
061: */
062: public String format(Key key) {
063: String name = key.toString();
064: return Util.translateString(RESOURCE_BUNDLE, name, name, false,
065: false);
066: }
067:
068: /*
069: * (non-Javadoc)
070: *
071: * @see org.eclipse.ui.keys.KeyFormatter#format(org.eclipse.ui.keys.KeySequence)
072: */
073: public String format(KeySequence keySequence) {
074: StringBuffer stringBuffer = new StringBuffer();
075:
076: Iterator keyStrokeItr = keySequence.getKeyStrokes().iterator();
077: while (keyStrokeItr.hasNext()) {
078: stringBuffer
079: .append(format((KeyStroke) keyStrokeItr.next()));
080:
081: if (keyStrokeItr.hasNext()) {
082: stringBuffer.append(getKeyStrokeDelimiter());
083: }
084: }
085:
086: return stringBuffer.toString();
087: }
088:
089: /*
090: * (non-Javadoc)
091: *
092: * @see org.eclipse.ui.keys.KeyFormatter#formatKeyStroke(org.eclipse.ui.keys.KeyStroke)
093: */
094: public String format(KeyStroke keyStroke) {
095: String keyDelimiter = getKeyDelimiter();
096:
097: // Format the modifier keys, in sorted order.
098: SortedSet modifierKeys = new TreeSet(getModifierKeyComparator());
099: modifierKeys.addAll(keyStroke.getModifierKeys());
100: StringBuffer stringBuffer = new StringBuffer();
101: Iterator modifierKeyItr = modifierKeys.iterator();
102: while (modifierKeyItr.hasNext()) {
103: stringBuffer.append(format((ModifierKey) modifierKeyItr
104: .next()));
105: stringBuffer.append(keyDelimiter);
106: }
107:
108: // Format the natural key, if any.
109: NaturalKey naturalKey = keyStroke.getNaturalKey();
110: if (naturalKey != null) {
111: stringBuffer.append(format(naturalKey));
112: }
113:
114: return stringBuffer.toString();
115:
116: }
117:
118: /**
119: * An accessor for the delimiter you wish to use between keys. This is used
120: * by the default format implementations to determine the key delimiter.
121: *
122: * @return The delimiter to use between keys; should not be <code>null</code>.
123: */
124: protected abstract String getKeyDelimiter();
125:
126: /**
127: * An accessor for the delimiter you wish to use between key strokes. This
128: * used by the default format implementations to determine the key stroke
129: * delimiter.
130: *
131: * @return The delimiter to use between key strokes; should not be <code>null</code>.
132: */
133: protected abstract String getKeyStrokeDelimiter();
134:
135: /**
136: * An accessor for the comparator to use for sorting modifier keys. This is
137: * used by the default format implementations to sort the modifier keys
138: * before formatting them into a string.
139: *
140: * @return The comparator to use to sort modifier keys; must not be <code>null</code>.
141: */
142: protected abstract Comparator getModifierKeyComparator();
143:
144: }
|