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.sampler.gui;
020:
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.FocusEvent;
024: import java.awt.event.FocusListener;
025:
026: import javax.swing.ButtonGroup;
027: import javax.swing.DefaultComboBoxModel;
028: import javax.swing.JComboBox;
029: import javax.swing.JLabel;
030: import javax.swing.JRadioButton;
031: import javax.swing.JTextField;
032: import javax.swing.event.ChangeEvent;
033: import javax.swing.event.ChangeListener;
034:
035: import org.apache.jmeter.gui.util.HorizontalPanel;
036: import org.apache.jmeter.sampler.TestAction;
037: import org.apache.jmeter.samplers.gui.AbstractSamplerGui;
038: import org.apache.jmeter.testelement.TestElement;
039: import org.apache.jmeter.util.JMeterUtils;
040: import org.apache.jorphan.gui.layout.VerticalLayout;
041:
042: public class TestActionGui extends AbstractSamplerGui {
043: // Gui components
044: private JComboBox targetBox;
045:
046: // private ButtonGroup actionButtons;
047: private JRadioButton pauseButton;
048:
049: private JRadioButton stopButton;
050:
051: private JTextField durationField;
052:
053: // State variables
054: private int target;
055:
056: private int action;
057:
058: private String durationString;
059:
060: // String in the panel
061: private static final String targetLabel = JMeterUtils
062: .getResString("test_action_target"); // $NON-NLS-1$
063:
064: private static final String threadTarget = JMeterUtils
065: .getResString("test_action_target_thread"); // $NON-NLS-1$
066:
067: private static final String testTarget = JMeterUtils
068: .getResString("test_action_target_test"); // $NON-NLS-1$
069:
070: private static final String actionLabel = JMeterUtils
071: .getResString("test_action_action"); // $NON-NLS-1$
072:
073: private static final String pauseAction = JMeterUtils
074: .getResString("test_action_pause"); // $NON-NLS-1$
075:
076: private static final String stopAction = JMeterUtils
077: .getResString("test_action_stop"); // $NON-NLS-1$
078:
079: private static final String durationLabel = JMeterUtils
080: .getResString("test_action_duration"); // $NON-NLS-1$
081:
082: public TestActionGui() {
083: super ();
084: target = TestAction.THREAD;
085: action = TestAction.PAUSE;
086: durationString = ""; // $NON-NLS-1$
087: init();
088: }
089:
090: public String getLabelResource() {
091: return "test_action_title"; // $NON-NLS-1$
092: }
093:
094: public void configure(TestElement element) {
095: super .configure(element);
096: TestAction ta = (TestAction) element;
097:
098: target = ta.getTarget();
099: if (target == TestAction.THREAD)
100: targetBox.setSelectedItem(threadTarget);
101: else
102: targetBox.setSelectedItem(testTarget);
103:
104: action = ta.getAction();
105: if (action == TestAction.PAUSE)
106: pauseButton.setSelected(true);
107: else
108: stopButton.setSelected(true);
109:
110: durationString = ta.getDurationAsString();
111: durationField.setText(durationString);
112: }
113:
114: /**
115: * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement()
116: */
117: public TestElement createTestElement() {
118: TestAction ta = new TestAction();
119: modifyTestElement(ta);
120: return ta;
121: }
122:
123: /**
124: * Modifies a given TestElement to mirror the data in the gui components.
125: *
126: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
127: */
128: public void modifyTestElement(TestElement element) {
129: super .configureTestElement(element);
130: TestAction ta = (TestAction) element;
131: ta.setAction(action);
132: ta.setTarget(target);
133: ta.setDuration(durationString);
134: }
135:
136: /**
137: * Implements JMeterGUIComponent.clearGui
138: */
139: public void clearGui() {
140: super .clearGui();
141:
142: targetBox.setSelectedIndex(0);
143: durationString = ""; //$NON-NLS-1$
144: durationField.setText(""); //$NON-NLS-1$
145: pauseButton.setSelected(true);
146: stopButton.setSelected(false);
147: action = TestAction.PAUSE;
148: target = TestAction.THREAD;
149:
150: }
151:
152: private void init() {
153: setLayout(new VerticalLayout(5, VerticalLayout.BOTH,
154: VerticalLayout.TOP));
155: setBorder(makeBorder());
156: add(makeTitlePanel());
157:
158: // Target
159: HorizontalPanel targetPanel = new HorizontalPanel();
160: targetPanel.add(new JLabel(targetLabel));
161: DefaultComboBoxModel targetModel = new DefaultComboBoxModel();
162: targetModel.addElement(threadTarget);
163: targetModel.addElement(testTarget);
164: targetBox = new JComboBox(targetModel);
165: targetBox.addActionListener(new ActionListener() {
166: public void actionPerformed(ActionEvent e) {
167: if (((String) targetBox.getSelectedItem())
168: .equals(threadTarget)) {
169: target = TestAction.THREAD;
170: } else {
171: target = TestAction.TEST;
172: }
173: }
174: });
175: targetPanel.add(targetBox);
176: add(targetPanel);
177:
178: // Action
179: HorizontalPanel actionPanel = new HorizontalPanel();
180: ButtonGroup actionButtons = new ButtonGroup();
181: pauseButton = new JRadioButton(pauseAction, true);
182: pauseButton.addChangeListener(new ChangeListener() {
183: public void stateChanged(ChangeEvent e) {
184: if (pauseButton.isSelected()) {
185: action = TestAction.PAUSE;
186: durationField.setEnabled(true);
187: }
188:
189: }
190: });
191: stopButton = new JRadioButton(stopAction, false);
192: stopButton.addChangeListener(new ChangeListener() {
193: public void stateChanged(ChangeEvent e) {
194: if (stopButton.isSelected()) {
195: action = TestAction.STOP;
196: durationField.setEnabled(false);
197: }
198: }
199: });
200: actionButtons.add(pauseButton);
201: actionButtons.add(stopButton);
202: actionPanel.add(new JLabel(actionLabel));
203: actionPanel.add(pauseButton);
204: actionPanel.add(stopButton);
205: add(actionPanel);
206:
207: // Duration
208: HorizontalPanel durationPanel = new HorizontalPanel();
209: durationField = new JTextField(15);
210: durationField.setText("");
211: durationField.addFocusListener(new FocusListener() {
212: public void focusLost(FocusEvent e) {
213: durationString = durationField.getText();
214: }
215:
216: public void focusGained(FocusEvent e) {
217: }
218: });
219: durationPanel.add(new JLabel(durationLabel));
220: durationPanel.add(durationField);
221: add(durationPanel);
222: }
223:
224: }
|