/*
* Copyright(c) 2000 Soltima, Inc.
* Soltima Wireless Publishing Platform (WPP)
*
* @author S.Coleman
*/
package org.apache.jmeter.gui;
import javax.swing.*;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.util.ArrayList;
import java.awt.event.FocusListener;
import java.awt.event.FocusEvent;
/**
* A Helper component that wraps a JTextField with a label into
* a JPanel (this). This component also has an efficient event handling
* mechanism for handling the text changing in the Text Field. The registered
* change listeners are only called when the text has changed.
*
* @author S.Coleman
*/
public class JLabeledTextField extends JPanel
{
private JLabel mLabel = new JLabel();
private JTextField mTextField = new JTextField(30);
private ArrayList mChangeListeners = new ArrayList(3); // Maybe move to vector if MT problems occur
/**
* Default constructor, The label and the Text field are left empty.
*/
public JLabeledTextField()
{
super();
init();
}
/**
* Constructs a new component with the label displaying the
* passed text.
*
* @param pLabel The text to in the label.
*/
public JLabeledTextField(String pLabel)
{
super();
mLabel.setText(pLabel);
init();
}
/**
* Initialises all of the components on this panel.
*/
private void init()
{
// Register the handler for focus listening. This handler will
// only notify the registered when the text changes from when
// the focus is gained to when it is lost.
mTextField.addFocusListener(new FocusListener(){
String oldValue = ""; // A temporary cache.
/**
* Callback method when the focus to the Text Field component
* is lost.
*
* @param pFocusEvent The focus event that occured.
*/
public void focusLost(FocusEvent pFocusEvent)
{
// Compare if the value has changed, since we received focus.
if (oldValue.equals(mTextField.getText()) == false)
{
notifyChangeListeners();
}
}
/**
* Catch what the value was when focus was gained.
*/
public void focusGained(FocusEvent pFocusEvent)
{
oldValue = mTextField.getText();
}
});
// Add the sub components
this.add(mLabel);
this.add(mTextField);
}
/**
* Set the text displayed in the label.
*
* @param pLabel The new label text.
*/
public void setLabel(String pLabel)
{
mLabel.setText(pLabel);
}
/**
* Set the text displayed in the Text Field.
*
* @param pText The new text to display in the text field.
*/
public void setText(String pText)
{
mTextField.setText(pText);
}
/**
* Returns the text in the Text Field.
*
* @return The text in the Text Field.
*/
public String getText()
{
return mTextField.getText();
}
/**
* Returns the text of the label.
*
* @return The text of the label.
*/
public String getLabel()
{
return mLabel.getText();
}
/**
* Adds a change listener, that will be notified when the text in the
* text field is changed. The ChangeEvent that will be passed
* to registered listeners will contain this object as the source, allowing
* the new text to be extracted using the {@link #getText() getText} method.
*
* @param pChangeListener The listener to add
*/
public void addChangeListener(ChangeListener pChangeListener)
{
mChangeListeners.add(pChangeListener);
}
/**
* Removes a change listener.
*
* @param pChangeListener The change listener to remove.
*/
public void removeChangeListener(ChangeListener pChangeListener)
{
mChangeListeners.remove(pChangeListener);
}
/**
* Notify all registered change listeners that the
* text in the text field has changed.
*/
private void notifyChangeListeners()
{
ChangeEvent ce = new ChangeEvent(this);
for (int index = 0; index < mChangeListeners.size(); index ++)
{
((ChangeListener)mChangeListeners.get(index)).stateChanged(ce);
}
}
}