
/*
    The ShapeDrawLauncher applet displays just one button.  When the
    user clicks that button, a window belonging to the class 
    ShapeDrawFrame is opened.  The name of the button changes from
    "Launch ShapeDraw" to "Close ShapeDraw".  The name changes
    back when the window is closed.  If the window is still open
    when the applet is destroyed, then the window will be closed
    automatically.  This applet depends on the classes defined
    in the file ShapeDrawFrame.java.
    
    David Eck
    August 1998
    
*/

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;


public class ShapeDrawLauncher extends Applet implements ActionListener {

   ShapeDrawFrame shapeDraw;  // the ShapeDraw window;
                              // this is null if the window is not open

   Button launchButton;   // a button for opening the window 
   

   public void init() {  // initialize the applet
        
      setBackground(Color.white);
      setLayout(new BorderLayout());

      launchButton = new Button("Launch ShapeDraw");
      add("Center",launchButton);
      launchButton.addActionListener(this);
      
   } // end init();


   synchronized public void actionPerformed(ActionEvent evt) {
            // respond to a click on the button; if no window
            // is open, open one; if a window is open, close it
            // by calling its dispose() method.
       if (shapeDraw == null) {
          shapeDraw = new ShapeDrawFrame();
          launchButton.setLabel("Close ShapeDraw");
          shapeDraw.addWindowListener(new FrameDrawListener());
              // This object will listen for the window
              // event that occurs when the window closes.
       }
       else {
          launchButton.setEnabled(false);  // disable button while waiting
                                           // for window to close
          shapeDraw.dispose();
       }
   }  
   
   synchronized public void destroy() {
         // when the applet is destroyed, check if there is
         // a window open; if so, close it.
      if (shapeDraw != null) {
          shapeDraw.dispose();
          shapeDraw = null;
       }
   }
   
   class FrameDrawListener extends WindowAdapter {
          // This nested class defines an object that
          // listens for the WindowEvent that is generated
          // when the window closes for any reason.
          // The button's name is changed back to "Launch ShapeDraw"
          // and the button is enabled (after possibly being disabled in the
          // actionPerformed() method, if that is what caused the
          // window to close.)
      synchronized public void windowClosed(WindowEvent evt) {
         launchButton.setLabel("Launch ShapeDraw");
         launchButton.setEnabled(true);
         shapeDraw = null;
      }
   }

}