KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/monitor/components/org/apache/jmeter/visualizers/MonitorPerformancePanel.java,v 1.5.2.1 2004/10/11 00:55:46 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.util.HashMap JavaDoc;
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Color JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.FlowLayout JavaDoc;
25
26 import javax.swing.ImageIcon JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JScrollPane JavaDoc;
30 import javax.swing.JSplitPane JavaDoc;
31 import javax.swing.JTree JavaDoc;
32 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
33 import javax.swing.tree.DefaultTreeModel JavaDoc;
34 import javax.swing.tree.TreeSelectionModel JavaDoc;
35 import javax.swing.event.TreeSelectionListener JavaDoc;
36 import javax.swing.event.TreeSelectionEvent JavaDoc;
37
38
39 import org.apache.jmeter.samplers.Clearable;
40 import org.apache.jmeter.samplers.SampleResult;
41 import org.apache.jmeter.util.JMeterUtils;
42
43 public class MonitorPerformancePanel extends JSplitPane JavaDoc
44     implements TreeSelectionListener JavaDoc, MonitorListener, Clearable
45 {
46
47     private JScrollPane JavaDoc TREEPANE;
48     private JPanel JavaDoc GRAPHPANEL;
49     private JTree JavaDoc SERVERTREE;
50     private DefaultTreeModel JavaDoc TREEMODEL;
51     private MonitorGraph GRAPH;
52     private DefaultMutableTreeNode JavaDoc ROOTNODE;
53     private HashMap JavaDoc SERVERMAP;
54     private MonitorAccumModel MODEL;
55     private SampleResult ROOTSAMPLE;
56     
57     public static final String JavaDoc LEGEND_HEALTH =
58         JMeterUtils.getResString("monitor_legend_health");
59     public static final String JavaDoc LEGEND_LOAD =
60         JMeterUtils.getResString("monitor_legend_load");
61     public static final String JavaDoc LEGEND_MEM =
62         JMeterUtils.getResString("monitor_legend_memory_per");
63     public static final String JavaDoc LEGEND_THREAD =
64         JMeterUtils.getResString("monitor_legend_thread_per");
65     public static final ImageIcon JavaDoc LEGEND_HEALTH_ICON =
66         JMeterUtils.getImage("monitor-green-legend.gif");
67     public static final ImageIcon JavaDoc LEGEND_LOAD_ICON =
68         JMeterUtils.getImage("monitor-blue-legend.gif");
69     public static final ImageIcon JavaDoc LEGEND_MEM_ICON =
70         JMeterUtils.getImage("monitor-orange-legend.gif");
71     public static final ImageIcon JavaDoc LEGEND_THREAD_ICON =
72         JMeterUtils.getImage("monitor-red-legend.gif");
73     public static final String JavaDoc GRID_LABEL_TOP =
74         JMeterUtils.getResString("monitor_label_left_top");
75     public static final String JavaDoc GRID_LABEL_MIDDLE =
76         JMeterUtils.getResString("monitor_label_left_middle");
77     public static final String JavaDoc GRID_LABEL_BOTTOM =
78         JMeterUtils.getResString("monitor_label_left_bottom");
79     public static final String JavaDoc GRID_LABEL_HEALTHY =
80         JMeterUtils.getResString("monitor_label_right_healthy");
81     public static final String JavaDoc GRID_LABEL_ACTIVE =
82         JMeterUtils.getResString("monitor_label_right_active");
83     public static final String JavaDoc GRID_LABEL_WARNING =
84         JMeterUtils.getResString("monitor_label_right_warning");
85     public static final String JavaDoc GRID_LABEL_DEAD =
86         JMeterUtils.getResString("monitor_label_right_dead");
87     
88     public static final String JavaDoc PERF_TITLE =
89         JMeterUtils.getResString("monitor_performance_title");
90     public static final String JavaDoc SERVER_TITLE =
91         JMeterUtils.getResString("monitor_performance_servers");
92
93     protected Font JavaDoc plaintext = new Font JavaDoc("plain", Font.TRUETYPE_FONT, 10);
94
95     /**
96      *
97      * @deprecated Only for use in unit testing
98      */

99     public MonitorPerformancePanel()
100     {
101         //log.warn("Only for use in unit testing");
102
}
103
104     /**
105      *
106      */

107     public MonitorPerformancePanel(MonitorAccumModel model, MonitorGraph graph)
108     {
109         super();
110         this.SERVERMAP = new HashMap JavaDoc();
111         this.MODEL = model;
112         this.MODEL.addListener(this);
113         this.GRAPH = graph;
114         init();
115     }
116
117     /**
118      * init() will create all the necessary swing panels,
119      * labels and icons for the performance panel.
120      */

121     protected void init(){
122         ROOTSAMPLE = new SampleResult();
123         ROOTSAMPLE.setSampleLabel(SERVER_TITLE);
124         ROOTSAMPLE.setSuccessful(true);
125         ROOTNODE = new DefaultMutableTreeNode JavaDoc(ROOTSAMPLE);
126         TREEMODEL = new DefaultTreeModel JavaDoc(ROOTNODE);
127         SERVERTREE = new JTree JavaDoc(TREEMODEL);
128         SERVERTREE.getSelectionModel().setSelectionMode(
129             TreeSelectionModel.SINGLE_TREE_SELECTION);
130         SERVERTREE.addTreeSelectionListener(this);
131         SERVERTREE.setShowsRootHandles(true);
132         TREEPANE = new JScrollPane JavaDoc(SERVERTREE);
133         TREEPANE.setPreferredSize(new Dimension JavaDoc(150,200));
134         this.add(TREEPANE,JSplitPane.LEFT);
135         this.setDividerLocation(0.18);
136     
137         JPanel JavaDoc right = new JPanel JavaDoc();
138         right.setLayout(new BorderLayout JavaDoc());
139         JLabel JavaDoc title = new JLabel JavaDoc(" " + PERF_TITLE);
140         title.setPreferredSize(new Dimension JavaDoc(200,40));
141         GRAPHPANEL = new JPanel JavaDoc();
142         GRAPHPANEL.setLayout(new BorderLayout JavaDoc());
143         GRAPHPANEL.setMaximumSize(
144             new Dimension JavaDoc(MODEL.getBufferSize(),MODEL.getBufferSize()));
145         GRAPHPANEL.setBackground(Color.white);
146         GRAPHPANEL.add(GRAPH,BorderLayout.CENTER);
147         right.add(GRAPHPANEL,BorderLayout.CENTER);
148         
149         right.add(title,BorderLayout.NORTH);
150         right.add(createLegend(),BorderLayout.SOUTH);
151         right.add(createLeftGridLabels(),BorderLayout.WEST);
152         right.add(createRightGridLabels(),BorderLayout.EAST);
153         this.add(right,JSplitPane.RIGHT);
154     }
155     
156     /**
157      * Method will create the legends at the bottom of
158      * the performance tab explaining the meaning of
159      * each line.
160      * @return JPanel
161      */

162     public JPanel JavaDoc createLegend(){
163         Dimension JavaDoc lsize = new Dimension JavaDoc(130,18);
164         
165         JPanel JavaDoc legend = new JPanel JavaDoc();
166         legend.setLayout(new FlowLayout JavaDoc());
167         JLabel JavaDoc health = new JLabel JavaDoc(LEGEND_HEALTH);
168         health.setFont(plaintext);
169         health.setPreferredSize(lsize);
170         health.setIcon(LEGEND_HEALTH_ICON);
171         legend.add(health);
172         
173         JLabel JavaDoc load = new JLabel JavaDoc(LEGEND_LOAD);
174         load.setFont(plaintext);
175         load.setPreferredSize(lsize);
176         load.setIcon(LEGEND_LOAD_ICON);
177         legend.add(load);
178         
179         JLabel JavaDoc mem = new JLabel JavaDoc(LEGEND_MEM);
180         mem.setFont(plaintext);
181         mem.setPreferredSize(lsize);
182         mem.setIcon(LEGEND_MEM_ICON);
183         legend.add(mem);
184         
185         JLabel JavaDoc thd = new JLabel JavaDoc(LEGEND_THREAD);
186         thd.setFont(plaintext);
187         thd.setPreferredSize(lsize);
188         thd.setIcon(LEGEND_THREAD_ICON);
189         legend.add(thd);
190         return legend;
191     }
192
193     /**
194      * Method is responsible for creating the left
195      * grid labels.
196      * @return JPanel
197      */

198     public JPanel JavaDoc createLeftGridLabels(){
199         Dimension JavaDoc lsize = new Dimension JavaDoc(33,20);
200         JPanel JavaDoc labels = new JPanel JavaDoc();
201         labels.setLayout(new BorderLayout JavaDoc());
202         
203         JLabel JavaDoc top = new JLabel JavaDoc(" " + GRID_LABEL_TOP);
204         top.setFont(plaintext);
205         top.setPreferredSize(lsize);
206         labels.add(top,BorderLayout.NORTH);
207         
208         JLabel JavaDoc mid = new JLabel JavaDoc(" " + GRID_LABEL_MIDDLE);
209         mid.setFont(plaintext);
210         mid.setPreferredSize(lsize);
211         labels.add(mid,BorderLayout.CENTER);
212         
213         JLabel JavaDoc bottom = new JLabel JavaDoc(" " + GRID_LABEL_BOTTOM);
214         bottom.setFont(plaintext);
215         bottom.setPreferredSize(lsize);
216         labels.add(bottom,BorderLayout.SOUTH);
217         return labels;
218     }
219     
220     /**
221      * Method is responsible for creating the grid labels
222      * on the right for "healthy" and "dead"
223      * @return JPanel
224      */

225     public JPanel JavaDoc createRightGridLabels(){
226         JPanel JavaDoc labels = new JPanel JavaDoc();
227         labels.setLayout(new BorderLayout JavaDoc());
228         labels.setPreferredSize(new Dimension JavaDoc(40,GRAPHPANEL.getWidth() - 100));
229         Dimension JavaDoc lsize = new Dimension JavaDoc(40,20);
230         JLabel JavaDoc h = new JLabel JavaDoc(GRID_LABEL_HEALTHY);
231         h.setFont(plaintext);
232         h.setPreferredSize(lsize);
233         labels.add(h,BorderLayout.NORTH);
234         
235         JLabel JavaDoc d = new JLabel JavaDoc(GRID_LABEL_DEAD);
236         d.setFont(plaintext);
237         d.setPreferredSize(lsize);
238         labels.add(d,BorderLayout.SOUTH);
239         return labels;
240     }
241     
242     /**
243      * MonitorAccumModel will call this method to notify
244      * the component data has changed.
245      */

246     public void addSample(MonitorModel model){
247         if (!SERVERMAP.containsKey(model.getURL())){
248             DefaultMutableTreeNode JavaDoc newnode =
249                 new DefaultMutableTreeNode JavaDoc(model);
250             newnode.setAllowsChildren(false);
251             SERVERMAP.put(model.getURL(),newnode);
252             ROOTNODE.add(newnode);
253             this.TREEPANE.updateUI();
254         }
255         DefaultMutableTreeNode JavaDoc node =
256             (DefaultMutableTreeNode JavaDoc) SERVERTREE.getLastSelectedPathComponent();
257         Object JavaDoc usrobj = node.getUserObject();
258         if (usrobj instanceof MonitorModel){
259             GRAPH.updateGui((MonitorModel)usrobj);
260         }
261     }
262     
263     /**
264      * When the user selects a different node in the
265      * tree, we get the selected node. From the node,
266      * we get the UserObject used to create the
267      * treenode in the constructor.
268      */

269     public void valueChanged(TreeSelectionEvent JavaDoc e)
270     {
271         // we check to see if the lastSelectedPath is null
272
// after we clear, it would return null
273
if (SERVERTREE.getLastSelectedPathComponent() != null){
274             DefaultMutableTreeNode JavaDoc node =
275                 (DefaultMutableTreeNode JavaDoc) SERVERTREE.getLastSelectedPathComponent();
276             Object JavaDoc usrobj = node.getUserObject();
277             if ( usrobj != null && usrobj instanceof MonitorModel){
278                 MonitorModel mo = (MonitorModel)usrobj;
279                 GRAPH.updateGui(mo);
280                 this.updateUI();
281             }
282             TREEPANE.updateUI();
283         }
284     }
285     
286     /**
287      * clear will remove all child nodes from the ROOTNODE,
288      * clear the HashMap, update the graph and jpanel for
289      * the server tree.
290      */

291     public void clear(){
292         this.SERVERMAP.clear();
293         ROOTNODE.removeAllChildren();
294         SERVERTREE.updateUI();
295         GRAPH.clear();
296     }
297 }
298
Popular Tags