KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/gui/ForeachControlPanel.java,v 1.4.2.1 2005/03/17 03:03:39 sebb Exp $
2
/*
3  * Copyright 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
23 import javax.swing.JCheckBox JavaDoc;
24 import javax.swing.JLabel JavaDoc;
25 import javax.swing.JPanel JavaDoc;
26 import javax.swing.JTextField JavaDoc;
27
28 import org.apache.jmeter.control.ForeachController;
29 import org.apache.jmeter.gui.util.VerticalPanel;
30 import org.apache.jmeter.testelement.TestElement;
31 import org.apache.jmeter.util.JMeterUtils;
32
33 /**
34  * The user interface for a foreach controller which specifies that its subcomponents
35  * should be executed some number of times in a loop. This component can be
36  * used standalone or embedded into some other component.
37  * Copyright: 2000
38  *
39  * @version $Revision: 1.4.2.1 $ on $Date
40  */

41
42 public class ForeachControlPanel
43     extends AbstractControllerGui
44 {
45
46     /**
47      * A field allowing the user to specify the input variable the controller
48      * should loop.
49      */

50     private JTextField JavaDoc inputVal;
51
52     /**
53      * A field allowing the user to specify output variable the controller
54      * should return.
55      */

56     private JTextField JavaDoc returnVal;
57
58     // Should we add the "_" separator?
59
private JCheckBox JavaDoc useSeparator;
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 INPUTVAL = "Input Field";
70
71     /** The name of the loops field component. */
72     private static final String JavaDoc RETURNVAL = "Return Field";
73
74     /**
75      * Create a new LoopControlPanel as a standalone component.
76      */

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

105     public void configure(TestElement element)
106     {
107         super.configure(element);
108         inputVal.setText(((ForeachController) element).getInputValString());
109         returnVal.setText(((ForeachController) element).getReturnValString());
110         useSeparator.setSelected(((ForeachController) element).getUseSeparator());
111     }
112
113     /* Implements JMeterGUIComponent.createTestElement() */
114     public TestElement createTestElement()
115     {
116         ForeachController lc = new ForeachController();
117         modifyTestElement(lc);
118         return lc;
119     }
120
121     /* Implements JMeterGUIComponent.modifyTestElement(TestElement) */
122     public void modifyTestElement(TestElement lc)
123     {
124         configureTestElement(lc);
125         if (lc instanceof ForeachController)
126         {
127             if (inputVal.getText().length() > 0)
128             {
129                 ((ForeachController) lc).setInputVal(inputVal.getText());
130             }
131             else
132             {
133                 ((ForeachController) lc).setInputVal("");
134             }
135             if (returnVal.getText().length() > 0)
136             {
137                 ((ForeachController) lc).setReturnVal(returnVal.getText());
138             }
139             else
140             {
141                 ((ForeachController) lc).setReturnVal("");
142             }
143             ((ForeachController) lc).setUseSeparator(useSeparator.isSelected());
144         }
145     }
146
147
148     public String JavaDoc getLabelResource()
149     {
150         return "foreach_controller_title";
151     }
152
153     /**
154      * Initialize the GUI components and layout for this component.
155      */

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

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

189     private JPanel JavaDoc createLoopCountPanel()
190     {
191 // JPanel loopPanel = new JPanel(new BorderLayout(5, 0));
192
VerticalPanel loopPanel = new VerticalPanel();
193
194         // LOOP LABEL
195
JLabel JavaDoc inputValLabel =
196             new JLabel JavaDoc(JMeterUtils.getResString("foreach_input"));
197         JLabel JavaDoc returnValLabel =
198             new JLabel JavaDoc(JMeterUtils.getResString("foreach_output"));
199
200         // TEXT FIELD
201
JPanel JavaDoc inputValSubPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
202         inputVal = new JTextField JavaDoc("", 5);
203         inputVal.setName(INPUTVAL);
204         inputValLabel.setLabelFor(inputVal);
205         inputValSubPanel.add(inputValLabel, BorderLayout.WEST);
206         inputValSubPanel.add(inputVal, BorderLayout.CENTER);
207
208         // TEXT FIELD
209
JPanel JavaDoc returnValSubPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
210         returnVal = new JTextField JavaDoc("", 5);
211         returnVal.setName(RETURNVAL);
212         returnValLabel.setLabelFor(returnVal);
213         returnValSubPanel.add(returnValLabel, BorderLayout.WEST);
214         returnValSubPanel.add(returnVal, BorderLayout.CENTER);
215
216         // Checkbox
217
useSeparator = new JCheckBox JavaDoc(JMeterUtils.getResString("foreach_use_separator"),true);
218         
219         loopPanel.add(inputValSubPanel);
220         loopPanel.add(returnValSubPanel);
221         loopPanel.add(useSeparator);
222
223         return loopPanel;
224     }
225
226 }
227
Popular Tags