KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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.Enumeration JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17 import java.util.Set JavaDoc;
18
19 import org.eclipse.jface.bindings.keys.IKeyLookup;
20 import org.eclipse.jface.bindings.keys.KeyLookupFactory;
21 import org.eclipse.jface.bindings.keys.KeySequence;
22 import org.eclipse.jface.bindings.keys.KeyStroke;
23 import org.eclipse.jface.util.Util;
24
25 /**
26  * <p>
27  * An abstract implementation of a key formatter that provides a lot of common
28  * key formatting functionality. It is recommended that implementations of
29  * <code>IKeyFormatter</code> subclass from here, rather than implementing
30  * <code>IKeyFormatter</code> directly.
31  * </p>
32  *
33  * @since 3.1
34  */

35 public abstract class AbstractKeyFormatter implements IKeyFormatter {
36
37     /**
38      * The key for the delimiter between keys. This is used in the
39      * internationalization bundles.
40      */

41     protected static final String JavaDoc KEY_DELIMITER_KEY = "KEY_DELIMITER"; //$NON-NLS-1$
42

43     /**
44      * The key for the delimiter between key strokes. This is used in the
45      * internationalization bundles.
46      */

47     protected static final String JavaDoc KEY_STROKE_DELIMITER_KEY = "KEY_STROKE_DELIMITER"; //$NON-NLS-1$
48

49     /**
50      * An empty integer array that can be used in
51      * <code>sortModifierKeys(int)</code>.
52      */

53     protected static final int[] NO_MODIFIER_KEYS = new int[0];
54
55     /**
56      * The bundle in which to look up the internationalized text for all of the
57      * individual keys in the system. This is the platform-agnostic version of
58      * the internationalized strings. Some platforms (namely Carbon) provide
59      * special Unicode characters and glyphs for some keys.
60      */

61     private static final ResourceBundle JavaDoc RESOURCE_BUNDLE = ResourceBundle
62             .getBundle(AbstractKeyFormatter.class.getName());
63
64     /**
65      * The keys in the resource bundle. This is used to avoid missing resource
66      * exceptions when they aren't necessary.
67      */

68     private static final Set JavaDoc resourceBundleKeys = new HashSet JavaDoc();
69
70     static {
71         final Enumeration JavaDoc keyEnumeration = RESOURCE_BUNDLE.getKeys();
72         while (keyEnumeration.hasMoreElements()) {
73             final Object JavaDoc element = keyEnumeration.nextElement();
74             resourceBundleKeys.add(element);
75         }
76     }
77
78     /*
79      * (non-Javadoc)
80      *
81      * @see org.eclipse.jface.bindings.keysKeyFormatter#format(org.eclipse.jface.bindings.keys.KeySequence)
82      */

83     public String JavaDoc format(final int key) {
84         final IKeyLookup lookup = KeyLookupFactory.getDefault();
85         final String JavaDoc name = lookup.formalNameLookup(key);
86
87         if (resourceBundleKeys.contains(name)) {
88             return Util.translateString(RESOURCE_BUNDLE, name, name);
89         }
90         
91         return name;
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.eclipse.jface.bindings.keys.KeyFormatter#format(org.eclipse.jface.bindings.keys.KeySequence)
98      */

99     public String JavaDoc format(KeySequence keySequence) {
100         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
101
102         final KeyStroke[] keyStrokes = keySequence.getKeyStrokes();
103         final int keyStrokesLength = keyStrokes.length;
104         for (int i = 0; i < keyStrokesLength; i++) {
105             stringBuffer.append(format(keyStrokes[i]));
106
107             if (i + 1 < keyStrokesLength) {
108                 stringBuffer.append(getKeyStrokeDelimiter());
109             }
110         }
111
112         return stringBuffer.toString();
113     }
114
115     /*
116      * (non-Javadoc)
117      *
118      * @see org.eclipse.jface.bindings.keys.KeyFormatter#formatKeyStroke(org.eclipse.jface.bindings.keys.KeyStroke)
119      */

120     public String JavaDoc format(final KeyStroke keyStroke) {
121         final String JavaDoc keyDelimiter = getKeyDelimiter();
122
123         // Format the modifier keys, in sorted order.
124
final int modifierKeys = keyStroke.getModifierKeys();
125         final int[] sortedModifierKeys = sortModifierKeys(modifierKeys);
126         final StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
127         if (sortedModifierKeys != null) {
128             for (int i = 0; i < sortedModifierKeys.length; i++) {
129                 final int modifierKey = sortedModifierKeys[i];
130                 if (modifierKey != KeyStroke.NO_KEY) {
131                     stringBuffer.append(format(modifierKey));
132                     stringBuffer.append(keyDelimiter);
133                 }
134             }
135         }
136
137         // Format the natural key, if any.
138
final int naturalKey = keyStroke.getNaturalKey();
139         if (naturalKey != 0) {
140             stringBuffer.append(format(naturalKey));
141         }
142
143         return stringBuffer.toString();
144
145     }
146
147     /**
148      * An accessor for the delimiter you wish to use between keys. This is used
149      * by the default format implementations to determine the key delimiter.
150      *
151      * @return The delimiter to use between keys; should not be
152      * <code>null</code>.
153      */

154     protected abstract String JavaDoc getKeyDelimiter();
155
156     /**
157      * An accessor for the delimiter you wish to use between key strokes. This
158      * used by the default format implementations to determine the key stroke
159      * delimiter.
160      *
161      * @return The delimiter to use between key strokes; should not be
162      * <code>null</code>.
163      */

164     protected abstract String JavaDoc getKeyStrokeDelimiter();
165
166     /**
167      * Separates the modifier keys from each other, and then places them in an
168      * array in some sorted order. The sort order is dependent on the type of
169      * formatter.
170      *
171      * @param modifierKeys
172      * The modifier keys from the key stroke.
173      * @return An array of modifier key values -- separated and sorted in some
174      * order. Any values in this array that are
175      * <code>KeyStroke.NO_KEY</code> should be ignored.
176      */

177     protected abstract int[] sortModifierKeys(final int modifierKeys);
178 }
179
Popular Tags