/*******************************************************************************
*                                                                              *
*  File:        Applet_04.java                          Revision:  1.0         *
*                                                                              *
*  Contents:    controlling a browser from within an applet                    *
*                                                                              *
*  Creation:    08.07.1996                     Last Modification:  17.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:    meanwhile, the original source code (for Java 1.0.2) has been  *
*               updated in order to compile under Java 1.1.8 and Java 1.4 w/o  *
*               major problems                                                 *
*                                                                              *
*******************************************************************************/

import java.applet.*;                                    // basic applet classes
import java.awt.*;                           // basic AWT classes (& interfaces)
import java.io.*;                            // Java file handling and stream IO
import java.net.*;                                       // Java network support
import java.util.*;                  // Java utility classes and data structures

public class Applet_04 extends java.applet.Applet {

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

  final static String  AppletInfo =
    "Applet_04 - controlling a browser from within an applet\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;

/**** desired applet insets ****/

  final static Insets  AppletInsets = new Insets(0,4,4,4);

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

  Button         ShowButton;          // initiates the display of a new web page
  Checkbox       SelfBox, ParentBox, TopBox, BlankBox, NameBox;
  CheckboxGroup  TargetGroup;

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

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

/*******************************************************************************
*                                                                              *
* getInsets                                       returns actual applet insets *
*                                                                              *
*******************************************************************************/

  public final Insets getInsets () {
    return AppletInsets;
  };

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

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

/*******************************************************************************
*                                                                              *
* handleEvent                                        main applet event handler *
*                                                                              *
*******************************************************************************/

  public synchronized boolean handleEvent (Event AWTEvent) {
    if ((AWTEvent.id == Event.ACTION_EVENT) && (AWTEvent.target == ShowButton)) {
      Checkbox TargetBox = TargetGroup.getSelectedCheckbox();
      if (TargetBox == SelfBox) {
        getAppletContext().showDocument(URL_of("self.html"), "_self");
        return true;
      };

      if (TargetBox == ParentBox) {
        getAppletContext().showDocument(URL_of("parent.html"), "_parent");
        return true;
      };

      if (TargetBox == TopBox) {
        getAppletContext().showDocument(URL_of("top.html"), "_top");
        return true;
      };

      if (TargetBox == BlankBox) {
        getAppletContext().showDocument(URL_of("blank.html"), "_blank");
        return true;
      };

      if (TargetBox == NameBox) {
        getAppletContext().showDocument(URL_of("named.html"), "separate Top-Level window");
        return true;
      };
    };

  /**** let somebody else handle this event ****/

    return super.handleEvent(AWTEvent); // applets don't seem to have a "processEvent"
  };

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

