KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > bindings > keys > KeyStroke


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;
13
14 import java.util.StringTokenizer JavaDoc;
15
16 import org.eclipse.jface.bindings.Trigger;
17 import org.eclipse.jface.bindings.keys.formatting.KeyFormatterFactory;
18 import org.eclipse.jface.util.Util;
19
20 /**
21  * <p>
22  * A <code>KeyStroke</code> is defined as an optional set of modifier keys
23  * followed optionally by a natural key. A <code>KeyStroke</code> is said to
24  * be complete if it contains a natural key. A natural key is any Unicode
25  * character (e.g., "backspace", etc.), any character belonging to a natural
26  * language (e.g., "A", "1", "[", etc.), or any special control character
27  * specific to computers (e.g., "F10", "PageUp", etc.).
28  * </p>
29  * <p>
30  * All <code>KeyStroke</code> objects have a formal string representation
31  * available via the <code>toString()</code> method. There are a number of
32  * methods to get instances of <code>KeyStroke</code> objects, including one
33  * which can parse this formal string representation.
34  * </p>
35  * <p>
36  * All <code>KeyStroke</code> objects, via the <code>format()</code> method,
37  * provide a version of their formal string representation translated by
38  * platform and locale, suitable for display to a user.
39  * </p>
40  * <p>
41  * <code>KeyStroke</code> objects are immutable. Clients are not permitted to
42  * extend this class.
43  * </p>
44  *
45  * @since 3.1
46  */

