KickJava   Java API By Example, From Geeks To Geeks.

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


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

4 package org.oddjob.monitor.view;
5
6 import java.util.Observable JavaDoc;
7 import java.util.Observer JavaDoc;
8
9 import javax.swing.JMenu JavaDoc;
10
11 import org.oddjob.monitor.model.JobMenu;
12 import org.oddjob.monitor.model.JobOption;
13
14 /**
15  * Implement a job option which is a menu to further job
16  * options as a Swing Menu.
17  *
18  * @author Rob Gordon.
19  */

20 public class JobSwingMenu extends JMenu JavaDoc {
21     private static final long serialVersionUID = 20051116;
22     private final JobMenu jobMenu;
23     
24     /** This observer recreates this menu every time
25      * the JobMenu notifies a change which will
26      * typically be when a job is selected.
27      */

28     private final Observer JavaDoc observer = new Observer JavaDoc() {
29         public void update(Observable JavaDoc o, Object JavaDoc arg) {
30             JobSwingMenu.this.update();
31         }
32     };
33     
34     /**
35      * Constructor.
36      *
37      * @param jobMenu The JobMenu that is the model for this
38      * menu view.
39      */

40     public JobSwingMenu(JobMenu jobMenu) {
41         super(jobMenu.getName());
42         this.jobMenu = jobMenu;
43         this.setEnabled(jobMenu.enabled());
44         jobMenu.addObserver(observer);
45     }
46     
47     /**
48      * Update this menu. Called when the model changes or after
49      * when a new menu is created.
50      *
51      */

52     public void update() {
53         JobSwingMenu.this.removeAll();
54         if (jobMenu.enabled()) {
55             JobMenuProcessor processor = new JobMenuProcessor(JobSwingMenu.this);
56             JobOption[] options = jobMenu.options();
57             for (int i = 0; i < options.length; ++i) {
58                 options[i].process(processor);
59             }
60         }
61         JobSwingMenu.this.setEnabled(jobMenu.enabled());
62         
63     }
64 }
65
Popular Tags