/*
 * @(#)WindowActionAdapter.java    2.3.3   2 May 2006
 *
 * Copyright 2006
 * 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.*;
import java.awt.event.*;
import java.io.Serializable;
import javax.swing.*;
import javax.swing.event.*;

/**
 * <P>An adapter class for receiving window events
 * and performing actions in response to those events.</P>
 *
 * @author  Jeff Raab
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   1.0.1
 */
public class WindowActionAdapter
    implements WindowListener, Cloneable, Serializable 
{
    /** 
     * List of actions to be performed upon the notification
     * of window activated events. 
     */
    protected ActionSequence activatedActions = new ActionSequence();
    
    /** 
     * List of actions to be performed upon the notification
     * of window closed events. 
     */
    protected ActionSequence closedActions = new ActionSequence();
    
    /** 
     * List of actions to be performed upon the notification
     * of window closing events. 
     */
    protected ActionSequence closingActions = new ActionSequence();
    
    /** 
     * List of actions to be performed upon the notification
     * of window deactivated events. 
     */
    protected ActionSequence deactivatedActions = new ActionSequence();
    
    /** 
     * List of actions to be performed upon the notification
     * of window deiconified events. 
     */
    protected ActionSequence deiconifiedActions = new ActionSequence();
    
    /** 
     * List of actions to be performed upon the notification
     * of window iconified events. 
     */
    protected ActionSequence iconifiedActions = new ActionSequence();
    
    /** 
     * List of actions to be performed upon the notification
     * of window opened events. 
     */
    protected ActionSequence openedActions = new ActionSequence();
    
    
    //////////////////
    // Constructors //
    //////////////////
    
    /**
     * Constructs a window action adapter.
     */
    public WindowActionAdapter() {
        this(null);
    }
    
    
    /**
     * Constructs a window action adapter
     * listening for window events
     * generated by the given window.
     *
     * If <CODE>null</CODE>, this adapter does not listen
     * to a particular window by default.
     *
     * @param target the window that is listened to
     */
    public WindowActionAdapter(Window target) {
        target.addWindowListener(this);
    }
    
    
    ////////////////////
    // WindowListener //
    ////////////////////
    
    /**
     * Performs the stored action sequence 
     * when the window is activated.
     *
     * @param evt the window event
     * @see #addWindowActivatedAction(ActionListener)
     * @see #getWindowActivatedActions()
     */
    public final void windowActivated(WindowEvent evt) {
        activatedActions.actionPerformed(
            new WindowActionEvent(evt, evt.getWindow()));
    }
    
    
    /**
     * Performs the stored action sequence 
     * when the window is closed.
     *
     * @param evt the window event
     * @see #addWindowClosedAction(ActionListener)
     * @see #getWindowClosedActions()
     */
    public final void windowClosed(WindowEvent evt) {
        closedActions.actionPerformed(
            new WindowActionEvent(evt, evt.getWindow()));
    }
    
    
    /**
     * Performs the stored action sequence 
     * when the window is closing.
     *
     * @param evt the window event
     * @see #addWindowClosingAction(ActionListener)
     * @see #getWindowClosingActions()
     */
    public final void windowClosing(WindowEvent evt) {
        closingActions.actionPerformed(
            new WindowActionEvent(evt, evt.getWindow()));
    }
    
    
    /**
     * Performs the stored action sequence 
     * when the window is deactivated.
     *
     * @param evt the window event
     * @see #addWindowDeactivatedAction(ActionListener)
     * @see #getWindowDeactivatedActions()
     */
    public final void windowDeactivated(WindowEvent evt) {
        deactivatedActions.actionPerformed(
            new WindowActionEvent(evt, evt.getWindow()));
    }
    
    
    /**
     * Performs the stored action sequence 
     * when the window is deiconified.
     *
     * @param evt the window event
     * @see #addWindowDeiconifiedAction(ActionListener)
     * @see #getWindowDeiconifiedActions()
     */
    public final void windowDeiconified(WindowEvent evt) {
        deiconifiedActions.actionPerformed(
            new WindowActionEvent(evt, evt.getWindow()));
    }
    
    
    /**
     * Performs the stored action sequence 
     * when the window is iconified.
     *
     * @param evt the window event
     * @see #addWindowIconifiedAction(ActionListener)
     * @see #getWindowIconifiedActions()
     */
    public final void windowIconified(WindowEvent evt) {
        iconifiedActions.actionPerformed(
            new WindowActionEvent(evt, evt.getWindow()));
    }
    
    
    /**
     * Performs the stored action sequence 
     * when the window is opened.
     *
     * @param evt the window event
     * @see #addWindowOpenedAction(ActionListener)
     * @see #getWindowOpenedActions()
     */
    public final void windowOpened(WindowEvent evt) {
        openedActions.actionPerformed(
            new WindowActionEvent(evt, evt.getWindow()));
    }
    
    
    ////////////////
    // Public API //
    ////////////////
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the window is activated.
     *
     * @param a the desired action listener
     * @see #removeWindowActivatedAction(ActionListener)
     * @see #setWindowActivatedActions(ActionSequence)
     * @see #getWindowActivatedActions()
     */
    public void addWindowActivatedAction(ActionListener a) {
        activatedActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the window is closed.
     *
     * @param a the desired action listener
     * @see #removeWindowClosedAction(ActionListener)
     * @see #setWindowClosedActions(ActionSequence)
     * @see #getWindowClosedActions()
     */
    public void addWindowClosedAction(ActionListener a) {
        closedActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the window is closing.
     *
     * @param a the desired action listener
     * @see #removeWindowClosingAction(ActionListener)
     * @see #setWindowClosingActions(ActionSequence)
     * @see #getWindowClosingActions()
     */
    public void addWindowClosingAction(ActionListener a) {
        closingActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the window is deactivated.
     *
     * @param a the desired action listener
     * @see #removeWindowDeactivatedAction(ActionListener)
     * @see #setWindowDeactivatedActions(ActionSequence)
     * @see #getWindowDeactivatedActions()
     */
    public void addWindowDeactivatedAction(ActionListener a) {
        deactivatedActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the window is deiconified.
     *
     * @param a the desired action listener
     * @see #removeWindowDeiconifiedAction(ActionListener)
     * @see #setWindowDeiconifiedActions(ActionSequence)
     * @see #getWindowDeiconifiedActions()
     */
    public void addWindowDeiconifiedAction(ActionListener a) {
        deiconifiedActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the window is iconified.
     *
     * @param a the desired action listener
     * @see #removeWindowIconifiedAction(ActionListener)
     * @see #setWindowIconifiedActions(ActionSequence)
     * @see #getWindowIconifiedActions()
     */
    public void addWindowIconifiedAction(ActionListener a) {
        iconifiedActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the window is opened.
     *
     * @param a the desired action listener
     * @see #removeWindowOpenedAction(ActionListener)
     * @see #setWindowOpenedActions(ActionSequence)
     * @see #getWindowOpenedActions()
     */
    public void addWindowOpenedAction(ActionListener a) {
        openedActions.add(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the window is activated.
     *
     * @param a the action listener to be removed
     * @see #addWindowActivatedAction(ActionListener)
     * @see #setWindowActivatedActions(ActionSequence)
     * @see #getWindowActivatedActions()
     */
    public void removeWindowActivatedAction(ActionListener a) {
        activatedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the window is closed.
     *
     * @param a the action listener to be removed
     * @see #addWindowClosedAction(ActionListener)
     * @see #setWindowClosedActions(ActionSequence)
     * @see #getWindowClosedActions()
     */
    public void removeWindowClosedAction(ActionListener a) {
        closedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the window is closing.
     *
     * @param a the action listener to be removed
     * @see #addWindowClosingAction(ActionListener)
     * @see #setWindowClosingActions(ActionSequence)
     * @see #getWindowClosingActions()
     */
    public void removeWindowClosingAction(ActionListener a) {
        closingActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the window is deactivated.
     *
     * @param a the action listener to be removed
     * @see #addWindowDeactivatedAction(ActionListener)
     * @see #setWindowDeactivatedActions(ActionSequence)
     * @see #getWindowDeactivatedActions()
     */
    public void removeWindowDeactivatedAction(ActionListener a) {
        deactivatedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the window is deiconified.
     *
     * @param a the action listener to be removed
     * @see #addWindowDeiconifiedAction(ActionListener)
     * @see #setWindowDeiconifiedActions(ActionSequence)
     * @see #getWindowDeiconifiedActions()
     */
    public void removeWindowDeiconifiedAction(ActionListener a) {
        deiconifiedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the window is iconified.
     *
     * @param a the action listener to be removed
     * @see #addWindowIconifiedAction(ActionListener)
     * @see #setWindowIconifiedActions(ActionSequence)
     * @see #getWindowIconifiedActions()
     */
    public void removeWindowIconifiedAction(ActionListener a) {
        iconifiedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the window is opened.
     *
     * @param a the action listener to be removed
     * @see #addWindowOpenedAction(ActionListener)
     * @see #setWindowOpenedActions(ActionSequence)
     * @see #getWindowOpenedActions()
     */
    public void removeWindowOpenedAction(ActionListener a) {
        openedActions.remove(a);
    }
    
    
    /**
     * Sets the action sequence for window activated events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for window activated events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getWindowActivatedActions()
     * @see #addWindowActivatedAction(ActionListener)
     * @see #removeWindowActivatedAction(ActionListener)
     */
    public void setWindowActivatedActions(ActionSequence sequence) {
        if (sequence == null)
            activatedActions.clear();
        else
            activatedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for window closed events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for window closed events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getWindowClosedActions()
     * @see #addWindowClosedAction(ActionListener)
     * @see #removeWindowClosedAction(ActionListener)
     */
    public void setWindowClosedActions(ActionSequence sequence) {
        if (sequence == null)
            closedActions.clear();
        else
            closedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for window closing events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for window closing events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getWindowClosingActions()
     * @see #addWindowClosingAction(ActionListener)
     * @see #removeWindowClosingAction(ActionListener)
     */
    public void setWindowClosingActions(ActionSequence sequence) {
        if (sequence == null)
            closingActions.clear();
        else
            closingActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for window deactivated events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for window deactivated events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getWindowDeactivatedActions()
     * @see #addWindowDeactivatedAction(ActionListener)
     * @see #removeWindowDeactivatedAction(ActionListener)
     */
    public void setWindowDeactivatedActions(ActionSequence sequence) {
        if (sequence == null)
            deactivatedActions.clear();
        else
            deactivatedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for window deiconified events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for window deiconified events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getWindowDeiconifiedActions()
     * @see #addWindowDeiconifiedAction(ActionListener)
     * @see #removeWindowDeiconifiedAction(ActionListener)
     */
    public void setWindowDeiconifiedActions(ActionSequence sequence) {
        if (sequence == null)
            deiconifiedActions.clear();
        else
            deiconifiedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for window iconified events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for window iconified events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getWindowIconifiedActions()
     * @see #addWindowIconifiedAction(ActionListener)
     * @see #removeWindowIconifiedAction(ActionListener)
     */
    public void setWindowIconifiedActions(ActionSequence sequence) {
        if (sequence == null)
            iconifiedActions.clear();
        else
            iconifiedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for window opened events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for window opened events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getWindowOpenedActions()
     * @see #addWindowOpenedAction(ActionListener)
     * @see #removeWindowOpenedAction(ActionListener)
     */
    public void setWindowOpenedActions(ActionSequence sequence) {
        if (sequence == null)
            openedActions.clear();
        else
            openedActions = sequence;
    }
    
    
    /**
     * Returns the action sequence for window activated events.
     *
     * @see #setWindowActivatedActions(ActionSequence)
     * @see #addWindowActivatedAction(ActionListener)
     * @see #removeWindowActivatedAction(ActionListener)
     */
    public ActionSequence getWindowActivatedActions() {
        return activatedActions;
    }
    
    
    /**
     * Returns the action sequence for window closed events.
     *
     * @see #setWindowClosedActions(ActionSequence)
     * @see #addWindowClosedAction(ActionListener)
     * @see #removeWindowClosedAction(ActionListener)
     */
    public ActionSequence getWindowClosedActions() {
        return closedActions;
    }
    
    
    /**
     * Returns the action sequence for window closing events.
     *
     * @see #setWindowClosingActions(ActionSequence)
     * @see #addWindowClosingAction(ActionListener)
     * @see #removeWindowClosingAction(ActionListener)
     */
    public ActionSequence getWindowClosingActions() {
        return closingActions;
    }
    
    
    /**
     * Returns the action sequence for window deactivated events.
     *
     * @see #setWindowDeactivatedActions(ActionSequence)
     * @see #addWindowDeactivatedAction(ActionListener)
     * @see #removeWindowDeactivatedAction(ActionListener)
     */
    public ActionSequence getWindowDeactivatedActions() {
        return deactivatedActions;
    }
    
    
    /**
     * Returns the action sequence for window deiconified events.
     *
     * @see #setWindowDeiconifiedActions(ActionSequence)
     * @see #addWindowDeiconifiedAction(ActionListener)
     * @see #removeWindowDeiconifiedAction(ActionListener)
     */
    public ActionSequence getWindowDeiconifiedActions() {
        return deiconifiedActions;
    }
    
    
    /**
     * Returns the action sequence for window iconified events.
     *
     * @see #setWindowIconifiedActions(ActionSequence)
     * @see #addWindowIconifiedAction(ActionListener)
     * @see #removeWindowIconifiedAction(ActionListener)
     */
    public ActionSequence getWindowIconifiedActions() {
        return iconifiedActions;
    }
    
    
    /**
     * Returns the action sequence for window opened events.
     *
     * @see #setWindowOpenedActions(ActionSequence)
     * @see #addWindowOpenedAction(ActionListener)
     * @see #removeWindowOpenedAction(ActionListener)
     */
    public ActionSequence getWindowOpenedActions() {
        return openedActions;
    }
    
}
