/*
 * @(#)ComponentActionAdapter.java    2.3.3  3 January 2005
 *
 * Copyright 2005
 * 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 component events
 * and performing actions in response to those events.</P>
 *
 * @author  Jeff Raab
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   2.2
 */
public class ComponentActionAdapter
    implements ComponentListener, Cloneable, Serializable 
{
    /** 
     * List of action listeners to be performed upon the
     * notification of component hidden events. 
     */
    protected ActionSequence hiddenActions = new ActionSequence();
    
    /** 
     * List of action listeners to be performed upon the
     * notification of component moved events. 
     */
    protected ActionSequence movedActions = new ActionSequence();
    
    /** 
     * List of action listeners to be performed upon the
     * notification of component resized events. 
     */
    protected ActionSequence resizedActions = new ActionSequence();
    
    /** 
     * List of action listeners to be performed upon the
     * notification of component shown events. 
     */
    protected ActionSequence shownActions = new ActionSequence();
    
    
    //////////////////
    // Constructors //
    //////////////////
    
    /**
     * Constructs a component action adapter.
     */
    public ComponentActionAdapter() {
        this(null);
    }
    
    
    /**
     * Constructs a component action adapter
     * listening for component events
     * generated by the given component.
     *
     * If <CODE>null</CODE>, this adapter does not listen
     * to a particular component by default.
     *
     * @param source the source component to listen to
     */
    public ComponentActionAdapter(Component target) {
        if (target == null)
            return;
            
        target.addComponentListener(this);
    }
    
    
    ///////////////////////
    // ComponentListener //
    ///////////////////////
    
    /**
     * Performs the stored action listener sequence 
     * when the component is hidden.
     *
     * @param evt the component event
     * @see #addComponentHiddenAction(ActionListener)
     * @see #getComponentHiddenActions()
     */
    public final void componentHidden(ComponentEvent evt) {
        hiddenActions.actionPerformed(
            new ComponentActionEvent(evt, evt.getSource()));
    }
    
    
    /**
     * Performs the stored action listener sequence 
     * when the component enters the component.
     *
     * @param evt the component event
     * @see #addComponentMovedAction(ActionListener)
     * @see #getComponentMovedActions()
     */
    public final void componentMoved(ComponentEvent evt) {
        movedActions.actionPerformed(
            new ComponentActionEvent(evt, evt.getSource()));
    }
    
    
    /**
     * Performs the stored action listener sequence 
     * when the component exits the component.
     *
     * @param evt the component event
     * @see #addComponentResizedAction(ActionListener)
     * @see #getComponentResizedActions()
     */
    public final void componentResized(ComponentEvent evt) {
        resizedActions.actionPerformed(
            new ComponentActionEvent(evt, evt.getSource()));
    }
    
    
    /**
     * Performs the stored action listener sequence 
     * when a component button is shown.
     *
     * @param evt the component event
     * @see #addComponentShownAction(ActionListener)
     * @see #getComponentShownActions()
     */
    public final void componentShown(ComponentEvent evt) {
        shownActions.actionPerformed(
            new ComponentActionEvent(evt, evt.getSource()));
    }
    
    
    ////////////////
    // Public API //
    ////////////////
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed
     * when the component is hidden.
     *
     * @param a the desired action listener
     * @see #removeComponentHiddenAction(ActionListener)
     * @see #setComponentHiddenActions(ActionSequence)
     * @see #getComponentHiddenActions()
     */
    public void addComponentHiddenAction(ActionListener a) {
        hiddenActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the component is moved.
     *
     * @param a the desired action listener
     * @see #removeComponentMovedAction(ActionListener)
     * @see #setComponentMovedActions(ActionSequence)
     * @see #getComponentMovedActions()
     */
    public void addComponentMovedAction(ActionListener a) {
        movedActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the component is resized.
     *
     * @param a the desired action listener
     * @see #removeComponentResizedAction(ActionListener)
     * @see #setComponentResizedActions(ActionSequence)
     * @see #getComponentResizedActions()
     */
    public void addComponentResizedAction(ActionListener a) {
        resizedActions.add(a);
    }
    
    
    /**
     * Adds the given action listener to the action sequence 
     * so that it will be performed 
     * when the component is shown.
     *
     * @param a the desired action listener
     * @see #removeComponentShownAction(ActionListener)
     * @see #setComponentShownActions(ActionSequence)
     * @see #getComponentShownActions()
     */
    public void addComponentShownAction(ActionListener a) {
        shownActions.add(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the component is hidden.
     *
     * @param a the action listener to be removed
     * @see #addComponentHiddenAction(ActionListener)
     * @see #setComponentHiddenActions(ActionSequence)
     * @see #getComponentHiddenActions()
     */
    public void removeComponentHiddenAction(ActionListener a) {
        hiddenActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the component is moved.
     *
     * @param a the action to be removed
     * @see #addComponentMovedAction(ActionListener)
     * @see #setComponentMovedActions(ActionSequence)
     * @see #getComponentMovedActions()
     */
    public void removeComponentMovedAction(ActionListener a) {
        movedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the component is resized.
     *
     * @param a the action listener to be removed
     * @see #addComponentResizedAction(ActionListener)
     * @see #setComponentResizedActions(ActionSequence)
     * @see #getComponentResizedActions()
     */
    public void removeComponentResizedAction(ActionListener a) {
        resizedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the component is shown.
     *
     * @param a the action to be removed
     * @see #addComponentShownAction(ActionListener)
     * @see #setComponentShownActions(ActionSequence)
     * @see #getComponentShownActions()
     */
    public void removeComponentShownAction(ActionListener a) {
        shownActions.remove(a);
    }
    
    
    /**
     * Sets the action sequence for component hidden events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for component hidden events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getComponentHiddenActions()
     * @see #addComponentHiddenAction(ActionListener)
     * @see #removeComponentHiddenAction(ActionListener)
     */
    public void setComponentHiddenActions(ActionSequence sequence) {
        if (sequence == null)
            hiddenActions.clear();
        else
            hiddenActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for component moved events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for component moved events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getComponentMovedActions()
     * @see #addComponentMovedAction(ActionListener)
     * @see #removeComponentMovedAction(ActionListener)
     */
    public void setComponentMovedActions(ActionSequence sequence) {
        if (sequence == null)
            movedActions.clear();
        else
            movedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for component resized events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for component resized events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #setComponentResizedActions(ActionSequence)
     * @see #addComponentResizedAction(ActionListener)
     * @see #removeComponentResizedAction(ActionListener)
     */
    public void setComponentResizedActions(ActionSequence sequence) {
        if (sequence == null)
            resizedActions.clear();
        else
            resizedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for component shown events
     * to the given action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for component shown events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getComponentShownActions()
     * @see #addComponentShownAction(ActionListener)
     * @see #removeComponentShownAction(ActionListener)
     */
    public void setComponentShownActions(ActionSequence sequence) {
        if (sequence == null)
            shownActions.clear();
        else
            shownActions = sequence;
    }
    
    
    /**
     * Returns the action sequence for component hidden events.
     *
     * @see #setComponentHiddenActions(ActionSequence)
     * @see #addComponentHiddenAction(ActionListener)
     * @see #removeComponentHiddenAction(ActionListener)
     */
    public ActionSequence getComponentHiddenActions() {
        return hiddenActions;
    }
    
    
    /**
     * Returns the action sequence for component moved events.
     *
     * @see #setComponentMovedActions(ActionSequence)
     * @see #addComponentMovedAction(ActionListener)
     * @see #removeComponentMovedAction(ActionListener)
     */
    public ActionSequence getComponentMovedActions() {
        return movedActions;
    }
    
    
    /**
     * Returns the action sequence for component resized events.
     *
     * @see #setComponentResizedActions(ActionSequence)
     * @see #addComponentResizedAction(ActionListener)
     * @see #removeComponentResizedAction(ActionListener)
     */
    public ActionSequence getComponentResizedActions() {
        return resizedActions;
    }
    
    
    /**
     * Returns the action sequence for component shown events.
     *
     * @see #setComponentShownActions(ActionSequence)
     * @see #addComponentShownAction(ActionListener)
     * @see #removeComponentShownAction(ActionListener)
     */
    public ActionSequence getComponentShownActions() {
        return shownActions;
    }
    
}
