KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > clif > console > lib > gui > GuiMonitorCard


1 /*
2 * CLIF is a Load Injection Framework
3 * Copyright (C) 2003, 2004 France Telecom R&D
4 * Copyright (C) 2003 INRIA
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * CLIF $Name: $
21 *
22 * Contact: clif@objectweb.org
23 */

24 package org.objectweb.clif.console.lib.gui;
25
26 import java.awt.*;
27 import java.awt.event.ActionEvent JavaDoc;
28 import java.awt.event.ActionListener JavaDoc;
29 import java.awt.event.ComponentListener JavaDoc;
30 import java.awt.event.ComponentEvent JavaDoc;
31 import java.awt.event.ContainerListener JavaDoc;
32 import java.awt.event.ContainerEvent JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import javax.swing.JButton JavaDoc;
36 import javax.swing.JComboBox JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39 import javax.swing.JScrollPane JavaDoc;
40 import javax.swing.JSplitPane JavaDoc;
41 import javax.swing.JTable JavaDoc;
42 import javax.swing.JTextField JavaDoc;
43 import javax.swing.Timer JavaDoc;
44 import javax.swing.event.TableModelEvent JavaDoc;
45 import javax.swing.event.TableModelListener JavaDoc;
46 import org.objectweb.clif.supervisor.api.TestControl;
47
48 /**
49  * @author Julien Buret
50  * @author Nicolas Droze
51  * @author Bruno Dillenseger
52  */

