//****************************************************************************
// CLASS NAME:	YesNoDialog.java
//
// AUTHOR:	Rick Brink
//		    rick@mail.csh.rit.edu
//		    http://www.csh.rit.edu/~rick
//
//          Adapted from an example in "Java in a Nutshell"
//		    by David Flanagam, O'Reilly & Associates, Inc.
//
// VERSION:	1.0
//
// HISTORY:	4/16/96		Initial Version
//
// COPYRIGHT INFORMATION:
//
// This program and the Java source is in the public domain.
// Permission to use, copy, modify, and distribute this software
// and its documentation for NON-COMMERCIAL purposes and
// without fee is hereby granted.
//
//    Copyright 1996
//
//    Rick Brink
//    1266 Brighton-Henrietta Townline Rd.
//    Rochester, NY 14623
//
// DISCLAIMER:
//
// The author claims no responsibility for any damage, direct or indirect,
// to any harware or software as a result of using this program.
//****************************************************************************

import java.awt.Button;
import java.awt.Event;
import java.awt.Dialog;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.Frame;

/**
  * A class representing a Dialog which displays a message
  * and accepts up to three possible buttons (Yes, No, Cancel).
  * The methods yes(), no(), and cancel() are meant to be
  * overridden by classes that subclass this class in order
  * to implement different repsonses.
  *
  * @see NewDesignDialog
  * @see ReallyQuitDialog
  * @see PetriToolFrame
  * @see java.awt.Button
  * @see java.awt.Event
  * @see java.awt.Dialog
  * @see java.awt.BorderLayout
  * @see java.awt.FlowLayout
  * @see java.awt.Panel
  * @see java.awt.Frame
  *
  * @version 1.0 July 3, 1996
  *
  * @author  Rick Brink
**/
public class YesNoDialog extends Dialog {
    /** Button for a Yes response **/
    protected Button yes_ = null;

    /** Button for a No response **/
    protected Button no_ = null;

    /** Button for a cancel response **/
    protected Button cancel_ = null;

    /** Message to be displayed in window **/
    protected MultiLineLabel tempLabel_;

    /**
      * Construct a new YesNoDialog, putting a String message
      * centered in the window, with up to three buttons (yes,
      * no, & cancel).
      *
      * @param parent The Frame that instantiated this object
      * @param title The title of the window
      * @param message The message to display in the window
      * @param yesLabel String text for the Yes button, or null
      *                 if this button is not desired
      * @param noLabel String text for the No button, or null
      *                 if this button is not desired
      * @param cancelLabel String text for the Cancel button, or null
      *                 if this button is not desired
    **/
    public YesNoDialog(Frame parent, String title, String message,
                       String yesLabel, String noLabel,
                       String cancelLabel)
    {
        // Create the window.
        super(parent, title, true);

        // Specify a LayoutManager for it
        this.setLayout(new BorderLayout(15, 15));

        // Put the message label in the middle of the window.
        tempLabel_ = new MultiLineLabel(message, 20, 20);
        this.add("Center", tempLabel_);

        // Create a panel of buttons, center the row of buttons in
        // the panel, and put the pane at the bottom of the window.
        Panel p = new Panel();
        p.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));

        if (yesLabel != null) {
            p.add(yes_ = new Button(yesLabel));
        }

        if (noLabel != null) {
            p.add(no_ = new Button(noLabel));
        }

        if (cancelLabel != null) {
            p.add(cancel_ = new Button(cancelLabel));
        }

        this.add("South", p);

        // Set the window to its preferred size.
        this.pack();
    }

    /**
      * Handle button events by calling yes(), no(), and cancel()
      * methods depending on the button the user clicked.
      * Subclasses define how the answer is processed by
      * overriding the  yes(), no(), and cancel() methods.
    **/
    public boolean action(Event e, Object arg)
    {
        if (e.target instanceof Button) {
            this.hide();
            this.dispose();

            if (e.target == yes_) {
                yes();
            }
            else if (e.target == no_) {
                no();
            }
            else {
                cancel();
            }

            return true;
        }
        else {
            return(false);
        }
    }

    /**
      * Override this method to provide functionality
      * for the Yes button
    **/
    protected void yes() {}

    /**
      * Override this method to provide functionality
      * for the No button
    **/
    protected void no() {}

    /**
      * Override this method to provide functionality
      * for the Cancel button
    **/
    protected void cancel() {}
}
