KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > view > JobMenuProcessor


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.monitor.view;
5
6 import java.util.HashMap JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import javax.swing.Action JavaDoc;
10 import javax.swing.JComponent JavaDoc;
11 import javax.swing.JMenuItem JavaDoc;
12 import javax.swing.KeyStroke JavaDoc;
13
14 import org.apache.log4j.Logger;
15 import org.oddjob.monitor.Standards;
16 import org.oddjob.monitor.action.ExecuteAction;
17 import org.oddjob.monitor.action.HardResetAction;
18 import org.oddjob.monitor.action.SchedulerMenu;
19 import org.oddjob.monitor.action.SetPropertyAction;
20 import org.oddjob.monitor.action.SoftResetAction;
21 import org.oddjob.monitor.action.StopAction;
22 import org.oddjob.monitor.model.JobAction;
23 import org.oddjob.monitor.model.JobMenu;
24 import org.oddjob.monitor.model.JobOptionProcessor;
25
26 /**
27  * Process JobOption objects into a Swing Menu.
28  *
29  * @author Rob Gordon.
30  */

31 public class JobMenuProcessor implements JobOptionProcessor {
32     private static final Logger logger = Logger.getLogger(SchedulerMenu.class);
33     
34     /** The menu this processor will add to */
35     private final JComponent JavaDoc menu;
36
37     /** Track actions and keys. */
38     private Map JavaDoc keys = new HashMap JavaDoc();
39     
40     /**
41      * Constructor.
42      *
43      * @param menu The menu this processor will be adding action
44      * and sub menus to.
45      */

46     public JobMenuProcessor(JComponent JavaDoc menu) {
47         this.menu = menu;
48         keys.put(SetPropertyAction.class,
49                 new Keys(Standards.PROPERTY_MNEMONIC_KEY, Standards.PROPERTY_ACCELERATOR_KEY));
50         keys.put(ExecuteAction.class,
51                 new Keys(Standards.RUN_MNEMONIC_KEY, Standards.RUN_ACCELERATOR_KEY));
52         keys.put(SoftResetAction.class,
53                 new Keys(Standards.SOFT_RESET_MNEMONIC_KEY, Standards.SOFT_RESET_ACCELERATOR_KEY));
54         keys.put(HardResetAction.class,
55                 new Keys(Standards.HARD_RESET_MNEMONIC_KEY, Standards.HARD_RESET_ACCELERATOR_KEY));
56         keys.put(StopAction.class,
57                 new Keys(Standards.STOP_MNEMONIC_KEY, Standards.STOP_ACCELERATOR_KEY));
58     }
59         
60     /*
61      * (non-Javadoc)
62      * @see org.oddjob.monitor.action.JobOptionProcessor#processMenu(org.oddjob.monitor.action.JobMenu)
63      */

64     public void processMenu(JobMenu jobMenu) {
65         JobSwingMenu swingMenu = new JobSwingMenu(jobMenu);
66         menu.add(swingMenu);
67         swingMenu.update();
68         logger.debug("Added sub menu [" + jobMenu.getName() + "]");
69     }
70
71     /*
72      * (non-Javadoc)
73      * @see org.oddjob.monitor.action.JobOptionProcessor#processAction(org.oddjob.monitor.action.JobAction)
74      */

75     public void processAction(JobAction jobAction) {
76         Action JavaDoc action = new JobSwingAction(jobAction);
77         Keys key = (Keys) keys.get(jobAction.getClass());
78         if (key != null) {
79             action.putValue(Action.MNEMONIC_KEY, key.getMnumonicKey());
80             action.putValue(Action.ACCELERATOR_KEY, key.getAcceleratorKey());
81         }
82         menu.add(new JMenuItem JavaDoc(
83                 action));
84         logger.debug("Added action [" + jobAction.getName() + "]");
85     }
86     
87     /**
88      * Utility class for collecting key shortcuts for an action.
89      *
90      */

91     class Keys {
92         private final Integer JavaDoc mnumonicKey;
93         private final KeyStroke JavaDoc acceleratorKey;
94         Keys(Integer JavaDoc mnumonicKey, KeyStroke JavaDoc acceleratorKey) {
95             this.mnumonicKey = mnumonicKey;
96             this.acceleratorKey = acceleratorKey;
97         }
98         public KeyStroke JavaDoc getAcceleratorKey() {
99             return acceleratorKey;
100         }
101         public Integer JavaDoc getMnumonicKey() {
102             return mnumonicKey;
103         }
104     }
105 }
106
Popular Tags