KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/control/gui/IfControllerPanel.java,v 1.3 2004/03/05 01:34:05 sebb Exp $
2
/*
3  * Copyright 2003-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.IfController;
31 import org.apache.jmeter.gui.util.FocusRequester;
32 import org.apache.jmeter.testelement.TestElement;
33 import org.apache.jmeter.util.JMeterUtils;
34
35 /**
36  * The user interface for a controller which specifies that its subcomponents
37  * should be executed while a condition holds. This component can be
38  * used standalone or embedded into some other component.
39  *
40  * @author Cyrus Montakab
41  * @version $Revision: 1.3 $ $Date: 2004/03/05 01:34:05 $
42  */

43
44 public class IfControllerPanel extends AbstractControllerGui
45     implements ActionListener JavaDoc
46 {
47
48       private static final String JavaDoc CONDITION_LABEL = "if_controller_label";
49
50     /**
51      * A field allowing the user to specify the number of times the controller
52      * should loop.
53      */

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

61     private boolean displayName = true;
62
63
64     /** The name of the loops field component. */
65     private static final String JavaDoc CONDITION = "JS_Condition";
66
67     /**
68      * Create a new LoopControlPanel as a standalone component.
69      */

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

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

97     public void configure(TestElement element) {
98         super.configure(element);
99         if (element instanceof IfController) {
100             theCondition.setText(((IfController) element).getCondition());
101         }
102
103     }
104
105     /**
106      * Implements JMeterGUIComponent.createTestElement()
107      */

108     public TestElement createTestElement() {
109         IfController controller = new IfController();
110         modifyTestElement(controller);
111         return controller;
112     }
113
114     /**
115      * Implements JMeterGUIComponent.modifyTestElement(TestElement)
116      */

117     public void modifyTestElement(TestElement controller) {
118         configureTestElement(controller);
119         if (controller instanceof IfController)
120         {
121             if (theCondition.getText().length() > 0) {
122                 ((IfController) controller).setCondition(theCondition.getText());
123             } else {
124                 ((IfController) controller).setCondition("");
125             }
126         }
127     }
128
129     /**
130      * Invoked when an action occurs. This implementation assumes that the
131      * target component is the infinite loops checkbox.
132      *
133      * @param event the event that has occurred
134      */

135     public void actionPerformed(ActionEvent JavaDoc event) {
136         new FocusRequester(theCondition);
137     }
138
139     public String JavaDoc getLabelResource() {
140         return "if_controller_title";
141     }
142
143     /**
144      * Initialize the GUI components and layout for this component.
145      */

146     private void init()
147     {
148         // Standalone
149
if (displayName) {
150             setLayout(new BorderLayout JavaDoc(0, 5));
151             setBorder(makeBorder());
152             add(makeTitlePanel(), BorderLayout.NORTH);
153
154             JPanel JavaDoc mainPanel = new JPanel JavaDoc(new BorderLayout JavaDoc());
155             mainPanel.add(createConditionPanel(), BorderLayout.NORTH);
156             add(mainPanel, BorderLayout.CENTER);
157
158         } else {
159             // Embedded
160
setLayout(new BorderLayout JavaDoc());
161             add(createConditionPanel(), BorderLayout.NORTH);
162         }
163     }
164
165
166     /**
167      * Create a GUI panel containing the condition.
168      *
169      * @return a GUI panel containing the condition components
170      */

171     private JPanel JavaDoc createConditionPanel() {
172         JPanel JavaDoc conditionPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
173
174         // Condition LABEL
175
JLabel JavaDoc conditionLabel =
176         new JLabel JavaDoc(JMeterUtils.getResString( CONDITION_LABEL ));
177         conditionPanel.add(conditionLabel, BorderLayout.WEST);
178
179         // TEXT FIELD
180
theCondition = new JTextField JavaDoc("");
181         theCondition.setName(CONDITION);
182         conditionLabel.setLabelFor(theCondition);
183         conditionPanel.add(theCondition, BorderLayout.CENTER);
184         theCondition.addActionListener(this);
185
186         conditionPanel.add(
187             Box.createHorizontalStrut( conditionLabel.getPreferredSize().width
188             + theCondition.getPreferredSize().width)
189             , BorderLayout.NORTH);
190
191         return conditionPanel;
192     }
193 }
Popular Tags