/*******************************************************************************
*                                                                              *
*  File:        Applet_06.java                          Revision:  1.0         *
*                                                                              *
*  Contents:    shows the applet's state in an external window                 *
*                                                                              *
*  Creation:    11.09.2000                     Last Modification:  23.04.2002  *
*                                                                              *
*  Platform:    IBM-compatible PC running Windows 98SE                         *
*                                                                              *
*  Environment: Java 1.4                                                       *
*                                                                              *
*  Author:      Andreas Rozek           Phone:  ++49 (711) 6770682             *
*               Kirschblütenweg 15      Fax:    -                              *
*             D-70569 Stuttgart         EMail:  Andreas.Rozek@T-Online.De      *
*               Germany                                                        *
*                                                                              *
*  URL:         http://www.Andreas-Rozek.de/                                   *
*                                                                              *
*  Copyright:   this software is published under the "GNU General Public Li-   *
*               cense" (see "http://www.gnu.org/copyleft/gpl.html" for addi-   *
*               tional information)                                            *
*                                                                              *
*  Comments:    (none)                                                         *
*                                                                              *
*******************************************************************************/

import java.applet.*;                                    // basic applet classes
import java.awt.*;                           // basic AWT classes (& interfaces)
import java.awt.event.*;                                   // AWT event handling
import java.awt.image.*;                             // image processing classes

public class Applet_06 extends java.applet.Applet {

/**** information about author, version and copyright of this applet ****/

  final static String  AppletInfo =
    "Applet_06 - shows the applet's state in an external window\n" +
    "\n" +
    "Andreas Rozek\n" +
    "Kirschblütenweg 15\n" +
    "D-70569 Stuttgart\n" +
    "Germany\n" +
    "\n" +
    "Phone: ++49 (711) 6770682\n" +
    "Fax: -\n" +
    "EMail: Andreas.Rozek@T-Online.De\n" +
    "URL: http://www.Andreas-Rozek.de";

/**** information about foreseen applet parameters ****/

  final static String[][]  ParameterInfo = null;

/*******************************************************************************
*                                                                              *
*                         Non-Public Instance Variables                        *
*                                                                              *
*******************************************************************************/

/**** bitmaps for state display ****/

  Image after_Constructor, after_init, after_start, after_stop;

/**** user interface components ****/

  Button            WindowButton;                    // opens an external window
  StateDisplayFrame ExternalFrame;  // an external window for the applet's state

/*******************************************************************************
*                                                                              *
*                           Public Instance Variables                          *
*                                                                              *
*******************************************************************************/

  Image actualState;      // refers to the image which reflects the actual state

/*******************************************************************************
*                                                                              *
*                                 Constructor                                  *
*                                                                              *
*******************************************************************************/

  public Applet_06 () {
    super();                                              // just to be complete

  /**** initialize internal variables ****/

    after_Constructor = null;    // bitmaps cannot be loaded yet: browser may...
    after_init        = null;    // ...crash within "getDocumentBase()"
    after_start       = null;
    after_stop        = null;

    actualState = after_Constructor;

    WindowButton  = null;
    ExternalFrame = null;
  };

/*******************************************************************************
*                                                                              *
*                           Public Instance Methods                            *
*                                                                              *
*******************************************************************************/

/*******************************************************************************
*                                                                              *
* destroy                          clean up before the applet will be unloaded *
*                                                                              *
*******************************************************************************/

  public void destroy () {
    if (ExternalFrame != null) {
      ExternalFrame.dispose();
    };
  };

/*******************************************************************************
*                                                                              *
* getAppletInfo       provides information about author, version and copyright *
*                                                                              *
*******************************************************************************/

  public String getAppletInfo () {
    return AppletInfo;               // see above for definition of "AppletInfo"
  };

/*******************************************************************************
*                                                                              *
* getParameterInfo       provides information about foreseen applet parameters *
*                                                                              *
*******************************************************************************/

  public String[][] getParameterInfo () {
    return ParameterInfo;         // see above for definition of "ParameterInfo"
  };

/*******************************************************************************
*                                                                              *
* init                  initialize an applet each time it's loaded or reloaded *
*                                                                              *
*******************************************************************************/

