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.Insets;
   22   import java.awt.event.ActionEvent;
   23   import java.awt.event.ActionListener;
   24   import java.awt.event.ItemEvent;
   25   import java.awt.event.ItemListener;
   26   import java.util.ArrayList;
   27   import java.util.LinkedList;
   28   import java.util.List;
   29   
   30   import javax.swing.BorderFactory;
   31   import javax.swing.JButton;
   32   import javax.swing.JComboBox;
   33   import javax.swing.JLabel;
   34   import javax.swing.JPanel;
   35   import javax.swing.event.ChangeEvent;
   36   import javax.swing.event.ChangeListener;
   37   
   38   public class JLabeledChoice extends JPanel implements JLabeledField {
   39       private static final Object[] EMPTY_OBJECT_ARRAY = new Object [0];
   40   
   41       private final JLabel mLabel = new JLabel();
   42   
   43       private final JComboBox choiceList;
   44   
   45       // Maybe move to vector if MT problems occur
   46       private final ArrayList mChangeListeners = new ArrayList(3);
   47   
   48       private JButton delete, add;
   49   
   50       /**
   51        * Default constructor, The label and the Text field are left empty.
   52        */
   53       public JLabeledChoice() {
   54           super();
   55           choiceList = new JComboBox();
   56           init();
   57       }
   58   
   59       public JLabeledChoice(String pLabel, boolean editable) {
   60           super();
   61           choiceList = new JComboBox();
   62           mLabel.setText(pLabel);
   63           choiceList.setEditable(editable);
   64           init();
   65       }
   66   
   67       /**
   68        * Constructs a non-edittable combo-box with the label displaying the passed text.
   69        *
   70        * @param pLabel - the text to display in the label.
   71        * @param items - the items to display in the Combo box
   72        */
   73       public JLabeledChoice(String pLabel, String[] items) {
   74           this(pLabel, items, false);
   75       }
   76   
   77       /**
   78        * Constructs a combo-box with the label displaying the passed text.
   79        *
   80        * @param pLabel - the text to display in the label.
   81        * @param items - the items to display in the Combo box
   82        * @param editable - if true, then Add and Delete buttons are created.
   83        *
   84        */
   85       public JLabeledChoice(String pLabel, String[] items, boolean editable) {
   86           super();
   87           mLabel.setText(pLabel);
   88           choiceList = new JComboBox(items);
   89           choiceList.setEditable(editable);
   90           init();
   91       }
   92   
   93       public List getComponentList() {
   94           List comps = new LinkedList();
   95           comps.add(mLabel);
   96           comps.add(choiceList);
   97           return comps;
   98       }
   99   
  100       public void setEditable(boolean editable) {
  101           choiceList.setEditable(editable);
  102       }
  103   
  104       public void addValue(String item) {
  105           choiceList.addItem(item);
  106       }
  107   
  108       public void setValues(String[] items) {
  109           choiceList.removeAllItems();
  110           for (int i = 0; i < items.length; i++) {
  111               choiceList.addItem(items[i]);
  112           }
  113       }
  114   
  115       /**
  116        * Initialises all of the components on this panel.
  117        */
  118       private void init() {
  119           /*
  120            * if(choiceList.isEditable()) { choiceList.addActionListener(new
  121            * ComboListener()); }
  122            */
  123           choiceList.setBorder(BorderFactory.createLoweredBevelBorder());
  124           // Register the handler for focus listening. This handler will
  125           // only notify the registered when the text changes from when
  126           // the focus is gained to when it is lost.
  127           choiceList.addItemListener(new ItemListener() {
  128               /**
  129                * Callback method when the focus to the Text Field component is
  130                * lost.
  131                *
  132                * @param e
  133                *            The focus event that occured.
  134                */
  135               public void itemStateChanged(ItemEvent e) {
  136                   if (e.getStateChange() == ItemEvent.SELECTED) {
  137                       notifyChangeListeners();
  138                   }
  139               }
  140           });
  141   
  142           // Add the sub components
  143           this.add(mLabel);
  144           this.add(choiceList);
  145           if (choiceList.isEditable()) {
  146               add = new JButton("Add");
  147               add.setMargin(new Insets(1, 1, 1, 1));
  148               add.addActionListener(new AddListener());
  149               this.add(add);
  150               delete = new JButton("Del");
  151               delete.setMargin(new Insets(1, 1, 1, 1));
  152               delete.addActionListener(new DeleteListener());
  153               this.add(delete);
  154           }
  155   
  156       }
  157   
  158       /**
  159        * Set the text displayed in the label.
  160        *
  161        * @param pLabel
  162        *            The new label text.
  163        */
  164       public void setLabel(String pLabel) {
  165           mLabel.setText(pLabel);
  166       }
  167   
  168       /**
  169        * Set the text displayed in the Text Field.
  170        *
  171        * @param pText
  172        *            The new text to display in the text field.
  173        */
  174       public void setText(String pText) {
  175           choiceList.setSelectedItem(pText);
  176       }
  177   
  178       public void setSelectedIndex(int index){
  179           choiceList.setSelectedIndex(index);
  180       }
  181       /**
  182        * Returns the text in the Text Field.
  183        *
  184        * @return The text in the Text Field. Never returns null.
  185        */
  186       public String getText() {
  187           Object item = choiceList.getSelectedItem();
  188           if (item == null) {
  189               return "";
  190           } else {
  191               return (String) item;
  192           }
  193       }
  194   
  195       public int getSelectedIndex(){
  196           return choiceList.getSelectedIndex();
  197       }
  198   
  199       public Object[] getSelectedItems() {
  200           Object list[]=choiceList.getSelectedObjects();
  201           if (list==null) {
  202               return EMPTY_OBJECT_ARRAY;
  203           }
  204           return list;
  205       }
  206   
  207       public String[] getItems() {
  208           String[] items = new String[choiceList.getItemCount()];
  209           for (int i = 0; i < items.length; i++) {
  210               items[i] = (String) choiceList.getItemAt(i);
  211           }
  212           return items;
  213       }
  214   
  215       /**
  216        * Returns the text of the label.
  217        *
  218        * @return The text of the label.
  219        */
  220       public String getLabel() {
  221           return mLabel.getText();
  222       }
  223   
  224      /**
  225       * Registers the text to display in a tool tip.
  226       * The text displays when the cursor lingers over the component.
  227       * @param text the string to display; if the text is null,
  228       *      the tool tip is turned off for this component
  229       */
  230      public void setToolTipText(String text) {
  231          choiceList.setToolTipText(text);
  232      }
  233   
  234      /**
  235        * Returns the tooltip string that has been set with setToolTipText
  236        * @return the text of the tool tip
  237        */
  238      public String getToolTipText() {
  239          if (choiceList == null){ // Necessary to avoid NPE when testing serialisation
  240              return null;
  241          }
  242          return choiceList.getToolTipText();
  243      }
  244   
  245       /**
  246        * Adds a change listener, that will be notified when the text in the text
  247        * field is changed. The ChangeEvent that will be passed to registered
  248        * listeners will contain this object as the source, allowing the new text
  249        * to be extracted using the {@link #getText() getText} method.
  250        *
  251        * @param pChangeListener
  252        *            The listener to add
  253        */
  254       public void addChangeListener(ChangeListener pChangeListener) {
  255           mChangeListeners.add(pChangeListener);
  256       }
  257   
  258       /**
  259        * Removes a change listener.
  260        *
  261        * @param pChangeListener
  262        *            The change listener to remove.
  263        */
  264       public void removeChangeListener(ChangeListener pChangeListener) {
  265           mChangeListeners.remove(pChangeListener);
  266       }
  267   
  268       /**
  269        * Notify all registered change listeners that the text in the text field
  270        * has changed.
  271        */
  272       private void notifyChangeListeners() {
  273           ChangeEvent ce = new ChangeEvent(this);
  274           for (int index = 0; index < mChangeListeners.size(); index++) {
  275               ((ChangeListener) mChangeListeners.get(index)).stateChanged(ce);
  276           }
  277       }
  278   
  279       private class AddListener implements ActionListener {
  280   
  281           public void actionPerformed(ActionEvent e) {
  282               Object item = choiceList.getSelectedItem();
  283               int index = choiceList.getSelectedIndex();
  284               if (!item.equals(choiceList.getItemAt(index))) {
  285                   choiceList.addItem(item);
  286               }
  287               choiceList.setSelectedItem(item);
  288               notifyChangeListeners();
  289           }
  290       }
  291   
  292       private class DeleteListener implements ActionListener {
  293   
  294           public void actionPerformed(ActionEvent e) {
  295               if (choiceList.getItemCount() > 1) {
  296                   choiceList.removeItemAt(choiceList.getSelectedIndex());
  297                   notifyChangeListeners();
  298               }
  299           }
  300       }
  301   }

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