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.lang.reflect.Field;
19  import java.lang.reflect.Modifier;
20  import java.util.ArrayList;
21  import java.util.HashMap;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import javax.swing.text.DefaultEditorKit;
26  
27  /**
28   * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
29   *
30   */
31  public class DefaultEditorKitActionFactory
32  {
33  
34      private static final HashMap ACTIONS;
35      
36      private static List VALUES;
37      
38      static
39      {
40          ACTIONS = new HashMap();
41          
42          Field[] fields = DefaultEditorKit.class.getFields();
43          
44          for ( int i = 0; i < fields.length; i++ )
45          {
46              Field field = fields[i];
47              
48              if ( field.getName().toLowerCase().endsWith( "action" ))
49              {
50                  try
51                  {
52                      
53                      if ( Modifier.isPublic( field.getModifiers() ) 
54                          && Modifier.isStatic( field.getModifiers() )
55                          && Modifier.isFinal( field.getModifiers() ) )
56                      {
57                          ACTIONS.put( field.getName()
58                                  , new KeyAction( field.getName(), 
59                                          field.get( DefaultEditorKit.class ) ) );
60                      }
61                  }
62                  catch ( Exception e )
63                  {
64                      // TODO Auto-generated catch block
65                      e.printStackTrace();
66                  }
67              }
68              
69          }
70      }
71      
72      private DefaultEditorKitActionFactory()
73      {
74          super();
75      }
76  
77      public static KeyAction getAction( String actionName )
78      {
79          
80          return ( KeyAction ) ACTIONS.get( actionName );      
81      }
82  
83      public static synchronized List getActions()
84      {
85          
86          if ( VALUES == null )
87          {
88              VALUES = new ArrayList();
89              
90              for ( Iterator iter = ACTIONS.values().iterator(); iter.hasNext(); )
91              {                
92                  VALUES.add( ( KeyAction ) iter.next() );
93              }
94              
95          }
96           
97          return VALUES;
98      }
99  }