KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > visualizers > ServerPanel


1 // $Header: /home/cvs/jakarta-jmeter/src/monitor/components/org/apache/jmeter/visualizers/ServerPanel.java,v 1.4 2004/03/20 22:10:02 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.jmeter.visualizers;
18
19 import java.awt.Dimension JavaDoc;
20 import java.awt.FlowLayout JavaDoc;
21
22 import javax.swing.ImageIcon JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JLabel JavaDoc;
25 import org.apache.jmeter.util.JMeterUtils;
26 import org.apache.jmeter.monitor.util.Stats;
27
28 /**
29  * The purpose of ServerPanel is to display an unique
30  * server and its current status. The server label
31  * consist of the protocol, host and port. For example,
32  * a system with multiple Tomcat's running on different
33  * ports would be different ServerPanel.
34  */

35 public class ServerPanel extends JPanel JavaDoc
36     implements MonitorGuiListener
37 {
38
39     private JLabel JavaDoc serverField;
40     private JLabel JavaDoc timestampField;
41     /**
42      * Preference size for the health icon
43      */

44     private Dimension JavaDoc prefsize = new Dimension JavaDoc(25,75);
45     private JLabel JavaDoc healthIcon;
46     private JLabel JavaDoc loadIcon;
47
48     /**
49      * Health Icons
50      */

51     public static final ImageIcon JavaDoc HEALTHY =
52         JMeterUtils.getImage("monitor-healthy.gif");
53     public static final ImageIcon JavaDoc ACTIVE =
54         JMeterUtils.getImage("monitor-active.gif");
55     public static final ImageIcon JavaDoc WARNING =
56         JMeterUtils.getImage("monitor-warning.gif");
57     public static final ImageIcon JavaDoc DEAD =
58         JMeterUtils.getImage("monitor-dead.gif");
59     /**
60      * Load Icons
61      */

62     public static final ImageIcon JavaDoc LOAD_1 =
63         JMeterUtils.getImage("monitor-load-1.gif");
64     public static final ImageIcon JavaDoc LOAD_2 =
65         JMeterUtils.getImage("monitor-load-2.gif");
66     public static final ImageIcon JavaDoc LOAD_3 =
67         JMeterUtils.getImage("monitor-load-3.gif");
68     public static final ImageIcon JavaDoc LOAD_4 =
69         JMeterUtils.getImage("monitor-load-4.gif");
70     public static final ImageIcon JavaDoc LOAD_5 =
71         JMeterUtils.getImage("monitor-load-5.gif");
72     public static final ImageIcon JavaDoc LOAD_6 =
73         JMeterUtils.getImage("monitor-load-6.gif");
74     public static final ImageIcon JavaDoc LOAD_7 =
75         JMeterUtils.getImage("monitor-load-7.gif");
76     public static final ImageIcon JavaDoc LOAD_8 =
77         JMeterUtils.getImage("monitor-load-8.gif");
78     public static final ImageIcon JavaDoc LOAD_9 =
79         JMeterUtils.getImage("monitor-load-9.gif");
80     public static final ImageIcon JavaDoc LOAD_10 =
81         JMeterUtils.getImage("monitor-load-10.gif");
82     
83     //private MonitorModel DATA;
84

85     /**
86      *
87      */

88     public ServerPanel(MonitorModel model)
89     {
90         super();
91         //DATA = model;
92
init(model);
93     }
94
95     /**
96      *
97      * @deprecated Only for use in unit testing
98      */

99     public ServerPanel()
100     {
101         //log.warn("Only for use in unit testing");
102
}
103
104     /**
105      * Init will create the JLabel widgets for the
106      * host, health, load and timestamp.
107      * @param model
108      */

109     private void init(MonitorModel model)
110     {
111         this.setLayout(new FlowLayout JavaDoc());
112         serverField = new JLabel JavaDoc(model.getURL());
113         this.add(serverField);
114         healthIcon = new JLabel JavaDoc(getHealthyImageIcon(model.getHealth()));
115         healthIcon.setPreferredSize(prefsize);
116         this.add(healthIcon);
117         loadIcon = new JLabel JavaDoc(getLoadImageIcon(model.getLoad()));
118         this.add(loadIcon);
119         timestampField = new JLabel JavaDoc(model.getTimestampString());
120         this.add(timestampField);
121     }
122     
123     /**
124      * Static method for getting the right ImageIcon for
125      * the health.
126      * @param health
127      * @return image for the status
128      */

129     public static ImageIcon JavaDoc getHealthyImageIcon(int health){
130         ImageIcon JavaDoc i = null;
131         switch(health){
132             case Stats.HEALTHY:
133                 i = HEALTHY;
134                 break;
135             case Stats.ACTIVE:
136                 i = ACTIVE;
137                 break;
138             case Stats.WARNING:
139                 i = WARNING;
140                 break;
141             case Stats.DEAD:
142                 i = DEAD;
143                 break;
144         }
145         return i;
146     }
147     
148     /**
149      * Static method looks up the right ImageIcon from
150      * the load value.
151      * @param load
152      * @return image for the load
153      */

154     public static ImageIcon JavaDoc getLoadImageIcon(int load){
155         if (load <= 10){
156             return LOAD_1;
157         } else if (load > 10 && load <= 20){
158             return LOAD_2;
159         } else if (load > 20 && load <= 30){
160             return LOAD_3;
161         } else if (load > 30 && load <= 40){
162             return LOAD_4;
163         } else if (load > 40 && load <= 50){
164             return LOAD_5;
165         } else if (load > 50 && load <= 60){
166             return LOAD_6;
167         } else if (load > 60 && load <= 70){
168             return LOAD_7;
169         } else if (load > 70 && load <= 80){
170             return LOAD_8;
171         } else if (load > 80 && load <= 90){
172             return LOAD_9;
173         } else {
174             return LOAD_10;
175         }
176     }
177     
178     /**
179      * Method will update the ServerPanel's health, load,
180      * and timestamp. For efficiency, it uses the static
181      * method to lookup the images.
182      */

183     public void updateGui(MonitorModel stat){
184         //this.DATA = null;
185
//this.DATA = stat;
186
loadIcon.setIcon(getLoadImageIcon(stat.getLoad()));
187         healthIcon.setIcon(getHealthyImageIcon(stat.getHealth()));
188         timestampField.setText(stat.getTimestampString());
189         this.updateGui();
190     }
191     
192     /**
193      * update the gui
194      */

195     public void updateGui(){
196         this.repaint();
197     }
198 }
199
Popular Tags