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.BorderLayout;
   22   import java.awt.Color;
   23   import java.awt.event.FocusEvent;
   24   import java.awt.event.FocusListener;
   25   import java.util.ArrayList;
   26   import java.util.LinkedList;
   27   import java.util.List;
   28   
   29   import javax.swing.JLabel;
   30   import javax.swing.JPanel;
   31   import javax.swing.JTextField;
   32   import javax.swing.event.ChangeEvent;
   33   import javax.swing.event.ChangeListener;
   34   
   35   /**
   36    * A Helper component that wraps a JTextField with a label into a JPanel (this).
   37    * This component also has an efficient event handling mechanism for handling
   38    * the text changing in the Text Field. The registered change listeners are only
   39    * called when the text has changed.
   40    *
   41    * @version $Revision: 773157 $
   42    */
   43   public class JLabeledTextField extends JPanel implements JLabeledField, FocusListener {
   44       private JLabel mLabel;
   45   
   46       private JTextField mTextField;
   47   
   48       // Maybe move to vector if MT problems occur
   49       private final ArrayList mChangeListeners = new ArrayList(3);
   50   
   51       // A temporary cache for the focus listener
   52       private String oldValue = "";
   53   
   54       /**
   55        * Default constructor, The label and the Text field are left empty.
   56        */
   57       public JLabeledTextField() {
   58           this("", 20);
   59       }
   60   
   61       /**
   62        * Constructs a new component with the label displaying the passed text.
   63        *
   64        * @param pLabel
   65        *            The text to in the label.
   66        */
   67       public JLabeledTextField(String pLabel) {
   68           this(pLabel, 20);
   69       }
   70   
   71       public JLabeledTextField(String pLabel, int size) {
   72           super();
   73           mTextField = createTextField(size);
   74           mLabel = new JLabel(pLabel);
   75           mLabel.setLabelFor(mTextField);
   76           init();
   77       }
   78   
   79       public JLabeledTextField(String pLabel, Color bk) {
   80           super();
   81           mTextField = createTextField(20);
   82           mLabel = new JLabel(pLabel);
   83           mLabel.setBackground(bk);
   84           mLabel.setLabelFor(mTextField);
   85           this.setBackground(bk);
   86           init();
   87       }
   88   
   89       public List getComponentList() {
   90           List comps = new LinkedList();
   91           comps.add(mLabel);
   92           comps.add(mTextField);
   93           return comps;
   94       }
   95   
   96       public void setEnabled(boolean enable) {
   97           super.setEnabled(enable);
   98           mTextField.setEnabled(enable);
   99       }
  100   
  101       protected JTextField createTextField(int size) {
  102           return new JTextField(size);
  103       }
  104   
  105       /**
  106        * Initialises all of the components on this panel.
  107        */
  108       private void init() {
  109           setLayout(new BorderLayout(5, 0));
  110           // Register the handler for focus listening. This handler will
  111           // only notify the registered when the text changes from when
  112           // the focus is gained to when it is lost.
  113           mTextField.addFocusListener(this);
  114   
  115           // Add the sub components
  116           add(mLabel, BorderLayout.WEST);
  117           add(mTextField, BorderLayout.CENTER);
  118       }
  119   
  120       /**
  121        * Callback method when the focus to the Text Field component is lost.
  122        *
  123        * @param pFocusEvent
  124        *            The focus event that occured.
  125        */
  126       public void focusLost(FocusEvent pFocusEvent) {
  127           // Compare if the value has changed, since we received focus.
  128           if (!oldValue.equals(mTextField.getText())) {
  129               notifyChangeListeners();
  130           }
  131       }
  132   
  133       /**
  134        * Catch what the value was when focus was gained.
  135        */
  136       public void focusGained(FocusEvent pFocusEvent) {
  137           oldValue = mTextField.getText();
  138       }
  139   
  140       /**
  141        * Set the text displayed in the label.
  142        *
  143        * @param pLabel
  144        *            The new label text.
  145        */
  146       public void setLabel(String pLabel) {
  147           mLabel.setText(pLabel);
  148       }
  149   
  150       /**
  151        * Set the text displayed in the Text Field.
  152        *
  153        * @param pText
  154        *            The new text to display in the text field.
  155        */
  156       public void setText(String pText) {
  157           mTextField.setText(pText);
  158       }
  159   
  160       /**
  161        * Returns the text in the Text Field.
  162        *
  163        * @return The text in the Text Field.
  164        */
  165       public String getText() {
  166           return mTextField.getText();
  167       }
  168   
  169       /**
  170        * Returns the text of the label.
  171        *
  172        * @return The text of the label.
  173        */
  174       public String getLabel() {
  175           return mLabel.getText();
  176       }
  177   
  178       /**
  179        * Registers the text to display in a tool tip.
  180        * The text displays when the cursor lingers over the component.
  181        * @param text the string to display; if the text is null,
  182        *      the tool tip is turned off for this component
  183        */
  184       public void setToolTipText(String text) {
  185           mTextField.setToolTipText(text);
  186       }
  187   
  188       /**
  189         * Returns the tooltip string that has been set with setToolTipText
  190         * @return the text of the tool tip
  191         */
  192       public String getToolTipText() {
  193           if (mTextField == null){ // Necessary to avoid NPE when testing serialisation
  194               return null;
  195           }
  196          return mTextField.getToolTipText();
  197       }
  198   
  199       /**
  200        * Adds a change listener, that will be notified when the text in the text
  201        * field is changed. The ChangeEvent that will be passed to registered
  202        * listeners will contain this object as the source, allowing the new text
  203        * to be extracted using the {@link #getText() getText} method.
  204        *
  205        * @param pChangeListener
  206        *            The listener to add
  207        */
  208       public void addChangeListener(ChangeListener pChangeListener) {
  209           mChangeListeners.add(pChangeListener);
  210       }
  211   
  212       /**
  213        * Removes a change listener.
  214        *
  215        * @param pChangeListener
  216        *            The change listener to remove.
  217        */
  218       public void removeChangeListener(ChangeListener pChangeListener) {
  219           mChangeListeners.remove(pChangeListener);
  220       }
  221   
  222       /**
  223        * Notify all registered change listeners that the text in the text field
  224        * has changed.
  225        */
  226       protected void notifyChangeListeners() {
  227           ChangeEvent ce = new ChangeEvent(this);
  228           for (int index = 0; index < mChangeListeners.size(); index++) {
  229               ((ChangeListener) mChangeListeners.get(index)).stateChanged(ce);
  230           }
  231       }
  232   }

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