/* InteractiveTurtleApplet2.java -- Interactive Turtle using the
** com.lrdev.turtle package, extends InteractiveTurtleApplet.
**
** Copyright (C) 1997-2003 Eric Laroche.  All rights reserved.
**
** @author Eric Laroche <laroche@lrdev.com>
** @version 1.1 @(#)$Id: InteractiveTurtleApplet2.java,v 1.1 2003/02/16 12:41:06 laroche Exp $
**
** This program is free software;
** you can redistribute it and/or modify it.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**
*/

// note: no wildcard imports

import java.awt.Graphics;

// import Callback;
// import DirectiveParser;
// import InteractiveTurtleApplet;

/** InteractiveTurtleApplet2: Interactive Turtle using the
* com.lrdev.turtle package, extends InteractiveTurtleApplet.
*
* @author Eric Laroche <laroche@lrdev.com>
* @version 1.1 @(#)$Id: InteractiveTurtleApplet2.java,v 1.1 2003/02/16 12:41:06 laroche Exp $
*/
public class InteractiveTurtleApplet2
	extends InteractiveTurtleApplet
	implements Callback
{
	/** Version.
	*/
	private final static String m_version =
		"Version: 1.1 @(#)$Id: InteractiveTurtleApplet2.java,v 1.1 2003/02/16 12:41:06 laroche Exp $";

	/** Author.
	*/
	private final static String m_author =
		"Author: Eric Laroche <laroche@lrdev.com>";

	// ----------------

	/** UI/mainthread synchronization.
	*/
	private boolean m_synchronized = false;

	// ----------------

	/** Constructor.
	*/
	public InteractiveTurtleApplet2()
	{
		super();

		// NOTE: must postpone getParameter() calls to init()
	}

	/** Called after the constructor and before start().
	* Sets the background color and reads in the applet parameters.
	*
	* Overwrites Applet.init and InteractiveTurtleApplet.init.
	*/
	public /*synchronized*/ void init()
	{
		super.init();

		String s = getParameter("synchronize");
		if (s != null) {
			m_synchronized = Boolean.valueOf(s).booleanValue();
		}

		rc(getParameter("rc"));
	}

	/** Init string handling.
	*/
	protected void rc(String execute)
	{
		if (m_synchronized) {
			synchronized (this) {
				unsynchronizedRc(execute);
			}
		} else {
			unsynchronizedRc(execute);
		}
	}

	/** Init string handling.
	*/
	protected void unsynchronizedRc(String execute)
	{
		if (execute == null) {
			return;
		}

		doString(execute);
		repaintIfNeeded();
	}

	/** Paint the component.
	*
	* Overwrites Component.paint and InteractiveTurtleApplet.paint.
	*/
	public /*synchronized*/ void paint(Graphics graphics)
	{
		if (m_synchronized) {
			synchronized (this) {
				unsynchronizedPaint(graphics);
			}
		} else {
			unsynchronizedPaint(graphics);
		}
	}

	/** Paint the component.
	*/
	public void unsynchronizedPaint(Graphics graphics)
	{
		super.paint(graphics);
   	}

	/** Interpret a whole command string.
	*/
	protected void doString(String s)
	{
		// [call this directly to skip directive parsing]
		// doSimpleString(s);

		// let DirectiveParser.execute() parse for directives
		// and call callback()
		DirectiveParser.execute(s, this);
	}

	/** Interpret a whole command string.
	*/
	protected void doSimpleString(String s)
	{
		// note: not handling directives (like "2[f]")
		for (int i = 0; i < s.length(); i++) {
			doCommand((int)s.charAt(i));
		}
	}

	/** Implements Callback.callback().
	*/
	public Object callback(Object object)
	{
		doSimpleString((String)object);
		return (Object)null;
	}
}

