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

public class PMHelper extends PopupMenu
implements MouseListener
{

 public String getVersion () { return "$Id: PMHelper.java,v 1.2 1998/07/02 18:44:55 mhirsch Exp $"; }

/** PMHelper extends (constructs) a PopupMenu,
 * adds it to the given Component,
 * adds Mouse Listeners and tests needed to pop it up,
 * and since it is an extension of PopupMenu,
 * the constructor returns a reference to the Popup Menu.
 *
 * The caller can then add MenuItems to this Popup Menu,
 * we provide a convenience call but they could do it too.
 *
 * Just adding the PopupMenu to a Component is not enough,
 * you need all the mouse event stuff to make it show,
 * taken care of by this helper class.
 *
 * You still need to handle the selections.
 *
 * With the old 1.0 event model,
 * this means action () code to either
 * compare saved handles to event target,
 * or compare known strings to the event arg.
 *
 * With the new 1.1 event model,
 * this means listeners (on each Item) for
 * ActionEvent when a MenuItem is chosen,
 * and the ItemEvent when a CheckboxMenuItem is chosen,
 * (although CheckboxMenuItem is broken last I checked -- MGH)
 * @author Morris Hirsch
 * IPD Inc Newport RI
 */

 private Component ct;

/** Construct a PopupMenu,
 * add it to the given Component,
 * give menu the given Name.
 * Add a Listener,
 * and support the various ways that different platforms do pop. */

  public PMHelper (Component ct, String PMName) {

     super (PMName);

     this.ct = ct;

     ct.add (this);

     ct.addMouseListener (this);
  }

  public void mousePressed (MouseEvent e) { showPopup (e); }

  public void mouseClicked (MouseEvent e) { showPopup (e); }

  public void mouseReleased (MouseEvent e) { showPopup (e); }

  public void mouseEntered (MouseEvent e) {}

  public void mouseExited (MouseEvent e) {}

/* Test the event for being how **OUR** platform does popup,
 * and show the menu if so. */

 void showPopup (MouseEvent evt) {
     if (evt.isPopupTrigger ())
	 show (ct, evt.getX (), evt.getY ());
 }

/** Add a MenuItem to the PopupMenu and return the item handle,
 * caller still needs to establish a listener. */

 public MenuItem addMenuItem (String ItemName) {
    MenuItem mi = new MenuItem (ItemName);
    add (mi);
    return mi;
 }

/** Add a MenuItem to the PopupMenu and return the item handle,
 * also establish given component as a listener. */

 public MenuItem addMenuItem (ActionListener caller, String ItemName) {
    MenuItem mi = new MenuItem (ItemName);
    add (mi);

    mi.addActionListener (caller);

    return mi;
 }

/** Add a Menu to the PopupMenu and return the handle, */

 public Menu addMenu (String Name) {
    Menu mi = new Menu (Name);
    add (mi);
    return mi;
 }

/** Add a CheckboxMenuItem to the PopupMenu and return the handle,
 * caller still needs to establish a listener. 
 */

 public CheckboxMenuItem addCheckboxMenuItem (String ItemName) {
    CheckboxMenuItem mi = new CheckboxMenuItem (ItemName);
    add (mi);
    return mi;
 }

/** Add a CheckboxMenuItem to the PopupMenu and return the handle,
 * also establish given component as a listener.
 */

 public CheckboxMenuItem addCheckboxMenuItem (ItemListener caller, String ItemName) {
    CheckboxMenuItem mi = new CheckboxMenuItem (ItemName);
    add (mi);

    mi.addItemListener (caller);

    return mi;
 }
}
/* <IMG SRC="/cgi-bin/counter">*/
