/*
 * @(#)ThreadedAction.java    2.0  21 February 2002
 *
 * Copyright 2004
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import java.awt.event.*;
import javax.swing.*;
import java.beans.*;
 
/**
 * <P>An <CODE>ActionWrapper<CODE> that performs 
 * its encapsulated action in a newly created separate thread.</P>
 *
 * @author Richard Rasala
 * @version 2.2
 * @since   2.0
 */
public class ThreadedAction extends ActionWrapper {
 
    //////////////////
    // Constructors //
    //////////////////
    
    /** Constructs an empty wrapper . */
    public ThreadedAction() {}
    
    /**
     * Constructs a wrapper that encapsulates the given action.
     *
     * @param action the action to encapsulate 
     *      and perform in a separate thread
     */
    public ThreadedAction(Action action) {
        setAction(action);
    }
    
    ////////////////////
    // ActionListener //
    ////////////////////
    
    /**
     * Invokes the <CODE>actionPerformed</CODE> method 
     * of the encapsulated action 
     * in a newly created separate thread 
     * that is executed as a daemon thread.
     *
     * @param event the event that invoked this action
     */
    public void actionPerformed(ActionEvent event) {
        // if there is no action then exit
        if (action == null)
            return;
        
        // define the new thread
        final ActionEvent eventcopy  = event;
        final Action      actioncopy = action;
        
        Thread thread = new Thread() {
            public void run() {
                actioncopy.actionPerformed(eventcopy);
            }
        };
        
        // run the thread
        thread.setDaemon(true);
        thread.start();
    }
}
