KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > threads > gui > ThreadGroupGui


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/threads/gui/ThreadGroupGui.java,v 1.26.2.1 2004/12/18 19:07:02 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.threads.gui;
20 import java.awt.BorderLayout JavaDoc;
21 import java.awt.Dimension JavaDoc;
22 import java.awt.event.ItemEvent JavaDoc;
23 import java.awt.event.ItemListener JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Date JavaDoc;
26
27 import javax.swing.BorderFactory JavaDoc;
28 import javax.swing.Box JavaDoc;
29 import javax.swing.ButtonGroup JavaDoc;
30 import javax.swing.JCheckBox JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JPanel JavaDoc;
33 import javax.swing.JPopupMenu JavaDoc;
34 import javax.swing.JRadioButton JavaDoc;
35 import javax.swing.JTextField JavaDoc;
36
37 import org.apache.jmeter.control.LoopController;
38 import org.apache.jmeter.control.gui.LoopControlPanel;
39 import org.apache.jmeter.gui.AbstractJMeterGuiComponent;
40 import org.apache.jmeter.gui.tree.JMeterTreeNode;
41 import org.apache.jmeter.gui.util.FocusRequester;
42 import org.apache.jmeter.gui.util.JDateField;
43 import org.apache.jmeter.gui.util.MenuFactory;
44 import org.apache.jmeter.gui.util.VerticalPanel;
45 import org.apache.jmeter.testelement.TestElement;
46 import org.apache.jmeter.testelement.property.BooleanProperty;
47 import org.apache.jmeter.testelement.property.LongProperty;
48 import org.apache.jmeter.testelement.property.StringProperty;
49 import org.apache.jmeter.threads.ThreadGroup;
50 import org.apache.jmeter.util.JMeterUtils;
51
52 /**
53  * @version $Revision: 1.26.2.1 $ on $Date: 2004/12/18 19:07:02 $
54  */

