package extensions.awt;
import java.awt.*;
import extensions.util.*;

/**
  * Standard dialog with user defined buttons across the bottom
  * @see extensions.awt.DialogExtended
  * @see java.awt.Dialog
  * @see extensions.awt.MessageLabel
  * @version 1.0.2
  * @author John Webster Small
  */
public class ButtonDialog
  extends DialogExtended
  implements ActionProtocol
{

  private ButtonActionPanel bap;

  /** ButtonDialog standard Ok|Cancel button labels. */
  public static String OkCancel = "Ok|Cancel";

  /** ButtonDialog standard Ok|Default|Cancel button labels. */
  public static String OkDefaultCancel = "Ok|Default|Cancel";

  /** ButtonDialog standard Ok button labels. */
  public static String Ok = "Ok";

  /** User defined buttons are numbered starting at 0.
    * Clicking the destroy dialog icon returns a number < 0.
    */
  public int getButtonClickedIdx()
    { return bap.getClickedIdx(); }

  /** Programmatically click a button.
    * @param idx index of button clicked
    */
  public void setClickedIdx(int idx)
    { bap.setClickedIdx(idx); }

  /** Return the label from the button clicked. */
  public String getButtonClickedLabel()
    { return bap.getClickedLabel(); }

  /** enable/disable indexed button.
    * @param cond if true enable button
    * @param buttonIdx button to enable/disable
    */
  public void enable(boolean cond, int buttonIdx)
  {
    ((ButtonAction)(bap.getComponent(buttonIdx))).enable(cond);
  }

  /** Is object a Button in this dialog? */
  public boolean isMemberButton(Object b)
  {
    if ((b instanceof ButtonAction)
      && bap.isMemberButton((ButtonAction)b))
      return true;
    return false;
  }

  /** Required by ActionProtocol interface, not called by user.
    * If a member button is clicked, destroy is called unless
    * update() is overridden returning true. It is called
    * as update(evt,what).
    * @see extensions.awt.DialogExtended#update
    */
  public void action(Component forward, Event evt, Object what)
  {
    if (isMemberButton(evt.target))  {
      if (!update(evt,what))
        destroy();
    }
    else if (evt.id == Event.WINDOW_DESTROY)
      destroy();
  }

  /**
    * Constructs a modal ButtonDialog.
    * @param parent dialog's parent frame
    * @param title dialog's title
    * @param buttons labels ("|" separated) appearing on "South" panel
    * @see #action
    */
  public ButtonDialog
    (Frame parent, String title, String buttons)
  {
    super(parent,title,true);  // Must be modal!!
    add("South",bap = new ButtonActionPanel(buttons,this));
    validate();
    pack();
  }

  /** Set focus on button.
    * @param idx index of button
    */
  public void requestButtonFocus(int idx)
    { bap.requestFocus(idx); }
}