  public void init () {
    Label  TextLabel;                         // temporarily stores a text label

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

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

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

    setLayout(new GridBagLayout());

    TargetGroup = new CheckboxGroup();

    TextLabel = new Label("Applet_04 - controlling a browser");
      TextLabel.setFont(new Font("Helvetica", Font.BOLD, 12));
      add_GridItem(TextLabel,  0,0, 2,1, fill_x,    0,0, 1,2,0,0, align_w, 0,0);
      add_GridItem(new ColorBox(0,2, Color.black),
                               0,1, 2,1, fill_x,    0,0, 0,0,4,0, align_w, 0,0);

    TextLabel = new Label("display a new web page");
      add_GridItem(TextLabel,  0,2, 1,1, fill_x,    0,0, 0,2,0,0, align_w, 0,0);
    ShowButton = new Button("Show");
      add_GridItem(ShowButton, 1,2, 1,1, fill_x,    0,0, 0,2,0,0, align_w, 0,0);

    SelfBox   = new Checkbox("in the current frame",  TargetGroup, true);
      add_GridItem(SelfBox,    0,3, 2,1, fill_x,    0,0, 0,8,0,0, align_w, 0,0);
    ParentBox = new Checkbox("in the parent frame",   TargetGroup, false);
      add_GridItem(ParentBox,  0,4, 2,1, fill_x,    0,0, 0,8,0,0, align_w, 0,0);
    TopBox    = new Checkbox("in the top-most frame", TargetGroup, false);
      add_GridItem(TopBox,     0,5, 2,1, fill_x,    0,0, 0,8,0,0, align_w, 0,0);
    BlankBox  = new Checkbox("in a new unnamed top-level window", TargetGroup, false);
      add_GridItem(BlankBox,   0,6, 2,1, fill_x,    0,0, 0,8,0,0, align_w, 0,0);
    NameBox   = new Checkbox("in a new named top-level window",   TargetGroup, false);
      add_GridItem(NameBox,    0,7, 2,1, fill_x,    0,0, 0,8,0,0, align_w, 0,0);

    TextLabel = new Label("");
      add_GridItem(TextLabel,  0,8, 1,1, fill_both, 0,0, 0,2,0,0, align_w, 9,9);

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

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

/*******************************************************************************
*                                                                              *
* paint                                              (re)draws applet contents *
*                                                                              *
*******************************************************************************/

  public void paint (Graphics Graf) {
    int  Width  = getSize().width-1;                // "cache" applet dimensions
    int  Height = getSize().height-1;

  /**** draw border with "3D" effect ****/

    Graf.setColor(Color.white);
      Graf.drawLine(0,0, Width-1,0);        Graf.drawLine(0,0, 0,Height);
    Graf.setColor(Color.gray);
      Graf.drawLine(Width,0, Width,Height); Graf.drawLine(1,Height, Width,Height);
  };

/*******************************************************************************
*                                                                              *
* URL_of                                 constructs an URL for a test document *
*                                                                              *
*******************************************************************************/

  public final URL URL_of (String DocName) {
    try {
      return new URL(getDocumentBase(), DocName);
    } catch (MalformedURLException Signal) {
      /* empty */
    };

    return getDocumentBase();
  };

/*******************************************************************************
*                                                                              *
*                         Assisting Fields and Methods                         *
*                                                                              *
*******************************************************************************/

/**** local equivalents of "GridBagConstraints" constants ****/

  final static int align_c  = GridBagConstraints.CENTER;
  final static int align_n  = GridBagConstraints.NORTH;
  final static int align_ne = GridBagConstraints.NORTHEAST;
  final static int align_e  = GridBagConstraints.EAST;
  final static int align_se = GridBagConstraints.SOUTHEAST;
  final static int align_s  = GridBagConstraints.SOUTH;
  final static int align_sw = GridBagConstraints.SOUTHWEST;
  final static int align_w  = GridBagConstraints.WEST;
  final static int align_nw = GridBagConstraints.NORTHWEST;

  final static int fill_none = GridBagConstraints.NONE;
  final static int fill_x    = GridBagConstraints.HORIZONTAL;
  final static int fill_y    = GridBagConstraints.VERTICAL;
  final static int fill_both = GridBagConstraints.BOTH;

/*******************************************************************************
*                                                                              *
* add_GridItem   adds a "Component" to a "GridBag" using the given constraints *
*                                                                              *
*******************************************************************************/

  void add_GridItem (
    Component  Item,                         // the item to be added to the grid
    int  GridX,              // leftmost column to be occupied by a grid element
    int  GridY,                  // topmost row to be occupied by a grid element
    int  GridWidth,        // number of columns to be occupied by a grid element
    int  GridHeight,          // number of rows to be occupied by a grid element
    int  Fill,          // in which direction should a grid element be expanded?
    int  IPadX,                                   // internal horizontal padding
    int  IPadY,                                     // internal vertical padding
    int  InsetTop,                   // top margin between cell and grid element
    int  InsetLeft,                 // left margin between cell and grid element
    int  InsetBottom,             // bottom margin between cell and grid element
    int  InsetRight,               // right margin between cell and grid element
    int  Anchor,                         // grid element alignment inside a cell
    int  WeightX,       // element "priority" for gaining extra horizontal space
    int  WeightY          // element "priority" for gaining extra vertical space
  ) {
    if (Item == null) return;

  /**** create a new "GridBagConstraints" object... ****/

    GridBagConstraints  Constraints = new GridBagConstraints();

  /**** fill in the given arguments... ****/

    Constraints.gridx = GridX;
    Constraints.gridy = GridY;

    Constraints.gridwidth  = GridWidth;
    Constraints.gridheight = GridHeight;

    Constraints.fill = Fill;

    Constraints.ipadx = IPadX;
    Constraints.ipady = IPadY;

    Constraints.insets = new Insets(InsetTop, InsetLeft, InsetBottom, InsetRight);

    Constraints.anchor = Anchor;

    Constraints.weightx = Math.max(0,Math.min(99,WeightX)) * 100.0;
    Constraints.weighty = Math.max(0,Math.min(99,WeightY)) * 100.0;

  /**** then add "Item" using the given constraints ****/

    add(Item);
    ((GridBagLayout) getLayout()).setConstraints(Item,Constraints);
  };
};




/*******************************************************************************
*                                                                              *
*                                   ColorBox                                   *
*                                                                              *
*******************************************************************************/

class ColorBox extends Canvas {
  int Width, Height;                                       // desired dimensions

  public ColorBox (int LineWidth, int LineHeight, Color LineColor) {
    super();                                              // just to be complete

    Width  = Math.max(0,LineWidth);
    Height = Math.max(0,LineHeight);

    setBackground(LineColor);
  };

  public Dimension getMinimumSize () {
    return new Dimension((Width == 0 ? 1 : Width),(Height == 0 ? 1 : Height));
  };

  public Dimension getPreferredSize () {
    return new Dimension((Width  == 0 ? Math.max(getSize().width, 1) : Width),
                         (Height == 0 ? Math.max(getSize().height,1) : Height));
  };
};
