KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > keys > KeyStroke


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

53 public final class KeyStroke implements Comparable JavaDoc {
54
55     /**
56      * The delimiter between multiple keys in a single key strokes -- expressed
57      * in the formal key stroke grammar. This is not to be displayed to the
58      * user. It is only intended as an internal representation.
59      */

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

62     /**
63      * An internal constant used only in this object's hash code algorithm.
64      */

65     private final static int HASH_FACTOR = 89;
66
67     /**
68      * An internal constant used only in this object's hash code algorithm.
69      */

70     private final static int HASH_INITIAL = KeyStroke.class.getName()
71             .hashCode();
72
73     /**
74      * The set of delimiters for <code>Key</code> objects allowed during
75      * parsing of the formal string representation.
76      */

77     public final static String JavaDoc KEY_DELIMITERS = KEY_DELIMITER;
78
79     /**
80      * Gets an instance of <code>KeyStroke</code> given a single modifier key
81      * and a natural key.
82      *
83      * @param modifierKey
84      * a modifier key. Must not be <code>null</code>.
85      * @param naturalKey
86      * the natural key. May be <code>null</code>.
87      * @return a key stroke. Guaranteed not to be <code>null</code>.
88      */

89     public static KeyStroke getInstance(ModifierKey modifierKey,
90             NaturalKey naturalKey) {
91         if (modifierKey == null) {
92             throw new NullPointerException JavaDoc();
93         }
94
95         return new KeyStroke(
96                 new TreeSet JavaDoc(Collections.singletonList(modifierKey)), naturalKey);
97     }
98
99     /**
100      * Gets an instance of <code>KeyStroke</code> given an array of modifier
101      * keys and a natural key.
102      *
103      * @param modifierKeys
104      * the array of modifier keys. This array may be empty, but it
105      * must not be <code>null</code>. If this array is not empty,
106      * it must not contain <code>null</code> elements.
107      * @param naturalKey
108      * the natural key. May be <code>null</code>.
109      * @return a key stroke. Guaranteed not to be <code>null</code>.
110      */

111     public static KeyStroke getInstance(ModifierKey[] modifierKeys,
112             NaturalKey naturalKey) {
113         Util.assertInstance(modifierKeys, ModifierKey.class);
114         return new KeyStroke(new TreeSet JavaDoc(Arrays.asList(modifierKeys)),
115                 naturalKey);
116     }
117
118     /**
119      * Gets an instance of <code>KeyStroke</code> given a natural key.
120      *
121      * @param naturalKey
122      * the natural key. May be <code>null</code>.
123      * @return a key stroke. This key stroke will have no modifier keys.
124      * Guaranteed not to be <code>null</code>.
125      */

126     public static KeyStroke getInstance(NaturalKey naturalKey) {
127         return new KeyStroke(Util.EMPTY_SORTED_SET, naturalKey);
128     }
129
130     /**
131      * Gets an instance of <code>KeyStroke</code> given a set of modifier
132      * keys and a natural key.
133      *
134      * @param modifierKeys
135      * the set of modifier keys. This set may be empty, but it must
136      * not be <code>null</code>. If this set is not empty, it
137      * must only contain instances of <code>ModifierKey</code>.
138      * @param naturalKey
139      * the natural key. May be <code>null</code>.
140      * @return a key stroke. Guaranteed not to be <code>null</code>.
141      */

142     public static KeyStroke getInstance(SortedSet JavaDoc modifierKeys,
143             NaturalKey naturalKey) {
144         return new KeyStroke(modifierKeys, naturalKey);
145     }
146
147     /**
148      * Gets an instance of <code>KeyStroke</code> by parsing a given a formal
149      * string representation.
150      *
151      * @param string
152      * the formal string representation to parse.
153      * @return a key stroke. Guaranteed not to be <code>null</code>.
154      * @throws ParseException
155      * if the given formal string representation could not be
156      * parsed to a valid key stroke.
157      */

158     public static KeyStroke getInstance(String JavaDoc string) throws ParseException {
159         if (string == null) {
160             throw new NullPointerException JavaDoc();
161         }
162
163         SortedSet JavaDoc modifierKeys = new TreeSet JavaDoc();
164         NaturalKey naturalKey = null;
165         StringTokenizer JavaDoc stringTokenizer = new StringTokenizer JavaDoc(string,
166                 KEY_DELIMITERS, true);
167         int i = 0;
168
169         while (stringTokenizer.hasMoreTokens()) {
170             String JavaDoc token = stringTokenizer.nextToken();
171
172             if (i % 2 == 0) {
173                 if (stringTokenizer.hasMoreTokens()) {
174                     token = token.toUpperCase();
175                     ModifierKey modifierKey = (ModifierKey) ModifierKey.modifierKeysByName
176                             .get(token);
177
178                     if (modifierKey == null || !modifierKeys.add(modifierKey)) {
179                         throw new ParseException(
180                                 "Cannot create key stroke with duplicate or non-existent modifier key: " //$NON-NLS-1$
181
+ token);
182                     }
183                 } else if (token.length() == 1) {
184                     naturalKey = CharacterKey.getInstance(token.charAt(0));
185                     break;
186                 } else {
187                     token = token.toUpperCase();
188                     naturalKey = (NaturalKey) CharacterKey.characterKeysByName
189                             .get(token);
190
191                     if (naturalKey == null) {
192                         naturalKey = (NaturalKey) SpecialKey.specialKeysByName
193                                 .get(token);
194                     }
195
196                     if (naturalKey == null) {
197                         throw new ParseException(
198                                 "Cannot create key stroke with invalid natural key: " //$NON-NLS-1$
199
+ token);
200                     }
201                 }
202             }
203
204             i++;
205         }
206
207         try {
208             return new KeyStroke(modifierKeys, naturalKey);
209         } catch (Throwable JavaDoc t) {
210             throw new ParseException("Cannot create key stroke with " //$NON-NLS-1$
211
+ modifierKeys + " and " + naturalKey); //$NON-NLS-1$
212
}
213     }
214
215     /**
216      * The cached hash code for this object. Because <code>KeyStroke</code>
217      * objects are immutable, their hash codes need only to be computed once.
218      * After the first call to <code>hashCode()</code>, the computed value
219      * is cached here for all subsequent calls.
220      */

221     private transient int hashCode;
222
223     /**
224      * A flag to determine if the <code>hashCode</code> field has already
225      * been computed.
226      */

227     private transient boolean hashCodeComputed;
228
229     /**
230      * The set of modifier keys for this key stroke.
231      */

232     private SortedSet JavaDoc modifierKeys;
233
234     /**
235      * The set of modifier keys for this key stroke in the form of an array.
236      * Used internally by <code>int compareTo(Object)</code>.
237      */

238     private transient ModifierKey[] modifierKeysAsArray;
239
240     /**
241      * The natural key for this key stroke.
242      */

243     private NaturalKey naturalKey;
244
245     /**
246      * Constructs an instance of <code>KeyStroke</code> given a set of
247      * modifier keys and a natural key.
248      *
249      * @param modifierKeys
250      * the set of modifier keys. This set may be empty, but it must
251      * not be <code>null</code>. If this set is not empty, it
252      * must only contain instances of <code>ModifierKey</code>.
253      * @param naturalKey
254      * the natural key. May be <code>null</code>.
255      */

256     private KeyStroke(SortedSet JavaDoc modifierKeys, NaturalKey naturalKey) {
257         this.modifierKeys = Util.safeCopy(modifierKeys, ModifierKey.class);
258         this.naturalKey = naturalKey;
259         this.modifierKeysAsArray = (ModifierKey[]) this.modifierKeys
260                 .toArray(new ModifierKey[this.modifierKeys.size()]);
261     }
262
263     /**
264      * @see java.lang.Comparable#compareTo(java.lang.Object)
265      */

266     public int compareTo(Object JavaDoc object) {
267         KeyStroke castedObject = (KeyStroke) object;
268         int compareTo = Util.compare(modifierKeysAsArray,
269                 castedObject.modifierKeysAsArray);
270
271         if (compareTo == 0) {
272             compareTo = Util.compare(naturalKey, castedObject.naturalKey);
273         }
274
275         return compareTo;
276     }
277
278     /**
279      * @see java.lang.Object#equals(java.lang.Object)
280      */

281     public boolean equals(Object JavaDoc object) {
282         if (!(object instanceof KeyStroke)) {
283             return false;
284         }
285
286         KeyStroke castedObject = (KeyStroke) object;
287         
288         if (!modifierKeys.equals(castedObject.modifierKeys)) {
289             return false;
290         }
291         return Util.equals(naturalKey, castedObject.naturalKey);
292     }
293
294     /**
295      * Formats this key stroke into the current default look.
296      *
297      * @return A string representation for this key stroke using the default
298      * look; never <code>null</code>.
299      */

300     public String JavaDoc format() {
301         return KeyFormatterFactory.getDefault().format(this);
302     }
303
304     /**
305      * Returns the set of modifier keys for this key stroke.
306      *
307      * @return the set of modifier keys. This set may be empty, but is
308      * guaranteed not to be <code>null</code>. If this set is not
309      * empty, it is guaranteed to only contain instances of <code>ModifierKey</code>.
310      */

311     public Set JavaDoc getModifierKeys() {
312         return Collections.unmodifiableSet(modifierKeys);
313     }
314
315     /**
316      * Returns the natural key for this key stroke.
317      *
318      * @return the natural key. May be <code>null</code>.
319      */

320     public NaturalKey getNaturalKey() {
321         return naturalKey;
322     }
323
324     /**
325      * @see java.lang.Object#hashCode()
326      */

327     public int hashCode() {
328         if (!hashCodeComputed) {
329             hashCode = HASH_INITIAL;
330             hashCode = hashCode * HASH_FACTOR + modifierKeys.hashCode();
331             hashCode = hashCode * HASH_FACTOR + Util.hashCode(naturalKey);
332             hashCodeComputed = true;
333         }
334
335         return hashCode;
336     }
337
338     /**
339      * Returns whether or not this key stroke is complete. Key strokes are
340      * complete iff they have a natural key which is not <code>null</code>.
341      *
342      * @return <code>true</code>, iff the key stroke is complete.
343      */

344     public boolean isComplete() {
345         return naturalKey != null;
346     }
347
348     /**
349      * Returns the formal string representation for this key stroke.
350      *
351      * @return The formal string representation for this key stroke. Guaranteed
352      * not to be <code>null</code>.
353      * @see java.lang.Object#toString()
354      */

355     public String JavaDoc toString() {
356         return KeyFormatterFactory.getFormalKeyFormatter().format(this);
357     }
358 }
359
Popular Tags