//---------------------------------
// AppControls.Java
// Written By: Russell Schwager
//             russells@jhu.edu
// February 10, 1997
//---------------------------------

// Insert Copyright comments

//---BEGIN UI CODE---------
import java.awt.*;
import java.applet.*;

class AppControls extends Panel 
{
	SortPanel panel;
    Choice objectChoice;
	Choice methodChoice;
	int algorithm = 0;
	int type = 0;
    Button startButton;

	public AppControls(SortPanel panel) 
	{ // Constructor

        setBackground(Color.gray);
		this.panel = panel;

		// Create Object to sort drop down menu
		Label objectLabel = new Label("Object to Sort:");
		objectChoice = new Choice();
		objectChoice.addItem("Character (object)");
		objectChoice.addItem("Character (primitive)");
		objectChoice.addItem("Double (object)");
		objectChoice.addItem("Double (primitive)");
		objectChoice.addItem("Float (object)");
		objectChoice.addItem("Float (primitive)");
		objectChoice.addItem("Integer (object)");
		objectChoice.addItem("Integer (primitive)");
		objectChoice.addItem("Long (object)");
		objectChoice.addItem("Long (primitive)");
		objectChoice.addItem("String (object)");

		// Create sort method drop down box
		Label methodLabel = new Label("Sort Algorithm:");
		methodChoice = new Choice();
		methodChoice.addItem("Merge Sort");
		methodChoice.addItem("Quick Sort");
		methodChoice.addItem("Radix Sort");

		// Start/Restart button
		// The extra white space is to make the button big
		// enough to handle the word "restart" later on
		startButton = new Button("  Start  ");
		
		// Add components to the panel
		add(methodLabel);
		add(methodChoice);
		add(objectLabel);
		add(objectChoice);
        add(startButton);
        add(new Button("  Stop  "));

		methodChoice.setBackground(Color.white);
		objectChoice.setBackground(Color.white);
    }

    public boolean action(Event ev, Object arg) 
	{ // Event handler
        
		if (ev.target instanceof Button) 
		{ // a button was pushed
            String label = (String)arg;

			if(label.equals("  Start  ") || 
			   label.equals("Restart"))
			{ // Start/Restart button was pressed
				startButton.setLabel("Restart");
				panel.begin(algorithm, type);
			}
			else if (label.equals("  Stop  "))
				{ // Stop button was pressed.
					startButton.setLabel("  Start  ");
					panel.end();
				}

            return true;
        }
		else if (ev.target instanceof Choice)
		{ // one the choice boxes was changed

			if (ev.target == objectChoice)
				type = objectChoice.getSelectedIndex();
			else
				algorithm = methodChoice.getSelectedIndex();
				
			return true;
		}

        return false;
    }

}

//---END UI CODE---------