  public void init () {

  /**** unify the visual appearance of an applet on different platforms ****/

    setBackground(Color.lightGray);
    setFont(new Font("Helvetica", Font.PLAIN, 11));

  /**** load botmaps ****/

    after_Constructor = getImage(getCodeBase(),"after_Constructor.gif");
    prepareImage(after_Constructor, this);                      // force loading

    after_init        = getImage(getCodeBase(),"after_init.gif");
    prepareImage(after_init, this);                             // force loading

    after_start       = getImage(getCodeBase(),"after_start.gif");
    prepareImage(after_start, this);                            // force loading

    after_stop        = getImage(getCodeBase(),"after_stop.gif");
    prepareImage(after_stop, this);                             // force loading

  /**** prepare user interface ****/

    setLayout(new BorderLayout());

    WindowButton = new Button("Open State Window");
      WindowButton.setFont(new Font("Helvetica", Font.BOLD, 12));
      WindowButton.setEnabled(ExternalFrame == null);
      WindowButton.addActionListener(new ActionListener() {
        public void actionPerformed (ActionEvent Event) {
          if (ExternalFrame == null) {
            Applet_06 Context = (Applet_06) ((Button) Event.getSource()).getParent();

            ExternalFrame = new StateDisplayFrame(Context);
            WindowButton.setEnabled(false);
          };
        };
      });
    add(WindowButton,"Center");

  /**** resize and show applet ****/

//  resize(preferredSize());
    setVisible(true);

  /**** update the state display ****/

    actualState = after_init;
    if (ExternalFrame != null) {
      ExternalFrame.repaint();
    };
  };

/*******************************************************************************
*                                                                              *
* start        start an applet when the accompanying document has been entered *
*                                                                              *
*******************************************************************************/

  public synchronized void start () {            // may be called several times!
    actualState = after_start;
    if (ExternalFrame != null) {
      ExternalFrame.repaint();
    };
  };

/*******************************************************************************
*                                                                              *
* stop             stop an applet when the accompanying document has been left *
*                                                                              *
*******************************************************************************/

  public synchronized void stop () {             // may be called several times!
    actualState = after_stop;
    if (ExternalFrame != null) {
      ExternalFrame.repaint();
    };
  };
};

/*******************************************************************************
*                                                                              *
*                              Additional Classes                              *
*                                                                              *
*******************************************************************************/

  class StateDisplay extends Component {
    Applet_06 Context;                   // a reference to the associated applet
    Dimension DisplaySize;          // the only acceptable size for this display


    public StateDisplay (Applet_06 Context) {
      super();
      this.Context = Context;       // keep a reference to the associated applet

      DisplaySize = new Dimension(352,105);
      setSize(DisplaySize);
    };


    public Dimension getMaximumSize()   {return DisplaySize;};
    public Dimension getMinimumSize()   {return DisplaySize;};
    public Dimension getPreferredSize() {return DisplaySize;};

    public void paint (Graphics Graf) {
      if (Context.actualState != null) {
        Graf.drawImage(Context.actualState, 0,0, this);
      };
    };
  };


  class StateDisplayFrame extends Frame {
    Applet_06 Context;                   // a reference to the associated applet


    public StateDisplayFrame (Applet_06 Context) {
      super("Applet_06 - State Display");

    /**** keep a reference to the associated applet ****/

      this.Context = Context;

    /**** enable window event processing ****/

      enableEvents(AWTEvent.WINDOW_EVENT_MASK);

    /**** prepare user interface ****/

      setLayout(new BorderLayout());

      Component Display = new StateDisplay(Context);
      add(Display,"Center");

    /**** resize and show the frame ****/

      pack();
      setVisible(true);

    /**** inhibit further size changes ****/

      setResizable(false);
    };


    protected void processWindowEvent (WindowEvent Event) {
      super.processWindowEvent(Event);

      if (Event.getID() == WindowEvent.WINDOW_CLOSING) {
        Context.ExternalFrame = null;
        Context.WindowButton.setEnabled(true);
        dispose();                                         // remove this window
      };
    };
  };