55 public class ThreadGroupGui
56     extends AbstractJMeterGuiComponent
57     implements ItemListener JavaDoc
58 {
59     LoopControlPanel loopPanel;
60     private VerticalPanel mainPanel;
61
62
63     private final static String JavaDoc THREAD_NAME = "Thread Field";
64     private final static String JavaDoc RAMP_NAME = "Ramp Up Field";
65
66     private JTextField JavaDoc threadInput;
67     private JTextField JavaDoc rampInput;
68
69     //private final static String SCHEDULER = "scheduler";
70
//private final static String START_TIME= "start_time";
71
//private final static String END_TIME= "end_time";
72

73     private JDateField start;
74     private JDateField end;
75     private JCheckBox JavaDoc scheduler;
76     private JTextField JavaDoc duration;
77     private JTextField JavaDoc delay; //Relative start-up time
78

79     // Sampler error action buttons
80
private JRadioButton JavaDoc continueBox;
81     private JRadioButton JavaDoc stopThrdBox;
82     private JRadioButton JavaDoc stopTestBox;
83     
84     public ThreadGroupGui()
85     {
86         super();
87         init();
88     }
89
90     public Collection JavaDoc getMenuCategories()
91     {
92         return null;
93     }
94
95     public TestElement createTestElement()
96     {
97         ThreadGroup JavaDoc tg = new ThreadGroup JavaDoc();
98         modifyTestElement(tg);
99         return tg;
100     }
101
102     /**
103      * Modifies a given TestElement to mirror the data in the gui components.
104      * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
105      */

106     public void modifyTestElement(TestElement tg)
107     {
108         super.configureTestElement(tg);
109         if (tg instanceof ThreadGroup JavaDoc)
110         {
111             ((ThreadGroup JavaDoc) tg).setSamplerController(
112                 (LoopController) loopPanel.createTestElement());
113         }
114
115         tg.setProperty(ThreadGroup.NUM_THREADS, threadInput.getText());
116         tg.setProperty(ThreadGroup.RAMP_TIME, rampInput.getText());
117         tg.setProperty(
118             new LongProperty(
119                 ThreadGroup.START_TIME,
120                 ((Date JavaDoc) start.getDate()).getTime()));
121         tg.setProperty(
122             new LongProperty(
123                 ThreadGroup.END_TIME,
124                 ((Date JavaDoc) end.getDate()).getTime()));
125         tg.setProperty(
126             new BooleanProperty(ThreadGroup.SCHEDULER, scheduler.isSelected()));
127         tg.setProperty(
128             new StringProperty(
129                 ThreadGroup.ON_SAMPLE_ERROR,onSampleError()));
130         tg.setProperty(ThreadGroup.DURATION, duration.getText());
131         tg.setProperty(ThreadGroup.DELAY, delay.getText());
132     }
133     
134     private void setSampleErrorBoxes(ThreadGroup JavaDoc te){
135         stopTestBox.setSelected(te.getOnErrorStopTest());
136         stopThrdBox.setSelected(te.getOnErrorStopThread());
137         continueBox.setSelected(!te.getOnErrorStopThread() && !te.getOnErrorStopTest());
138     }
139     
140     private String JavaDoc onSampleError(){
141         if (stopTestBox.isSelected()) return ThreadGroup.ON_SAMPLE_ERROR_STOPTEST;
142         if (stopThrdBox.isSelected()) return ThreadGroup.ON_SAMPLE_ERROR_STOPTHREAD;
143
144         // Defaults to continue
145
return ThreadGroup.ON_SAMPLE_ERROR_CONTINUE;
146     }
147
148     public void configure(TestElement tg)
149     {
150         super.configure(tg);
151         threadInput.setText(tg.getPropertyAsString(ThreadGroup.NUM_THREADS));
152         rampInput.setText(tg.getPropertyAsString(ThreadGroup.RAMP_TIME));
153         loopPanel.configure(
154             (TestElement) tg
155                 .getProperty(ThreadGroup.MAIN_CONTROLLER)
156                 .getObjectValue());
157         scheduler.setSelected(tg.getPropertyAsBoolean(ThreadGroup.SCHEDULER));
158
159         if (scheduler.isSelected())
160         {
161             mainPanel.setVisible(true);
162         }
163         else
164         {
165             mainPanel.setVisible(false);
166         }
167
168         // Check if the property exists
169
String JavaDoc s = tg.getPropertyAsString(ThreadGroup.START_TIME);
170         if (s.length() == 0) {// Must be an old test plan
171
start.setDate(new Date JavaDoc());
172             end.setDate(new Date JavaDoc());
173         } else {
174             start.setDate(new Date JavaDoc(tg.getPropertyAsLong(ThreadGroup.START_TIME)));
175             end.setDate(new Date JavaDoc(tg.getPropertyAsLong(ThreadGroup.END_TIME)));
176         }
177         duration.setText(tg.getPropertyAsString(ThreadGroup.DURATION));
178         delay.setText(tg.getPropertyAsString(ThreadGroup.DELAY));
179         
180         setSampleErrorBoxes((ThreadGroup JavaDoc) tg);
181     }
182
183     public void itemStateChanged(ItemEvent JavaDoc ie)
184     {
185         if (ie.getItem().equals(scheduler))
186         {
187             if (scheduler.isSelected())
188             {
189                 mainPanel.setVisible(true);
190             }
191             else
192             {
193                 mainPanel.setVisible(false);
194             }
195         }
196     }
197
198     public JPopupMenu JavaDoc createPopupMenu()
199     {
200         JPopupMenu JavaDoc pop = new JPopupMenu JavaDoc();
201         pop.add(
202             MenuFactory.makeMenus(
203                 new String JavaDoc[] {
204                     MenuFactory.CONTROLLERS,
205                     MenuFactory.LISTENERS,
206                     MenuFactory.SAMPLERS,
207                     MenuFactory.TIMERS,
208                     MenuFactory.CONFIG_ELEMENTS,
209                     MenuFactory.PRE_PROCESSORS,
210                     MenuFactory.POST_PROCESSORS },
211                 JMeterUtils.getResString("Add"),
212                 "Add"));
213         MenuFactory.addEditMenu(pop, true);
214         MenuFactory.addFileMenu(pop);
215         return pop;
216     }
217
218     public JPanel JavaDoc createControllerPanel()
219     {
220         loopPanel = new LoopControlPanel(false);
221         LoopController looper = (LoopController) loopPanel.createTestElement();
222         looper.setLoops(1);
223         loopPanel.configure(looper);
224         return loopPanel;
225     }
226
227
228     /**
229      * Create a panel containing the StartTime field and corresponding label.
230      *
231      * @return a GUI panel containing the StartTime field
232      */

233     private JPanel JavaDoc createStartTimePanel()
234     {
235         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
236         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("starttime"));
237         panel.add(label, BorderLayout.WEST);
238         Date JavaDoc today = new Date JavaDoc();
239         start = new JDateField(today);
240         panel.add(start, BorderLayout.CENTER);
241         return panel;
242     }
243
244     /**
245      * Create a panel containing the EndTime field and corresponding label.
246      *
247      * @return a GUI panel containing the EndTime field
248      */

249     private JPanel JavaDoc createEndTimePanel()
250     {
251         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
252         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("endtime"));
253         panel.add(label, BorderLayout.WEST);
254         Date JavaDoc today = new Date JavaDoc();
255         end = new JDateField(today);
256         panel.add(end, BorderLayout.CENTER);
257         return panel;
258     }
259
260     /**
261      * Create a panel containing the Duration field and corresponding label.
262      *
263      * @return a GUI panel containing the Duration field
264      */

265     private JPanel JavaDoc createDurationPanel()
266     {
267         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
268         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("duration"));
269         panel.add(label, BorderLayout.WEST);
270         duration = new JTextField JavaDoc();
271         panel.add(duration, BorderLayout.CENTER);
272         return panel;
273     }
274
275     /**
276      * Create a panel containing the Duration field and corresponding label.
277      *
278      * @return a GUI panel containing the Duration field
279      */

