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.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.ItemEvent;
024: import java.awt.event.ItemListener;
025:
026: import javax.swing.DefaultComboBoxModel;
027: import javax.swing.JCheckBox;
028: import javax.swing.JComboBox;
029: import javax.swing.JLabel;
030: import javax.swing.JPanel;
031: import javax.swing.JTextField;
032:
033: import org.apache.jmeter.control.ThroughputController;
034: import org.apache.jmeter.testelement.TestElement;
035: import org.apache.jmeter.util.JMeterUtils;
036: import org.apache.jorphan.gui.layout.VerticalLayout;
037:
038: public class ThroughputControllerGui extends AbstractControllerGui {
039: private JComboBox styleBox;
040:
041: private int style;
042:
043: private JTextField throughput;
044:
045: private JCheckBox perthread;
046:
047: private boolean isPerThread = true;
048:
049: private String BYNUMBER_LABEL = JMeterUtils
050: .getResString("throughput_control_bynumber_label"); // $NON-NLS-1$
051:
052: private String BYPERCENT_LABEL = JMeterUtils
053: .getResString("throughput_control_bypercent_label"); // $NON-NLS-1$
054:
055: private String THROUGHPUT_LABEL = JMeterUtils
056: .getResString("throughput_control_tplabel"); // $NON-NLS-1$
057:
058: private String THROUGHPUT = "Througput Field"; // $NON-NLS-1$
059:
060: private String PERTHREAD_LABEL = JMeterUtils
061: .getResString("throughput_control_perthread_label"); // $NON-NLS-1$
062:
063: public ThroughputControllerGui() {
064: init();
065: }
066:
067: public TestElement createTestElement() {
068: ThroughputController tc = new ThroughputController();
069: modifyTestElement(tc);
070: return tc;
071: }
072:
073: /**
074: * Modifies a given TestElement to mirror the data in the gui components.
075: *
076: * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
077: */
078: public void modifyTestElement(TestElement tc) {
079: configureTestElement(tc);
080: ((ThroughputController) tc).setStyle(style);
081: ((ThroughputController) tc).setPerThread(isPerThread);
082: if (style == ThroughputController.BYNUMBER) {
083: try {
084: ((ThroughputController) tc).setMaxThroughput(Integer
085: .parseInt(throughput.getText().trim()));
086: } catch (NumberFormatException e) {
087: ((ThroughputController) tc).setMaxThroughput(throughput
088: .getText());
089: }
090: } else {
091: try {
092: ((ThroughputController) tc).setPercentThroughput(Float
093: .parseFloat(throughput.getText().trim()));
094: } catch (NumberFormatException e) {
095: ((ThroughputController) tc)
096: .setPercentThroughput(throughput.getText());
097: }
098: }
099: }
100:
101: /**
102: * Implements JMeterGUIComponent.clearGui
103: */
104: public void clearGui() {
105: super .clearGui();
106: styleBox.setSelectedIndex(0);
107: throughput.setText("1"); // $NON-NLS-1$
108: perthread.setSelected(true);
109: }
110:
111: public void configure(TestElement el) {
112: super .configure(el);
113: if (((ThroughputController) el).getStyle() == ThroughputController.BYNUMBER) {
114: styleBox.getModel().setSelectedItem(BYNUMBER_LABEL);
115: throughput.setText(String
116: .valueOf(((ThroughputController) el)
117: .getMaxThroughput()));
118: } else {
119: styleBox.setSelectedItem(BYPERCENT_LABEL);
120: throughput.setText(String
121: .valueOf(((ThroughputController) el)
122: .getPercentThroughput()));
123: }
124: perthread
125: .setSelected(((ThroughputController) el).isPerThread());
126: }
127:
128: public String getLabelResource() {
129: return "throughput_control_title"; // $NON-NLS-1$
130: }
131:
132: private void init() {
133: setLayout(new VerticalLayout(5, VerticalLayout.BOTH,
134: VerticalLayout.TOP));
135: setBorder(makeBorder());
136: add(makeTitlePanel());
137:
138: DefaultComboBoxModel styleModel = new DefaultComboBoxModel();
139: styleModel.addElement(BYNUMBER_LABEL);
140: styleModel.addElement(BYPERCENT_LABEL);
141: styleBox = new JComboBox(styleModel);
142: styleBox.addActionListener(new ActionListener() {
143: public void actionPerformed(ActionEvent e) {
144: if (((String) styleBox.getSelectedItem())
145: .equals(BYNUMBER_LABEL)) {
146: style = ThroughputController.BYNUMBER;
147: } else {
148: style = ThroughputController.BYPERCENT;
149: }
150: }
151: });
152: add(styleBox);
153:
154: // TYPE FIELD
155: JPanel tpPanel = new JPanel();
156: JLabel tpLabel = new JLabel(THROUGHPUT_LABEL);
157: tpPanel.add(tpLabel);
158:
159: // TEXT FIELD
160: throughput = new JTextField(5);
161: tpPanel.add(throughput);
162: throughput.setName(THROUGHPUT);
163: throughput.setText("1"); // $NON-NLS-1$
164: // throughput.addActionListener(this);
165: tpPanel.add(throughput);
166: add(tpPanel);
167:
168: // PERTHREAD FIELD
169: perthread = new JCheckBox(PERTHREAD_LABEL, isPerThread);
170: perthread.addItemListener(new ItemListener() {
171: public void itemStateChanged(ItemEvent event) {
172: if (event.getStateChange() == ItemEvent.SELECTED) {
173: isPerThread = true;
174: } else {
175: isPerThread = false;
176: }
177: }
178: });
179: add(perthread);
180: }
181: }
|