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.JLabel;
027: import javax.swing.JPanel;
028: import javax.swing.JTextField;
029:
030: import org.apache.jmeter.control.WhileController;
031: import org.apache.jmeter.gui.util.FocusRequester;
032: import org.apache.jmeter.testelement.TestElement;
033: import org.apache.jmeter.util.JMeterUtils;
034:
035: public class WhileControllerGui extends AbstractControllerGui implements
036: ActionListener {
037:
038: private static final String CONDITION_LABEL = "while_controller_label"; // $NON-NLS-1$
039:
040: /**
041: * A field allowing the user to specify the condition (not yet used).
042: */
043: private JTextField theCondition;
044:
045: /** The name of the condition field component. */
046: private static final String CONDITION = "While_Condition"; // $NON-NLS-1$
047:
048: /**
049: * Create a new LoopControlPanel as a standalone component.
050: */
051: public WhileControllerGui() {
052: init();
053: }
054:
055: /**
056: * A newly created component can be initialized with the contents of a Test
057: * Element object by calling this method. The component is responsible for
058: * querying the Test Element object for the relevant information to display
059: * in its GUI.
060: *
061: * @param element
062: * the TestElement to configure
063: */
064: public void configure(TestElement element) {
065: super .configure(element);
066: if (element instanceof WhileController) {
067: theCondition.setText(((WhileController) element)
068: .getCondition());
069: }
070:
071: }
072:
073: /**
074: * Implements JMeterGUIComponent.createTestElement()
075: */
076: public TestElement createTestElement() {
077: WhileController controller = new WhileController();
078: modifyTestElement(controller);
079: return controller;
080: }
081:
082: /**
083: * Implements JMeterGUIComponent.modifyTestElement(TestElement)
084: */
085: public void modifyTestElement(TestElement controller) {
086: configureTestElement(controller);
087: if (controller instanceof WhileController) {
088: if (theCondition.getText().length() > 0) {
089: ((WhileController) controller)
090: .setCondition(theCondition.getText());
091: } else {
092: ((WhileController) controller).setCondition(""); // $NON-NLS-1$
093: }
094: }
095: }
096:
097: /**
098: * Implements JMeterGUIComponent.clearGui
099: */
100: public void clearGui() {
101: super .clearGui();
102: theCondition.setText(""); // $NON-NLS-1$
103: }
104:
105: /**
106: * Invoked when an action occurs. This implementation assumes that the
107: * target component is the infinite loops checkbox.
108: *
109: * @param event
110: * the event that has occurred
111: */
112: public void actionPerformed(ActionEvent event) {
113: new FocusRequester(theCondition);
114: }
115:
116: public String getLabelResource() {
117: return "while_controller_title"; // $NON-NLS-1$
118: }
119:
120: /**
121: * Initialize the GUI components and layout for this component.
122: */
123: private void init() {
124: setLayout(new BorderLayout(0, 5));
125: setBorder(makeBorder());
126: add(makeTitlePanel(), BorderLayout.NORTH);
127:
128: JPanel mainPanel = new JPanel(new BorderLayout());
129: mainPanel.add(createConditionPanel(), BorderLayout.NORTH);
130: add(mainPanel, BorderLayout.CENTER);
131:
132: }
133:
134: /**
135: * Create a GUI panel containing the condition. TODO make use of the field
136: *
137: * @return a GUI panel containing the condition components
138: */
139: private JPanel createConditionPanel() {
140: JPanel conditionPanel = new JPanel(new BorderLayout(5, 0));
141:
142: // Condition LABEL
143: JLabel conditionLabel = new JLabel(JMeterUtils
144: .getResString(CONDITION_LABEL));
145: conditionPanel.add(conditionLabel, BorderLayout.WEST);
146:
147: // TEXT FIELD
148: // This means exit if last sample failed
149: theCondition = new JTextField(""); // $NON-NLS-1$
150: theCondition.setName(CONDITION);
151: conditionLabel.setLabelFor(theCondition);
152: conditionPanel.add(theCondition, BorderLayout.CENTER);
153: theCondition.addActionListener(this);
154:
155: conditionPanel.add(Box.createHorizontalStrut(conditionLabel
156: .getPreferredSize().width
157: + theCondition.getPreferredSize().width),
158: BorderLayout.NORTH);
159:
160: return conditionPanel;
161: }
162: }
|