View Javadoc

1   /*
2    * Copyright 2007 Markku Saarela 
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, 
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. 
14   * See the License for the specific language governing permissions and limitations under the License. 
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                  // TODO Auto-generated catch block
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  }