KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/gui/LoopControlPanel.java,v 1.13 2004/03/05 01:34:05 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.JCheckBox JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import javax.swing.JPanel JavaDoc;
29 import javax.swing.JTextField JavaDoc;
30
31 import org.apache.jmeter.control.LoopController;
32 import org.apache.jmeter.gui.util.FocusRequester;
33 import org.apache.jmeter.testelement.TestElement;
34 import org.apache.jmeter.util.JMeterUtils;
35
36 /**
37  * The user interface for a controller which specifies that its subcomponents
38  * should be executed some number of times in a loop. This component can be
39  * used standalone or embedded into some other component.
40  *
41  * @version $Revision: 1.13 $ on $Date: 2004/03/05 01:34:05 $
42  */

43
44 public class LoopControlPanel
45     extends AbstractControllerGui
46     implements ActionListener JavaDoc
47 {
48     /**
49      * A checkbox allowing the user to specify whether or not the controller
50      * should loop forever.
51      */

52     private JCheckBox JavaDoc infinite;
53     
54     /**
55      * A field allowing the user to specify the number of times the controller
56      * should loop.
57      */

58     private JTextField JavaDoc loops;
59
60    
61     /**
62      * Boolean indicating whether or not this component should display its
63      * name. If true, this is a standalone component. If false, this component
64      * is intended to be used as a subpanel for another component.
65      */

66     private boolean displayName = true;
67     
68     /** The name of the infinite checkbox component. */
69     private static final String JavaDoc INFINITE = "Infinite Field";
70     
71     /** The name of the loops field component. */
72     private static final String JavaDoc LOOPS = "Loops Field";
73
74     /**
75      * Create a new LoopControlPanel as a standalone component.
76      */

77     public LoopControlPanel()
78     {
79         this(true);
80     }
81
82     /**
83      * Create a new LoopControlPanel as either a standalone or an embedded
84      * component.
85      *
86      * @param displayName indicates whether or not this component should
87      * display its name. If true, this is a standalone
88      * component. If false, this component is intended
89      * to be used as a subpanel for another component.
90      */

91     public LoopControlPanel(boolean displayName)
92     {
93         this.displayName = displayName;
94         init();
95         setState(1);
96     }
97
98     /**
99      * A newly created component can be initialized with the contents of
100      * a Test Element object by calling this method. The component is
101      * responsible for querying the Test Element object for the
102      * relevant information to display in its GUI.
103      *
104      * @param element the TestElement to configure
105      */

106     public void configure(TestElement element)
107     {
108         super.configure(element);
109         if (element instanceof LoopController)
110         {
111             setState(((LoopController) element).getLoopString());
112         }
113         else
114         {
115             setState(1);
116         }
117     }
118
119     /* Implements JMeterGUIComponent.createTestElement() */
120     public TestElement createTestElement()
121     {
122         LoopController lc = new LoopController();
123         modifyTestElement(lc);
124         return lc;
125     }
126
127     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
128     public void modifyTestElement(TestElement lc)
129     {
130         configureTestElement(lc);
131         if (lc instanceof LoopController)
132         {
133             if (loops.getText().length() > 0)
134             {
135                 ((LoopController) lc).setLoops(loops.getText());
136             }
137             else
138             {
139                 ((LoopController) lc).setLoops(-1);
140             }
141         }
142     }
143
144     /**
145      * Invoked when an action occurs. This implementation assumes that the
146      * target component is the infinite loops checkbox.
147      *
148      * @param event the event that has occurred
149      */

150     public void actionPerformed(ActionEvent JavaDoc event)
151     {
152         if (infinite.isSelected())
153         {
154             loops.setText("");
155             loops.setEnabled(false);
156         }
157         else
158         {
159             loops.setEnabled(true);
160             new FocusRequester(loops);
161         }
162     }
163
164     public String JavaDoc getLabelResource()
165     {
166         return "loop_controller_title";
167     }
168
169     /**
170      * Initialize the GUI components and layout for this component.
171      */

172     private void init()
173     {
174         // The Loop Controller panel can be displayed standalone or inside
175
// another panel. For standalone, we want to display the TITLE, NAME,
176
// etc. (everything). However, if we want to display it within another
177
// panel, we just display the Loop Count fields (not the TITLE and
178
// NAME).
179

180         // Standalone
181
if (displayName)
182         {
183             setLayout(new BorderLayout JavaDoc(0, 5));
184             setBorder(makeBorder());
185             add(makeTitlePanel(), BorderLayout.NORTH);
186             
187             JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
188             mainPanel.add(createLoopCountPanel(), BorderLayout.NORTH);
189             add(mainPanel, BorderLayout.CENTER);
190         }
191         else
192         {
193             // Embedded
194
setLayout(new BorderLayout JavaDoc());
195             add(createLoopCountPanel(), BorderLayout.NORTH);
196         }
197     }
198
199     /**
200      * Create a GUI panel containing the components related to the number of
201      * loops which should be executed.
202      *
203      * @return a GUI panel containing the loop count components
204      */

205     private JPanel JavaDoc createLoopCountPanel()
206     {
207         JPanel JavaDoc loopPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
208
209         // LOOP LABEL
210
JLabel JavaDoc loopsLabel =
211             new JLabel JavaDoc(JMeterUtils.getResString("iterator_num"));
212         loopPanel.add(loopsLabel, BorderLayout.WEST);
213
214         JPanel JavaDoc loopSubPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
215
216         // TEXT FIELD
217
loops = new JTextField JavaDoc("1", 5);
218         loops.setName(LOOPS);
219         loopsLabel.setLabelFor(loops);
220         loopSubPanel.add(loops, BorderLayout.CENTER);
221
222         // FOREVER CHECKBOX
223
infinite = new JCheckBox JavaDoc(JMeterUtils.getResString("infinite"));
224         infinite.setActionCommand(INFINITE);
225         infinite.addActionListener(this);
226         loopSubPanel.add(infinite, BorderLayout.WEST);
227         
228         loopPanel.add(loopSubPanel,BorderLayout.CENTER);
229     
230         loopPanel.add(
231             Box.createHorizontalStrut(
232                 loopsLabel.getPreferredSize().width
233                     + loops.getPreferredSize().width
234                     + infinite.getPreferredSize().width),
235             BorderLayout.NORTH);
236
237         return loopPanel;
238     }
239     
240     /**
241      * Set the number of loops which should be reflected in the GUI. The
242      * loopCount parameter should contain the String representation of an
243      * integer. This integer will be treated as the number of loops. If this
244      * integer is less than 0, the number of loops will be assumed to be
245      * infinity.
246      *
247      * @param loopCount the String representation of the number of loops
248      */

249     private void setState(String JavaDoc loopCount)
250     {
251         if (loopCount.startsWith("-"))
252         {
253             setState(-1);
254         }
255         else
256         {
257             loops.setText(loopCount);
258             infinite.setSelected(false);
259             loops.setEnabled(true);
260         }
261     }
262
263     /**
264      * Set the number of loops which should be reflected in the GUI. If the
265      * loopCount is less than 0, the number of loops will be assumed to be
266      * infinity.
267      *
268      * @param loopCount the number of loops
269      */

270     private void setState(int loopCount)
271     {
272         if (loopCount <= -1)
273         {
274             infinite.setSelected(true);
275             loops.setEnabled(false);
276             loops.setText("");
277         }
278         else
279         {
280             infinite.setSelected(false);
281             loops.setEnabled(true);
282             loops.setText("" + loopCount);
283         }
284     }
285 }
286
Popular Tags