KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > MainFrame


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/MainFrame.java,v 1.24 2004/02/13 02:40:53 sebb Exp $
2
/*
3  * Copyright 2001-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 */

18
19 package org.apache.jmeter.gui;
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Component JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.awt.Font JavaDoc;
24 import java.awt.Insets JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.awt.event.ActionListener JavaDoc;
27 import java.awt.event.WindowAdapter JavaDoc;
28 import java.awt.event.WindowEvent JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Set JavaDoc;
31
32 import javax.swing.BorderFactory JavaDoc;
33 import javax.swing.Box JavaDoc;
34 import javax.swing.BoxLayout JavaDoc;
35 import javax.swing.ImageIcon JavaDoc;
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.JDialog JavaDoc;
39 import javax.swing.JFrame JavaDoc;
40 import javax.swing.JLabel JavaDoc;
41 import javax.swing.JMenu JavaDoc;
42 import javax.swing.JPanel JavaDoc;
43 import javax.swing.JPopupMenu JavaDoc;
44 import javax.swing.JScrollPane JavaDoc;
45 import javax.swing.JSplitPane JavaDoc;
46 import javax.swing.JTree JavaDoc;
47 import javax.swing.MenuElement JavaDoc;
48 import javax.swing.SwingUtilities JavaDoc;
49 import javax.swing.tree.DefaultTreeCellRenderer JavaDoc;
50 import javax.swing.tree.TreeCellRenderer JavaDoc;
51 import javax.swing.tree.TreeModel JavaDoc;
52
53 import org.apache.jmeter.engine.event.LoopIterationEvent;
54 import org.apache.jmeter.gui.action.ActionRouter;
55 import org.apache.jmeter.gui.action.GlobalMouseListener;
56 import org.apache.jmeter.gui.tree.JMeterCellRenderer;
57 import org.apache.jmeter.gui.tree.JMeterTreeListener;
58 import org.apache.jmeter.gui.util.JMeterMenuBar;
59 import org.apache.jmeter.samplers.Remoteable;
60 import org.apache.jmeter.testelement.TestListener;
61 import org.apache.jmeter.util.JMeterUtils;
62 import org.apache.jorphan.gui.ComponentUtil;
63
64 /**
65  * The main JMeter frame, containing the menu bar, test tree, and an area for
66  * JMeter component GUIs.
67  *
68  * @author Michael Stover
69  * @version $Revision: 1.24 $
70  */

