/**
 * @(#)ThreadLauncherDialog.java 1.0.1 15/11/1998
 *
 * Copyright 1997-1998 by Eric Lefevre
 *
 */

import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

/**
 *
 * <P>This class creates a Dialog containing a single Cancel button and 
 * launches a thread. This thread can be stopped by the Cancel button.
 * <BR>Exemple of use: while loading a big file, or while doing lots of computing.</P>
 * <P>Requirements: Java 1.1</P>
 * <P>Warning: this class calls the method Thread.stop(). One should be aware that this
 * method is deprecated in Java 1.2. It would therefore be wise to change this call
 * to a direct call to a specific method in the task.</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 Java stuff to download.&nbsp;**</P>
 *
 * @version 
 * v1.0.1, 15 Nov 1998:
 * <UL>
 * <LI>new constructor added, without the parameter that concerns the label on the button</LI>
 * <LI>cosmetic modifications in the documentation</LI>
 * <LI>the go() method is renamed show(). Therefore, it now overrides the usual show() method.</LI>
 * </UL>
 * v1.0, 21 Nov 1997.
 *
 * @author Eric Lefevre (e-mail: <A HREF="mailto:">eric.lefevre@usa.net</A>)
 */

public class ThreadLauncherDialog
    extends Dialog
    implements ActionListener
{
    /** The Thread containing the action to execute. */
    private Thread theThreadToRun;

    /** The Cancel Button. */
    private Button cancelButton;

    /**
     * Constructs a dialog box containing a Cancel Button and launches a Thread.
     * @param title the text in the title bar
     * @param message the text in the dialog box
     * @param cancelLabel the label on the Cancel Button
     * @param taskToLaunch the task that we want to run in the background
     */
    public ThreadLauncherDialog(Frame parent, String title, String message,
                         String cancelLabel, Runnable taskToLaunch)
    {
        super(parent,title,true);

        setResizable(false);
        setBackground(Color.lightGray);
        setLayout(new BorderLayout());

        cancelButton = new Button(cancelLabel);
        cancelButton.requestFocus();
        add(cancelButton, "South");
        cancelButton.addActionListener(this);

        add(new Label(message), "North");

        pack();

    	Rectangle parentBounds = getParent().getBounds();
    	Rectangle myBounds = this.getBounds();
    	setLocation(new Point(parentBounds.x + (parentBounds.width  - myBounds.width) / 2,
                              parentBounds.y + (parentBounds.height - myBounds.height)/ 2) );

        theThreadToRun = new Thread(taskToLaunch);
    }

    /**
     * Constructs a dialog box containing a Cancel Button and launches a Thread. The label
     * on the button is "Cancel" by default.
     * @param title the text in the title bar
     * @param message the text in the dialog box
     * @param taskToLaunch the task that we want to run in the background
     */
    public ThreadLauncherDialog(Frame parent, String title, String message, Runnable taskToLaunch)
    {
        this(parent, title, message, "Cancel", taskToLaunch);
    }

    /**
     * Shows the dialog box and launches the thread.
     */
    public void show()
    {
        theThreadToRun.start();
        super.show();
    }

    /** 
     * 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() == cancelButton)
        {
            theThreadToRun.stop();
            setVisible(false);
            dispose();
        }
    }

}
