/*
 * @(#)FocusActionAdapter.java    2.3.3   6 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 java.util.*;
import javax.swing.*;

/**
 * <P>An adapter class for receiving focus 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 FocusActionAdapter 
    implements FocusListener, Cloneable, Serializable
{
    /** 
     * List of action listeners to be performed upon the
     * notification of focus gained events. 
     */
    protected ActionSequence gainedActions = new ActionSequence();
    
    /**
     * List of action listeners to be performed upon the
     * notification of focus lost events.
     */
    protected ActionSequence lostActions = new ActionSequence();
    
    
    //////////////////
    // Constructors //
    //////////////////
    
    /**
     * Constructs a focus action adapter.
     */
    public FocusActionAdapter() {
        this(null);
    }
    
    
    /**
     * Constructs a focus action adapter
     * listening for focus events
     * generated by the given component.
     *
     * If <CODE>null</CODE>, this adapter does not listen
     * to a particular component by default.
     *
     * @param target the component that is listened to
     */
    public FocusActionAdapter(Component target) {
        if (target != null)
            target.addFocusListener(this);
    }
    
    
    ///////////////////
    // FocusListener //
    ///////////////////

    /**
     * Performs the stored action sequence 
     * when the focus has been gained.
     *
     * @param evt the focus gained event to be handled
     */
    public final void focusGained(FocusEvent evt) {
        gainedActions.actionPerformed(
            new FocusActionEvent(
                evt,
                evt.getSource()));
    }
    
    
    /**
     * Performs the stored action sequence 
     * when the focus has been lost.
     *
     * @param evt the focus lost event to be handled
     */
    public final void focusLost(FocusEvent evt) {
        lostActions.actionPerformed(
            new FocusActionEvent(
                evt,
                evt.getSource()));
    }
    
    
    ////////////////
    // Public API //
    ////////////////
    
    /**
     * Adds an action listener to this adapter
     * so that it will be performed
     * when the focus is gained.
     *
     * @param a the action listener to be performed
     * @see #removeFocusGainedAction(ActionListener)
     * @see #setFocusGainedActions(ActionSequence)
     * @see #getFocusGainedActions()
     */
    public void addFocusGainedAction(ActionListener a) {
        gainedActions.add(a);
    }
    
    
    /**
     * Adds an action listener to this adapter
     * so that it will be performed
     * when the focus is lost.
     *
     * @param a the action listener to be performed
     * @see #removeFocusLostAction(ActionListener)
     * @see #setFocusLostActions(ActionSequence)
     * @see #getFocusLostActions()
     */
    public void addFocusLostAction(ActionListener a) {
        lostActions.add(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the focus is gained.
     *
     * @param a the action listener to be removed
     * @see #addFocusGainedAction(ActionListener)
     * @see #setFocusGainedActions(ActionSequence)
     * @see #getFocusGainedActions()
     */
    public void removeFocusGainedAction(ActionListener a) {
        gainedActions.remove(a);
    }
    
    
    /**
     * Removes the given action listener from this adapter
     * so that it will be no longer be performed 
     * when the focus is lost.
     *
     * @param a the action listener to be removed
     * @see #addFocusLostAction(ActionListener)
     * @see #setFocusLostActions(ActionSequence)
     * @see #getFocusLostActions()
     */
    public void removeFocusLostAction(ActionListener a) {
        lostActions.remove(a);
    }
    
    
    /**
     * Sets the action sequence for focus gained events
     * to the provided action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for focus gained events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getFocusGainedActions()
     * @see #addFocusGainedAction(ActionListener)
     * @see #removeFocusGainedAction(ActionListener)
     */
    public void setFocusGainedActions(ActionSequence sequence) {
        if (sequence == null)
            gainedActions.clear();
        else
            gainedActions = sequence;
    }
    
    
    /**
     * Sets the action sequence for focus lost events
     * to the provided action sequence.
     *
     * If <CODE>null</CODE>, the action sequence
     * for focus lost events is cleared.
     *
     * @param sequence the desired action sequence
     * @see #getFocusLostActions()
     * @see #addFocusLostAction(ActionListener)
     * @see #removeFocusLostAction(ActionListener)
     */
    public void setFocusLostActions(ActionSequence sequence) {
        if (sequence == null)
            lostActions.clear();
        else
            lostActions = sequence;
    }
    
    
    /**
     * Returns the action sequence for focus gained events.
     *
     * @see #setFocusGainedActions(ActionSequence)
     * @see #addFocusGainedAction(ActionListener)
     * @see #removeFocusGainedAction(ActionListener)
     */
    public ActionSequence getFocusGainedActions() {
        return gainedActions;
    }
    
    
    /**
     * Returns the action sequence for focus lost events.
     *
     * @see #setFocusLostActions(ActionSequence)
     * @see #addFocusLostAction(ActionListener)
     * @see #removeFocusLostAction(ActionListener)
     */
    public ActionSequence getFocusLostActions() {
        return lostActions;
    }
    
}
