import java.awt.*;
import java.awt.event.*;
import org.python.util.PythonInterpreter;

/**
 * A simple Frame containing Turtle and a TextArea.  The user can control
 * the Turtle by entering scripts in the TextArea.
 *
 * @author Weiqi Gao, Object Computing, Inc.
 * @version $Id: EtchSketch.java,v 1.3 2003/07/26 13:28:24 weiqi Exp $
 */
public class EtchSketch extends Frame
    implements ActionListener {
    // The Turtle to be scripted
    private Turtle turtle = new Turtle();

    // The TextArea to type in the script
    private TextArea ta = new TextArea(10, 40);

    // The OK button to execute the script
    private Button b = new Button("OK"); { b.addActionListener(this); }

    // The Jython interpreter
    private PythonInterpreter interp = new PythonInterpreter();

    /**
     * Constructor.  We build up the GUI and put the turtle object into
     * the Jython interpreter.
     */
    public EtchSketch() {
	super("Scripting Turtle with Jython");
	add(turtle, BorderLayout.CENTER);

	// Create a binding in the toplevel of the Jython interpreter
	// that references the Java turtle object.
	interp.set("turtle", turtle);

	Panel p = new Panel();
	p.add(ta);
	p.add(b);
	add(p, BorderLayout.SOUTH);
	pack();
	setVisible(true);
	addWindowListener(new WindowAdapter() {
		public void windowClosing(WindowEvent e) {
		    System.exit(0);
		}
	    });
    } // End public EtchSketch()

    /**
     * Implementation of ActionListener
     *
     * @param e The ActionEvent object
     */
    public void actionPerformed(ActionEvent e) {
	String script = ta.getText();
	// Execute what is in the TextArea in the Jython interpreter.
	interp.exec(script);
	ta.selectAll();
	ta.requestFocus();
    } // End public void actionPerformed(ActionEvent e)

    /**
     * The main method.
     *
     * @param args Command line arguments
     */
    public static void main(String[] args) {
	EtchSketch f = new EtchSketch();
    } // End public static void main(String[] args)
} // End public class EtchSketch extends Frame
