• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 /**
18  * @author Dmitry A. Durnev
19  * @version $Revision$
20  */
21 
22 package java.awt;
23 
24 import java.awt.event.InputEvent;
25 import java.awt.event.KeyEvent;
26 import java.io.ObjectStreamException;
27 import java.io.Serializable;
28 import java.lang.reflect.Constructor;
29 import java.lang.reflect.Field;
30 import java.util.HashMap;
31 import java.util.Map;
32 import java.util.NoSuchElementException;
33 import java.util.StringTokenizer;
34 
35 import org.apache.harmony.awt.internal.nls.Messages;
36 
37 /**
38  * The AWTKeyStroke holds all of the information for the complete act of
39  * typing a character. This includes the events that are generated when
40  * the key is pressed, released, or typed (pressed and released generating
41  * a Unicode character result) which are associated with the event
42  * objects KeyEvent.KEY_PRESSED, KeyEvent.KEY_RELEASED, or KeyEvent.KEY_TYPED.
43  * It also holds information about which modifiers (such as control or
44  * shift) were used in conjunction with the keystroke. The following masks
45  * are available to identify the modifiers:
46  * <ul>
47  * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK</li>
48  * <li>java.awt.event.InputEvent.ALT_DOWN_MASK</li>
49  * <li>java.awt.event.InputEvent.CTRL_DOWN_MASK</li>
50  * <li>java.awt.event.InputEvent.META_DOWN_MASK</li>
51  * <li>java.awt.event.InputEvent.SHIFT_DOWN_MASK</li>
52  * <li>java.awt.event.InputEvent.ALT_GRAPH_MASK</li>
53  * <li>java.awt.event.InputEvent.ALT_MASK</li>
54  * <li>java.awt.event.InputEvent.CTRL_MASK</li>
55  * <li>java.awt.event.InputEvent.META_MASK</li>
56  * <li>java.awt.event.InputEvent.SHIFT_MASK</li>
57  * </ul>
58  * <br>
59  *  The AWTKeyStroke is unique, and applications should not create their own
60  *  instances of AWTKeyStroke. All applications should use getAWTKeyStroke
61  *  methods for obtaining instances of AWTKeyStroke.
62  *
63  *  @since Android 1.0
64  */
65 public class AWTKeyStroke implements Serializable {
66 
67     /**
68      * The Constant serialVersionUID.
69      */
70     private static final long serialVersionUID = -6430539691155161871L;
71 
72     /**
73      * The Constant cache.
74      */
75     private static final Map<AWTKeyStroke, AWTKeyStroke> cache = new HashMap<AWTKeyStroke, AWTKeyStroke>(); // Map
76 
77     // <
78     // AWTKeyStroke
79     // ,
80     // ?
81     // extends
82     // AWTKeyStroke
83     // >
84 
85     /**
86      * The Constant keyEventTypesMap.
87      */
88     private static final Map<Integer, String> keyEventTypesMap = new HashMap<Integer, String>(); // Map
89 
90     // <
91     // int
92     // ,
93     // String
94     // >
95 
96     private static Constructor<?> subConstructor;
97 
98     static {
keyEventTypesMap.put(new Integer(KeyEvent.KEY_PRESSED), "pressed")99         keyEventTypesMap.put(new Integer(KeyEvent.KEY_PRESSED), "pressed"); //$NON-NLS-1$
keyEventTypesMap.put(new Integer(KeyEvent.KEY_RELEASED), "released")100         keyEventTypesMap.put(new Integer(KeyEvent.KEY_RELEASED), "released"); //$NON-NLS-1$
keyEventTypesMap.put(new Integer(KeyEvent.KEY_TYPED), "typed")101         keyEventTypesMap.put(new Integer(KeyEvent.KEY_TYPED), "typed"); //$NON-NLS-1$
102     }
103 
104     /**
105      * The key char.
106      */
107     private char keyChar;
108 
109     /**
110      * The key code.
111      */
112     private int keyCode;
113 
114     /**
115      * The modifiers.
116      */
117     private int modifiers;
118 
119     /**
120      * The on key release.
121      */
122     private boolean onKeyRelease;
123 
124     /**
125      * Instantiates a new AWTKeyStroke. getAWTKeyStroke method should be used by
126      * applications code.
127      *
128      * @param keyChar
129      *            the key char.
130      * @param keyCode
131      *            the key code.
132      * @param modifiers
133      *            the modifiers.
134      * @param onKeyRelease
135      *            true if AWTKeyStroke is for a key release, false otherwise.
136      */
AWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease)137     protected AWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease) {
138         setAWTKeyStroke(keyChar, keyCode, modifiers, onKeyRelease);
139     }
140 
141     /**
142      * Sets the AWT key stroke.
143      *
144      * @param keyChar
145      *            the key char.
146      * @param keyCode
147      *            the key code.
148      * @param modifiers
149      *            the modifiers.
150      * @param onKeyRelease
151      *            the on key release.
152      */
setAWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease)153     private void setAWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease) {
154         this.keyChar = keyChar;
155         this.keyCode = keyCode;
156         this.modifiers = modifiers;
157         this.onKeyRelease = onKeyRelease;
158     }
159 
160     /**
161      * Instantiates a new AWTKeyStroke with default parameters:
162      * KeyEvent.CHAR_UNDEFINED key char, KeyEvent.VK_UNDEFINED key code, without
163      * modifiers and false key realized value.
164      */
AWTKeyStroke()165     protected AWTKeyStroke() {
166         this(KeyEvent.CHAR_UNDEFINED, KeyEvent.VK_UNDEFINED, 0, false);
167     }
168 
169     /**
170      * Returns the unique number value for AWTKeyStroke object.
171      *
172      * @return the integer unique value of the AWTKeyStroke object.
173      */
174     @Override
hashCode()175     public int hashCode() {
176         return modifiers + (keyCode != KeyEvent.VK_UNDEFINED ? keyCode : keyChar)
177                 + (onKeyRelease ? -1 : 0);
178     }
179 
180     /**
181      * Gets the set of modifiers for the AWTKeyStroke object.
182      *
183      * @return the integer value which contains modifiers.
184      */
getModifiers()185     public final int getModifiers() {
186         return modifiers;
187     }
188 
189     /**
190      * Compares this AWTKeyStroke object to the specified object.
191      *
192      * @param anObject
193      *            the specified AWTKeyStroke object to compare with this
194      *            instance.
195      * @return true if objects are identical, false otherwise.
196      */
197     @Override
equals(Object anObject)198     public final boolean equals(Object anObject) {
199         if (anObject instanceof AWTKeyStroke) {
200             AWTKeyStroke key = (AWTKeyStroke)anObject;
201             return ((key.keyCode == keyCode) && (key.keyChar == keyChar)
202                     && (key.modifiers == modifiers) && (key.onKeyRelease == onKeyRelease));
203         }
204         return false;
205     }
206 
207     /**
208      * Returns the string representation of the AWTKeyStroke. This string should
209      * contain key stroke properties.
210      *
211      * @return the string representation of the AWTKeyStroke.
212      */
213     @Override
toString()214     public String toString() {
215         int type = getKeyEventType();
216         return InputEvent.getModifiersExText(getModifiers()) + " " + //$NON-NLS-1$
217                 keyEventTypesMap.get(new Integer(type)) + " " + //$NON-NLS-1$
218                 (type == KeyEvent.KEY_TYPED ? new String(new char[] {
219                     keyChar
220                 }) : KeyEvent.getKeyText(keyCode));
221     }
222 
223     /**
224      * Gets the key code for the AWTKeyStroke object.
225      *
226      * @return the key code for the AWTKeyStroke object.
227      */
getKeyCode()228     public final int getKeyCode() {
229         return keyCode;
230     }
231 
232     /**
233      * Gets the key character for the AWTKeyStroke object.
234      *
235      * @return the key character for the AWTKeyStroke object.
236      */
getKeyChar()237     public final char getKeyChar() {
238         return keyChar;
239     }
240 
241     /**
242      * Gets the AWT key stroke.
243      *
244      * @param keyChar
245      *            the key char.
246      * @param keyCode
247      *            the key code.
248      * @param modifiers
249      *            the modifiers.
250      * @param onKeyRelease
251      *            the on key release.
252      * @return the AWT key stroke.
253      */
getAWTKeyStroke(char keyChar, int keyCode, int modifiers, boolean onKeyRelease)254     private static AWTKeyStroke getAWTKeyStroke(char keyChar, int keyCode, int modifiers,
255             boolean onKeyRelease) {
256         AWTKeyStroke key = newInstance(keyChar, keyCode, modifiers, onKeyRelease);
257 
258         AWTKeyStroke value = cache.get(key);
259         if (value == null) {
260             value = key;
261             cache.put(key, value);
262         }
263         return value;
264     }
265 
266     /**
267      * New instance.
268      *
269      * @param keyChar
270      *            the key char.
271      * @param keyCode
272      *            the key code.
273      * @param modifiers
274      *            the modifiers.
275      * @param onKeyRelease
276      *            the on key release.
277      * @return the AWT key stroke.
278      */
newInstance(char keyChar, int keyCode, int modifiers, boolean onKeyRelease)279     private static AWTKeyStroke newInstance(char keyChar, int keyCode, int modifiers,
280             boolean onKeyRelease) {
281         AWTKeyStroke key;
282         // ???AWT
283         // if (subConstructor == null) {
284         key = new AWTKeyStroke();
285         // ???AWT
286         // } else {
287         // try {
288         // key = (AWTKeyStroke) subConstructor.newInstance();
289         // } catch (Exception e) {
290         // throw new RuntimeException(e);
291         // }
292         // }
293         int allModifiers = getAllModifiers(modifiers);
294         key.setAWTKeyStroke(keyChar, keyCode, allModifiers, onKeyRelease);
295         return key;
296     }
297 
298     /**
299      * Adds the mask.
300      *
301      * @param mod
302      *            the mod.
303      * @param mask
304      *            the mask.
305      * @return the int.
306      */
addMask(int mod, int mask)307     private static int addMask(int mod, int mask) {
308         return ((mod & mask) != 0) ? (mod | mask) : mod;
309     }
310 
311     /**
312      * Return all (old & new) modifiers corresponding to.
313      *
314      * @param mod
315      *            old or new modifiers.
316      * @return old and new modifiers together.
317      */
getAllModifiers(int mod)318     static int getAllModifiers(int mod) {
319         int allMod = mod;
320         int shift = (InputEvent.SHIFT_MASK | InputEvent.SHIFT_DOWN_MASK);
321         int ctrl = (InputEvent.CTRL_MASK | InputEvent.CTRL_DOWN_MASK);
322         int meta = (InputEvent.META_MASK | InputEvent.META_DOWN_MASK);
323         int alt = (InputEvent.ALT_MASK | InputEvent.ALT_DOWN_MASK);
324         int altGr = (InputEvent.ALT_GRAPH_MASK | InputEvent.ALT_GRAPH_DOWN_MASK);
325         // button modifiers are not converted between old & new
326 
327         allMod = addMask(allMod, shift);
328         allMod = addMask(allMod, ctrl);
329         allMod = addMask(allMod, meta);
330         allMod = addMask(allMod, alt);
331         allMod = addMask(allMod, altGr);
332 
333         return allMod;
334     }
335 
336     /**
337      * Returns an instance of AWTKeyStroke for parsed string. The string must
338      * have the following syntax:
339      *<p>
340      * &lt;modifiers&gt;* (&lt;typedID&gt; | &lt;pressedReleasedID&gt;)
341      *<p>
342      * modifiers := shift | control | ctrl | meta | alt | altGraph <br>
343      * typedID := typed <typedKey> <br>
344      * typedKey := string of length 1 giving the Unicode character. <br>
345      * pressedReleasedID := (pressed | released) <key> <br>
346      * key := KeyEvent key code name, i.e. the name following "VK_".
347      * <p>
348      *
349      * @param s
350      *            the String which contains key stroke parameters.
351      * @return the AWTKeyStroke for string.
352      * @throws IllegalArgumentException
353      *             if string has incorrect format or null.
354      */
getAWTKeyStroke(String s)355     public static AWTKeyStroke getAWTKeyStroke(String s) {
356         if (s == null) {
357             // awt.65=null argument
358             throw new IllegalArgumentException(Messages.getString("awt.65")); //$NON-NLS-1$
359         }
360 
361         StringTokenizer tokenizer = new StringTokenizer(s);
362 
363         Boolean release = null;
364         int modifiers = 0;
365         int keyCode = KeyEvent.VK_UNDEFINED;
366         char keyChar = KeyEvent.CHAR_UNDEFINED;
367         boolean typed = false;
368         long modifier = 0;
369         String token = null;
370         do {
371             token = getNextToken(tokenizer);
372             modifier = parseModifier(token);
373             modifiers |= modifier;
374         } while (modifier > 0);
375 
376         typed = parseTypedID(token);
377 
378         if (typed) {
379             token = getNextToken(tokenizer);
380             keyChar = parseTypedKey(token);
381 
382         }
383         if (keyChar == KeyEvent.CHAR_UNDEFINED) {
384             release = parsePressedReleasedID(token);
385             if (release != null) {
386                 token = getNextToken(tokenizer);
387             }
388             keyCode = parseKey(token);
389         }
390         if (tokenizer.hasMoreTokens()) {
391             // awt.66=Invalid format
392             throw new IllegalArgumentException(Messages.getString("awt.66")); //$NON-NLS-1$
393         }
394 
395         return getAWTKeyStroke(keyChar, keyCode, modifiers, release == Boolean.TRUE);
396     }
397 
398     /**
399      * Gets the next token.
400      *
401      * @param tokenizer
402      *            the tokenizer.
403      * @return the next token.
404      */
getNextToken(StringTokenizer tokenizer)405     private static String getNextToken(StringTokenizer tokenizer) {
406         try {
407             return tokenizer.nextToken();
408         } catch (NoSuchElementException exception) {
409             // awt.66=Invalid format
410             throw new IllegalArgumentException(Messages.getString("awt.66")); //$NON-NLS-1$
411         }
412     }
413 
414     /**
415      * Gets the key code.
416      *
417      * @param s
418      *            the s.
419      * @return the key code.
420      */
getKeyCode(String s)421     static int getKeyCode(String s) {
422         try {
423             Field vk = KeyEvent.class.getField("VK_" + s); //$NON-NLS-1$
424             return vk.getInt(null);
425         } catch (Exception e) {
426             if (s.length() != 1) {
427                 // awt.66=Invalid format
428                 throw new IllegalArgumentException(Messages.getString("awt.66")); //$NON-NLS-1$
429             }
430             return KeyEvent.VK_UNDEFINED;
431         }
432     }
433 
434     /**
435      * Gets an instance of the AWTKeyStroke for specified character.
436      *
437      * @param keyChar
438      *            the keyboard character value.
439      * @return a AWTKeyStroke for specified character.
440      */
getAWTKeyStroke(char keyChar)441     public static AWTKeyStroke getAWTKeyStroke(char keyChar) {
442         return getAWTKeyStroke(keyChar, KeyEvent.VK_UNDEFINED, 0, false);
443     }
444 
445     /**
446      * Returns an instance of AWTKeyStroke for a given key code, set of
447      * modifiers, and specified key released flag value. The key codes are
448      * defined in java.awt.event.KeyEvent class. The set of modifiers is given
449      * as a bitwise combination of masks taken from the following list:
450      * <ul>
451      * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK</li> <li>
452      * java.awt.event.InputEvent.ALT_DOWN_MASK</li> <li>
453      * java.awt.event.InputEvent.CTRL_DOWN_MASK</li> <li>
454      * java.awt.event.InputEvent.META_DOWN_MASK</li> <li>
455      * java.awt.event.InputEvent.SHIFT_DOWN_MASK</li> <li>
456      * java.awt.event.InputEvent.ALT_GRAPH_MASK</li> <li>
457      * java.awt.event.InputEvent.ALT_MASK</li> <li>
458      * java.awt.event.InputEvent.CTRL_MASK</li> <li>
459      * java.awt.event.InputEvent.META_MASK</li> <li>
460      * java.awt.event.InputEvent.SHIFT_MASK</li>
461      * </ul>
462      * <br>
463      *
464      * @param keyCode
465      *            the specified key code of keyboard.
466      * @param modifiers
467      *            the bit set of modifiers.
468      * @param onKeyRelease
469      *            the value which represents whether this AWTKeyStroke shall
470      *            represents a key release.
471      * @return the AWTKeyStroke.
472      */
getAWTKeyStroke(int keyCode, int modifiers, boolean onKeyRelease)473     public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers, boolean onKeyRelease) {
474         return getAWTKeyStroke(KeyEvent.CHAR_UNDEFINED, keyCode, modifiers, onKeyRelease);
475     }
476 
477     /**
478      * Returns AWTKeyStroke for a specified character and set of modifiers. The
479      * set of modifiers is given as a bitwise combination of masks taken from
480      * the following list:
481      * <ul>
482      * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK</li> <li>
483      * java.awt.event.InputEvent.ALT_DOWN_MASK</li> <li>
484      * java.awt.event.InputEvent.CTRL_DOWN_MASK</li> <li>
485      * java.awt.event.InputEvent.META_DOWN_MASK</li> <li>
486      * java.awt.event.InputEvent.SHIFT_DOWN_MASK</li> <li>
487      * java.awt.event.InputEvent.ALT_GRAPH_MASK</li> <li>
488      * java.awt.event.InputEvent.ALT_MASK</li> <li>
489      * java.awt.event.InputEvent.CTRL_MASK</li> <li>
490      * java.awt.event.InputEvent.META_MASK</li> <li>
491      * java.awt.event.InputEvent.SHIFT_MASK</li>
492      * </ul>
493      *
494      * @param keyChar
495      *            the Character object which represents keyboard character
496      *            value.
497      * @param modifiers
498      *            the bit set of modifiers.
499      * @return the AWTKeyStroke object.
500      * @throws IllegalArgumentException
501      *             if keyChar value is null.
502      */
getAWTKeyStroke(Character keyChar, int modifiers)503     public static AWTKeyStroke getAWTKeyStroke(Character keyChar, int modifiers) {
504         if (keyChar == null) {
505             // awt.01='{0}' parameter is null
506             throw new IllegalArgumentException(Messages.getString("awt.01", "keyChar")); //$NON-NLS-1$ //$NON-NLS-2$
507         }
508         return getAWTKeyStroke(keyChar.charValue(), KeyEvent.VK_UNDEFINED, modifiers, false);
509     }
510 
511     /**
512      * Returns an instance of AWTKeyStroke for a specified key code and set of
513      * modifiers. The key codes are defined in java.awt.event.KeyEvent class.
514      * The set of modifiers is given as a bitwise combination of masks taken
515      * from the following list:
516      * <ul>
517      * <li>java.awt.event.InputEvent.ALT_GRAPH_DOWN_MASK</li> <li>
518      * java.awt.event.InputEvent.ALT_DOWN_MASK</li> <li>
519      * java.awt.event.InputEvent.CTRL_DOWN_MASK</li> <li>
520      * java.awt.event.InputEvent.META_DOWN_MASK</li> <li>
521      * java.awt.event.InputEvent.SHIFT_DOWN_MASK</li> <li>
522      * java.awt.event.InputEvent.ALT_GRAPH_MASK</li> <li>
523      * java.awt.event.InputEvent.ALT_MASK</li> <li>
524      * java.awt.event.InputEvent.CTRL_MASK</li> <li>
525      * java.awt.event.InputEvent.META_MASK</li> <li>
526      * java.awt.event.InputEvent.SHIFT_MASK</li>
527      * </ul>
528      *
529      * @param keyCode
530      *            the specified key code of keyboard.
531      * @param modifiers
532      *            the bit set of modifiers.
533      * @return the AWTKeyStroke.
534      */
getAWTKeyStroke(int keyCode, int modifiers)535     public static AWTKeyStroke getAWTKeyStroke(int keyCode, int modifiers) {
536         return getAWTKeyStroke(keyCode, modifiers, false);
537     }
538 
539     /**
540      * Gets the AWTKeyStroke for a key event. This method obtains the key char
541      * and key code from the specified key event.
542      *
543      * @param anEvent
544      *            the key event which identifies the desired AWTKeyStroke.
545      * @return the AWTKeyStroke for the key event.
546      */
getAWTKeyStrokeForEvent(KeyEvent anEvent)547     public static AWTKeyStroke getAWTKeyStrokeForEvent(KeyEvent anEvent) {
548         int id = anEvent.getID();
549         char undef = KeyEvent.CHAR_UNDEFINED;
550         char keyChar = (id == KeyEvent.KEY_TYPED ? anEvent.getKeyChar() : undef);
551         int keyCode = (keyChar == undef ? anEvent.getKeyCode() : KeyEvent.VK_UNDEFINED);
552         return getAWTKeyStroke(keyChar, keyCode, anEvent.getModifiersEx(),
553                 id == KeyEvent.KEY_RELEASED);
554     }
555 
556     /**
557      * Gets the key event type for the AWTKeyStroke object.
558      *
559      * @return the key event type: KeyEvent.KEY_PRESSED, KeyEvent.KEY_TYPED, or
560      *         KeyEvent.KEY_RELEASED.
561      */
getKeyEventType()562     public final int getKeyEventType() {
563         if (keyCode == KeyEvent.VK_UNDEFINED) {
564             return KeyEvent.KEY_TYPED;
565         }
566         return (onKeyRelease ? KeyEvent.KEY_RELEASED : KeyEvent.KEY_PRESSED);
567     }
568 
569     /**
570      * Returns true if the key event is associated with the AWTKeyStroke is
571      * KEY_RELEASED, false otherwise.
572      *
573      * @return true, if if the key event associated with the AWTKeyStroke is
574      *         KEY_RELEASED, false otherwise.
575      */
isOnKeyRelease()576     public final boolean isOnKeyRelease() {
577         return onKeyRelease;
578     }
579 
580     /**
581      * Read resolve.
582      *
583      * @return the object.
584      * @throws ObjectStreamException
585      *             the object stream exception.
586      */
readResolve()587     protected Object readResolve() throws ObjectStreamException {
588         return getAWTKeyStroke(this.keyChar, this.keyCode, this.modifiers, this.onKeyRelease);
589     }
590 
591     /**
592      * Register subclass.
593      *
594      * @param subclass
595      *            the subclass.
596      */
registerSubclass(Class<?> subclass)597     protected static void registerSubclass(Class<?> subclass) {
598         // ???AWT
599         /*
600          * if (subclass == null) { // awt.01='{0}' parameter is null throw new
601          * IllegalArgumentException(Messages.getString("awt.01", "subclass"));
602          * //$NON-NLS-1$ //$NON-NLS-2$ } if (!
603          * AWTKeyStroke.class.isAssignableFrom(subclass)) { // awt.67=subclass
604          * is not derived from AWTKeyStroke throw new
605          * ClassCastException(Messages.getString("awt.67")); //$NON-NLS-1$ } try
606          * { subConstructor = subclass.getDeclaredConstructor();
607          * subConstructor.setAccessible(true); } catch (SecurityException e) {
608          * throw new RuntimeException(e); } catch (NoSuchMethodException e) { //
609          * awt.68=subclass could not be instantiated throw new
610          * IllegalArgumentException(Messages.getString("awt.68")); //$NON-NLS-1$
611          * } cache.clear(); //flush the cache
612          */
613     }
614 
615     /**
616      * Parses the modifier.
617      *
618      * @param strMod
619      *            the str mod.
620      * @return the long.
621      */
parseModifier(String strMod)622     private static long parseModifier(String strMod) {
623         long modifiers = 0l;
624         if (strMod.equals("shift")) { //$NON-NLS-1$
625             modifiers |= InputEvent.SHIFT_DOWN_MASK;
626         } else if (strMod.equals("control") || strMod.equals("ctrl")) { //$NON-NLS-1$ //$NON-NLS-2$
627             modifiers |= InputEvent.CTRL_DOWN_MASK;
628         } else if (strMod.equals("meta")) { //$NON-NLS-1$
629             modifiers |= InputEvent.META_DOWN_MASK;
630         } else if (strMod.equals("alt")) { //$NON-NLS-1$
631             modifiers |= InputEvent.ALT_DOWN_MASK;
632         } else if (strMod.equals("altGraph")) { //$NON-NLS-1$
633             modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK;
634         } else if (strMod.equals("button1")) { //$NON-NLS-1$
635             modifiers |= InputEvent.BUTTON1_DOWN_MASK;
636         } else if (strMod.equals("button2")) { //$NON-NLS-1$
637             modifiers |= InputEvent.BUTTON2_DOWN_MASK;
638         } else if (strMod.equals("button3")) { //$NON-NLS-1$
639             modifiers |= InputEvent.BUTTON3_DOWN_MASK;
640         }
641         return modifiers;
642     }
643 
644     /**
645      * Parses the typed id.
646      *
647      * @param strTyped
648      *            the str typed.
649      * @return true, if successful.
650      */
parseTypedID(String strTyped)651     private static boolean parseTypedID(String strTyped) {
652         if (strTyped.equals("typed")) { //$NON-NLS-1$
653             return true;
654         }
655 
656         return false;
657     }
658 
659     /**
660      * Parses the typed key.
661      *
662      * @param strChar
663      *            the str char.
664      * @return the char.
665      */
parseTypedKey(String strChar)666     private static char parseTypedKey(String strChar) {
667         char keyChar = KeyEvent.CHAR_UNDEFINED;
668 
669         if (strChar.length() != 1) {
670             // awt.66=Invalid format
671             throw new IllegalArgumentException(Messages.getString("awt.66")); //$NON-NLS-1$
672         }
673         keyChar = strChar.charAt(0);
674         return keyChar;
675     }
676 
677     /**
678      * Parses the pressed released id.
679      *
680      * @param str
681      *            the str.
682      * @return the boolean.
683      */
parsePressedReleasedID(String str)684     private static Boolean parsePressedReleasedID(String str) {
685 
686         if (str.equals("pressed")) { //$NON-NLS-1$
687             return Boolean.FALSE;
688         } else if (str.equals("released")) { //$NON-NLS-1$
689             return Boolean.TRUE;
690         }
691         return null;
692     }
693 
694     /**
695      * Parses the key.
696      *
697      * @param strCode
698      *            the str code.
699      * @return the int.
700      */
parseKey(String strCode)701     private static int parseKey(String strCode) {
702         int keyCode = KeyEvent.VK_UNDEFINED;
703 
704         keyCode = getKeyCode(strCode);
705 
706         if (keyCode == KeyEvent.VK_UNDEFINED) {
707             // awt.66=Invalid format
708             throw new IllegalArgumentException(Messages.getString("awt.66")); //$NON-NLS-1$
709         }
710         return keyCode;
711     }
712 }
713