/*
 * File: p05.java
 *
 * Description:  Display the following:
 *       - An entry field
 *       - A Print Button
 *       - A Quit Button
 *
 * Modifications:  11/6/95 CValcarcel Created.
 *
 * NO WARRANTY, EXPRESSED OR IMPLIED, BLAH, BLAH, BLAH.
 *
 * THIS IS EXAMPLE CODE AND SHOULD NOT BE USED FOR ANYTHING
 * EXCEPT INSTRUCTIONAL PURPOSES.
 *
 */

//
// Use an asterisk so the compiler goes out and looks
// through the entire awt library for anything we use.
//
import java.awt.*;

//
// Another name change.  EntryApp is a Frame and
// has a Panel.
//
class EntryApp extends Frame
{
    // Static variables (class-level scope)
    private static int FRAME_WIDTH  = 225;
    private static int FRAME_HEIGHT =  60;

    // Instance variable (object-level scope)
    Panel pMain;

    public EntryApp( String title )
    {
        super( title );  // Set the Frame title

        //
        // Panels are the premier way of getting things to
        // congregate without too much undue pressure on
        // the cranial cavity.  GridLayout slices up the Panel
        // into equal parts based on the first two arguments
        // (row, col).  In this case the first two args will
        // force the Panel to take in GUI objects and lay them
        // out one on top of the other.
        //
        pMain = new Panel();
        pMain.setLayout( new GridLayout(2, 1, 10, 10) );

        //
        // Another new class.  This will create a TextField
        // with a label attached.  This is useful class to have
        // in any case!
        //
        LabeledTextField ltfInputField =
                     new LabeledTextField( "Print string:" );
        pMain.add( ltfInputField );

        //
        // Another new class!  This will create a Panel with
        // the two buttons laid out however we want them to.
        // If we want to change them later it should have
        // minimal impact on this code.
        //
        // We pass in the LabeledTextField object as we
        // need access to it to grab whatever was typed in it.
        //
        pMain.add( new ButtonPanel(ltfInputField) );

        add( pMain );

        resize( FRAME_WIDTH, FRAME_HEIGHT );
        show();

        // This must be done after show().  If done before
        // show() the focus will not go where expected.
        ltfInputField.inputField.requestFocus();

        //
        // We do this AFTER show() because it will stop resize
        // from working properly if done before.
        //
        setResizable( false );
    }

    public void repaint()
    {
        pMain.resize( size() );
    }

    //
    // My, my.  What a short application...
    //
    public static void main( String args[] )
    {
        EntryApp app = new EntryApp( "Entry Field Application" );
    }
}

//
// LabeledTextField is a Panel and has a Label and
// a TextField (you might be fooled into thinking
// a class called LabeledTextField is a TextField
// and has a Label.  Well, how are you going to
// attach them?).
//
// A more complete implimentation would have:
//  - a constructor that allowed the caller to vary
//    the size of the entry field.
//  - a default string for the label in case none was specified
//
// I leave the above as an exercise to the reader ;-).
//
class LabeledTextField extends Panel
{
    // Static variable
    private static int MIN_LENGTH = 20;

    // Instance variable.  TextField takes in user
    // input and allows minimal editing.
    public TextField inputField;

    public LabeledTextField( String tfLabel )
    {
        // As GridLayout forces the pieces being laid out
        // to be equal sizes we go with BorderLayout instead.
        setLayout( new BorderLayout() );

        inputField = new TextField(MIN_LENGTH);
        add( "West", new Label(tfLabel) );
        add( "East", inputField );
    }
}

//
// ButtonPanel is a Panel and has Buttons
//
class ButtonPanel extends Panel
{
    public ButtonPanel( LabeledTextField inputField )
    {
        //
        // I want the buttons to be the same size so
        // GridLayout() will be used.
        //
        setLayout( new GridLayout(1, 2) );

        add( new PrintButton(inputField) );
        add( new QuitButton() );
    }
}

//
// PrintButton is a Button and has a LabeledTextField
//
class PrintButton extends Button
{
    LabeledTextField ltfField;  // Allows us to access the user's input.

    public PrintButton( LabeledTextField inputField )
    {
        super( "Print to stdout" );

        //
        // Save this so we can access its contents
        //
        ltfField = inputField;
    }

    //
    // We don't even bother to check what kind
    // of event has just happened (as it so happens
    // an ACTION_EVENT has just occurred).
    //
    public boolean handleEvent( Event event )
    {
         System.out.println( ltfField.inputField.getText() );

         return true;
    }
}

//
// This is exactly what we used in the last example
//
class QuitButton extends Button
{
    public QuitButton()
    {
        super( "Quit" );
    }

    public boolean handleEvent( Event event )
    {
         System.exit( 0 );

         return true;
    }
}