280     private JPanel JavaDoc createDelayPanel()
281     {
282         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
283         JLabel JavaDoc label = new JLabel JavaDoc(JMeterUtils.getResString("delay"));
284         panel.add(label, BorderLayout.WEST);
285         delay = new JTextField JavaDoc();
286         panel.add(delay, BorderLayout.CENTER);
287         return panel;
288     }
289
290     public String JavaDoc getLabelResource()
291     {
292         return "threadgroup";
293     }
294
295     private JPanel JavaDoc createOnErrorPanel()
296     {
297         JPanel JavaDoc panel = new JPanel JavaDoc();
298         panel.setBorder(
299             BorderFactory.createTitledBorder(
300                 JMeterUtils.getResString("sampler_on_error_action")));
301
302         ButtonGroup JavaDoc group = new ButtonGroup JavaDoc();
303
304         continueBox =
305             new JRadioButton JavaDoc(JMeterUtils.getResString("sampler_on_error_continue"));
306         group.add(continueBox);
307         continueBox.setSelected(true);
308         panel.add(continueBox);
309
310         stopThrdBox =
311             new JRadioButton JavaDoc(JMeterUtils.getResString("sampler_on_error_stop_thread"));
312         group.add(stopThrdBox);
313         panel.add(stopThrdBox);
314
315         stopTestBox =
316             new JRadioButton JavaDoc(JMeterUtils.getResString("sampler_on_error_stop_test"));
317         group.add(stopTestBox);
318         panel.add(stopTestBox);
319
320         return panel;
321     }
322
323
324
325     private void init()
326     {
327         setLayout(new BorderLayout JavaDoc(0, 5));
328         setBorder(makeBorder());
329
330         Box JavaDoc box = Box.createVerticalBox();
331         box.add(makeTitlePanel());
332         box.add(createOnErrorPanel());
333         add(box,BorderLayout.NORTH);
334
335         //JPanel mainPanel = new JPanel(new BorderLayout());
336

337         // THREAD PROPERTIES
338
VerticalPanel threadPropsPanel = new VerticalPanel();
339         threadPropsPanel.setBorder(
340             BorderFactory.createTitledBorder(
341                 BorderFactory.createEtchedBorder(),
342                 JMeterUtils.getResString("thread_properties")));
343
344         // NUMBER OF THREADS
345
JPanel JavaDoc threadPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
346
347         JLabel JavaDoc threadLabel =
348             new JLabel JavaDoc(JMeterUtils.getResString("number_of_threads"));
349         threadPanel.add(threadLabel, BorderLayout.WEST);
350
351         threadInput = new JTextField JavaDoc("1", 5);
352         threadInput.setName(THREAD_NAME);
353         threadLabel.setLabelFor(threadInput);
354         threadPanel.add(threadInput, BorderLayout.CENTER);
355
356         threadPropsPanel.add(threadPanel);
357         new FocusRequester(threadInput);
358
359         // RAMP-UP
360
JPanel JavaDoc rampPanel = new JPanel JavaDoc(new BorderLayout JavaDoc(5, 0));
361         JLabel JavaDoc rampLabel = new JLabel JavaDoc(JMeterUtils.getResString("ramp_up"));
362         rampPanel.add(rampLabel, BorderLayout.WEST);
363         
364         rampInput = new JTextField JavaDoc("1", 5);
365         rampInput.setName(RAMP_NAME);
366         rampLabel.setLabelFor(rampInput);
367         rampPanel.add(rampInput, BorderLayout.CENTER);
368         
369         threadPropsPanel.add(rampPanel);
370
371         // LOOP COUNT
372
threadPropsPanel.add(createControllerPanel());
373
374         // mainPanel.add(threadPropsPanel, BorderLayout.NORTH);
375
//add(mainPanel, BorderLayout.CENTER);
376

377         scheduler = new JCheckBox JavaDoc( JMeterUtils.getResString("scheduler"));
378         scheduler.addItemListener(this);
379         threadPropsPanel.add(scheduler);
380         mainPanel = new VerticalPanel();
381         mainPanel.setBorder(
382             BorderFactory.createTitledBorder(
383                 BorderFactory.createEtchedBorder(),
384                 JMeterUtils.getResString("scheduler_configuration")));
385         mainPanel.add(createStartTimePanel());
386         mainPanel.add(createEndTimePanel());
387         mainPanel.add(createDurationPanel());
388         mainPanel.add(createDelayPanel());
389         mainPanel.setVisible(false);
390         VerticalPanel intgrationPanel = new VerticalPanel();
391         intgrationPanel.add(threadPropsPanel);
392         intgrationPanel.add(mainPanel);
393         add(intgrationPanel, BorderLayout.CENTER);
394     }
395
396     public void setNode(JMeterTreeNode node)
397     {
398         getNamePanel().setNode(node);
399     }
400     
401     public Dimension JavaDoc getPreferredSize()
402     {
403         return getMinimumSize();
404     }
405 }
406
Popular Tags