/* Spiro by Jimmc
 * Copyright 1996 Jim McBeath
 *
 * Permission to use, copy, and distribute Spiro, in source or binary form,
 * for personal non-commercial use is hereby granted without fee, provided
 * that this copyright notice appears in all copies, and that no charge is
 * associated with such copies.
 *
 * Permission to modify Spiro and make derivative works for personal
 * non-commercial use, and to distribute such modified versions, is hereby
 * granted without fee, provided that the users of such a modified version
 * are clearly notified that the work is a modified version of Spiro and
 * not the original Spiro.  As with unmodified distributions, this copyight
 * notice must appear in all modified copies, and no charge may be associated
 * with such copies.
 *
 * The author makes no representations about the suitability of this software
 * for any purpose.  It is provided "as-is", without any express or implied
 * warranty.  In no event will the author be held liable for any damages
 * arising from the use of this software.
 */
/* Spiro - an artwork generator
 *
 * Jim McBeath, May 1996
 */

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Spiro extends java.applet.Applet {
	public static String Version = "v1.1 March 26, 2004";
	public static String Copyright = "Copyright 1996,2004 Jim McBeath";
	boolean isApplication;	//as opposed to applet
	boolean canLoadFiles;	//true if we think we can load&save local files
	SpiroCanvas canvas;
	SpiroControls controls;
	Frame frame;

	public static void main(String args[]) {
		/* This main function makes it possible to run this java
		 * program as an application, as well as an applet */
		Spiro sp = new Spiro();
		sp.frame = new Frame("Spiro");
		sp.isApplication = true;
		sp.init();
		sp.start();
		sp.frame.add("Center",sp);
		sp.frame.resize(720,650);
		sp.frame.show();
		sp.frame.addWindowListener(new WindowAdapter() {
		    public void windowClosing(WindowEvent ev) {
		        System.exit(0);
		    }
		});
	}

	public void init() {
		checkLoadFile();	//see if we can load files
		GridBagLayout gb = new GridBagLayout();
		GridBagConstraints gbc = new GridBagConstraints();
		canvas = new SpiroCanvas(this);
		controls = new SpiroControls(canvas,isApplication,canLoadFiles);
		setLayout(gb);
		gbc.weighty = 0.0;
		gbc.gridwidth = GridBagConstraints.REMAINDER; //rest of row
		gb.setConstraints(controls,gbc);
		add(controls);
		gbc.fill = GridBagConstraints.BOTH;
		gbc.weightx = 1.0;
		gbc.weighty = 1.0;
		gbc.gridwidth = 1;	/* default */
		gb.setConstraints(canvas,gbc);
		add(canvas);
		controls.showStatus(Version);
	}

	public void start() {
		if (canvas!=null)
			canvas.runFlag = true;
	}

	public void stop() {
		if (canvas!=null)
			canvas.runFlag = false;
	}

	public Frame getFrame() {
		if (frame!=null)
			return frame;
		Container c = getParent();
		if (c instanceof Frame)
			return (Frame)c;
		throw new RuntimeException("Parent is not a Frame");
	}

	public boolean hasFrame() {
		return (getParent() instanceof Frame);
	}

	public boolean handleEvent(Event e) {
		if (e.id==Event.WINDOW_DESTROY) {
			System.exit(0);
			return true;
		}
		return false;
	}

	public String getAppletInfo() {
		return "Spiro " + Version + ", " + Copyright;
	}

	private void checkLoadFile() {
		try {
			System.getProperty("user.home"); //see if exception
			canLoadFiles = true;	//no problem
		} catch (Exception e) {
			canLoadFiles = false;	//can't do it
		}
	}

	public void errorMessage(String s) {
		ErrorDialog e = new ErrorDialog(frame,"Error",s);
		e.show();
		//let garbage collection clean it up
	}
}

class ErrorDialog extends Dialog {
	Button continueW;

	public ErrorDialog(Frame f, String title, String message) {
		//TBD - clean up layout
		super(f,title,true);
		setLayout(new FlowLayout());
		add(new Label(message));
		add(continueW=new Button("Continue"));
	}

	public void show() {
		pack();
		super.show();
	}

	public boolean action(Event ev, Object arg) {
		if (ev.target instanceof Button) {
			Button btn = (Button)(ev.target);
			if (ev.target==continueW) {
				hide();		//take down the dialog
				return true;
			}
		}
		return false;
	}
}

class SpiroStringDialog extends Dialog {
	boolean status;
	TextField tf;

	public SpiroStringDialog(Frame f, String title, String message) {
		//TBD - clean up layout
		super(f,title,true);
		setLayout(new FlowLayout());

		add(new Label(message));
		tf = new TextField("",40);
		add(tf);
		add(new Button("OK"));
		add(new Button("Cancel"));
	}

	public void show() {
		pack();
		super.show();
	}

	public boolean getStatus() {
		return status;
	}

	public String getText() {
		String text;
System.out.println("Status is " + status);
		if (!status)
			return null;	/* Cancelled */
		text = tf.getText();
System.out.println("Text is " + text);
		return text;
	}

	public boolean action(Event ev, Object arg) {
		if (ev.target instanceof Button) {
			Button btn = (Button)(ev.target);
			if (ev.arg=="OK") {
				hide();		//take down the dialog
				status = true;
				return true;
			}
			if (ev.arg=="Cancel") {
				hide();
				status = false;
				return true;
			}
		}
		return false;
	}
}

/* end */
