import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;


/*
    TokenSelection class - presents user with a set of checkboxes, one for each
    possible token. The selected tokens are encoded into a boolean array and
    transition is created between the start and finish states.
*/


class TokenSelection extends Frame
{
    /*
        INSTANCE VARIABLES
    */


	/* top-level panel to ensure borders */

	private Panel		topPanel;

    /* panel containing checkboxes */

    private Panel       checkPanel;

    /* panel containing OK-button, thus button is not as wide any more. */

    private Panel		okPanel;

    /* machine object associated with this dialog */

    private Machine     machine;

    /* hint line object */

    private Help        help;

    /* parent object */

    private Panel       parent;

    private PatienceThread	patienceThread;
    private PatienceBox		patienceBox;

    private InputSignal		inputSignal;


    /*
        PUBLIC METHODS
    */


    /*
        constructor method
    */

    public TokenSelection (Panel par, InputSignal inps)
    {
        /* initialize variables */

        parent = par;
        inputSignal = inps;

        //setBackground (new Color (150, 150, 150));

        setLayout(new BorderLayout());
        topPanel = new Panel();
        topPanel.setLayout(new BorderLayout(0, 20));
        add ("Center", topPanel);

        /* create and add to dialog command label */

        Label l = new Label ("Select tokens for this transition:", Label.CENTER);
        topPanel.add ("North", l);

        /* create panel for holding checkboxes and set its layout to display
           10 elements in a row */

        checkPanel = new Panel ();
        checkPanel.setLayout(new GridLayout (0, 2));

        /* add a checkbox for each token */

        for (int i = 0; i < inputSignal.size(); i++)
        {
            String str = (String) inputSignal.elementAt(i);
            checkPanel.add (new Checkbox(str));
        }

        /* add a checkbox for all tokens */

        checkPanel.add (new Checkbox ("All"));

        /* add the panel with checkboxes to dialog */

        topPanel.add ("Center", checkPanel);

        /* create and add to dialog a button */

        okPanel = new Panel();
        okPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 100, 0));

        okPanel.add (new Button ("OK"));
        okPanel.add (new Button ("Cancel"));

        topPanel.add ("South", okPanel);

        /* make sure dialog does not display until specifically told to */

        hide ();

    } /* end TokenSelection */


    /*
        set machine object that will receive the entered parameters
    */

    public void setMachine (Machine m, Help h, PatienceBox p)
    {
        machine = m;
        help = h;
        patienceBox = p;

    } /* end setMachine */


    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);

    } /* end Insets */


    /*
        handle events
    */

    public boolean handleEvent (Event e)
    {
        /* only handle button presses */

        if (e.target instanceof Button)
        {
            String label = ((Button)(e.target)).getLabel ();
        	if (label.equals("OK"))
        	{
        		patienceBox.show();
				patienceThread = new PatienceThread((long) 333, patienceBox);
				//patienceThread.setPriority(Thread.MAX_PRIORITY);
				patienceThread.start();

	            /* Allocate array that will hold name of each input signal
    	           selected. If "All" is checked, then "All"
    	           is added. */

    	        InputSignal tokens = new InputSignal(Simulator.ESTIMATED_INPUTS, 1);

        	    /* This boolean will make sure at least one token is selected. */

	            boolean exists = false;

    	        /* go through the checkboxes and extract their values into the
        	       array */

            	for (int i = 0; i <= inputSignal.size(); i++)
	            {
	            	System.out.println("tokenSelection rechnet: " + i + " von " + inputSignal.size());
    	            Checkbox cb = (Checkbox) checkPanel.getComponent (i);

    	            boolean tmpState = cb.getState ();

					if (tmpState)
					{
						tokens.addElement(cb.getLabel());
					}

        	        exists = exists || tmpState;

            	    /* reset the checkbox state to clear */

                	//T if (tokens [i])
                    //T 	cb.setState (false);
	            }

    	        /* if at least one token was selected - create the transition */

        	    if (exists)
        	    {
            	    machine.addTransition (tokens);
            	}
	            else
    	            help.setHelp (Help.NO_INPUTS);

				patienceThread.stop();
				patienceBox.hide();
				patienceBox.noDots();

        	    /* hide our dialog and force repaint of the parent */

            	hide();
	            parent.repaint ();

    	        return true;
    	    }
           	else if (label.equals("Cancel"))
           	{
           		hide();
           		return true;
           	}
        }
        else if (e.id == Event.WINDOW_DESTROY)
	    {
	        hide();
	        return true;
	    }

 	return false;

    } /* end handleEvent */

} /* end TokenSelection */

