import java.applet.Applet;

import java.awt.*;
import java.awt.event.*;

/** Example code showing use of regular and image buttons */

public class ButtonDemo extends Applet implements
ActionListener,
ItemListener
{

    private Panel toolbar;

    private BubbleHelper BH;

    private RadioGroup RG;

    private Checkbox
	CB_Up,
	CB_Down;

    private CheckImageButton
	TB_Local_CB,
	TB_Font_Incr_B,
	TB_Help_CB,
	TB_Font_Decr_B,
	TB_MinMax_CB;

    private Button
	B_One,
	B_Two;

    private PushImageButton
	TB_Anim_B,
	TB_Stop_B;

    private TextArea textarea;

        private Image
	  TB_stop = null,
	  TB_zone = null,
	  TB_bigger = null,
	  TB_smaller = null,
	  TB_minmax = null,
	  TB_help = null;

/** To run the demo as an application,
 * construct it and show it in a frame.
 * This must be a static (class not instance) method,
 * because the instance won't exist until we construct it here. */

  public static void main (String []argv) {

      Frame ff = new Frame ("ButtonDemo");
      ff.setBounds (100, 100, 600, 500);

      ButtonDemo cd = new ButtonDemo ();
      ff.add (cd);

      cd.loadLocalImages ();

      cd.init ();

      ff.pack ();
      ff.show ();

/* Only now that it is showing we can get the layout right */

      cd.invalidate ();
      cd.validate ();
  }

/** Constructor */

  public ButtonDemo () {

	setBackground (Color.gray);

	BH = new BubbleHelper ();

/* Probelms with doing layout before we know sizes...
It is claimed that an explicit setBounds before we add does the trick.
 */

	setLayout (new GridLayout (2, 0));

        add (toolbar = new Panel ());

	toolbar.setLayout (new WrapLayout ());

/* Couple of regular Button */

	toolbar.add
	   (B_One
	   = new Button ("One"));

        BH.setHelp (B_One, "AWT Button One");

	toolbar.add
	   (B_Two
	   = new Button ("Two"));

        BH.setHelp (B_Two, "AWT Button Two");

/* This one uses an animated drawing instead of an image */

        toolbar.add
          (TB_Anim_B
          = new PushImageButton (new WhiteBar ()));

        BH.setHelp (TB_Anim_B, "I Move");

/* The images are still nulls -- will get them soon */

        toolbar.add
          (TB_Stop_B
          = new PushImageButton (TB_stop));

        TB_Stop_B.setEnabled(false);

        BH.setHelp (TB_Stop_B, "Disabled but help works");

        toolbar.add
          (TB_Local_CB
          = new CheckImageButton (TB_zone));

        BH.setHelp (TB_Local_CB, "Local / GMT");

/* Manage a Radio Group,
 * extension of CheckboxGroup that can manage both,
 * regular Checkbox and CheckImageButton */

	RG = new RadioGroup ();

        toolbar.add
          (TB_Font_Incr_B
          = new CheckImageButton (TB_bigger));

        BH.setHelp (TB_Font_Incr_B, "Bigger");

	  TB_Font_Incr_B.setRadioGroup (RG);

        toolbar.add
          (TB_Font_Decr_B
          = new CheckImageButton (TB_smaller));

        BH.setHelp (TB_Font_Decr_B, "Smaller");

	  TB_Font_Decr_B.setRadioGroup (RG);

/* Couple of regular Checkbox */

        toolbar.add
	  (CB_Up
	  = new Checkbox ("Up"));

        BH.setHelp (CB_Up, "Up CB");

	CB_Up.setCheckboxGroup (RG);

        toolbar.add
	  (CB_Down
	  = new Checkbox ("Down"));

        BH.setHelp (CB_Down, "Down CB");

	CB_Down.setCheckboxGroup (RG);

        toolbar.add
          (TB_MinMax_CB
          = new CheckImageButton (TB_minmax));

        BH.setHelp (TB_MinMax_CB, "Min / Max");

/* Add this one last so it is furthest to right, */

        toolbar.add
          (TB_Help_CB
          = new CheckImageButton (TB_help));

        BH.setHelp (TB_Help_CB, "Help On / Off");

        TB_Help_CB.setState (true);

        add (textarea = new TextArea (12, 60));
	textarea.append ("\nButtonDemo\n");
    }

/** These work for application via local files */

   public void loadLocalImages () {
        TB_stop = Toolkit.getDefaultToolkit ().getImage("TB_stop.gif");
        TB_zone = Toolkit.getDefaultToolkit ().getImage("TB_zone.gif");
        TB_bigger = Toolkit.getDefaultToolkit ().getImage("TB_bigger.gif");
        TB_smaller = Toolkit.getDefaultToolkit ().getImage("TB_smaller.gif");
        TB_minmax = Toolkit.getDefaultToolkit ().getImage("TB_minmax.gif");
        TB_help = Toolkit.getDefaultToolkit ().getImage("TB_help.gif");
    }

    public void init () {

/* Must be an applet if no images yet so get them now remotely */

        if (null == TB_stop) {

	    TB_stop = getImage (getDocumentBase (), "TB_stop.gif");

	    TB_zone = getImage (getDocumentBase (), "TB_zone.gif");

	    TB_bigger = getImage (getDocumentBase (), "TB_bigger.gif");

	    TB_smaller = getImage (getDocumentBase (), "TB_smaller.gif");

	    TB_minmax = getImage (getDocumentBase (), "TB_minmax.gif");

	    TB_help = getImage (getDocumentBase (), "TB_help.gif");
        }

        MediaTracker tracker = new java.awt.MediaTracker (this);

        tracker.addImage (TB_stop, 0);
        tracker.addImage (TB_zone, 0);
        tracker.addImage (TB_bigger, 0);
        tracker.addImage (TB_smaller, 0);
        tracker.addImage (TB_minmax, 0);
        tracker.addImage (TB_help, 0);

        try { tracker.waitForID (0);
	      if (tracker.isErrorID (0))
	          { System.out.println ("FAILED"); return; }
        }
        catch (Exception e) { System.out.println ("FAILED "+e); e.printStackTrace (); }

/* Now we have images */

        TB_Stop_B.setImage (TB_stop);
        TB_Local_CB.setImage (TB_zone);
        TB_Font_Incr_B.setImage (TB_bigger);
        TB_Font_Decr_B.setImage (TB_smaller);
        TB_MinMax_CB.setImage (TB_minmax);
        TB_Help_CB.setImage (TB_help);

	textarea.append ("ButtonDemo\n");
	textarea.append ("Hello\n");

/* Make all possible connections between
 * all our listener methods and all our toolbar components */

        ListenerAdder.add_Listeners (this, toolbar);

        toolbar.invalidate ();
        invalidate ();

        validate ();
    }

    public void itemStateChanged (ItemEvent e) {
	textarea.append ("\n "+e);
	if ((e.getSource () == TB_Help_CB)
	 && (null != BH))
	    BH.setDoHelp (TB_Help_CB.getState ());
    }

    public void actionPerformed (ActionEvent e) {
	textarea.append ("\n "+e);
    }
}
/* <IMG SRC="/cgi-bin/counter">*/
