KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > control > gui > RunTimeGui


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/gui/RunTimeGui.java,v 1.1.2.1 2004/06/03 00:08:36 sebb Exp $
2
/*
3  * Copyright 2000-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.control.gui;
20
21 import java.awt.BorderLayout JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24
25 import javax.swing.Box JavaDoc;
26 import javax.swing.JLabel JavaDoc;
27 import javax.swing.JPanel JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29
30 import org.apache.jmeter.control.RunTime;
31 import org.apache.jmeter.testelement.TestElement;
32 import org.apache.jmeter.util.JMeterUtils;
33
34 /**
35  * The user interface for a controller which specifies that its subcomponents
36  * should be executed some number of seconds in a loop. This component can be
37  * used standalone or embedded into some other component.
38  *
39  * @version $Revision: 1.1.2.1 $ on $Date: 2004/06/03 00:08:36 $
40  */

41
42 public class RunTimeGui
43     extends AbstractControllerGui
44     implements ActionListener JavaDoc
45 {
46     /**
47      * A field allowing the user to specify the number of seconds the controller
48      * should loop.
49      */

50     private JTextField JavaDoc seconds;
51
52    
53     /**
54      * Boolean indicating whether or not this component should display its
55      * name. If true, this is a standalone component. If false, this component
56      * is intended to be used as a subpanel for another component.
57      */

58     private boolean displayName = true;
59     
60     /** The name of the seconds field component. */
61     private static final String JavaDoc SECONDS = "Seconds Field";
62
63     /**
64      * Create a new LoopControlPanel as a standalone component.
65      */

66     public RunTimeGui()
67     {
68         this(true);
69     }
70
71     /**
72      * Create a new LoopControlPanel as either a standalone or an embedded
73      * component.
74      *
75      * @param displayName indicates whether or not this component should
76      * display its name. If true, this is a standalone
77      * component. If false, this component is intended
78      * to be used as a subpanel for another component.
79      */

80     public RunTimeGui(boolean displayName)
81     {
82         this.displayName = displayName;
83         init();
84         setState(1);
85     }
86
87     /**
88      * A newly created component can be initialized with the contents of
89      * a Test Element object by calling this method. The component is
90      * responsible for querying the Test Element object for the
91      * relevant information to display in its GUI.
92      *
93      * @param element the TestElement to configure
94      */

95     public void configure(TestElement element)
96     {
97         super.configure(element);
98         if (element instanceof RunTime)
99         {
100             setState(((RunTime) element).getRuntimeString());
101         }
102         else
103         {
104             setState(1);
105         }
106     }
107
108     /* Implements JMeterGUIComponent.createTestElement() */
109     public TestElement createTestElement()
110     {
111         RunTime lc = new RunTime();
112         modifyTestElement(lc);
113         return lc;
114     }
115
116     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
117     public void modifyTestElement(TestElement lc)
118     {
119         configureTestElement(lc);
120         if (lc instanceof RunTime)
121         {
122             if (seconds.getText().length() > 0)
123             {
124                 ((RunTime) lc).setRuntime(seconds.getText());
125             }
126             else
127             {
128                 ((RunTime) lc).setRuntime(0);
129             }
130         }
131     }
132
133     /**
134      * Invoked when an action occurs. This implementation assumes that the
135      * target component is the infinite seconds checkbox.
136      *
137      * @param event the event that has occurred
138      */

139     public void actionPerformed(ActionEvent JavaDoc event)
140     {
141         seconds.setEnabled(true);
142     }
143
144     public String JavaDoc getLabelResource()
145     {
146         return "runtime_controller_title";
147     }
148
149     /**
150      * Initialize the GUI components and layout for this component.
151      */

152     private void init()
153     {
154         // The Loop Controller panel can be displayed standalone or inside
155
// another panel. For standalone, we want to display the TITLE, NAME,
156
// etc. (everything). However, if we want to display it within another
157
// panel, we just display the Loop Count fields (not the TITLE and
158
// NAME).
159

160         // Standalone
161
if (displayName)
162         {
163             setLayout(new BorderLayout JavaDoc(0, 5));
164             setBorder(makeBorder());
165             add(makeTitlePanel(), BorderLayout.NORTH);
166             
167             JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
168             mainPanel.add(createLoopCountPanel(), BorderLayout.NORTH);
169             add(mainPanel, BorderLayout.CENTER);
170         }
171         else
172         {
173             // Embedded
174
setLayout(new BorderLayout JavaDoc());
175             add(createLoopCountPanel(), BorderLayout.NORTH);
176         }
177     }
178
179     /**
180      * Create a GUI panel containing the components related to the number of
181      * seconds which should be executed.
182      *
183      * @return a GUI panel containing the loop count components
184      */

185     private JPanel JavaDoc createLoopCountPanel()
186     {
187         JPanel JavaDoc loopPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
188
189         // SECONDS LABEL
190
JLabel JavaDoc secondsLabel =
191             new JLabel JavaDoc(JMeterUtils.getResString("runtime_seconds"));
192         loopPanel.add(secondsLabel, BorderLayout.WEST);
193
194         JPanel JavaDoc loopSubPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
195
196         // TEXT FIELD
197
seconds = new JTextField JavaDoc("60", 5);
198         secondsLabel.setLabelFor(seconds);
199         loopSubPanel.add(seconds, BorderLayout.CENTER);
200
201         loopPanel.add(loopSubPanel,BorderLayout.CENTER);
202     
203         loopPanel.add(
204             Box.createHorizontalStrut(
205                 secondsLabel.getPreferredSize().width
206                     + seconds.getPreferredSize().width),
207             BorderLayout.NORTH);
208
209         return loopPanel;
210     }
211     
212     /**
213      * Set the number of seconds which should be reflected in the GUI. The
214      * secsCount parameter should contain the String representation of an
215      * integer. This integer will be treated as the number of seconds. If this
216      * integer is less than 0, the number of seconds will be assumed to be
217      * infinity.
218      *
219      * @param secsCount the String representation of the number of seconds
220      */

221     private void setState(String JavaDoc secsCount)
222     {
223         seconds.setText(secsCount);
224         seconds.setEnabled(true);
225     }
226
227     /**
228      * Set the number of seconds which should be reflected in the GUI. If the
229      * secsCount is less than 0, the number of seconds will be assumed to be
230      * infinity.
231      *
232      * @param secsCount the number of seconds
233      */

234     private void setState(long secsCount)
235     {
236         seconds.setEnabled(true);
237         seconds.setText("" + secsCount);
238     }
239 }
240
Popular Tags