1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sourceforge.jivalo.editor.key;
17
18 import java.awt.event.KeyEvent;
19 import java.lang.reflect.Field;
20 import java.lang.reflect.Modifier;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.Iterator;
24 import java.util.List;
25
26 /**
27 * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
28 *
29 */
30 public class KeyEventKeyCodeFactory
31 {
32
33 private static final HashMap KEY_CODES;
34
35 private static List VALUES;
36
37 static
38 {
39 KEY_CODES = new HashMap();
40
41 Field[] fields = KeyEvent.class.getFields();
42
43 for ( int i = 0; i < fields.length; i++ )
44 {
45 Field field = fields[i];
46
47 try
48 {
49
50 if ( Modifier.isPublic( field.getModifiers() )
51 && Modifier.isStatic( field.getModifiers() )
52 && Modifier.isFinal( field.getModifiers() )
53 && field.getType() == int.class
54 && !field.getName().toUpperCase().startsWith( "KEY_" ) )
55 {
56 KEY_CODES.put( field.getName(), new KeyCode( field
57 .getName(), field.getInt( KeyEvent.class ) ) );
58 }
59 }
60 catch ( Exception e )
61 {
62
63 e.printStackTrace();
64 }
65 }
66 }
67
68 private KeyEventKeyCodeFactory()
69 {
70 super();
71 }
72
73 public static KeyCode getKeyCode( String keyName )
74 {
75
76 return ( KeyCode ) KEY_CODES.get( keyName );
77 }
78
79 public static synchronized List getKeyCodes()
80 {
81
82 if ( VALUES == null )
83 {
84 VALUES = new ArrayList();
85
86 for ( Iterator iter = KEY_CODES.values().iterator(); iter.hasNext(); )
87 {
88 VALUES.add( ( KeyCode ) iter.next() );
89 }
90
91 }
92
93 return VALUES;
94 }
95 }