/*
 * @(#)PropertyChangeActionAdapter.java    2.3.3   4 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.beans.*;
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.*;
import javax.swing.*;

/**
 * <P>An adapter class for receiving property change events, and 
 * performing action listeners in response to those events.</P>
 * 
 * @author  Jeff Raab
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   1.0.1
 */
public class PropertyChangeActionAdapter 
    implements PropertyChangeListener, Cloneable, Serializable
{
    /** 
     * List of action listeners to be performed upon the
     * notification of property change events. 
     */
    protected ActionSequence changeActions = new ActionSequence();
    
    
    //////////////////
    // Constructors //
    //////////////////
    
    /**
     * Constructs a property change action adapter.
     */
    public PropertyChangeActionAdapter() {
        this(null);
    }
    
    
    /**
     * Constructs a property change action adapter
     * listening for property change events
     * generated by the given component.
     *
     * If <CODE>null</CODE>, this adapter does not listen
     * to a particular component by default.
     *
     * @param target the source component to listen to
     */
    public PropertyChangeActionAdapter(Component target) {
        if (target != null)
            target.addPropertyChangeListener(this);
    }
    
    
    ////////////////////////////
    // PropertyChangeListener //
    ////////////////////////////
    
    /**
     * Performs the stored action sequence 
     * when a property has been changed.
     *
     * @param evt the property change event to be handled
     */
    public final void propertyChange(PropertyChangeEvent evt) {
        changeActions.actionPerformed(
            new PropertyChangeActionEvent(
                evt,
                evt.getSource()));
    }
    
    
    ////////////////
    // Public API //
    ////////////////
    
    /**
     * Adds an action listener to this adapter
     * so that it will be performed
     * when a property is changed.
     *
     * @param a the action listener to be performed
     * @see #removePropertyChangeAction(ActionListener)
     * @see #setPropertyChangeActions(ActionSequence)
     * @see #getPropertyChangeActions()
     */
    public void addPropertyChangeAction(ActionListener a) {
        changeActions.add(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when a property is changed.
     *
     * @param a the action listener to be removed
     * @see #addPropertyChangeAction(ActionListener)
     * @see #setPropertyChangeActions(ActionSequence)
     * @see #getPropertyChangeActions()
     */
    public void removePropertyChangeAction(ActionListener a) {
        changeActions.remove(a);
    }
    
    
    /**
     * Sets the action sequence for property change events
     * to the provided action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for property change events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getPropertyChangeActions()
     * @see #addPropertyChangeAction(ActionListener)
     * @see #removePropertyChangeAction(ActionListener)
     */
    public void setPropertyChangeActions(ActionSequence sequence) {
        if (sequence == null)
            changeActions.clear();
        else
            changeActions = sequence;
    }
    
    
    /**
     * Returns the action sequence for property change events.
     *
     * @see #setPropertyChangeActions(ActionSequence)
     * @see #addPropertyChangeAction(ActionListener)
     * @see #removePropertyChangeAction(ActionListener)
     */
    public ActionSequence getPropertyChangeActions() {
        return changeActions;
    }
    
}
