import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * Illustrates JTextFields, subclasses of these
 * (implemented incorrectly and correctly)
 * and the JList class/model/view 
 *
 * Entries in the top text field are echoed/stored
 * in the bottom text field and stored in middle ExpandableList.
 * Entries selected (double-click) from ExpandableList are
 * stored in bottom text field
 *
 *
 * @author Owen Astrachan
 * @version 1.0 February 20, 2001
 * @see JTextField
 * @see LowerTextField
 * @see FlawedLowerTextField
 * @see ExpandableList
 * @see JList
 */

public class TextFieldDemo extends JFrame
{
    /**
     * construct the simple GUI for demo
     */
    
    public TextFieldDemo()
    {
	setTitle("TextField Demo");
	JPanel panel = (JPanel) getContentPane();
	panel.setLayout(new BorderLayout());
	setDefaultCloseOperation(EXIT_ON_CLOSE);

	mySpecialListener = new SpecialTextAdder();
	makeTopRow();
	makeMiddle();
	makeBottomRow();

	pack();
	setVisible(true);
    }

    /**
     * put a label and LowerTextField in the bottom
     */
    
    private void makeBottomRow()
    {
	JPanel pan = new JPanel();
	pan.add(new JLabel("Special TextField"));
//	mySpecialField = new FlawedLowerTextField(20);
	mySpecialField = new LowerTextField(20);
	pan.add(mySpecialField);
	getContentPane().add(pan,BorderLayout.SOUTH); 
    }

    /**
     * put ExpandableList in middle of GUI
     */
    
    private void makeMiddle()
    {
	myList = new ExpandableList();
	
	myList.addActionListener(mySpecialListener);
	
	JScrollPane scroller = new JScrollPane(myList);
	getContentPane().add(scroller,BorderLayout.CENTER);
    }
    
    /**
     * put a label and (normal) JTextField in the top
     * and wire the action of the textfield to the special text field
     */
    
    private void makeTopRow()
    {
	JPanel pan = new JPanel();
	pan.add(new JLabel("Normal TextField"));
	
	myText = new JTextField(20);
	
	myText.addActionListener(mySpecialListener);
	myText.addActionListener(new ActionListener()
	    {
		public void actionPerformed(ActionEvent ev)
		{
		    myList.add(ev.getActionCommand());
		}
	    });
	    
	pan.add(myText);
	getContentPane().add(pan,BorderLayout.NORTH);
    }

    private JTextField     mySpecialField;
    private JTextField     myText;
    private ExpandableList myList;
    private ActionListener mySpecialListener;
    
    class SpecialTextAdder implements ActionListener
    {
	public void actionPerformed(ActionEvent ev)
	{
	    mySpecialField.setText(ev.getActionCommand());
	}
    }
    
    public static void main(String args[])
    {
	TextFieldDemo demo = new TextFieldDemo();
    }
}
