//******************************************************************************
// DLSimFrame.java:	
//
//******************************************************************************
import java.awt.*;
import java.io.*;
import java.util.*;

//==============================================================================
// STANDALONE APPLICATION SUPPORT
// 	This frame class acts as a top-level window in which the applet appears
// when it's run as a standalone application.
//==============================================================================
class DLSimFrame extends Frame
{
	DLSim theApplet;
	final static String EXTSN = "cct";
	String ftitle;
	File curFile = null;

	// DLSimFrame constructor
	//--------------------------------------------------------------------------
	public DLSimFrame(String str)
	{
		// TODO: Add additional construction code here
		super (str);
		ftitle = str;
	}

	// The handleEvent() method receives all events generated within the frame
	// window. You can use this method to respond to window events. To respond
	// to events generated by menus, buttons, etc. or other controls in the
	// frame window but not managed by the applet, override the window's
	// action() method.
	//--------------------------------------------------------------------------
	public boolean handleEvent(Event evt)
	{
		switch (evt.id)
		{
			// Application shutdown (e.g. user chooses Close from the system menu).
			//------------------------------------------------------------------
			case Event.WINDOW_DESTROY:
				// TODO: Place additional clean up code here
				dispose();
				System.exit(0);
				return true;

			default:
				return super.handleEvent(evt);
		}			 
	}
	
	public boolean action(Event evt, Object what)
	{
		if (evt.target instanceof MenuItem) {
			String itemName = (String)what;
			if (itemName.compareTo("Clear") == 0) {
				theApplet.pegboard.objlist = new Vector();
				curFile = null;
				setTitle(ftitle);
				repaint();
				theApplet.pegboard.repaint();
			}
			else if (itemName.compareTo("Load ...") == 0) {
				FileDialog fdlg = new FileDialog(this, "Load File", FileDialog.LOAD);
				fdlg.setFilenameFilter(new EndsWithFilter(EXTSN));
				fdlg.show();
				curFile = new File(fdlg.getDirectory(), fdlg.getFile());
				setTitle(ftitle + " - " + curFile.getName());
				repaint();
				try {
					FileInputStream in = 
						new FileInputStream(curFile);
					theApplet.pegboard.unserialize(in);
				} catch (Exception e) {System.out.println(e);}
			}
			else if (itemName.compareTo("Save") == 0 || 
					 itemName.compareTo("Save as ...") == 0) {
				if (itemName.compareTo("Save as ...") == 0 || curFile == null) {
					String fname;
					FileDialog fdlg = new FileDialog(this, "Save File", FileDialog.SAVE);
					fdlg.setFilenameFilter(new EndsWithFilter(EXTSN));
					fdlg.show();
					fname = fdlg.getFile();
					if (fname.endsWith(".*.*")) 
					fname = fname.substring(0, fname.lastIndexOf(".*.*"));
					if (!fname.endsWith("."+EXTSN))	fname = fname + "." + EXTSN;
					curFile = new File(fdlg.getDirectory(), fname);
					setTitle(ftitle + " - " + curFile.getName());
					repaint();
				}
//				System.out.println(fdlg.getDirectory());
//				System.out.println(fname);
				try {
					FileOutputStream out = new FileOutputStream(curFile);
					PrintStream pout = new PrintStream(out, true);
					theApplet.pegboard.serializeAll(pout);
					pout.close();
				} catch (Exception e) {System.out.println(e);}
			}
			return true;
		}
		return super.action(evt, what);
	}
}

class EndsWithFilter implements FilenameFilter
{
	private String extension;
	EndsWithFilter(String extension){this.extension = extension;}
	public boolean accept(File dir, String name)
	{
		if (name.endsWith(extension)) return true;
		else return (new File(dir,name)).isDirectory();
	}
}

			/*	public void resize(Dimension d){resize(d.width, d.height);}
	public void resize(int width, int height)
	{
		if (theApplet != null && theApplet.pegboard != null) {
			System.out.println("howdy");
			theApplet.remove(theApplet.pegboard);
			theApplet.pegboard.resize(width - insets().left - insets().right - 70,
									  height- insets().top  - insets().bottom - 10);
			theApplet.add(theApplet.pegboard);
		}
		super.resize(width, height);
	}
*/

