import java.applet.*;
import java.awt.*;
import java.lang.*;
import java.util.*;


class InputActivation extends Frame
{
	final static int ADD	= 1;
	final static int DELETE = 2;

	private Panel		topPanel;
    public  Panel       checkPanel;
    private Panel		okPanel;

    private Panel       parent;
    private InputSignal	inputSignal;

    private Rectangle	ibounds;

    public InputActivation (Panel par, InputSignal inp)
    {
        parent = par;
        inputSignal = inp;

        setLayout(new BorderLayout());
        topPanel = new Panel();
        topPanel.setLayout(new BorderLayout(0, 20));
        add("Center", topPanel);

        Label l = new Label ("Set values of inputs:", Label.CENTER);
        add ("North", l);

        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 the panel with checkboxes to dialog */

        add ("Center", checkPanel);

        /* create and add to dialog a button */

        okPanel = new Panel();
        okPanel.setLayout(new GridLayout (0, 3));

        okPanel.add ("CENTER", new Button ("All"));
        okPanel.add ("CENTER", new Button ("None"));
        okPanel.add ("CENTER", new Button ("Invert"));
        okPanel.add ("CENTER", new Button ("Hide"));
        add ("South", okPanel);

        /* make sure dialog does not display until specifically told to */

        hide ();

    } /* end InputActivation */


 	// An input has been renamed

    public void paint (String oldName, String newName)
    {
   		ibounds = bounds();
    	for (int i = 0; i < checkPanel.countComponents(); i++)
    	{
    		Checkbox cb = (Checkbox) checkPanel.getComponent(i);
    		if (cb.getLabel().equals(oldName))
    			((Checkbox) checkPanel.getComponent(i)).setLabel(newName);
    	}
    	checkPanel.repaint();
    	//pack();
    	//move(ibounds.x, ibounds.y);
    } /* end paint (1) */


    // An input has been added or deleted

    public void paint (int action, String objekt)
    {
    	ibounds = bounds();
    	if (action == ADD)
    	{
    		Checkbox cb = new Checkbox(objekt);
    		checkPanel.add(cb);
    	}
    	else  // (action == DELETE)
    	{
    		for (int i = 0; i < checkPanel.countComponents(); i++)
    		{
    			Checkbox cb = (Checkbox) checkPanel.getComponent(i);
    			if (cb.getLabel().equals(objekt))
    				checkPanel.remove(cb);
    		}
    	}
    	checkPanel.repaint();
    	//pack();
    	//move(ibounds.x, ibounds.y);
    } /* end paint (2) */

    /*
        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 ();
            int number = checkPanel.countComponents();

            if (label.equals("Hide"))
            {
            	hide();
            	return true;
            }
            else
            {
            	for (int i = 0; i < number; i++)
            	{
            		if (label.equals("All"))
           				((Checkbox) checkPanel.getComponent(i)).setState(true);
           			else if (label.equals("None"))
           				((Checkbox) checkPanel.getComponent(i)).setState(false);
           			else if (label.equals("Invert"))
           			{
		            		boolean cbstate = ((Checkbox) checkPanel.getComponent(i)).getState();
		            		((Checkbox) checkPanel.getComponent(i)).setState(! cbstate);
		            }
            	}
            }
        }
        else if (e.id == Event.WINDOW_DESTROY)
	    {
	        hide();
	        return true;
	    }

 	return false;

    } /* end handleEvent */

} /* end InputActivation */

