//////////////////////////////////////////////////
// Standard packages.
import java.awt.*;
import java.io.*;
import java.util.*;
import java.applet.Applet;

// Packages specific to network design tool.
import gui.GUIManager;
//////////////////////////////////////////////////
public class LaunchPad extends Applet implements Runnable {
private GUIManager MainWindow;
// Data Members required to bring up Applet/Application.
private int frameNumber = 1;
private String buttonText = null;
private String windowText = null;
private Button button = null;
private Thread windowThread = null;
private Label label = null;
private boolean pleaseCreate = false;
//************************************************
public void init() {

  buttonText = getParameter("BUTTONTEXT");
  if (buttonText == null) {
    buttonText = "Click here to start Network Design Tool";
  }

  windowText = getParameter("WINDOWTEXT");
  if (windowText == null) {
    windowText = "Network Design Tool";
  }

  setLayout(new GridLayout(2,0));
  add(button = new Button(buttonText));
  button.setFont(new Font("Helvetica", Font.PLAIN, 14));

  add(label = new Label("", Label.CENTER));
}
//************************************************
public void start() {
  if (windowThread == null) {
    windowThread = new Thread(this, "GUIManager Bringup Thread"); 
    windowThread.start();
  }
}
//************************************************
public synchronized void run() {
  Class tmp = null;
  String name = null;
	
  // Make sure the window class, GUIManager exists.
  // This has the added benefit of pre-loading the class,
  // which makes it much quicker for the first window to come up.
  try {
    Class.forName("gui.GUIManager");
  } catch (ClassNotFoundException cnfe) {
    label.setText("Can't create window: Couldn't find class GUIManager");
    button.disable();
  }

  //Everything's OK. Wait until we're asked to create a window.
  while (windowThread != null) {
    while (pleaseCreate == false) {
      try {
	wait();
      } catch (InterruptedException e) {
      }
    }

    //We've been asked to bring up a window.
    pleaseCreate = false;
    String WindowTitle;
    if (frameNumber == 1)
      WindowTitle = windowText;
    else
      WindowTitle = windowText + ": " + frameNumber;

    try {
      CommonCode(Boolean.TRUE, this, WindowTitle);
    } catch (Exception e) {
      label.setText("Couldn't create instance of Network Design Tool");
    }
    ++frameNumber;
    label.setText("");
  }
}
//************************************************
public void stop() {
  windowThread.stop();
  windowThread = null;
}
//************************************************
public synchronized boolean action(Event event, Object what) {
  if (event.target instanceof Button) {
    //signal the window thread to build a window
    label.setText("Please wait while the window comes up...");
    pleaseCreate = true;
    notify();
  } 
  return false;
}
//************************************************
public static void main(String args[]) {
  try {
    CommonCode(Boolean.FALSE, null, "Network Design Tool");
  } 
  catch(Exception e) {
    System.out.println(e.getMessage());
    System.exit(0);
  }
} // End of main()
//************************************************
private static void CommonCode(Boolean InApplet, Applet app, String
			       WindowTitle) throws Exception {  
// REQUIRES: 1. this function is executed both by applet as well as
// standalone application.
// RETURNS: nothing.
// PROMISES: Creates Main Window of user interface.

  GUIManager MainWindow = new GUIManager(InApplet, app);
  MainWindow.setTitle(WindowTitle);
}
//************************************************
} // End of LaunchPad
//////////////////////////////////////////////////
