/******************************************************************************
Create a Quit menu item.
******************************************************************************/

package FlexOr.utility;

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

/**
Create a Quit menu item.
 <P>
Basic definition comes from "Understanding Object Oriented Programming with
Java", Timothy Budd, Addison-Wesley, 1998, p228.
<P>
Modified as follows.
</UL>
<LI>Moved redundant program text into private support routines.
<LI>Store a pointer to the application locally for use in dispose.
<LI>Uses inApplet flag to select between dispose and exit.
</UL>
@author Gunnar Gotshalks
@version 1.0 1999 Jan 16
*/

public class QuitItem implements ActionListener {
  Frame application;
  boolean inApplet;

/** Create new menubar with a new menu Quit containing the menu item Quit.

@param application the application which should be disposed of or exited.
@param inApplet set to True if invoked from an applet so dispose is the
action performed.  Set to False if invoked from a stand alone application so
exit is the action performed.
*/
  public QuitItem (Frame application, boolean inApplet) {
    this.application = application;
    this.inApplet = inApplet;
    createMenuBar();
  }
  
/** Create new menu Quit containing the menu item Quit.

@param mbar the menubar in which to place the menu.
@param application the application which should be disposed of or exited.
@param inApplet set to True if invoked from an applet so dispose is the
action performed.  Set to False if invoked from a stand alone application so
exit is the action performed.
*/
  public QuitItem (MenuBar mbar, Frame application, boolean inApplet) {
    this.application = application;
    this.inApplet = inApplet;
    addToMenuBar(mbar);
  }
  
/** Create new menu item menu item Quit.

@param menu where to place the menu item.
@param application the application which should be disposed of or exited.
@param inApplet set to True if invoked from an applet so dispose is the
action performed.  Set to False if invoked from a stand alone application so
exit is the action performed.
*/
  public QuitItem (Menu menu, Frame application, boolean inApplet) {
    this.application = application;
    this.inApplet = inApplet;
    setMenuItem(menu);
  }

  private void createMenuBar() {
    MenuBar mbar = new MenuBar();
    application.setMenuBar(mbar);
    addToMenuBar(mbar);
  }
  
  private void addToMenuBar(MenuBar mbar) {
    Menu menu = new Menu("Quit");
    mbar.add(menu);
    setMenuItem(menu);
  }
  
  private void setMenuItem(Menu menu) {
    MenuItem mitem = new MenuItem("Quit", new MenuShortcut(KeyEvent.VK_Q));
    mitem.setActionCommand("Quit");
    mitem.addActionListener(this);
    menu.add(mitem);
  }

/** Attach the action to dispose the applet window or exit the application.

@param mitem the menu item to which to attach the dispose/exit action.
@param application the application which should be disposed of or exited.
@param inApplet set to True if invoked from an applet so dispose is the
action performed.  Set to False if invoked from a stand alone application so
exit is the action performed.
*/
  public QuitItem (MenuItem mitem, Frame application, boolean inApplet) {
    this.application = application;
    this.inApplet = inApplet;
    mitem.addActionListener(this);
  }
  
  public void actionPerformed (ActionEvent e) {
    if (inApplet) { application.dispose(); }
    else { System.exit(0); }}
}