71 public class MainFrame extends JFrame JavaDoc implements TestListener, Remoteable
72 {
73
74     /** The menu bar. */
75     private JMeterMenuBar menuBar;
76     
77     /** The main panel where components display their GUIs. */
78     private JScrollPane JavaDoc mainPanel;
79     
80     /** The panel where the test tree is shown. */
81     private JScrollPane JavaDoc treePanel;
82     
83     /** The test tree. */
84     private JTree JavaDoc tree;
85
86
87     /** An image which is displayed when a test is running. */
88     private ImageIcon JavaDoc runningIcon = JMeterUtils.getImage("thread.enabled.gif");
89     
90     /** An image which is displayed when a test is not currently running. */
91     private ImageIcon JavaDoc stoppedIcon = JMeterUtils.getImage("thread.disabled.gif");
92     
93     /** The button used to display the running/stopped image. */
94     private JButton JavaDoc runningIndicator;
95
96
97     /** The x coordinate of the last location where a component was dragged. */
98     private int previousDragXLocation = 0;
99     
100     /** The y coordinate of the last location where a component was dragged. */
101     private int previousDragYLocation = 0;
102     
103     /** The set of currently running hosts. */
104     private Set JavaDoc hosts = new HashSet JavaDoc();
105     
106     /** A message dialog shown while JMeter threads are stopping. */
107     private JDialog JavaDoc stoppingMessage;
108
109
110     /**
111      * Create a new JMeter frame.
112      *
113      * @param actionHandler this parameter is not used
114      * @param treeModel the model for the test tree
115      * @param treeListener the listener for the test tree
116      */

117     public MainFrame(
118         ActionListener JavaDoc actionHandler,
119         TreeModel JavaDoc treeModel,
120         JMeterTreeListener treeListener)
121     {
122         // TODO: actionHandler isn't used -- remove it from the parameter list
123
//this.actionHandler = actionHandler;
124

125         // TODO: Make the running indicator its own class instead of a JButton
126
runningIndicator = new JButton JavaDoc(stoppedIcon);
127         runningIndicator.setMargin(new Insets JavaDoc(0, 0, 0, 0));
128         runningIndicator.setBorder(BorderFactory.createEmptyBorder());
129         
130         tree = makeTree(treeModel, treeListener);
131         
132         GuiPackage.getInstance().setMainFrame(this);
133         init();
134
135         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
136     }
137
138     /**
139      * Default constructor for the JMeter frame. This constructor will not
140      * properly initialize the tree, so don't use it.
141      */

142     public MainFrame()
143     {
144         // TODO: Can we remove this constructor? JMeter won't behave properly
145
// if it used.
146
}
147
148
149     // MenuBar related methods
150
// TODO: Do we really need to have all these menubar methods duplicated
151
// here? Perhaps we can make the menu bar accessible through GuiPackage?
152

153     /**
154      * Specify whether or not the File|Load menu item should be enabled.
155      *
156      * @param enabled true if the menu item should be enabled, false otherwise
157      */

158     public void setFileLoadEnabled(boolean enabled)
159     {
160         menuBar.setFileLoadEnabled(enabled);
161     }
162
163     /**
164      * Specify whether or not the File|Save menu item should be enabled.
165      *
166      * @param enabled true if the menu item should be enabled, false otherwise
167      */

168     public void setFileSaveEnabled(boolean enabled)
169     {
170         menuBar.setFileSaveEnabled(enabled);
171     }
172
173     /**
174      * Set the menu that should be used for the Edit menu.
175      *
176      * @param menu the new Edit menu
177      */

178     public void setEditMenu(JPopupMenu JavaDoc menu)
179     {
180         menuBar.setEditMenu(menu);
181     }
182
183     /**
184      * Specify whether or not the Edit menu item should be enabled.
185      *
186      * @param enabled true if the menu item should be enabled, false otherwise
187      */

188     public void setEditEnabled(boolean enabled)
189     {
190         menuBar.setEditEnabled(enabled);
191     }
192
193     /**
194      * Set the menu that should be used for the Edit|Add menu.
195      *
196      * @param menu the new Edit|Add menu
197      */

198     public void setEditAddMenu(JMenu JavaDoc menu)
199     {
200         menuBar.setEditAddMenu(menu);
201     }
202
203     /**
204      * Specify whether or not the Edit|Add menu item should be enabled.
205      *
206      * @param enabled true if the menu item should be enabled, false otherwise
207      */

208     public void setEditAddEnabled(boolean enabled)
209     {
210         menuBar.setEditAddEnabled(enabled);
211     }
212
213     /**
214      * Specify whether or not the Edit|Remove menu item should be enabled.
215      *
216      * @param enabled true if the menu item should be enabled, false otherwise
217      */

218     public void setEditRemoveEnabled(boolean enabled)
219     {
220         menuBar.setEditRemoveEnabled(enabled);
221     }
222
223     /**
224      * Close the currently selected menu.
225      */

226     public void closeMenu()
227     {
228         if (menuBar.isSelected())
229         {
230             MenuElement JavaDoc[] menuElement = menuBar.getSubElements();
231             if (menuElement != null)
232             {
233                 for (int i = 0; i < menuElement.length; i++)
234                 {
235                     JMenu JavaDoc menu = (JMenu JavaDoc) menuElement[i];
236                     if (menu.isSelected())
237                     {
238                         menu.setPopupMenuVisible(false);
239                         menu.setSelected(false);
240                         break;
241                     }
242                 }
243             }
244         }
245     }
246
247     /**
248      * Show a dialog indicating that JMeter threads are stopping on a
249      * particular host.
250      *
251      * @param host the host where JMeter threads are stopping
252      */

253     public void showStoppingMessage(String JavaDoc host)
254     {
255         stoppingMessage =
256             new JDialog JavaDoc(
257                 this,
258                 JMeterUtils.getResString("stopping_test_title"),
259                 true);
260         JLabel JavaDoc stopLabel =
261             new JLabel JavaDoc(JMeterUtils.getResString("stopping_test") + ": " + host);
262         stopLabel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
263         stoppingMessage.getContentPane().add(stopLabel);
264         stoppingMessage.pack();
265         ComponentUtil.centerComponentInComponent(this, stoppingMessage);
266         SwingUtilities.invokeLater(new Runnable JavaDoc()
267         {
268             public void run()
269             {
270                 if (stoppingMessage != null)
271                 {
272                     stoppingMessage.show();
273                 }
274             }
275         });
276     }
277
278     /****************************************
279      * !ToDo (Method description)
280      *
281      *@param comp !ToDo (Parameter description)
282      ***************************************/

283     public void setMainPanel(JComponent JavaDoc comp)
284     {
285         mainPanel.setViewportView(comp);
286     }
287
288     /****************************************
289      * !ToDoo (Method description)
290      *
291      *@return !ToDo (Return description)
292      ***************************************/

293     public JTree JavaDoc getTree()
294     {
295         return tree;
296     }
297
298
299
300     // TestListener implementation
301

302     /**
303      * Called when a test is started on the local system. This implementation
304      * sets the running indicator and ensures that the menubar is enabled and
305      * in the running state.
306      */

307     public void testStarted()
308     {
309         testStarted("local");
310         menuBar.setEnabled(true);
311     }
312
313     /**
314      * Called when a test is started on a specific host. This implementation
315      * sets the running indicator and ensures that the menubar is in the
316      * running state.
317      *
318      * @param host the host where the test is starting
319      */

320     public void testStarted(String JavaDoc host)
321     {
322         hosts.add(host);
323         runningIndicator.setIcon(runningIcon);
324         menuBar.setRunning(true, host);
325     }
326
327     /**
328      * Called when a test is ended on the local system. This implementation
329      * disables the menubar, stops the running indicator, and closes the
330      * stopping message dialog.
331      */

332     public void testEnded()
333     {
334         testEnded("local");
335         menuBar.setEnabled(false);
336     }
337
338     /**
339      * Called when a test is ended on the remote system. This implementation
340      * stops the running indicator and closes the stopping message dialog.
341      *
342      * @param host the host where the test is ending
343      */

344     public void testEnded(String JavaDoc host)
345     {
346         hosts.remove(host);
347         if (hosts.size() == 0)
348         {
349             runningIndicator.setIcon(stoppedIcon);
350         }
351         menuBar.setRunning(false, host);
352         if (stoppingMessage != null)
353         {
354             stoppingMessage.dispose();
355             stoppingMessage = null;
356         }
357     }
358     
359     /* Implements TestListener#testIterationStart(LoopIterationEvent) */
360     public void testIterationStart(LoopIterationEvent event)
361     {
362     }
363
364
365
366     /**
367      * Create the GUI components and layout.
368      */

369     private void init()
370     {
371         menuBar = new JMeterMenuBar();
372         setJMenuBar(menuBar);
373         
374
375         JPanel JavaDoc all = new JPanel JavaDoc(new BorderLayout JavaDoc());
376         all.add(createToolBar(), BorderLayout.NORTH);
377         
378         JSplitPane JavaDoc treeAndMain = new JSplitPane JavaDoc(JSplitPane.HORIZONTAL_SPLIT);
379
380         treePanel = createTreePanel();
381         treeAndMain.setLeftComponent(treePanel);
382
383         mainPanel = createMainPanel();
384         treeAndMain.setRightComponent(mainPanel);
385
386
387         treeAndMain.setResizeWeight(.2);
388         treeAndMain.setContinuousLayout(true);
389         all.add(treeAndMain, BorderLayout.CENTER);
390
391         getContentPane().add(all);
392
393         tree.setSelectionRow(1);
394         addWindowListener(new WindowHappenings());
395         addMouseListener(new GlobalMouseListener());
396     }
397
398     /**
399      * Create the JMeter tool bar pane containing the running indicator.
400      *
401      * @return a panel containing the running indicator
402      */

403     private Component JavaDoc createToolBar()
404     {
405         Box JavaDoc toolPanel = new Box JavaDoc(BoxLayout.X_AXIS);
406         toolPanel.add(Box.createRigidArea(new Dimension JavaDoc(10, 15)));
407         toolPanel.add(Box.createGlue());
408         toolPanel.add(runningIndicator);
409         return toolPanel;
410     }
411
412     /**
413      * Create the panel where the GUI representation of the test tree is
414      * displayed. The tree should already be created before calling this
415      * method.
416      *
417      * @return a scroll pane containing the test tree GUI
418      */

419     private JScrollPane JavaDoc createTreePanel()
420     {
421         JScrollPane JavaDoc treePanel = new JScrollPane JavaDoc(tree);
422         treePanel.setMinimumSize(new Dimension JavaDoc(100, 0));
423         return treePanel;
424     }
425
426     /**
427      * Create the main panel where components can display their GUIs.
428      *
429      * @return the main scroll pane
430      */

431     private JScrollPane JavaDoc createMainPanel()
432     {
433         return new JScrollPane JavaDoc();
434     }
435
436     /**
437      * Create and initialize the GUI representation of the test tree.
438      *
439      * @param treeModel the test tree model
440      * @param treeListener the test tree listener
441      *
442      * @return the initialized test tree GUI
443      */

444     private JTree JavaDoc makeTree(TreeModel JavaDoc treeModel, JMeterTreeListener treeListener)
445     {
446         JTree JavaDoc tree = new JTree JavaDoc(treeModel);
447         tree.setCellRenderer(getCellRenderer());
448         tree.setRootVisible(false);
449         tree.setShowsRootHandles(true);
450
451         treeListener.setJTree(tree);
452         tree.addTreeSelectionListener(treeListener);
453         tree.addMouseListener(treeListener);
454         tree.addMouseMotionListener(treeListener);
455         tree.addKeyListener(treeListener);
456
457         return tree;
458     }
459
460     /**
461      * Create the tree cell renderer used to draw the nodes in the test tree.
462      *
463      * @return a renderer to draw the test tree nodes
464      */

465     private TreeCellRenderer JavaDoc getCellRenderer()
466     {
467         DefaultTreeCellRenderer JavaDoc rend = new JMeterCellRenderer();
468         rend.setFont(new Font JavaDoc("Dialog", Font.PLAIN, 11));
469         return rend;
470     }
471
472     /**
473      * Repaint pieces of the GUI as needed while dragging. This method should
474      * only be called from the Swing event thread.
475      *
476      * @param dragIcon the component being dragged
477      * @param x the current mouse x coordinate
478      * @param y the current mouse y coordinate
479      */

480     public void drawDraggedComponent(Component JavaDoc dragIcon, int x, int y)
481     {
482         Dimension JavaDoc size = dragIcon.getPreferredSize();
483         treePanel.paintImmediately(
484             previousDragXLocation,
485             previousDragYLocation,
486             size.width,
487             size.height);
488         this.getLayeredPane().setLayer(dragIcon, 400);
489         SwingUtilities.paintComponent(
490             treePanel.getGraphics(),
491             dragIcon,
492             treePanel,
493             x,
494             y,
495             size.width,
496             size.height);
497         previousDragXLocation = x;
498         previousDragYLocation = y;
499     }
500
501
502     /**
503      * A window adapter used to detect when the main JMeter frame is being
504      * closed.
505      */

506     private class WindowHappenings extends WindowAdapter JavaDoc
507     {
508         /**
509          * Called when the main JMeter frame is being closed. Sends a
510          * notification so that JMeter can react appropriately.
511          *
512          * @param event the WindowEvent to handle
513          */

514         public void windowClosing(WindowEvent JavaDoc event)
515         {
516             ActionRouter.getInstance().actionPerformed(
517                 new ActionEvent JavaDoc(this, event.getID(), "exit"));
518         }
519     }
520 }
521
Popular Tags