KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > monitor > control > PropertyPolling


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

4 package org.oddjob.monitor.control;
5
6 import java.beans.PropertyChangeEvent JavaDoc;
7 import java.beans.PropertyChangeListener JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Observable JavaDoc;
11 import java.util.Observer JavaDoc;
12
13 import org.apache.log4j.Logger;
14 import org.oddjob.monitor.model.Describer;
15 import org.oddjob.monitor.model.DetailModel;
16 import org.oddjob.monitor.model.PropertyModel;
17
18 /**
19  * Populate a property model with a subjects properties.
20  *
21  * @author Rob Gordon.
22  */

23 public class PropertyPolling implements PropertyChangeListener JavaDoc, Observer JavaDoc {
24     private static final Logger logger = Logger
25             .getLogger(PropertyPolling.class);
26
27     private Object JavaDoc subject;
28     private Object JavaDoc kick;
29     
30     private PropertyModel propertyModel;
31
32     /**
33      * Constructor.
34      *
35      * @param kick An object notify to kick the polling.
36      */

37     public PropertyPolling(Object JavaDoc kick) {
38         this.kick = kick;
39     }
40     
41     /**
42      * Poll for property changes.
43      *
44      */

45     public void poll() {
46         Object JavaDoc subject = getSubject();
47         if (subject == null) {
48             if (propertyModel != null) {
49                 propertyModel.setProperties(new HashMap JavaDoc());
50             }
51         } else {
52             Map JavaDoc props = null;
53             props = Describer.describe(subject);
54             propertyModel.setProperties(props);
55         }
56     }
57
58     /**
59      * Change table model when a property changes.
60      *
61      * @param e
62      * The property change event.
63      */

64     public void propertyChange(PropertyChangeEvent JavaDoc e) {
65         synchronized (kick) {
66             kick.notifyAll();
67         }
68     }
69
70     /**
71      * @return Returns the subject.
72      */

73     public synchronized Object JavaDoc getSubject() {
74         return subject;
75     }
76
77     /**
78      * @param subject
79      * The subject to set.
80      */

81     public synchronized void setSubject(Object JavaDoc subject) {
82         logger.debug("Setting subject to [" + subject + "]");
83         if (this.subject != null) {
84             PropertyChangeHelper.removePropertyChangeListener(this.subject,
85                     this);
86         }
87         this.subject = subject;
88         if (this.subject != null) {
89             PropertyChangeHelper.addPropertyChangeListener(this.subject, this);
90         }
91         synchronized (kick) {
92             kick.notifyAll();
93         }
94     }
95
96     /**
97      * @return Returns the propertyModel.
98      */

99     public synchronized PropertyModel getPropertyModel() {
100         return propertyModel;
101     }
102
103     /**
104      * @param propertyModel
105      * The propertyModel to set.
106      */

107     public synchronized void setPropertyModel(PropertyModel propertyModel) {
108         this.propertyModel = propertyModel;
109     }
110     
111     /**
112      * Called when the DetailModel changes. Usually when the job node
113      * select changes.
114      *
115      * @param o The DetailModel.
116      * @param arg Ignored.
117      */

118     public void update(Observable JavaDoc o, Object JavaDoc arg) {
119         DetailModel explorerModel = (DetailModel) o;
120         if (explorerModel.getTabSelected() != DetailModel.PROPERTIES_TAB
121                 || explorerModel.getSelectedJob() == null) {
122             setSubject(null);
123             return;
124         }
125         setSubject(explorerModel.getSelectedJob());
126     }
127 }
128
Popular Tags