// GraphApplet.java
// by Brian Goldsmith and Alexander Smith
// Adapted from DirectedGraphEditorApplet.java
// Copyright (C) 2000 Linyuan Lu

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class GraphApplet extends Applet implements ActionListener {
    GraphCanvas canvas;
    private int h;
    private int w;
    public static TextField gField;
    public static TextField stepField;
    public static TextArea tArea;
   
    public void init(){
	setLayout(new BorderLayout());
	setBackground(Color.white);
	
	Panel textPanel = new Panel();
	textPanel.setLayout(new BorderLayout());
	gField = new TextField("[ graph info here after initialize'd ]");
	gField.setBackground(Color.white);
	gField.setEditable(false);
	textPanel.add("North", gField);
        add("North", textPanel);
        
	Panel buttonPanel = new Panel();
	buttonPanel.setBackground(Color.blue);
	buttonPanel.setLayout(new FlowLayout());
	/* Button initButton = new Button("Init");
	initButton.addActionListener(this);
	buttonPanel.add(initButton);
	Button stepButton = new Button("Step");
	stepButton.addActionListener(this);
	buttonPanel.add(stepButton); */
	stepField = new TextField("# steps",6);
	stepField.setBackground(Color.white);
	/* buttonPanel.add(stepField);
        Button stepI = new Button("Inf Step");
	stepI.addActionListener(this);
	buttonPanel.add(stepI); */
        Button showInfo = new Button("Info");
	showInfo.addActionListener(this);
	buttonPanel.add(showInfo);
	Button coord = new Button("DoIt");
	coord.addActionListener(this);
	buttonPanel.add(coord);
	Button spinB = new Button("Spin");
	spinB.addActionListener(this);
	buttonPanel.add(spinB);
	Button zoomI = new Button("Zoom In");
	zoomI.addActionListener(this);
	buttonPanel.add(zoomI);
	Button zoomO = new Button("Zoom Out");
	zoomO.addActionListener(this);
	buttonPanel.add(zoomO);
        
        Panel bottomPanel = new Panel();
        bottomPanel.setLayout(new BorderLayout());
        tArea = new TextArea("[graph info output here]", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);
        tArea.setBackground(Color.white);
	tArea.setEditable(false);
	//bottomPanel.add("North", tArea);
        bottomPanel.add("South", buttonPanel);
        add("South", bottomPanel);
        
	canvas=new GraphCanvas();
	h=Integer.parseInt(getParameter("HEIGHT"));
	w=Integer.parseInt(getParameter("Width"));
	canvas.setSize(w,h);
	add("Center",canvas);
	Selectable.selectedColor = Color.blue;
    }
    
    public void actionPerformed(ActionEvent e)
    {   // somebody clicked the button!
        if(e.getActionCommand().equals("Init"))
	{
	    //clicked init
            canvas.init();
	}
	else if(e.getActionCommand().equals("Step"))
	{
	    //clicked step
	    canvas.stepd();
	}
        else if(e.getActionCommand().equals("Inf Step"))
	{
	    //clicked stepI
	    canvas.stepInf();
	}
        else if(e.getActionCommand().equals("Info"))
	{
	    //clicked showInfo
	    canvas.graph.writeInfo(true);
	}
        else if(e.getActionCommand().equals("DoIt"))
	{
	    //clicked coord 
	    canvas.getCoord();
	}
        else if(e.getActionCommand().equals("Spin"))
	{
	    //clicked spin 
	    canvas.graph.spin();
	}
        else if(e.getActionCommand().equals("Zoom In"))
	{
	    //clicked spin 
	    canvas.graph.zoom(true);
	}
        else if(e.getActionCommand().equals("Zoom Out"))
	{
	    //clicked spin 
	    canvas.graph.zoom(false);
	}
	else
	{   /* mustve clicked Reset
	    canvas.clickedReset(); */
	}

        repaint(0);
    }
}