package util.swing;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;

/**
  * A {@link JTextField} that writes it current contents into a String referenced through an array. This is useful
  * so that you will not need references to the <code>JTextField</code> in your code that might eventually lead
  * to the text field being serialized which, in turn, might not work properly.
  *
  * <p><b>Note:</b> This class is not meant to be serialized!</p>
  *
  * @author Steffen Zschaler
  * @version 2.0 28/07/1999
  * @since v2.0
  */
public class JTextInput extends JTextField {

  /**
    * The currently referenced output observer. The current text of the input field can be found as the first
    * element of the array.
    *
    * @serial This class is not meant to be serialized!
    */
  protected String[] m_asOutput;

  /**
    * Create a new <code>JTextInput</code>.
    *
    * @param asOutput the output observer. The current text of the input field can be found as the first
    * element of the array at any time.
    * @param sInitial the initial value of the text field.
    */
  public JTextInput (String[] asOutput, String sInitial) {
    super (sInitial);

    m_asOutput = asOutput;

    getDocument().addDocumentListener (new DocumentListener() {
      public void changedUpdate (DocumentEvent e) {
        performUpdate();
      }

      public void insertUpdate (DocumentEvent e) {
        performUpdate();
      }

      public void removeUpdate (DocumentEvent e) {
        performUpdate();
      }

      private void performUpdate() {
        m_asOutput[0] = getText();
      }
    });
  }
}