001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.control.gui;
020:
021: import java.awt.BorderLayout;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024:
025: import javax.swing.Box;
026: import javax.swing.JCheckBox;
027: import javax.swing.JLabel;
028: import javax.swing.JPanel;
029: import javax.swing.JTextField;
030:
031: import org.apache.jmeter.control.IfController;
032: import org.apache.jmeter.gui.util.FocusRequester;
033: import org.apache.jmeter.testelement.TestElement;
034: import org.apache.jmeter.util.JMeterUtils;
035:
036: /**
037: * The user interface for a controller which specifies that its subcomponents
038: * should be executed while a condition holds. This component can be used
039: * standalone or embedded into some other component.
040: *
041: */
042:
043: public class IfControllerPanel extends AbstractControllerGui implements
044: ActionListener {
045:
046: /**
047: * A field allowing the user to specify the number of times the controller
048: * should loop.
049: */
050: private JTextField theCondition;
051:
052: private JCheckBox evaluateAll;
053:
054: /**
055: * Boolean indicating whether or not this component should display its name.
056: * If true, this is a standalone component. If false, this component is
057: * intended to be used as a subpanel for another component.
058: */
059: private boolean displayName = true;
060:
061: /**
062: * Create a new LoopControlPanel as a standalone component.
063: */
064: public IfControllerPanel() {
065: this (true);
066: }
067:
068: /**
069: * Create a new IfControllerPanel as either a standalone or an embedded
070: * component.
071: *
072: * @param displayName
073: * indicates whether or not this component should display its
074: * name. If true, this is a standalone component. If false, this
075: * component is intended to be used as a subpanel for another
076: * component.
077: */
078: public IfControllerPanel(boolean displayName) {
079: this .displayName = displayName;
080: init();
081: }
082:
083: /**
084: * A newly created component can be initialized with the contents of a Test
085: * Element object by calling this method. The component is responsible for
086: * querying the Test Element object for the relevant information to display
087: * in its GUI.
088: *
089: * @param element
090: * the TestElement to configure
091: */
092: public void configure(TestElement element) {
093: super .configure(element);
094: if (element instanceof IfController) {
095: IfController ifController = (IfController) element;
096: theCondition.setText(ifController.getCondition());
097: evaluateAll.setSelected(ifController.isEvaluateAll());
098: }
099:
100: }
101:
102: /**
103: * Implements JMeterGUIComponent.createTestElement()
104: */
105: public TestElement createTestElement() {
106: IfController controller = new IfController();
107: modifyTestElement(controller);
108: return controller;
109: }
110:
111: /**
112: * Implements JMeterGUIComponent.modifyTestElement(TestElement)
113: */
114: public void modifyTestElement(TestElement controller) {
115: configureTestElement(controller);
116: if (controller instanceof IfController) {
117: IfController ifController = (IfController) controller;
118: ifController.setCondition(theCondition.getText());
119: ifController.setEvaluateAll(evaluateAll.isSelected());
120: }
121: }
122:
123: /**
124: * Implements JMeterGUIComponent.clearGui
125: */
126: public void clearGui() {
127: super .clearGui();
128: theCondition.setText(""); // $NON-NLS-1$
129: evaluateAll.setSelected(false);
130: }
131:
132: /**
133: * Invoked when an action occurs. This implementation assumes that the
134: * target component is the infinite loops checkbox.
135: *
136: * @param event
137: * the event that has occurred
138: */
139: public void actionPerformed(ActionEvent event) {
140: new FocusRequester(theCondition);
141: }
142:
143: public String getLabelResource() {
144: return "if_controller_title"; // $NON-NLS-1$
145: }
146:
147: /**
148: * Initialize the GUI components and layout for this component.
149: */
150: private void init() {
151: // Standalone
152: if (displayName) {
153: setLayout(new BorderLayout(0, 5));
154: setBorder(makeBorder());
155: add(makeTitlePanel(), BorderLayout.NORTH);
156:
157: JPanel mainPanel = new JPanel(new BorderLayout());
158: mainPanel.add(createConditionPanel(), BorderLayout.NORTH);
159: add(mainPanel, BorderLayout.CENTER);
160:
161: } else {
162: // Embedded
163: setLayout(new BorderLayout());
164: add(createConditionPanel(), BorderLayout.NORTH);
165: }
166: }
167:
168: /**
169: * Create a GUI panel containing the condition.
170: *
171: * @return a GUI panel containing the condition components
172: */
173: private JPanel createConditionPanel() {
174: JPanel conditionPanel = new JPanel(new BorderLayout(5, 0));
175:
176: // Condition LABEL
177: JLabel conditionLabel = new JLabel(JMeterUtils
178: .getResString("if_controller_label")); // $NON-NLS-1$
179: conditionPanel.add(conditionLabel, BorderLayout.WEST);
180:
181: // TEXT FIELD
182: theCondition = new JTextField(""); // $NON-NLS-1$
183: conditionLabel.setLabelFor(theCondition);
184: conditionPanel.add(theCondition, BorderLayout.CENTER);
185: theCondition.addActionListener(this );
186:
187: conditionPanel.add(Box.createHorizontalStrut(conditionLabel
188: .getPreferredSize().width
189: + theCondition.getPreferredSize().width),
190: BorderLayout.NORTH);
191:
192: // Evaluate All checkbox
193: evaluateAll = new JCheckBox(JMeterUtils
194: .getResString("if_controller_evaluate_all")); // $NON-NLS-1$
195: conditionPanel.add(evaluateAll, BorderLayout.SOUTH);
196: return conditionPanel;
197: }
198: }
|