Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.threads.gui » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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   
   21   import java.awt.BorderLayout;
   22   import java.awt.Dimension;
   23   import java.awt.event.ItemEvent;
   24   import java.awt.event.ItemListener;
   25   import java.util.Collection;
   26   import java.util.Date;
   27   
   28   import javax.swing.BorderFactory;
   29   import javax.swing.Box;
   30   import javax.swing.ButtonGroup;
   31   import javax.swing.JCheckBox;
   32   import javax.swing.JLabel;
   33   import javax.swing.JPanel;
   34   import javax.swing.JPopupMenu;
   35   import javax.swing.JRadioButton;
   36   import javax.swing.JTextField;
   37   
   38   import org.apache.jmeter.control.LoopController;
   39   import org.apache.jmeter.control.gui.LoopControlPanel;
   40   import org.apache.jmeter.gui.AbstractJMeterGuiComponent;
   41   import org.apache.jmeter.gui.action.ActionNames;
   42   import org.apache.jmeter.gui.tree.JMeterTreeNode;
   43   import org.apache.jmeter.gui.util.FocusRequester;
   44   import org.apache.jmeter.gui.util.JDateField;
   45   import org.apache.jmeter.gui.util.MenuFactory;
   46   import org.apache.jmeter.gui.util.VerticalPanel;
   47   import org.apache.jmeter.testelement.TestElement;
   48   import org.apache.jmeter.testelement.property.BooleanProperty;
   49   import org.apache.jmeter.testelement.property.LongProperty;
   50   import org.apache.jmeter.testelement.property.StringProperty;
   51   import org.apache.jmeter.threads.ThreadGroup;
   52   import org.apache.jmeter.util.JMeterUtils;
   53   
   54   public class ThreadGroupGui extends AbstractJMeterGuiComponent implements ItemListener {
   55       private LoopControlPanel loopPanel;
   56   
   57       private VerticalPanel mainPanel;
   58   
   59       private final static String THREAD_NAME = "Thread Field";
   60   
   61       private final static String RAMP_NAME = "Ramp Up Field";
   62   
   63       private JTextField threadInput;
   64   
   65       private JTextField rampInput;
   66   
   67       private JDateField start;
   68   
   69       private JDateField end;
   70   
   71       private JCheckBox scheduler;
   72   
   73       private JTextField duration;
   74   
   75       private JTextField delay; // Relative start-up time
   76   
   77       // Sampler error action buttons
   78       private JRadioButton continueBox;
   79   
   80       private JRadioButton stopThrdBox;
   81   
   82       private JRadioButton stopTestBox;
   83   
   84       private JRadioButton stopTestNowBox;
   85       
   86       public ThreadGroupGui() {
   87           super();
   88           init();
   89           initGui();
   90       }
   91   
   92       public Collection getMenuCategories() {
   93           return null;
   94       }
   95   
   96       public TestElement createTestElement() {
   97           ThreadGroup tg = new ThreadGroup();
   98           modifyTestElement(tg);
   99           return tg;
  100       }
  101   
  102       /**
  103        * Modifies a given TestElement to mirror the data in the gui components.
  104        *
  105        * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
  106        */
  107       public void modifyTestElement(TestElement tg) {
  108           super.configureTestElement(tg);
  109           if (tg instanceof ThreadGroup) {
  110               ((ThreadGroup) tg).setSamplerController((LoopController) loopPanel.createTestElement());
  111           }
  112   
  113           tg.setProperty(ThreadGroup.NUM_THREADS, threadInput.getText());
  114           tg.setProperty(ThreadGroup.RAMP_TIME, rampInput.getText());
  115           tg.setProperty(new LongProperty(ThreadGroup.START_TIME, start.getDate().getTime()));
  116           tg.setProperty(new LongProperty(ThreadGroup.END_TIME, end.getDate().getTime()));
  117           tg.setProperty(new BooleanProperty(ThreadGroup.SCHEDULER, scheduler.isSelected()));
  118           tg.setProperty(new StringProperty(ThreadGroup.ON_SAMPLE_ERROR, onSampleError()));
  119           tg.setProperty(ThreadGroup.DURATION, duration.getText());
  120           tg.setProperty(ThreadGroup.DELAY, delay.getText());
  121       }
  122   
  123       private void setSampleErrorBoxes(ThreadGroup te) {
  124           if (te.getOnErrorStopTest()) {
  125               stopTestBox.setSelected(true);
  126           } else if (te.getOnErrorStopTestNow()) {
  127               stopTestNowBox.setSelected(true);
  128           } else if (te.getOnErrorStopThread()) {
  129               stopThrdBox.setSelected(true);
  130           } else {
  131               continueBox.setSelected(true);
  132           }
  133       }
  134   
  135       private String onSampleError() {
  136           if (stopTestBox.isSelected()) {
  137               return ThreadGroup.ON_SAMPLE_ERROR_STOPTEST;
  138           }
  139           if (stopTestNowBox.isSelected()) {
  140               return ThreadGroup.ON_SAMPLE_ERROR_STOPTEST_NOW;
  141           }
  142           if (stopThrdBox.isSelected()) {
  143               return ThreadGroup.ON_SAMPLE_ERROR_STOPTHREAD;
  144           }
  145   
  146           // Defaults to continue
  147           return ThreadGroup.ON_SAMPLE_ERROR_CONTINUE;
  148       }
  149   
  150       public void configure(TestElement tg) {
  151           super.configure(tg);
  152           threadInput.setText(tg.getPropertyAsString(ThreadGroup.NUM_THREADS));
  153           rampInput.setText(tg.getPropertyAsString(ThreadGroup.RAMP_TIME));
  154           loopPanel.configure((TestElement) tg.getProperty(ThreadGroup.MAIN_CONTROLLER).getObjectValue());
  155           scheduler.setSelected(tg.getPropertyAsBoolean(ThreadGroup.SCHEDULER));
  156   
  157           if (scheduler.isSelected()) {
  158               mainPanel.setVisible(true);
  159           } else {
  160               mainPanel.setVisible(false);
  161           }
  162   
  163           // Check if the property exists
  164           String s = tg.getPropertyAsString(ThreadGroup.START_TIME);
  165           if (s.length() == 0) {// Must be an old test plan
  166               start.setDate(new Date());
  167               end.setDate(new Date());
  168           } else {
  169               start.setDate(new Date(tg.getPropertyAsLong(ThreadGroup.START_TIME)));
  170               end.setDate(new Date(tg.getPropertyAsLong(ThreadGroup.END_TIME)));
  171           }
  172           duration.setText(tg.getPropertyAsString(ThreadGroup.DURATION));
  173           delay.setText(tg.getPropertyAsString(ThreadGroup.DELAY));
  174   
  175           setSampleErrorBoxes((ThreadGroup) tg);
  176       }
  177   
  178       public void itemStateChanged(ItemEvent ie) {
  179           if (ie.getItem().equals(scheduler)) {
  180               if (scheduler.isSelected()) {
  181                   mainPanel.setVisible(true);
  182               } else {
  183                   mainPanel.setVisible(false);
  184               }
  185           }
  186       }
  187   
  188       public JPopupMenu createPopupMenu() {
  189           JPopupMenu pop = new JPopupMenu();
  190           pop.add(MenuFactory.makeMenus(new String[] {
  191                   MenuFactory.CONTROLLERS,
  192                   MenuFactory.CONFIG_ELEMENTS,
  193                   MenuFactory.TIMERS,
  194                   MenuFactory.PRE_PROCESSORS,
  195                   MenuFactory.SAMPLERS,
  196                   MenuFactory.POST_PROCESSORS,
  197                   MenuFactory.ASSERTIONS,
  198                   MenuFactory.LISTENERS,
  199                   },
  200                   JMeterUtils.getResString("add"), // $NON-NLS-1$
  201                   ActionNames.ADD));
  202           MenuFactory.addEditMenu(pop, true);
  203           MenuFactory.addFileMenu(pop);
  204           return pop;
  205       }
  206   
  207       private JPanel createControllerPanel() {
  208           loopPanel = new LoopControlPanel(false);
  209           LoopController looper = (LoopController) loopPanel.createTestElement();
  210           looper.setLoops(1);
  211           loopPanel.configure(looper);
  212           return loopPanel;
  213       }
  214   
  215       /**
  216        * Create a panel containing the StartTime field and corresponding label.
  217        *
  218        * @return a GUI panel containing the StartTime field
  219        */
  220       private JPanel createStartTimePanel() {
  221           JPanel panel = new JPanel(new BorderLayout(5, 0));
  222           JLabel label = new JLabel(JMeterUtils.getResString("starttime")); //$NON-NLS-1$
  223           panel.add(label, BorderLayout.WEST);
  224           start = new JDateField();
  225           panel.add(start, BorderLayout.CENTER);
  226           return panel;
  227       }
  228   
  229       /**
  230        * Create a panel containing the EndTime field and corresponding label.
  231        *
  232        * @return a GUI panel containing the EndTime field
  233        */
  234       private JPanel createEndTimePanel() {
  235           JPanel panel = new JPanel(new BorderLayout(5, 0));
  236           JLabel label = new JLabel(JMeterUtils.getResString("endtime")); // $NON-NLS-1$
  237           panel.add(label, BorderLayout.WEST);
  238   
  239           end = new JDateField();
  240           panel.add(end, BorderLayout.CENTER);
  241           return panel;
  242       }
  243   
  244       /**
  245        * Create a panel containing the Duration field and corresponding label.
  246        *
  247        * @return a GUI panel containing the Duration field
  248        */
  249       private JPanel createDurationPanel() {
  250           JPanel panel = new JPanel(new BorderLayout(5, 0));
  251           JLabel label = new JLabel(JMeterUtils.getResString("duration")); // $NON-NLS-1$
  252           panel.add(label, BorderLayout.WEST);
  253           duration = new JTextField();
  254           panel.add(duration, BorderLayout.CENTER);
  255           return panel;
  256       }
  257   
  258       /**
  259        * Create a panel containing the Duration field and corresponding label.
  260        *
  261        * @return a GUI panel containing the Duration field
  262        */
  263       private JPanel createDelayPanel() {
  264           JPanel panel = new JPanel(new BorderLayout(5, 0));
  265           JLabel label = new JLabel(JMeterUtils.getResString("delay")); // $NON-NLS-1$
  266           panel.add(label, BorderLayout.WEST);
  267           delay = new JTextField();
  268           panel.add(delay, BorderLayout.CENTER);
  269           return panel;
  270       }
  271   
  272       public String getLabelResource() {
  273           return "threadgroup"; // $NON-NLS-1$
  274       }
  275   
  276       private JPanel createOnErrorPanel() {
  277           JPanel panel = new JPanel();
  278           panel.setBorder(BorderFactory.createTitledBorder(JMeterUtils.getResString("sampler_on_error_action"))); // $NON-NLS-1$
  279   
  280           ButtonGroup group = new ButtonGroup();
  281   
  282           continueBox = new JRadioButton(JMeterUtils.getResString("sampler_on_error_continue")); // $NON-NLS-1$
  283           group.add(continueBox);
  284           panel.add(continueBox);
  285   
  286           stopThrdBox = new JRadioButton(JMeterUtils.getResString("sampler_on_error_stop_thread")); // $NON-NLS-1$
  287           group.add(stopThrdBox);
  288           panel.add(stopThrdBox);
  289   
  290           stopTestBox = new JRadioButton(JMeterUtils.getResString("sampler_on_error_stop_test")); // $NON-NLS-1$
  291           group.add(stopTestBox);
  292           panel.add(stopTestBox);
  293   
  294           stopTestNowBox = new JRadioButton(JMeterUtils.getResString("sampler_on_error_stop_test_now")); // $NON-NLS-1$
  295           group.add(stopTestNowBox);
  296           panel.add(stopTestNowBox);
  297   
  298           return panel;
  299       }
  300   
  301       public void clearGui(){
  302           super.clearGui();
  303           initGui();
  304       }
  305   
  306       // Initialise the gui field values
  307       private void initGui(){
  308           threadInput.setText("1"); // $NON-NLS-1$
  309           rampInput.setText("1"); // $NON-NLS-1$
  310           continueBox.setSelected(true);
  311           loopPanel.clearGui();
  312           scheduler.setSelected(false);
  313           Date today = new Date();
  314           end.setDate(today);
  315           start.setDate(today);
  316           delay.setText(""); // $NON-NLS-1$
  317           duration.setText(""); // $NON-NLS-1$
  318       }
  319   
  320       private void init() {
  321           setLayout(new BorderLayout(0, 5));
  322           setBorder(makeBorder());
  323   
  324           Box box = Box.createVerticalBox();
  325           box.add(makeTitlePanel());
  326           box.add(createOnErrorPanel());
  327           add(box, BorderLayout.NORTH);
  328   
  329           // JPanel mainPanel = new JPanel(new BorderLayout());
  330   
  331           // THREAD PROPERTIES
  332           VerticalPanel threadPropsPanel = new VerticalPanel();
  333           threadPropsPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
  334                   JMeterUtils.getResString("thread_properties"))); // $NON-NLS-1$
  335   
  336           // NUMBER OF THREADS
  337           JPanel threadPanel = new JPanel(new BorderLayout(5, 0));
  338   
  339           JLabel threadLabel = new JLabel(JMeterUtils.getResString("number_of_threads")); // $NON-NLS-1$
  340           threadPanel.add(threadLabel, BorderLayout.WEST);
  341   
  342           threadInput = new JTextField(5);
  343           threadInput.setName(THREAD_NAME);
  344           threadLabel.setLabelFor(threadInput);
  345           threadPanel.add(threadInput, BorderLayout.CENTER);
  346   
  347           threadPropsPanel.add(threadPanel);
  348           new FocusRequester(threadInput);
  349   
  350           // RAMP-UP
  351           JPanel rampPanel = new JPanel(new BorderLayout(5, 0));
  352           JLabel rampLabel = new JLabel(JMeterUtils.getResString("ramp_up")); // $NON-NLS-1$
  353           rampPanel.add(rampLabel, BorderLayout.WEST);
  354   
  355           rampInput = new JTextField(5);
  356           rampInput.setName(RAMP_NAME);
  357           rampLabel.setLabelFor(rampInput);
  358           rampPanel.add(rampInput, BorderLayout.CENTER);
  359   
  360           threadPropsPanel.add(rampPanel);
  361   
  362           // LOOP COUNT
  363           threadPropsPanel.add(createControllerPanel());
  364   
  365           // mainPanel.add(threadPropsPanel, BorderLayout.NORTH);
  366           // add(mainPanel, BorderLayout.CENTER);
  367   
  368           scheduler = new JCheckBox(JMeterUtils.getResString("scheduler")); // $NON-NLS-1$
  369           scheduler.addItemListener(this);
  370           threadPropsPanel.add(scheduler);
  371           mainPanel = new VerticalPanel();
  372           mainPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),
  373                   JMeterUtils.getResString("scheduler_configuration"))); // $NON-NLS-1$
  374           mainPanel.add(createStartTimePanel());
  375           mainPanel.add(createEndTimePanel());
  376           mainPanel.add(createDurationPanel());
  377           mainPanel.add(createDelayPanel());
  378           mainPanel.setVisible(false);
  379           VerticalPanel intgrationPanel = new VerticalPanel();
  380           intgrationPanel.add(threadPropsPanel);
  381           intgrationPanel.add(mainPanel);
  382           add(intgrationPanel, BorderLayout.CENTER);
  383       }
  384   
  385       public void setNode(JMeterTreeNode node) {
  386           getNamePanel().setNode(node);
  387       }
  388   
  389       public Dimension getPreferredSize() {
  390           return getMinimumSize();
  391       }
  392   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.threads.gui » [javadoc | source]