/*
 * @(#)ListSelectionActionAdapter.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.beans.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * <P>An adapter class for receiving list selection events, and 
 * performing action listeners in response to those events.</P>
 * 
 * @author  Jeff Raab
 * @author  Richard Rasala
 * @version 2.3.3
 * @since   2.1
 */
public class ListSelectionActionAdapter 
    implements ListSelectionListener, Cloneable, Serializable
{
    /** 
     * List of action listeners to be performed upon the
     * notification of list selection events. 
     */
    protected ActionSequence selectionActions = new ActionSequence();
    
    
    //////////////////
    // Constructors //
    //////////////////
    
    /**
     * Constructs a list selection action adapter.
     */
    public ListSelectionActionAdapter() {
        this(null);
    }
    
    
    /**
     * Constructs a list selection action adapter
     * listening for list selection events
     * generated by the given list selection model.
     *
     * If <CODE>null</CODE>, this adapter does not listen
     * to a particular list selection model by default.
     *
     * @param model the list selection model to listen to
     */
    public ListSelectionActionAdapter(ListSelectionModel model) {
        if (model != null)
            model.addListSelectionListener(this);
    }
    
    
    ///////////////////////////
    // ListSelectionListener //
    ///////////////////////////
    
    /**
     * Performs the stored action listener sequence 
     * when a list selection has been made.
     *
     * @param evt the list selection event to be handled
     */
    public final void valueChanged(ListSelectionEvent evt) {
        selectionActions.actionPerformed(
            new ListSelectionActionEvent(
                evt,
                evt.getSource()));
    }
    
    
    ////////////////
    // Public API //
    ////////////////
    
    /**
     * Adds an action listener to this adapter
     * so that it will be performed
     * when a list selection is made.
     *
     * @param a the action listener to be performed
     * @see #removeListSelectionAction(ActionListener)
     * @see #setListSelectionActions(ActionSequence)
     * @see #getListSelectionActions()
     */
    public void addListSelectionAction(ActionListener a) {
        selectionActions.add(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when a list selection is made.
     *
     * @param a the action listener to be removed
     * @see #addListSelectionAction(ActionListener)
     * @see #setListSelectionActions(ActionSequence)
     * @see #getListSelectionActions()
     */
    public void removeListSelectionAction(ActionListener a) {
        selectionActions.remove(a);
    }
    
    
    /**
     * Sets the action sequence for list selection events
     * to the provided action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for list selection events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getListSelectionActions()
     * @see #addListSelectionAction(ActionListener)
     * @see #removeListSelectionAction(ActionListener)
     */
    public void setListSelectionActions(ActionSequence sequence) {
        if (sequence == null)
            selectionActions.clear();
        else
            selectionActions = sequence;
    }
    
    
    /**
     * Returns the action sequence for list selection events.
     *
     * @see #setListSelectionActions(ActionSequence)
     * @see #addListSelectionAction(ActionListener)
     * @see #removeListSelectionAction(ActionListener)
     */
    public ActionSequence getListSelectionActions() {
        return selectionActions;
    }
    
}
