View Javadoc

1   package edu.asu.cri.MirkE.gui;
2   
3   import java.awt.event.ActionEvent;
4   import java.awt.event.ActionListener;
5   import java.lang.reflect.Method;
6   import java.util.Hashtable;
7   import java.util.Map;
8   
9   /***
10   * This class is listener for all the menu options. 
11   * <code>actionPerformed</code> of this class will delegate the activity to the
12   * class that is configured in menu.xml
13   * 
14   * @author Siva B.S.D.S
15   * @version $Id: MenuActionListener.java,v 1.4 2005/02/02 17:20:07 smenor Exp $
16   */
17  public class MenuActionListener implements ActionListener
18  {
19      /***
20       * Cache for the classes holding the implementation of the menu options.
21       */
22      Map eventHandlers = null;
23  
24      /***
25       * Empty constructor
26       */
27      public MenuActionListener()
28      {
29          eventHandlers = new Hashtable();
30      }
31      
32      /***
33       * Action performed
34       * @param event
35       */
36      public void actionPerformed(ActionEvent event)
37      {
38          String command = null;
39          Object handler = null;
40          String className = null;
41          String methodName = null;
42          Method method = null;
43          try
44          {
45              command = event.getActionCommand();
46              //Getting the class name
47              className = command.substring(0, command.indexOf(' '));
48              //Getting the method name
49              methodName = command.substring(command.indexOf(' ') + 1);
50              System.out.println("Class Name : " + className);
51              System.out.println("Method Name : " + methodName);
52              
53              //Checking the handler class is already instantiated or not
54              if ((handler = eventHandlers.get(className)) == null)
55              {
56                  //Instantiating the handler for the first time
57                  handler = Class.forName(className).newInstance();
58                  eventHandlers.put(className, handler);
59              }
60              
61              //Invoking the method
62              method =
63                  handler.getClass().getDeclaredMethod(
64                      methodName,
65                      new Class[0]);
66              method.invoke(handler, new Object[0]);
67          }
68          catch (Throwable exception)
69          {
70              exception.printStackTrace();
71          }
72      }
73  }