KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > keys > AbstractKeyFormatter


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ui.internal.keys;
13
14 import java.util.Comparator JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.ResourceBundle JavaDoc;
17 import java.util.SortedSet JavaDoc;
18 import java.util.TreeSet JavaDoc;
19
20 import org.eclipse.ui.internal.util.Util;
21 import org.eclipse.ui.keys.IKeyFormatter;
22 import org.eclipse.ui.keys.Key;
23 import org.eclipse.ui.keys.KeySequence;
24 import org.eclipse.ui.keys.KeyStroke;
25 import org.eclipse.ui.keys.ModifierKey;
26 import org.eclipse.ui.keys.NaturalKey;
27
28 /**
29  * An abstract implementation of a key formatter that provides a lot of common
30  * key formatting functionality. It is recommended that those people
31  * implementing their own key formatters subclass from here, rather than
32  * implementing <code>KeyFormatter</code> directly.
33  *
34  * @since 3.0
35  */

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

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

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

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

50     /**
51      * The bundle in which to look up the internationalized text for all of the
52      * individual keys in the system. This is the platform-agnostic version of
53      * the internationalized strings. Some platforms (namely Carbon) provide
54      * special Unicode characters and glyphs for some keys.
55      */

56     private final static ResourceBundle JavaDoc RESOURCE_BUNDLE = ResourceBundle
57             .getBundle(AbstractKeyFormatter.class.getName());
58
59     /*
60      * (non-Javadoc)
61      *
62      * @see org.eclipse.ui.keys.KeyFormatter#format(org.eclipse.ui.keys.KeySequence)
63      */

64     public String JavaDoc format(Key key) {
65         String JavaDoc name = key.toString();
66         return Util.translateString(RESOURCE_BUNDLE, name, name, false, false);
67     }
68
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.eclipse.ui.keys.KeyFormatter#format(org.eclipse.ui.keys.KeySequence)
73      */

74     public String JavaDoc format(KeySequence keySequence) {
75         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
76
77         Iterator JavaDoc keyStrokeItr = keySequence.getKeyStrokes().iterator();
78         while (keyStrokeItr.hasNext()) {
79             stringBuffer.append(format((KeyStroke) keyStrokeItr.next()));
80
81             if (keyStrokeItr.hasNext()) {
82                 stringBuffer.append(getKeyStrokeDelimiter());
83             }
84         }
85
86         return stringBuffer.toString();
87     }
88
89     /*
90      * (non-Javadoc)
91      *
92      * @see org.eclipse.ui.keys.KeyFormatter#formatKeyStroke(org.eclipse.ui.keys.KeyStroke)
93      */

94     public String JavaDoc format(KeyStroke keyStroke) {
95         String JavaDoc keyDelimiter = getKeyDelimiter();
96
97         // Format the modifier keys, in sorted order.
98
SortedSet JavaDoc modifierKeys = new TreeSet JavaDoc(getModifierKeyComparator());
99         modifierKeys.addAll(keyStroke.getModifierKeys());
100         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
101         Iterator JavaDoc modifierKeyItr = modifierKeys.iterator();
102         while (modifierKeyItr.hasNext()) {
103             stringBuffer.append(format((ModifierKey) modifierKeyItr.next()));
104             stringBuffer.append(keyDelimiter);
105         }
106
107         // Format the natural key, if any.
108
NaturalKey naturalKey = keyStroke.getNaturalKey();
109         if (naturalKey != null) {
110             stringBuffer.append(format(naturalKey));
111         }
112
113         return stringBuffer.toString();
114
115     }
116
117     /**
118      * An accessor for the delimiter you wish to use between keys. This is used
119      * by the default format implementations to determine the key delimiter.
120      *
121      * @return The delimiter to use between keys; should not be <code>null</code>.
122      */

123     protected abstract String JavaDoc getKeyDelimiter();
124
125     /**
126      * An accessor for the delimiter you wish to use between key strokes. This
127      * used by the default format implementations to determine the key stroke
128      * delimiter.
129      *
130      * @return The delimiter to use between key strokes; should not be <code>null</code>.
131      */

132     protected abstract String JavaDoc getKeyStrokeDelimiter();
133
134     /**
135      * An accessor for the comparator to use for sorting modifier keys. This is
136      * used by the default format implementations to sort the modifier keys
137      * before formatting them into a string.
138      *
139      * @return The comparator to use to sort modifier keys; must not be <code>null</code>.
140      */

141     protected abstract Comparator JavaDoc getModifierKeyComparator();
142
143 }
144
Popular Tags