KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.objectweb.clif.datacollector.lib.InjectorDataCollector;
48
49 /**
50  * @author Julien Buret
51  * @author Nicolas Droze
52  * @author Bruno Dillenseger
53  */

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

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

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

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

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

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

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

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

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

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

380     /**
381      * Method invoked when the user clicks a button.
382      * The action to perform is dispatched according to the button command.
383      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
384      */

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

451
452     /**
453      * This method is called each time an event occured in the injector table
454      * It is necessary for an immediate graphics update when checking/unchecking display box.
455      * @see javax.swing.event.TableModelListener#tableChanged(javax.swing.event.TableModelEvent)
456      */

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

474                     if (data.booleanValue()) {
475                         graphTestReport.addPointsOnDisplay(
476                             (String JavaDoc) tModelInjectors.getValueAt(
477                                 row,
478                                 column + 2));
479                         graphTestReport.updateGraph();
480                     } else {
481                         graphTestReport.removePointsFromDisplay(
482                             (String JavaDoc) tModelInjectors.getValueAt(
483                                 row,
484                                 column + 2));
485                         graphTestReport.updateGraph();
486                     }
487                 }
488             }
489         }
490     }
491 }
Popular Tags