47 public final class KeyStroke extends Trigger implements Comparable JavaDoc {
48
49     /**
50      * The delimiter between multiple keys in a single key strokes -- expressed
51      * in the formal key stroke grammar. This is not to be displayed to the
52      * user. It is only intended as an internal representation.
53      */

54     public static final String JavaDoc KEY_DELIMITER = "\u002B"; //$NON-NLS-1$
55

56     /**
57      * The set of delimiters for <code>Key</code> objects allowed during
58      * parsing of the formal string representation.
59      */

60     public static final String JavaDoc KEY_DELIMITERS = KEY_DELIMITER;
61
62     /**
63      * The representation for no key.
64      */

65     public static final int NO_KEY = 0;
66
67     /**
68      * Creates an instance of <code>KeyStroke</code> given a natural key.
69      *
70      * @param naturalKey
71      * the natural key. The format of this integer is defined by
72      * whichever widget toolkit you are using; <code>NO_KEY</code>
73      * always means no natural key.
74      * @return a key stroke. This key stroke will have no modifier keys.
75      * Guaranteed not to be <code>null</code>.
76      * @see SWTKeySupport
77      */

78     public static final KeyStroke getInstance(final int naturalKey) {
79         return new KeyStroke(NO_KEY, naturalKey);
80     }
81
82     /**
83      * Creates an instance of <code>KeyStroke</code> given a set of modifier keys
84      * and a natural key.
85      *
86      * @param modifierKeys
87      * the modifier keys. The format of this integer is defined by
88      * whichever widget toolkit you are using; <code>NO_KEY</code>
89      * always means no modifier keys.
90      * @param naturalKey
91      * the natural key. The format of this integer is defined by
92      * whichever widget toolkit you are using; <code>NO_KEY</code>
93      * always means no natural key.
94      * @return a key stroke. Guaranteed not to be <code>null</code>.
95      * @see SWTKeySupport
96      */

97     public static final KeyStroke getInstance(final int modifierKeys,
98             final int naturalKey) {
99         return new KeyStroke(modifierKeys, naturalKey);
100     }
101
102     /**
103      * Creates an instance of <code>KeyStroke</code> by parsing a given a formal
104      * string representation.
105      *
106      * @param string
107      * the formal string representation to parse.
108      * @return a key stroke. Guaranteed not to be <code>null</code>.
109      * @throws ParseException
110      * if the given formal string representation could not be parsed
111      * to a valid key stroke.
112      */

113     public static final KeyStroke getInstance(final String JavaDoc string)
114             throws ParseException {
115         if (string == null) {
116             throw new NullPointerException JavaDoc("Cannot parse a null string"); //$NON-NLS-1$
117
}
118
119         final IKeyLookup lookup = KeyLookupFactory.getDefault();
120         int modifierKeys = NO_KEY;
121         int naturalKey = NO_KEY;
122         final StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(string,
123                 KEY_DELIMITERS, true);
124         int i = 0;
125
126         while (stringTokenizer.hasMoreTokens()) {
127             String JavaDoc token = stringTokenizer.nextToken();
128
129             if (i % 2 == 0) {
130                 if (stringTokenizer.hasMoreTokens()) {
131                     token = token.toUpperCase();
132                     final int modifierKey = lookup.formalModifierLookup(token);
133                     if (modifierKey == NO_KEY) {
134                         throw new ParseException(
135                                 "Cannot create key stroke with duplicate or non-existent modifier key: " //$NON-NLS-1$
136
+ token);
137                     }
138
139                     modifierKeys |= modifierKey;
140
141                 } else if (token.length() == 1) {
142                     naturalKey = token.charAt(0);
143
144                 } else {
145                     token = token.toUpperCase();
146                     naturalKey = lookup.formalKeyLookup(token);
147                 }
148             }
149
150             i++;
151         }
152
153         return new KeyStroke(modifierKeys, naturalKey);
154     }
155
156     /**
157      * An integer representation of the modifier keys; <code>NO_KEY</code>
158      * means that there is no modifier key.
159      */

160     private final int modifierKeys;
161
162     /**
163      * The natural key for this key stroke. This value is <code>NO_KEY</code>
164      * if the key stroke is incomplete (i.e., has no natural key).
165      */

166     private final int naturalKey;
167
168     /**
169      * Constructs an instance of <code>KeyStroke</code> given a set of
170      * modifier keys and a natural key.
171      *
172      * @param modifierKeys
173      * the modifier keys. The format of this integer is defined by
174      * whichever widget toolkit you are using; <code>NO_KEY</code>
175      * always means no modifier keys.
176      * @param naturalKey
177      * the natural key. The format of this integer is defined by
178      * whichever widget toolkit you are using; <code>NO_KEY</code>
179      * always means no natural key.
180      * @see SWTKeySupport
181      */

182     private KeyStroke(final int modifierKeys, final int naturalKey) {
183         this.modifierKeys = modifierKeys;
184         this.naturalKey = naturalKey;
185     }
186
187     /*
188      * (non-Javadoc)
189      *
190      * @see java.lang.Comparable#compareTo(java.lang.Object)
191      */

192     public final int compareTo(final Object JavaDoc object) {
193         final KeyStroke keyStroke = (KeyStroke) object;
194         int compareTo = Util.compare(modifierKeys, keyStroke.modifierKeys);
195
196         if (compareTo == 0) {
197             compareTo = Util.compare(naturalKey, keyStroke.naturalKey);
198         }
199
200         return compareTo;
201     }
202
203     /*
204      * (non-Javadoc)
205      *
206      * @see java.lang.Object#equals(java.lang.Object)
207      */

208     public final boolean equals(final Object JavaDoc object) {
209         if (!(object instanceof KeyStroke)) {
210             return false;
211         }
212
213         final KeyStroke keyStroke = (KeyStroke) object;
214         if (modifierKeys != keyStroke.modifierKeys) {
215             return false;
216         }
217
218         return (naturalKey == keyStroke.naturalKey);
219     }
220
221     /**
222      * Formats this key stroke into the current default look.
223      *
224      * @return A string representation for this key stroke using the default
225      * look; never <code>null</code>.
226      */

227     public final String JavaDoc format() {
228         return KeyFormatterFactory.getDefault().format(this);
229     }
230
231     /**
232      * Returns the modifier keys for this key stroke.
233      *
234      * @return the bit mask of modifier keys; <code>NO_KEY</code> means that
235      * there is no modifier key.
236      */

237     public final int getModifierKeys() {
238         return modifierKeys;
239     }
240
241     /**
242      * Returns the natural key for this key stroke.
243      *
244      * @return The natural key for this key stroke. This value is
245      * <code>NO_KEY</code> if the key stroke is incomplete (i.e., has
246      * no natural key).
247      */

248     public final int getNaturalKey() {
249         return naturalKey;
250     }
251
252     /*
253      * (non-Javadoc)
254      *
255      * @see java.lang.Object#hashCode()
256      */

257     public final int hashCode() {
258         return modifierKeys << 4 + naturalKey;
259     }
260
261     /**
262      * Returns whether or not this key stroke is complete. Key strokes are
263      * complete iff they have a natural key which is not <code>NO_KEY</code>.
264      *
265      * @return <code>true</code>, iff the key stroke is complete.
266      */

267     public final boolean isComplete() {
268         return (naturalKey != NO_KEY);
269     }
270
271     /**
272      * Returns the formal string representation for this key stroke.
273      *
274      * @return The formal string representation for this key stroke. Guaranteed
275      * not to be <code>null</code>.
276      * @see java.lang.Object#toString()
277      */

278     public final String JavaDoc toString() {
279         return KeyFormatterFactory.getFormalKeyFormatter().format(this);
280     }
281 }
282
Popular Tags