import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;


/*
    Input class - an abstract class for text input dialog. Allows user to
    enter some textual data and pass it to the state machine. Subclasses
    must implement passInput method.
*/

abstract class Input extends Frame
{
    /*
        INSTANCE VARIABLES
    */


    /* text field where user will be entering parameters */

    private TextField   input;

    /* parent object */

    private Container       parent;

    /* state machine object that will be given entered parameters */

    protected Machine   machine;


    /* hint line object */

    protected Help      help;


    /*
        PUBLIC METHODS
    */


    /*
        constructor method
    */

    public Input (Container par, String label)
    {
        /* initialize instance variables */

        parent  = par;
        setTitle(label);

        machine = (Machine) null;
        help    = (Help) null;

        //setBackground (new Color (150, 150, 150));

        setLayout(new BorderLayout());
        Panel topPanel = new Panel();
        topPanel.setLayout(new BorderLayout(0, 20));
        add("Center", topPanel);

        /* create and add to dialog command label */

        Label l = new Label (label);
        l.setAlignment (Label.CENTER);
        topPanel.add ("North", l);

        /* create and add to dialog text field */

        input = new TextField (50);
        topPanel.add ("Center", input);

        /* create and add to dialog a panel holding two buttons */

        Panel p = new Panel ();
        p.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 0));
        p.add (new Button ("OK"));
        p.add (new Button ("Cancel"));
        topPanel.add ("South", p);

        /* make sure dialog does not display until specifically told to */

        hide ();

    } /* end Input */


	public void setTextfield (String i)
	{
		input.setText(i);
	}


    /*
        set machine object that will receive the entered parameters
    */

    public void setMachine (Machine m, Help h)
    {
        machine = m;
        help = h;

    } /* end setMachine */


    /*
        during repaint draw a 3d rectangle around the dialog for cooler look
    */

    public void paint (Graphics g)
    {
        //Rectangle bounds = bounds ();

        //g.setColor (getBackground ());
        //g.draw3DRect (0, 0, bounds.width - 1, bounds.height - 1, true);

    } /* end paint */


    /*
        leave some space around the edges
    */

    public Insets insets()
    {
        return new Insets (30, 20, 30, 20);
        //format: north, west, south, east

    } /* end Insets */


    /*
        pass data to the state machine - must be implemented by subclass
    */

    abstract public void passInput (String i);


    /*
        handle action events (button presses)
    */

    public boolean handleEvent (Event e)
    {
        String label;

        if (e.target instanceof Button)
        {
   			label = ((Button)(e.target)).getLabel();

            if (label.equals ("OK"))
            {
                /* pass entered text to the machine */

                passInput (input.getText ());

                /* hide our dialog and force repaint of the parent */

                hide();
                parent.repaint ();

                return true;
            }
            else if (label.equals ("Cancel"))
            {
                /* clear the text field */

                input.setText ("");

                /* hide our dialog and force repaint of the parent */

                hide();
                parent.repaint ();

                return true;
            }
        }
        else if (e.id == Event.WINDOW_DESTROY)
        {
            hide();
            return true;
        }

        return false;
        //Let the superior class handle this - needed, or else
        //no textual-input will be possible.


    } /* action */

} /* end Input */

