//******************************************************************************
// 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;
  Vector cLoadlist;

  // DLSimFrame constructor
  //--------------------------------------------------------------------------
  public DLSimFrame(String str)
  {
    // TODO: Add additional construction code here
    super (str);
    ftitle = str;
    cLoadlist = new Vector();
  }

  // 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)
  {
    File CFile;
    String fname;
    FileDialog fdlg = null;
    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();
	return true;
      }
      if (itemName.compareTo("Open ...") == 0) {
	fdlg = new FileDialog(this, "Open File", FileDialog.LOAD);
	fdlg.setFilenameFilter(new EndsWithFilter(EXTSN));
	fdlg.show();
	if (fdlg.getDirectory()==null || fdlg.getFile()==null) return true;
	curFile = new File(fdlg.getDirectory(), fdlg.getFile());
	setTitle(ftitle + " - " + curFile.getName());
	repaint();
	try {
	  FileInputStream in = new FileInputStream(curFile);
	  theApplet.pegboard.objlist =
	    theApplet.pegboard.unserialize(in, curFile.getParent());
	  theApplet.pegboard.repaint();
	} catch (Exception e) {
	  new MsgDialog(theApplet, "File Not Found",
		      "File not found: "+curFile.getName()).show();
	  System.out.println(e);
	}
	return true;
      }
      if (itemName.compareTo("Save") == 0 || 
	       itemName.compareTo("Save as ...") == 0) {
	if (itemName.compareTo("Save as ...") == 0 || curFile == null) {
	  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();
	}
	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;
      }
      if (itemName.compareTo("Load ...") == 0) {
	fdlg =
	  new FileDialog(this, "Load Circuit", FileDialog.LOAD);
	fdlg.setFilenameFilter(new EndsWithFilter(EXTSN));
	fdlg.show();
	if (fdlg.getDirectory()==null || fdlg.getFile()==null) return true;
	fname = fdlg.getFile();
	int idx = fname.lastIndexOf("."+EXTSN);
	if (idx > 0) fname = fname.substring(0, idx);
	CFile = new File(fdlg.getDirectory(), fdlg.getFile());
      } else {
	fname = itemName;
	String temp = fname+"."+EXTSN;
	CFile = null;
	for (int i = 0; i < cLoadlist.size(); i++) {
	  File loadFile = (File)cLoadlist.elementAt(i);
	  if (temp.compareTo(loadFile.getName()) == 0) CFile = loadFile;
	}
	if (CFile == null) return true;
      }
      try { 
	FileInputStream in = new FileInputStream(CFile);
	DLobj newobj =
	  new DLabs(DCanvas.readcct(in), theApplet, fname, CFile.getParent());
	((DLabs)newobj).init();
	newobj.bx = 0;
	newobj.by = 0;
	theApplet.pegboard.objlist.addElement(newobj);
	theApplet.pegboard.repaint();
	if (itemName.compareTo("Load ...") == 0) 
	  addToCirMenu(fdlg.getDirectory(), fname);
      } catch (FileNotFoundException e) {
	new MsgDialog(theApplet, "File Not Found",
		      "File not found: "+CFile.getName()).show();
	System.out.println(e);
      }
      return true;
    }
    return super.action(evt, what);
  }

  void addToCirMenu(String d, String s)
  {
    Menu CirMenu = getMenuBar().getMenu(2);
    int j;
    File CFile = null;
    for (j = 1; j < CirMenu.countItems(); j++)
      if (s.compareTo(CirMenu.getItem(j).getLabel()) == 0) break;
    if (j == CirMenu.countItems()) {
      CirMenu.add(s);
      cLoadlist.addElement((CFile = new File(d, s+"."+EXTSN)));
    }
  }
}

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);
	}
*/

