Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jorphan.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.jorphan.gui;
   20   
   21   import java.awt.event.ActionEvent;
   22   import java.awt.event.ActionListener;
   23   import java.util.ArrayList;
   24   import java.util.Enumeration;
   25   import java.util.LinkedList;
   26   import java.util.List;
   27   
   28   import javax.swing.ButtonGroup;
   29   import javax.swing.JComponent;
   30   import javax.swing.JLabel;
   31   import javax.swing.JPanel;
   32   import javax.swing.JRadioButton;
   33   import javax.swing.event.ChangeEvent;
   34   import javax.swing.event.ChangeListener;
   35   
   36   /**
   37    * JLabeledRadio will create a set of Radio buttons with a label.
   38    * 
   39    * @see org.apache.jmeter.gui.util.JLabeledRadioI18N for a version that is better suited to
   40    * multi-language use.
   41    */
   42   public class JLabeledRadio extends JPanel implements JLabeledField, ActionListener {
   43   
   44       private JLabel mLabel = new JLabel();
   45   
   46       private ButtonGroup bGroup = new ButtonGroup();
   47   
   48       private ArrayList mChangeListeners = new ArrayList(3);
   49   
   50       /**
   51        *
   52        */
   53       public JLabeledRadio() {
   54           super();
   55           this.add(mLabel);
   56       }
   57   
   58       public JLabeledRadio(String label, String[] items) {
   59           mLabel.setText(label);
   60           init(items, null);
   61       }
   62   
   63       public JLabeledRadio(String label, String[] items, String selectedItem) {
   64           mLabel.setText(label);
   65           init(items, selectedItem);
   66       }
   67   
   68       /**
   69        * Method is responsible for creating the JRadioButtons and adding them to
   70        * the ButtonGroup.
   71        *
   72        * @param items
   73        */
   74       private void init(String[] items, String selected) {
   75           this.add(mLabel);
   76           for (int idx = 0; idx < items.length; idx++) {
   77               JRadioButton btn = new JRadioButton(items[idx]);
   78               btn.setActionCommand(items[idx]);
   79               btn.addActionListener(this);
   80               // add the button to the button group
   81               this.bGroup.add(btn);
   82               // add the button
   83               this.add(btn);
   84               if (selected != null && selected.equals(items[idx])) {
   85                   btn.setSelected(true);
   86               }
   87           }
   88       }
   89   
   90       /**
   91        * setItems will set the radio button items. The implementation first
   92        * removes the old JRadioButton, then it creates new ones.
   93        *
   94        * @param items
   95        */
   96       public void setItems(String[] items) {
   97           Enumeration en = this.bGroup.getElements();
   98           while (en.hasMoreElements()) {
   99               JComponent comp = (JComponent) en.nextElement();
  100               this.bGroup.remove((JRadioButton) comp);
  101               this.remove(comp);
  102           }
  103           init(items, null);
  104       }
  105   
  106       /**
  107        * The implementation will get the Text value from the selected radio button
  108        * in the JButtonGroup.
  109        */
  110       public String getText() {
  111           return this.bGroup.getSelection().getActionCommand();
  112       }
  113   
  114       /**
  115        * The implementation will iterate through the radio buttons and find the
  116        * match. It then sets it to selected and sets all other radion buttons as
  117        * not selected.
  118        */
  119       public void setText(String text) {
  120           Enumeration en = this.bGroup.getElements();
  121           while (en.hasMoreElements()) {
  122               JRadioButton jrb = (JRadioButton) en.nextElement();
  123               if (jrb.getText().equals(text)) {
  124                   this.bGroup.setSelected(jrb.getModel(), true);
  125               } else {
  126                   this.bGroup.setSelected(jrb.getModel(), false);
  127               }
  128           }
  129       }
  130   
  131       /*
  132        * (non-Javadoc)
  133        *
  134        * @see org.apache.jorphan.gui.JLabeledField#setLabel(java.lang.String)
  135        */
  136       public void setLabel(String pLabel) {
  137           this.mLabel.setText(pLabel);
  138       }
  139   
  140       /*
  141        * (non-Javadoc)
  142        *
  143        * @see org.apache.jorphan.gui.JLabeledField#addChangeListener(javax.swing.event.ChangeListener)
  144        */
  145       public void addChangeListener(ChangeListener pChangeListener) {
  146           this.mChangeListeners.add(pChangeListener);
  147       }
  148   
  149       /**
  150        * Notify all registered change listeners that the text in the text field
  151        * has changed.
  152        */
  153       private void notifyChangeListeners() {
  154           ChangeEvent ce = new ChangeEvent(this);
  155           for (int index = 0; index < mChangeListeners.size(); index++) {
  156               ((ChangeListener) mChangeListeners.get(index)).stateChanged(ce);
  157           }
  158       }
  159   
  160       /**
  161        * Method will return all the label and JRadioButtons. ButtonGroup is
  162        * excluded from the list.
  163        */
  164       public List getComponentList() {
  165           List comps = new LinkedList();
  166           comps.add(mLabel);
  167           Enumeration en = this.bGroup.getElements();
  168           while (en.hasMoreElements()) {
  169               comps.add(en.nextElement());
  170           }
  171           return comps;
  172       }
  173   
  174       /**
  175        * When a radio button is clicked, an ActionEvent is triggered.
  176        */
  177       public void actionPerformed(ActionEvent e) {
  178           this.notifyChangeListeners();
  179       }
  180   
  181   }

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