package extensions.awt;
import java.awt.*;
import java.util.*;

/**
  * Standard multi-line message dialog.
  * @see java.awt.ButtonDialog
  * @version 1.0.2
  * @author John Webster Small
  */
public class MessageDialog extends ButtonDialog
{

  /**
    * Construct and show a new MessageDialog.
    * @param parent parent frame if this dialog
    * @param title title of this dialog
    * @param msg multi-line message displayed in dialog
    * @param buttons user response options
    * @param width if both width and height must be > 0
    *   resize(width,height) is called, otherwise pack()
    * @param height (see width)
    */
  public MessageDialog
    (Frame parent, String title, String msg, String buttons,
     int width, int height)
  {
    super(parent,title,buttons);
    if (msg != null)
      add("Center",new LabelPanel(msg,LabelPanel.CENTER));
    validate();
    if (width > 0 && height > 0)
      resize(width,height);
    else
      pack();
    show();
  }

  /**
    * Construct and show a new MessageDialog.
    * @param parent parent frame if this dialog
    * @param title title of this dialog
    * @param msg multi-line message displayed in dialog
    * @param buttons user response options
    */
  public MessageDialog
    (Frame parent, String title, String msg, String buttons)
    { this(parent,title,msg,buttons,0,0); }

  /**
    * Construct and show a new MessageDialog.
    * @param parent parent frame if this dialog
    * @param title title of this dialog
    * @param msg multi-line message displayed in dialog
    */
  public MessageDialog
    (Frame parent, String title, String msg, int width, int height)
    { this(parent,title,msg,Ok,width,height); }

  /**
    * Construct and show a new MessageDialog.
    * @param parent parent frame if this dialog
    * @param title title of this dialog
    * @param msg multi-line message displayed in dialog
    */
  public MessageDialog
    (Frame parent, String title, String msg)
    { this(parent,title,msg,0,0); }

  /** 
    * Construct and show a new MessageDialog.
    * @param parent parent frame if this dialog
    * @param title title of this dialog
    * @param msg multi-line message displayed in dialog
    * @param buttons user response options
    * @return returns index of button clicked or -1 if
    *   window destroyed
    */
  public static int inquire
    (Frame parent, String title, String msg, String buttons)
  {
    MessageDialog md = new MessageDialog
           (parent,title,msg,buttons);
    return md.getButtonClickedIdx();
  }

  /** 
    * Construct and show a new MessageDialog.
    * Though modal, dialog is in separate thread.
    * @param parent parent component if this dialog
    * @param title title of this dialog
    * @param msg multi-line message displayed in dialog
    */
  public static void notifyUser
    (Component parent, String title, String msg)
  {
    new MessageDialogNotifyUser
      (FrameExtended.getParentFrame(parent),title,msg);
  }
}

final class MessageDialogNotifyUser extends Thread
{
  private Frame parent;
  private String title;
  private String msg;

  MessageDialogNotifyUser
    (Frame parent, String title, String msg)
  {
    super("MessageDialogNotifyUser");
    this.parent = parent;
    this.title = title;
    this.msg = msg;
    start();
  }

  public void run()
    { new MessageDialog(parent,title,msg); }
}