KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/gui/ThroughputControllerGui.java,v 1.10 2004/03/05 01:32:37 sebb Exp $
2
/*
3  * Copyright 2001-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.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.awt.event.ItemEvent JavaDoc;
24 import java.awt.event.ItemListener JavaDoc;
25
26 import javax.swing.DefaultComboBoxModel JavaDoc;
27 import javax.swing.JCheckBox JavaDoc;
28 import javax.swing.JComboBox JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JPanel JavaDoc;
31 import javax.swing.JTextField JavaDoc;
32
33 import org.apache.jmeter.control.ThroughputController;
34 import org.apache.jmeter.testelement.TestElement;
35 import org.apache.jmeter.util.JMeterUtils;
36 import org.apache.jorphan.gui.layout.VerticalLayout;
37
38 /**
39  * @version $Revision: 1.10 $ on $Date: 2004/03/05 01:32:37 $
40  */

41 public class ThroughputControllerGui extends AbstractControllerGui
42 {
43     private JComboBox JavaDoc styleBox;
44     private int style;
45     private JTextField JavaDoc throughput;
46     private JCheckBox JavaDoc perthread;
47     private boolean isPerThread = true;
48
49     private String JavaDoc BYNUMBER_LABEL =
50         JMeterUtils.getResString("throughput_control_bynumber_label");
51     private String JavaDoc BYPERCENT_LABEL =
52         JMeterUtils.getResString("throughput_control_bypercent_label");
53     private String JavaDoc THROUGHPUT_LABEL =
54         JMeterUtils.getResString("throughput_control_tplabel");
55     private String JavaDoc THROUGHPUT = "Througput Field";
56     private String JavaDoc PERTHREAD_LABEL =
57         JMeterUtils.getResString("throughput_control_perthread_label");
58
59     public ThroughputControllerGui()
60     {
61         init();
62     }
63
64     public TestElement createTestElement()
65     {
66         ThroughputController tc = new ThroughputController();
67         modifyTestElement(tc);
68         return tc;
69     }
70
71     /**
72      * Modifies a given TestElement to mirror the data in the gui components.
73      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
74      */

75     public void modifyTestElement(TestElement tc)
76     {
77         configureTestElement(tc);
78         ((ThroughputController) tc).setStyle(style);
79         ((ThroughputController) tc).setPerThread(isPerThread);
80         if (style == ThroughputController.BYNUMBER)
81         {
82             try
83             {
84                 ((ThroughputController) tc).setMaxThroughput(
85                     Integer.parseInt(throughput.getText().trim()));
86             }
87             catch (NumberFormatException JavaDoc e)
88             {
89                 ((ThroughputController) tc).setMaxThroughput(
90                     throughput.getText());
91             }
92         }
93         else
94         {
95             try
96             {
97                 ((ThroughputController) tc).setPercentThroughput(
98                     Float.parseFloat(throughput.getText().trim()));
99             }
100             catch (NumberFormatException JavaDoc e)
101             {
102                 ((ThroughputController) tc).setPercentThroughput(
103                     throughput.getText());
104             }
105         }
106     }
107
108     public void configure(TestElement el)
109     {
110         super.configure(el);
111         if (((ThroughputController) el).getStyle()
112             == ThroughputController.BYNUMBER)
113         {
114             styleBox.getModel().setSelectedItem(BYNUMBER_LABEL);
115             throughput.setText(
116                 String.valueOf(((ThroughputController) el).getMaxThroughput()));
117         }
118         else
119         {
120             styleBox.setSelectedItem(BYPERCENT_LABEL);
121             throughput.setText(
122                 String.valueOf(
123                     ((ThroughputController) el).getPercentThroughput()));
124         }
125         perthread.setSelected(((ThroughputController) el).isPerThread());
126     }
127
128     public String JavaDoc getLabelResource()
129     {
130         return "throughput_control_title";
131     }
132
133     private void init()
134     {
135         setLayout(
136             new VerticalLayout(5, VerticalLayout.LEFT, VerticalLayout.TOP));
137         setBorder(makeBorder());
138         add(makeTitlePanel());
139
140         DefaultComboBoxModel JavaDoc styleModel = new DefaultComboBoxModel JavaDoc();
141         styleModel.addElement(BYNUMBER_LABEL);
142         styleModel.addElement(BYPERCENT_LABEL);
143         styleBox = new JComboBox JavaDoc(styleModel);
144         styleBox.addActionListener(new ActionListener JavaDoc()
145         {
146             public void actionPerformed(ActionEvent JavaDoc e)
147             {
148                 if (((String JavaDoc) styleBox.getSelectedItem())
149                     .equals(BYNUMBER_LABEL))
150                 {
151                     style = ThroughputController.BYNUMBER;
152                 }
153                 else
154                 {
155                     style = ThroughputController.BYPERCENT;
156                 }
157             }
158         });
159         add(styleBox);
160
161         // TYPE FIELD
162
JPanel JavaDoc tpPanel = new JPanel JavaDoc();
163         JLabel JavaDoc tpLabel = new JLabel JavaDoc(THROUGHPUT_LABEL);
164         tpPanel.add(tpLabel);
165
166         // TEXT FIELD
167
throughput = new JTextField JavaDoc(5);
168         tpPanel.add(throughput);
169         throughput.setName(THROUGHPUT);
170         throughput.setText("1");
171         // throughput.addActionListener(this);
172
tpPanel.add(throughput);
173         add(tpPanel);
174
175         // PERTHREAD FIELD
176
perthread = new JCheckBox JavaDoc(PERTHREAD_LABEL, isPerThread);
177         perthread.addItemListener(new ItemListener JavaDoc()
178         {
179             public void itemStateChanged(ItemEvent JavaDoc event)
180             {
181                 if (event.getStateChange() == ItemEvent.SELECTED)
182                 {
183                     isPerThread = true;
184                 }
185                 else
186                 {
187                     isPerThread = false;
188                 }
189             }
190         });
191         add(perthread);
192     }
193 }
194
Popular Tags