/*
   Source code by David Eck
                  Department of Mathematics and Computer Science
                  Hobart and William Smith Colleges
                  Geneva, NY 14456
                  eck@hws.edu
                  
   This Java source code file can be used IN UNMODIFIED FORM for
   any purpose.  You can also make and distribute modified versions,
   as long as you include an acknowledgement of the original
   author in the modified version.
*/

/*
   <applet code="EdgeOfChaosLauncher.class" width=300 height=40>
      <param name="Example1" value="ca_params_first.txt">
      <param name="Example2" value="ca_params_second.txt">
      <param name="Example3" value="bogus.txt">
   </applet>
*/


import xca.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.Vector;

public class EdgeOfChaosLauncher extends java.applet.Applet 
                        implements ActionListener, WindowListener{

   private Frame frame;
   private Button button;

   public EdgeOfChaosLauncher() {
       button = new Button("Launch EdgeOfChaos CA");
       button.addActionListener(this);
       setLayout(new BorderLayout());
       add(button,BorderLayout.CENTER);
   }
   
   synchronized private void showFrame() {
      if (frame != null)
         return;
      button.setEnabled(false);
      try {
         String[] fileList = getFileParams();
         CAFrame ca = new CAFrame(null,true);
         ca.addWindowListener(this);
         if (fileList != null && fileList.length > 0)
            ((Menus)ca.getJMenuBar()).addExamplesMenu(fileList,(java.applet.Applet)this);
         frame = ca;
      }
      catch (Exception e) {
         button.setLabel("Can't open; Java 1.4 needed!");
         e.printStackTrace();
      }
   }
   
   synchronized private void closeFrame() {
      if (frame != null) {
         button.setEnabled(false);
         frame.dispose();
      }
   }
   
   synchronized public void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == button) {
         if (frame == null)
            showFrame();
         else
            closeFrame();
      }
   }
   
   synchronized public void windowClosed(WindowEvent evt) {
      if (evt.getSource() == frame) {
         button.setLabel("Launch EdgeOfChaos CA");
         button.setEnabled(true);
         frame = null;
      }
   }
   
   synchronized public void windowOpened(WindowEvent evt) {
      if (evt.getSource() == frame) {
         button.setLabel("Close EdgeOfChaos CA");
         button.setEnabled(true);
      }
   }
   
   public void windowIconified(WindowEvent evt) { }
   public void windowDeiconified(WindowEvent evt) { }
   public void windowClosing(WindowEvent evt) {  }
   public void windowActivated(WindowEvent evt) { }
   public void windowDeactivated(WindowEvent evt) { }
   
   private String[] getFileParams() {
      Vector v = new Vector();
      String s = getParameter("Example");
      if (s != null)
         v.addElement(s);
      s = getParameter("Example0");
      if (s != null)
         v.addElement(s);
      int ct = 1;
      do {
         s = getParameter("Example" + ct);
         ct++;
         if (s != null)
            v.addElement(s);
      } while (s != null);
      if (v.size() == 0)
         return null;
      else {
         String[] files = new String[v.size()];
         for (int i = 0; i < v.size(); i++)
            files[i] = (String)v.elementAt(i);
         return files;
      }
   }

}

