1 18 19 package org.apache.jmeter.threads.gui; 20 import java.awt.BorderLayout ; 21 import java.awt.Dimension ; 22 import java.awt.event.ItemEvent ; 23 import java.awt.event.ItemListener ; 24 import java.util.Collection ; 25 import java.util.Date ; 26 27 import javax.swing.BorderFactory ; 28 import javax.swing.Box ; 29 import javax.swing.ButtonGroup ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JLabel ; 32 import javax.swing.JPanel ; 33 import javax.swing.JPopupMenu ; 34 import javax.swing.JRadioButton ; 35 import javax.swing.JTextField ; 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 55 public class ThreadGroupGui 56 extends AbstractJMeterGuiComponent 57 implements ItemListener 58 { 59 LoopControlPanel loopPanel; 60 private VerticalPanel mainPanel; 61 62 63 private final static String THREAD_NAME = "Thread Field"; 64 private final static String RAMP_NAME = "Ramp Up Field"; 65 66 private JTextField threadInput; 67 private JTextField rampInput; 68 69 73 private JDateField start; 74 private JDateField end; 75 private JCheckBox scheduler; 76 private JTextField duration; 77 private JTextField delay; 79 private JRadioButton continueBox; 81 private JRadioButton stopThrdBox; 82 private JRadioButton stopTestBox; 83 84 public ThreadGroupGui() 85 { 86 super(); 87 init(); 88 } 89 90 public Collection getMenuCategories() 91 { 92 return null; 93 } 94 95 public TestElement createTestElement() 96 { 97 ThreadGroup tg = new ThreadGroup (); 98 modifyTestElement(tg); 99 return tg; 100 } 101 102 106 public void modifyTestElement(TestElement tg) 107 { 108 super.configureTestElement(tg); 109 if (tg instanceof ThreadGroup ) 110 { 111 ((ThreadGroup ) 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 ) start.getDate()).getTime())); 121 tg.setProperty( 122 new LongProperty( 123 ThreadGroup.END_TIME, 124 ((Date ) 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 te){ 135 stopTestBox.setSelected(te.getOnErrorStopTest()); 136 stopThrdBox.setSelected(te.getOnErrorStopThread()); 137 continueBox.setSelected(!te.getOnErrorStopThread() && !te.getOnErrorStopTest()); 138 } 139 140 private String onSampleError(){ 141 if (stopTestBox.isSelected()) return ThreadGroup.ON_SAMPLE_ERROR_STOPTEST; 142 if (stopThrdBox.isSelected()) return ThreadGroup.ON_SAMPLE_ERROR_STOPTHREAD; 143 144 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 String s = tg.getPropertyAsString(ThreadGroup.START_TIME); 170 if (s.length() == 0) { start.setDate(new Date ()); 172 end.setDate(new Date ()); 173 } else { 174 start.setDate(new Date (tg.getPropertyAsLong(ThreadGroup.START_TIME))); 175 end.setDate(new Date (tg.getPropertyAsLong(ThreadGroup.END_TIME))); 176 } 177 duration.setText(tg.getPropertyAsString(ThreadGroup.DURATION)); 178 delay.setText(tg.getPropertyAsString(ThreadGroup.DELAY)); 179 180 setSampleErrorBoxes((ThreadGroup ) tg); 181 } 182 183 public void itemStateChanged(ItemEvent 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 createPopupMenu() 199 { 200 JPopupMenu pop = new JPopupMenu (); 201 pop.add( 202 MenuFactory.makeMenus( 203 new String [] { 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 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 233 private JPanel createStartTimePanel() 234 { 235 JPanel panel = new JPanel (new BorderLayout (5, 0)); 236 JLabel label = new JLabel (JMeterUtils.getResString("starttime")); 237 panel.add(label, BorderLayout.WEST); 238 Date today = new Date (); 239 start = new JDateField(today); 240 panel.add(start, BorderLayout.CENTER); 241 return panel; 242 } 243 244 249 private JPanel createEndTimePanel() 250 { 251 JPanel panel = new JPanel (new BorderLayout (5, 0)); 252 JLabel label = new JLabel (JMeterUtils.getResString("endtime")); 253 panel.add(label, BorderLayout.WEST); 254 Date today = new Date (); 255 end = new JDateField(today); 256 panel.add(end, BorderLayout.CENTER); 257 return panel; 258 } 259 260 265 private JPanel createDurationPanel() 266 { 267 JPanel panel = new JPanel (new BorderLayout (5, 0)); 268 JLabel label = new JLabel (JMeterUtils.getResString("duration")); 269 panel.add(label, BorderLayout.WEST); 270 duration = new JTextField (); 271 panel.add(duration, BorderLayout.CENTER); 272 return panel; 273 } 274 275 280 private JPanel createDelayPanel() 281 { 282 JPanel panel = new JPanel (new BorderLayout (5, 0)); 283 JLabel label = new JLabel (JMeterUtils.getResString("delay")); 284 panel.add(label, BorderLayout.WEST); 285 delay = new JTextField (); 286 panel.add(delay, BorderLayout.CENTER); 287 return panel; 288 } 289 290 public String getLabelResource() 291 { 292 return "threadgroup"; 293 } 294 295 private JPanel createOnErrorPanel() 296 { 297 JPanel panel = new JPanel (); 298 panel.setBorder( 299 BorderFactory.createTitledBorder( 300 JMeterUtils.getResString("sampler_on_error_action"))); 301 302 ButtonGroup group = new ButtonGroup (); 303 304 continueBox = 305 new JRadioButton (JMeterUtils.getResString("sampler_on_error_continue")); 306 group.add(continueBox); 307 continueBox.setSelected(true); 308 panel.add(continueBox); 309 310 stopThrdBox = 311 new JRadioButton (JMeterUtils.getResString("sampler_on_error_stop_thread")); 312 group.add(stopThrdBox); 313 panel.add(stopThrdBox); 314 315 stopTestBox = 316 new JRadioButton (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 (0, 5)); 328 setBorder(makeBorder()); 329 330 Box box = Box.createVerticalBox(); 331 box.add(makeTitlePanel()); 332 box.add(createOnErrorPanel()); 333 add(box,BorderLayout.NORTH); 334 335 337 VerticalPanel threadPropsPanel = new VerticalPanel(); 339 threadPropsPanel.setBorder( 340 BorderFactory.createTitledBorder( 341 BorderFactory.createEtchedBorder(), 342 JMeterUtils.getResString("thread_properties"))); 343 344 JPanel threadPanel = new JPanel (new BorderLayout (5, 0)); 346 347 JLabel threadLabel = 348 new JLabel (JMeterUtils.getResString("number_of_threads")); 349 threadPanel.add(threadLabel, BorderLayout.WEST); 350 351 threadInput = new JTextField ("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 JPanel rampPanel = new JPanel (new BorderLayout (5, 0)); 361 JLabel rampLabel = new JLabel (JMeterUtils.getResString("ramp_up")); 362 rampPanel.add(rampLabel, BorderLayout.WEST); 363 364 rampInput = new JTextField ("1", 5); 365 rampInput.setName(RAMP_NAME); 366 rampLabel.setLabelFor(rampInput); 367 rampPanel.add(rampInput, BorderLayout.CENTER); 368 369 threadPropsPanel.add(rampPanel); 370 371 threadPropsPanel.add(createControllerPanel()); 373 374 377 scheduler = new JCheckBox ( 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 getPreferredSize() 402 { 403 return getMinimumSize(); 404 } 405 } 406 | Popular Tags |