KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > model > DetailModel


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

4 package org.oddjob.monitor.model;
5
6 import java.util.Observable JavaDoc;
7
8 import org.apache.log4j.Logger;
9 import org.oddjob.Stateful;
10 import org.oddjob.logging.ConsoleArchiver;
11 import org.oddjob.logging.LogArchiver;
12 import org.oddjob.logging.LogLevel;
13 import org.oddjob.state.JobStateEvent;
14 import org.oddjob.state.JobStateListener;
15
16 public class DetailModel extends Observable JavaDoc {
17     private static final Logger logger = Logger.getLogger(DetailModel.class);
18
19     public static final int STATE_TAB = 0;
20     public static final int CONSOLE_TAB = 1;
21     public static final int LOG_TAB = 2;
22     public static final int PROPERTIES_TAB = 3;
23
24     /** Tab selected */
25     private int tabSelected = STATE_TAB;
26
27     /** The selected Job */
28     private Object JavaDoc selectedJob;
29     
30     /** Console model */
31     private final LogModel consoleModel = new LogModel();
32     
33     /** Log model */
34     private final LogModel logModel = new LogModel();
35
36     /** Property model */
37     private PropertyModel propertyModel = new PropertyModel();
38
39     /** State model */
40     private final StateModel stateModel = new StateModel();
41
42     /** State control */
43     private final JobStateListener stateListener = new JobStateListener() {
44         public void jobStateChange(JobStateEvent event) {
45             stateModel.change(event);
46         }
47     };
48     
49     /**
50      * Get the model for the Console panel.
51      *
52      * @return The LogModel for the console.
53      */

54     public LogModel getConsoleModel() {
55         return consoleModel;
56     }
57
58     /**
59      * Get the model for the Log panel.
60      *
61      * @return The LogModel for the log;
62      */

63     public LogModel getLogModel() {
64         return logModel;
65     }
66
67     /**
68      * Get the model for the Property panel.
69      *
70      * @return The PropertyModel.
71      */

72     public PropertyModel getPropertyModel() {
73         return propertyModel;
74     }
75     
76     /**
77      * Set currently selected tab.
78      *
79      * @param tabSelected The tab number.
80      */

81     public void setTabSelected(int tabSelected) {
82         this.tabSelected = tabSelected;
83     }
84     
85     /**
86      * Get the selected detail tab.
87      *
88      * @return The selected tab.
89      */

90     public int getTabSelected() {
91         return tabSelected;
92     }
93     
94     /**
95      * Unselect a node.
96      *
97      * @param context The old node context.
98      * @param old The old node.
99      */

100     public void deSelect(ExplorerContext context, Object JavaDoc old) {
101         logger.debug("DeSelected node [" + old + "]");
102         selectedJob = null;
103
104         // state
105
stateModel.clear();
106         if (old instanceof Stateful) {
107             ((Stateful) old).removeJobStateListener(stateListener);
108         }
109         
110         // console
111
ConsoleArchiver consoleArchiver = context.getConsoleArchiver();
112         consoleArchiver.removeConsoleListener(consoleModel, old);
113         consoleModel.setClear();
114         
115         // logger
116
LogArchiver logArchiver = context.getLogArchiver();
117         logArchiver.removeLogListener(logModel, old);
118         logModel.setClear();
119         
120         setChanged();
121         notifyObservers(context);
122     }
123     
124     /**
125      * Select a node.
126      *
127      * @param context
128      * @param job
129      */

130     public void select(ExplorerContext context, Object JavaDoc job) {
131         logger.debug("Selected node [" + job + "]");
132         selectedJob = job;
133         
134         if (tabSelected == STATE_TAB) {
135             // stateful
136
if (job instanceof Stateful) {
137                 ((Stateful) job).addJobStateListener(stateListener);
138             }
139         }
140         else if (tabSelected == CONSOLE_TAB) {
141             ConsoleArchiver consoleArchiver = context.getConsoleArchiver();
142             consoleArchiver.addConsoleListener(consoleModel, job, -1, 1000);
143         }
144         else if (tabSelected == LOG_TAB) {
145             // add logging pane
146
LogArchiver logArchiver = context.getLogArchiver();
147             logArchiver.addLogListener(logModel, job, LogLevel.DEBUG, -1, 1000);
148         }
149         
150         setChanged();
151         notifyObservers(context);
152     }
153
154     /**
155      * Get the selected job.
156      *
157      * @return The selected job or null if none is.
158      */

159     public Object JavaDoc getSelectedJob() {
160         return selectedJob;
161     }
162     
163     /**
164      * Get the state model.
165      *
166      * @return The StateModel.
167      */

168     public Observable JavaDoc getStateModel() {
169         return stateModel;
170     }
171     
172 }
173
Popular Tags