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.event.FocusEvent;
   23   import java.awt.event.FocusListener;
   24   import java.util.ArrayList;
   25   import java.util.LinkedList;
   26   import java.util.List;
   27   
   28   import javax.swing.JLabel;
   29   import javax.swing.JPanel;
   30   import javax.swing.JScrollPane;
   31   import javax.swing.JTextArea;
   32   import javax.swing.event.ChangeEvent;
   33   import javax.swing.event.ChangeListener;
   34   import javax.swing.text.Document;
   35   
   36   /**
   37    * A Helper component that wraps a JTextField with a label into a JPanel (this).
   38    * This component also has an efficient event handling mechanism for handling
   39    * the text changing in the Text Field. The registered change listeners are only
   40    * called when the text has changed.
   41    *
   42    */
   43   public class JLabeledTextArea extends JPanel implements JLabeledField, FocusListener {
   44       private JLabel mLabel;
   45   
   46       private JTextArea mTextArea;
   47   
   48       // Maybe move to vector if MT problems occur
   49       private 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 JLabeledTextArea() {
   58           this("", null);
   59       }
   60   
   61       /**
   62        * Constructs a new component with the label displaying the passed text.
   63        *
   64        * @param label
   65        *            The text to display in the label.
   66        */
   67       public JLabeledTextArea(String label) {
   68           this(label, null);
   69       }
   70   
   71       /**
   72        * Constructs a new component with the label displaying the passed text.
   73        *
   74        * @param pLabel
   75        *            The text to display in the label.
   76        * @param docModel the document for the text area
   77        */
   78       public JLabeledTextArea(String pLabel, Document docModel) {
   79           super();
   80           mLabel = new JLabel(pLabel);
   81           init();
   82           if (docModel != null) {
   83               mTextArea.setDocument(docModel);
   84           }
   85       }
   86   
   87       public List getComponentList() {
   88           List comps = new LinkedList();
   89           comps.add(mLabel);
   90           comps.add(mTextArea);
   91           return comps;
   92       }
   93   
   94       public void setDocumentModel(Document docModel) {
   95           mTextArea.setDocument(docModel);
   96       }
   97   
   98       /**
   99        * Initialises all of the components on this panel.
  100        */
  101       private void init() {
  102           setLayout(new BorderLayout());
  103   
  104           mTextArea = new JTextArea();
  105           mTextArea.setRows(4);
  106           mTextArea.setLineWrap(true);
  107           mTextArea.setWrapStyleWord(true);
  108           // Register the handler for focus listening. This handler will
  109           // only notify the registered when the text changes from when
  110           // the focus is gained to when it is lost.
  111           mTextArea.addFocusListener(this);
  112   
  113           // Add the sub components
  114           this.add(mLabel, BorderLayout.NORTH);
  115           this.add(new JScrollPane(mTextArea), BorderLayout.CENTER);
  116       }
  117   
  118       /**
  119        * Callback method when the focus to the Text Field component is lost.
  120        *
  121        * @param pFocusEvent
  122        *            The focus event that occured.
  123        */
  124       public void focusLost(FocusEvent pFocusEvent) {
  125           // Compare if the value has changed, since we received focus.
  126           if (!oldValue.equals(mTextArea.getText())) {
  127               notifyChangeListeners();
  128           }
  129       }
  130   
  131       /**
  132        * Catch what the value was when focus was gained.
  133        */
  134       public void focusGained(FocusEvent pFocusEvent) {
  135           oldValue = mTextArea.getText();
  136       }
  137   
  138       /**
  139        * Set the text displayed in the label.
  140        *
  141        * @param pLabel
  142        *            The new label text.
  143        */
  144       public void setLabel(String pLabel) {
  145           mLabel.setText(pLabel);
  146       }
  147   
  148       /**
  149        * Set the text displayed in the Text Field.
  150        *
  151        * @param pText
  152        *            The new text to display in the text field.
  153        */
  154       public void setText(String pText) {
  155           mTextArea.setText(pText);
  156       }
  157   
  158       /**
  159        * Returns the text in the Text Field.
  160        *
  161        * @return The text in the Text Field.
  162        */
  163       public String getText() {
  164           return mTextArea.getText();
  165       }
  166   
  167       /**
  168        * Returns the text of the label.
  169        *
  170        * @return The text of the label.
  171        */
  172       public String getLabel() {
  173           return mLabel.getText();
  174       }
  175   
  176       public void setEnabled(boolean enable) {
  177           super.setEnabled(enable);
  178           mTextArea.setEnabled(enable);
  179       }
  180   
  181       /**
  182        * Registers the text to display in a tool tip.
  183        * The text displays when the cursor lingers over the component.
  184        * @param text the string to display; if the text is null,
  185        *      the tool tip is turned off for this component
  186        */
  187       public void setToolTipText(String text) {
  188           mTextArea.setToolTipText(text);
  189       }
  190   
  191       /**
  192         * Returns the tooltip string that has been set with setToolTipText
  193         * @return the text of the tool tip
  194         */
  195       public String getToolTipText() {
  196           return mTextArea.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       private 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]