/**
 * @(#)MessageBox.java 1.0.1 15/11/1998
 *
 * Copyright 1997-1998 by Eric Lefevre
 *
 */

import java.awt.*;
import java.awt.event.*;

/**
 * <P>This class is a dialog box that displays a title, a message and some buttons such as
 * Yes, No, Cancel, OK, Abort, Retry, or Ignore.</P>
 *
 * <P>Please report any bug/enhancement proposal to the author.</P>
 *
 * <P>**&nbsp;See <A HREF="http://www.chez.com/elefevre">http://www.chez.com/elefevre</A>
 * for updates and more stuff to download.&nbsp;**</P>
 *
 * @version 
 * v1.0.1, 15 Nov 1998:
 * <UL>
 * <LI>all variables not of class Button are made private</LI>
 * <LI>cosmetic enhancements in the documentation</LI>
 * </UL>
 * v1.0, 26 Oct 1997.
 *
 * @author Eric Lefevre (e-mail: <A HREF="mailto:">eric.lefevre@usa.net</A>)
 */

public class MessageBox
    extends Dialog 
    implements ActionListener
{
    /** The zone that contains the buttons. */
    private Panel controlPanel;

    /** A Yes Button to give in parameter. */
    public static Button YES = new Button("Yes");

    /** A No Button to give in parameter. */
    public static Button NO = new Button("No");

    /** A Cancel Button to give in parameter. */
    public static Button CANCEL = new Button("Cancel");

    /** A OK Button to give in parameter. */
    public static Button OK = new Button("OK");

    /** A Cancel Button to give in parameter. */
    public static Button ABORT = new Button("Abort");

    /** A Retry Button to give in parameter. */
    public static Button RETRY = new Button("Retry");

    /** A Ignore Button to give in parameter. */
    public static Button IGNORE = new Button("Ignore");

    /** An empty button. */
    private static Button nobuttonButton = new Button();

    /** Buttons for the dialog box. */
    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;
    private Button button5;
    private Button button6;
    private Button button7;

    /** The Button that will be returned. */
    private Button buttonReturn;

    /** The message in the box. */
    private Label messageLabel;

    /**
     * Constructs a dialog box with seven chosen buttons.
     * @param parent a reference on the frame that has called this MessageBox
     * @param title the sentence displayed inthe title bar
     * @param message the message displayed in the dialog box
     * @param buttons b1, b2, b3, b4, b5, b6, b7 are the buttons given in parameter.
     * It is recommended, but not compulsory, that these buttons be among the constants defined
     * in this class.
     */
    public MessageBox(Frame parent, String title, String message,
                      Button b1, Button b2, Button b3,
                      Button b4, Button b5, Button b6,
                      Button b7)
    {
        super(parent,title,true);

        setResizable(false);
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());
        controlPanel = new Panel();
        controlPanel.setLayout(new GridLayout(1,0,10,10));

        button1 = b1; button1.addActionListener(this);
        button2 = b2; button2.addActionListener(this);
        button3 = b3; button3.addActionListener(this);
        button4 = b4; button4.addActionListener(this);
        button5 = b5; button5.addActionListener(this);
        button6 = b6; button6.addActionListener(this);
        button7 = b7; button7.addActionListener(this);
        controlPanel.add(button1);
        if ( button2 != nobuttonButton ) controlPanel.add(button2);
        if ( button3 != nobuttonButton ) controlPanel.add(button3);
        if ( button4 != nobuttonButton ) controlPanel.add(button4);
        if ( button5 != nobuttonButton ) controlPanel.add(button5);
        if ( button6 != nobuttonButton ) controlPanel.add(button6);
        if ( button7 != nobuttonButton ) controlPanel.add(button7);
        button1.requestFocus();

        add(controlPanel,"South");
        messageLabel = new Label(message);
        add(messageLabel,"North");
        pack();

    	Rectangle parentBounds = getParent().getBounds();
    	Rectangle myBounds = getBounds();
    	setLocation(parentBounds.x + (parentBounds.width - myBounds.width)/ 2,
                    parentBounds.y + (parentBounds.height - myBounds.height)/2);

     }

    /**
     * Constructs a dialog box with six chosen buttons.
     * @param parent a reference on the frame that has called this MessageBox
     * @param title the sentence displayed inthe title bar
     * @param message the message displayed in the dialog box
     * @param buttons b1, b2, b3, b4, b5, b6 are the buttons given in parameter.
     * It is recommended, but not compulsory, that these buttons be among the constants defined
     * in this class.
     */
    public MessageBox(Frame parent, String title, String message,
                      Button b1, Button b2, Button b3,
                      Button b4, Button b5, Button b6)
    {
        this(parent,title,message,b1,b2,b3,b4,b5,b6,nobuttonButton);
    }

    /**
     * Constructs a dialog box with five chosen buttons.
     * @param parent a reference on the frame that has called this MessageBox
     * @param title the sentence displayed inthe title bar
     * @param message the message displayed in the dialog box
     * @param buttons b1, b2, b3, b4, b5 are the buttons given in parameter.
     * It is recommended, but not compulsory, that these buttons be among the constants defined
     * in this class.
     */
    public MessageBox(Frame parent, String title, String message,
                      Button b1, Button b2, Button b3,
                      Button b4, Button b5)
    {
        this(parent,title,message,b1,b2,b3,b4,b5,nobuttonButton,nobuttonButton);
    }

    /**
     * Constructs a dialog box with four chosen buttons.
     * @param parent a reference on the frame that has called this MessageBox
     * @param title the sentence displayed inthe title bar
     * @param message the message displayed in the dialog box
     * @param buttons b1, b2, b3, b4 are the buttons given in parameter.
     * It is recommended, but not compulsory, that these buttons be among the constants defined
     * in this class.
     */
    public MessageBox(Frame parent, String title, String message,
                      Button b1, Button b2, Button b3,
                      Button b4)
    {
        this(parent,title,message,b1,b2,b3,b4,nobuttonButton,nobuttonButton,nobuttonButton);
    }

    /**
     * Constructs a dialog box with three chosen buttons.
     * @param parent a reference on the frame that has called this MessageBox
     * @param title the sentence displayed inthe title bar
     * @param message the message displayed in the dialog box
     * @param buttons b1, b2, b3 are the buttons given in parameter.
     * It is recommended, but not compulsory, that these buttons be among the constants defined
     * in this class.
     */
    public MessageBox(Frame parent, String title, String message,
                      Button b1, Button b2, Button b3)
    {
        this(parent,title,message,b1,b2,b3,nobuttonButton,nobuttonButton,nobuttonButton,nobuttonButton);
    }

    /**
     * Constructs a dialog box with two chosen buttons.
     * @param parent a reference on the frame that has called this MessageBox
     * @param title the sentence displayed inthe title bar
     * @param message the message displayed in the dialog box
     * @param buttons b1, b2 are the buttons given in parameter.
     * It is recommended, but not compulsory, that these buttons be among the constants defined
     * in this class.
     */
    public MessageBox(Frame parent, String title, String message,
                      Button b1, Button b2)
    {
        this(parent,title,message,b1,b2,nobuttonButton,nobuttonButton,nobuttonButton,nobuttonButton,nobuttonButton);
    }

    /**
     * Constructs a dialog box with one chosen button.
     * @param parent a reference on the frame that has called this MessageBox
     * @param title the sentence displayed inthe title bar
     * @param message the message displayed in the dialog box
     * @param buttons b1 are the buttons given in parameter.
     * It is recommended, but not compulsory, that these buttons be among the constants defined
     * in this class.
     */
    public MessageBox(Frame parent, String title, String message,
                      Button b1)
    {
        this(parent,title,message,b1,nobuttonButton,nobuttonButton,nobuttonButton,nobuttonButton,nobuttonButton,nobuttonButton);
    }

    /** 
     * Tests if the Cancel Button has been clicked.
     * In that case, stops the thread and closes the dialog box.
     */
    public void actionPerformed(ActionEvent event)
    {
        if ( event.getSource() instanceof Button ) {
            buttonReturn = (Button)event.getSource();
            dispose();
        }
    }

    /**
     * Shows the dialog box and returns the button chosen by the user.
     * @return the button clicked by the user
     */
    public Button proceed()
    {
        setVisible(true);
        return buttonReturn;
    }

}