53 public class GuiMonitorCard
54     extends JPanel JavaDoc
55     implements ActionListener JavaDoc, ComponentListener JavaDoc, ContainerListener JavaDoc, TableModelListener JavaDoc
56 {
57     private JTextField JavaDoc intervalField = new JTextField JavaDoc("1", 4);
58     private JTextField JavaDoc timeframeField = new JTextField JavaDoc(4);
59
60     private JButton JavaDoc refreshButton = new JButton JavaDoc("Set/Draw");
61     private JButton JavaDoc stopButton = new JButton JavaDoc("Stop");
62     private JButton JavaDoc resetButton = new JButton JavaDoc("Reset");
63
64     private JPanel JavaDoc panelInjectors = new JPanel JavaDoc();
65     private JPanel JavaDoc panelHosts = new JPanel JavaDoc();
66     private JPanel JavaDoc panelGraph = new JPanel JavaDoc();
67     private JPanel JavaDoc panelControl = new JPanel JavaDoc();
68     private JPanel JavaDoc panelTop = new JPanel JavaDoc();
69     private JPanel JavaDoc panelMain = new JPanel JavaDoc();
70
71     private JComboBox JavaDoc viewComboInjectors = null;
72
73     private ActionTestReport updateTestReport = null;
74     private Timer JavaDoc testReportTimer = null;
75
76     private String JavaDoc[] cname = { "Display", "Collect", "Blade" };
77
78     private GraphTableModel tModelInjectors = new GraphTableModel(cname);
79     private JTable JavaDoc tableInjectors = new JTable JavaDoc(tModelInjectors);
80
81     private JScrollPane JavaDoc scrollPaneInjectors = new JScrollPane JavaDoc(tableInjectors);
82
83     private Graph graphTestReport = null;
84
85     private GridBagLayout gb = new GridBagLayout();
86     private GridBagConstraints c = new GridBagConstraints();
87
88     private JSplitPane JavaDoc splitPane = null;
89
90     private GraphCellRenderer cellRendererInjector;
91
92     private int lastNbPoints;
93     private int nbPoints;
94     private int interval;
95
96     /**
97      * The constructor that will initialize the internal panels.
98      * @param testCtrl the TestControl interface of the Supervisor component
99      */

100     public GuiMonitorCard(Map JavaDoc testPlan, String JavaDoc[] statLabels, TestControl testCtrl)
101     {
102         tModelInjectors.setId(1);
103
104         tModelInjectors.addTableModelListener(this);
105         this.setBackground(Color.white);
106         this.setLayout(new GridLayout(1, 1));
107         graphTestReport = new Graph(new String JavaDoc[0], statLabels.length, true);
108         createInjectorsPanel(statLabels);
109         createHostsPanel();
110         createGraphPanel();
111         createTopPanel();
112         createControlPanel();
113         createMainPanel();
114
115         Iterator JavaDoc iter = testPlan.keySet().iterator();
116         while (iter.hasNext())
117         {
118             String JavaDoc name = (String JavaDoc)iter.next();
119             graphTestReport.addInjector(name);
120             cellRendererInjector =
121                 new GraphCellRenderer(graphTestReport.allHosts);
122             tableInjectors.getColumnModel().getColumn(2).setCellRenderer(
123                 cellRendererInjector);
124             tModelInjectors.addInjector(name);
125         }
126         add(panelMain);
127         addComponentListener(this);
128         // Initialize the action that will update the report
129
updateTestReport =
130             new ActionTestReport(testCtrl, graphTestReport, tModelInjectors);
131     }
132
133     /**
134      * Create the panel that represents the list of all the injectors,
135      * with the associated combo box.
136      */

137     private void createInjectorsPanel(String JavaDoc[] statLabels)
138     {
139         viewComboInjectors = new JComboBox JavaDoc(statLabels);
140         viewComboInjectors.setSelectedIndex(0);
141         graphTestReport.setView(viewComboInjectors.getSelectedIndex());
142
143         panelInjectors.setLayout(gb);
144
145         c.gridx = 0;
146         c.gridy = 0;
147         c.gridheight = 1;
148         c.gridwidth = 1;
149         c.weightx = 100;
150         c.weighty = 100;
151         c.fill = GridBagConstraints.BOTH;
152         c.anchor = GridBagConstraints.NORTH;
153
154         scrollPaneInjectors.setSize(100, 50);
155         panelInjectors.add(scrollPaneInjectors, c);
156
157         c.gridx = 0;
158         c.gridy = 1;
159         c.gridheight = 1;
160         c.gridwidth = 1;
161         c.weightx = 100;
162         c.weighty = 0;
163         c.fill = GridBagConstraints.NONE;
164         c.anchor = GridBagConstraints.WEST;
165
166         viewComboInjectors.addActionListener(this);
167         panelInjectors.add(viewComboInjectors, c);
168     }
169
170
171     /**
172      * Create an intermediate panel which contains both panels: monitor panel
173      * and injector panel.
174      */

175     private void createHostsPanel() {
176         panelHosts.setLayout(gb);
177
178         c.gridx = 0;
179         c.gridy = 0;
180         c.gridheight = 1;
181         c.gridwidth = 1;
182         c.weightx = 100;
183         c.weighty = 100;
184         c.fill = GridBagConstraints.BOTH;
185         c.anchor = GridBagConstraints.NORTH;
186         panelHosts.add(panelInjectors, c);
187     }
188
189     /**
190      * Create the control panel which contains the control buttons for the graph.
191      */

192     private void createControlPanel() {
193         panelControl.setLayout(gb);
194
195         // common constraints for every element
196
c.gridheight = 1;
197         c.gridwidth = 1;
198         c.weightx = 100;
199         c.weighty = 100;
200         c.gridy = 0;
201
202         // drawing timeframe setting
203
c.gridx = 0;
204         c.fill = GridBagConstraints.NONE;
205         c.anchor = GridBagConstraints.EAST;
206         panelControl.add(new JLabel JavaDoc("Drawing timeframe:"), c);
207
208         c.gridx = 1;
209         c.fill = GridBagConstraints.HORIZONTAL;
210         panelControl.add(timeframeField, c);
211
212         c.gridx = 2;
213         c.fill = GridBagConstraints.NONE;
214         c.anchor = GridBagConstraints.WEST;
215         panelControl.add(new JLabel JavaDoc("sec."), c);
216
217         // polling period setting
218
c.gridx = 3;
219         c.fill = GridBagConstraints.NONE;
220         c.anchor = GridBagConstraints.EAST;
221         panelControl.add(new JLabel JavaDoc("Polling period:"), c);
222
223         c.gridx = 4;
224         c.fill = GridBagConstraints.HORIZONTAL;
225         panelControl.add(intervalField, c);
226
227         c.gridx = 5;
228         c.fill = GridBagConstraints.NONE;
229         c.anchor = GridBagConstraints.WEST;
230         panelControl.add(new JLabel JavaDoc("sec."), c);
231
232         // all buttons are centered, and should not be resized
233
c.fill = GridBagConstraints.NONE;
234         c.anchor = GridBagConstraints.CENTER;
235
236         // set/draw button
237
c.gridx = 6;
238         refreshButton.addActionListener(this);
239         panelControl.add(refreshButton, c);
240
241         // stop button
242
c.gridx = 7;
243         stopButton.addActionListener(this);
244         stopButton.setEnabled(false);
245         panelControl.add(stopButton, c);
246
247         // reset button
248
c.gridx = 8;
249         resetButton.addActionListener(this);
250         panelControl.add(resetButton, c);
251     }
252
253     /**
254      * Create an intermediate panel which contains the host panel and the graphs panel.
255      */

256     private void createTopPanel() {
257         panelTop.setLayout(gb);
258
259         splitPane =
260             new JSplitPane JavaDoc(JSplitPane.HORIZONTAL_SPLIT, panelHosts, panelGraph);
261         splitPane.setDividerLocation(panelHosts.getMinimumSize().width);
262         splitPane.setOneTouchExpandable(true);
263
264         c.gridx = 0;
265         c.gridy = 0;
266         c.gridheight = 1;
267         c.gridwidth = 1;
268         c.weightx = 100;
269         c.weighty = 100;
270         c.fill = GridBagConstraints.BOTH;
271         c.anchor = GridBagConstraints.NORTH;
272         panelTop.add(splitPane, c);
273     }
274
275     /**
276      * Create an intermediate panel which contains the injector graph
277      * and the monitor graph.
278      */

279     private void createGraphPanel() {
280         panelGraph.setLayout(gb);
281
282         c.gridx = 0;
283         c.gridy = 0;
284         c.gridheight = 1;
285         c.gridwidth = 1;
286         c.weightx = 100;
287         c.weighty = 100;
288         c.fill = GridBagConstraints.BOTH;
289         c.anchor = GridBagConstraints.NORTH;
290         panelGraph.add(graphTestReport, c);
291     }
292
293     /**
294      * Create the main panel which is the assembly of all the intermediate panels.
295      */

296     private void createMainPanel() {
297         panelMain.setLayout(gb);
298
299         c.gridx = 0;
300         c.gridy = 0;
301         c.gridheight = 1;
302         c.gridwidth = 1;
303         c.weightx = 100;
304         c.weighty = 100;
305         c.fill = GridBagConstraints.BOTH;
306         c.anchor = GridBagConstraints.NORTH;
307         panelMain.add(panelTop, c);
308
309         c.gridx = 0;
310         c.gridy = 1;
311         c.gridheight = 1;
312         c.gridwidth = 1;
313         c.weightx = 100;
314         c.weighty = 0;
315         c.fill = GridBagConstraints.HORIZONTAL;
316         c.anchor = GridBagConstraints.CENTER;
317         panelMain.add(panelControl, c);
318     }
319
320
321     /////////////////////////////////
322
// interface ComponentListener //
323
/////////////////////////////////
324

325
326     public void componentHidden(ComponentEvent JavaDoc e)
327     {
328     }
329
330
331     public void componentMoved(ComponentEvent JavaDoc e)
332     {
333     }
334
335
336     public void componentResized(ComponentEvent JavaDoc e)
337     {
338         if (timeframeField.getText().length() == 0 && graphTestReport.getGraphAreaWidth() > 0)
339         {
340             timeframeField.setText(String.valueOf(graphTestReport.getGraphAreaWidth()));
341         }
342     }
343
344
345     public void componentShown(ComponentEvent JavaDoc e)
346     {
347         componentResized(e);
348     }
349
350
351     /////////////////////////////////
352
// interface ContainerListener //
353
/////////////////////////////////
354

355
356     public void componentAdded(ContainerEvent JavaDoc ev)
357     {
358     }
359
360
361     public void componentRemoved(ContainerEvent JavaDoc ev)
362     {
363         if (ev.getChild() == this && testReportTimer != null)
364         {
365             testReportTimer.removeActionListener(updateTestReport);
366             testReportTimer.stop();
367         }
368     }
369
370
371     //////////////////////////////
372
// interface ActionListener //
373
//////////////////////////////
374

375     /**
376      * Method invoked when the user clicks a button.
377      * The action to perform is dispatched according to the button command.
378      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
379      */

380     public void actionPerformed(ActionEvent JavaDoc e)
381     {
382         Object JavaDoc source = e.getSource();
383         if (source == this.refreshButton) // Refresh button
384
{
385             resetButton.setEnabled(false);
386             try
387             {
388                 interval = Integer.parseInt(intervalField.getText());
389                 nbPoints = 1 + Integer.parseInt(timeframeField.getText()) / interval;
390                 if (nbPoints != lastNbPoints)
391                 {
392                     lastNbPoints = nbPoints;
393                     graphTestReport.setNbPoints(lastNbPoints);
394                 }
395                 // Launch a thread that will get the report of all injectors every "interval" seconds
396
updateTestReport.setTime(interval);
397                 if (testReportTimer == null)
398                 {
399                     testReportTimer = new Timer JavaDoc(interval * 1000, updateTestReport);
400                 }
401                 else
402                 {
403                     testReportTimer.stop();
404                     testReportTimer.setDelay(interval * 1000);
405                 }
406                 testReportTimer.start();
407                 stopButton.setEnabled(true);
408             }
409             catch (Exception JavaDoc ex)
410             {
411                 System.err.println("unexpected non-integer values in period fields");
412             }
413         }
414         else if (source == this.stopButton) // Stop button
415
{
416             if (testReportTimer != null)
417             {
418                 testReportTimer.stop();
419             }
420             stopButton.setEnabled(false);
421             resetButton.setEnabled(true);
422         }
423         else if (source == this.resetButton) // Reset button
424
{
425             updateTestReport.reset();
426             graphTestReport.setView(viewComboInjectors.getSelectedIndex());
427             graphTestReport.clear();
428         }
429         else if (source == this.viewComboInjectors) // Blade monitoring list of values
430
{
431             // Inform the graph that the view has changed
432
graphTestReport.setView(viewComboInjectors.getSelectedIndex());
433             graphTestReport.removeAllPointsFromDisplay(
434                 tModelInjectors.getInjectorsToDisplay());
435             graphTestReport.addAllPointsOnDisplay(
436                 tModelInjectors.getInjectorsToDisplay());
437             graphTestReport.updateGraph();
438         }
439     }
440
441
442     //////////////////////////////////
443
// interface TableModelListener //
444
//////////////////////////////////
445

446
447     /**
448      * This method is called each time an event occured in the injector table
449      * It is necessary for an immediate graphics update when checking/unchecking display box.
450      * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
451      */

452     public void tableChanged(TableModelEvent JavaDoc e) {
453
454         // We handle only the UPDATE event, ie the checkbox state has changed
455
if (e.getType() == TableModelEvent.UPDATE) {
456             int row = e.getFirstRow();
457             int column = e.getColumn();
458             Boolean JavaDoc data;
459
460             // Injectors table
461
if (((GraphTableModel) e.getSource()).getId() == 1) {
462                 // We get the new value of the checkbox
463
data = (Boolean JavaDoc) tModelInjectors.getValueAt(row, column);
464                 // Display column
465
if (column == 0) {
466                     /* Depending on the checkbox value, we inform the graph to
467                      * display/remove the graph of the injector
468                      */

469                     if (data.booleanValue()) {
470                         graphTestReport.addPointsOnDisplay(
471                             (String JavaDoc) tModelInjectors.getValueAt(
472                                 row,
473                                 column + 2));
474                         graphTestReport.updateGraph();
475                     } else {
476                         graphTestReport.removePointsFromDisplay(
477                             (String JavaDoc) tModelInjectors.getValueAt(
478                                 row,
479                                 column + 2));
480                         graphTestReport.updateGraph();
481                     }
482                 }
483             }
484         }
485     }
486 }
Popular Tags