KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > panoptes > view > swing > widgets > AttributeLabel


1 /*
2  * Created on Sep 4, 2003
3  *
4  */

5 package net.sf.panoptes.view.swing.widgets;
6
7 import java.awt.Color JavaDoc;
8 import java.awt.event.ActionEvent JavaDoc;
9 import java.awt.event.ActionListener JavaDoc;
10
11 import javax.management.ObjectName JavaDoc;
12 import javax.swing.JLabel JavaDoc;
13 import javax.swing.Timer JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17
18 import net.sf.panoptes.component.jmx.model.MBeanWrapper;
19 import net.sf.panoptes.component.jmx.model.MBeanServerNode;
20
21 /**
22  *
23  *
24  * @author Dag Liodden
25  * @version 0.1
26  */

27 public class AttributeLabel extends JLabel JavaDoc {
28     
29     private static Log log = LogFactory.getLog(AttributeLabel.class);
30     
31     private String JavaDoc attributeName;
32     private Timer JavaDoc timer = null;
33
34     private int refreshRate;
35
36     private MBeanWrapper node;
37
38     public AttributeLabel(MBeanWrapper node, String JavaDoc attributeName, int refreshRate) {
39         this.node = node;
40         this.attributeName = attributeName;
41         this.refreshRate = refreshRate;
42         setText(getAttributeValue());
43         setForeground(Color.BLUE);
44     }
45     
46     private String JavaDoc getAttributeValue() {
47         try {
48             return node.getAttributeValue(attributeName).toString();
49         } catch (Exception JavaDoc e) {
50             log.error("Unable to get attribute value", e);
51             return "Unable to get";
52         }
53     }
54     
55     public void addNotify() {
56         super.addNotify();
57         timer = new Timer JavaDoc(refreshRate, new ActionListener JavaDoc() {
58             public void actionPerformed(ActionEvent JavaDoc e) {
59                 setText(getAttributeValue());
60             }
61         });
62         timer.start();
63     }
64
65     public void removeNotify() {
66         super.removeNotify();
67         timer.stop();
68     }
69
70 }
71
Popular Tags