import java.applet.Applet;

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

/** Example code */

public class PopupDemo extends Applet implements
ActionListener,
ItemListener
{

    private Panel toolbar;

    private Button
	B_One,
	B_Two;

    private Label
	L_One,
	L_Two;

    private TextArea textarea;

/** 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 ("PopupDemo");
      ff.setBounds (100, 100, 600, 500);

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

      cd.init ();

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

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

/** Constructor */

  public PopupDemo () {

	setBackground (Color.gray);

	setLayout (new GridLayout (2, 0));

        add (toolbar = new Panel ());

	toolbar.setLayout (new WrapLayout ());

/* Panel can have a PopupMenu with no problems. */

        PMHelper TBPMH = new PMHelper (toolbar, "Outer Popup");
	TBPMH.addMenuItem ("Outer Hello");
	TBPMH.addMenuItem ("Outer Goodbye");

/* Label can have a PopupMenu with no problems. */

	toolbar.add
	   (L_One
	   = new Label ("Label One"));

        PMHelper LOnePMH = new PMHelper (L_One, "One Popup");
	LOnePMH.addMenuItem ("Hello from Label One");

/* You can also add CheckboxMenuItem to a PopupMenu,
 * or to a pullright Menu under a PopupMenu,
 * they didn't work for a long time but 1.1.5 finally fixed them. */

	LOnePMH.add (new CheckboxMenuItem ("Label One Toggle One", true));
	LOnePMH.add (new CheckboxMenuItem ("Label One Toggle Two", false));

/* Here we add a pullright Menu,
 * to which we add a couple of choices and couple of toggles.
 * That add can use a String directly,
 * or a pre-constructed MenuItem (String) */

	Menu LOH = LOnePMH.addMenu ("Pullright Label One");
	LOH.add ("LOH This");
	LOH.add ("LOH That");
	LOH.add (new CheckboxMenuItem ("LOH Toggle One", true));
	LOH.add (new CheckboxMenuItem ("LOH Toggle Two", false));

/* Button can have a PopupMenu but it has problems.
 * Different mouse buttons for regular press and for menu.
 * AWT BUG! All will show same text as the Button itself! */

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

        PMHelper BOnePMH = new PMHelper (B_One, "One Popup");
	Menu BOH = BOnePMH.addMenu ("Hello from One");
	BOH.add ("BOH This");
	BOH.add ("BOH That");
	BOnePMH.addMenuItem ("Goodbye from One");

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

        add (textarea = new TextArea (12, 70));

/* TextArea can have a PopupMenu,
 * but it already has one for Cut and Paste,
 * so you will get both of them...
 * BETTER to find handle to the existing one,
 * and add our custom choices to it.
 * CAN WE DO THAT?
 * There are Component.add (PopupMenu) and Component.remove (Menu),
 * but there isn't any Menu Component.getMenu (),
 * so no we can't. */

        PMHelper TextPMH = new PMHelper (textarea, "Text Popup");
	TextPMH.addMenuItem ("Text Hello");
	TextPMH.addMenuItem ("Text Goodbye");

	textarea.append ("\nPopupDemo\n");

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

        ListenerAdder.add_Listeners (this, this);

/* But PopupMenu is not in the regular tree,
 * and there isn't any Menu Component.getMenu (),
 * so we have to add (the top of) each PopupMenu here,
 * it will walk the menu tree and add each final choice. */

        ListenerAdder.add_Listeners (this, TBPMH);
        ListenerAdder.add_Listeners (this, BOnePMH);
        ListenerAdder.add_Listeners (this, LOnePMH);
        ListenerAdder.add_Listeners (this, TextPMH);
    }

    public void init () {

	textarea.append ("Hello\n");

        toolbar.invalidate ();
        invalidate ();

        validate ();
    }

    public void itemStateChanged (ItemEvent e) {
	textarea.append ("\n "+e);
    }

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