/*
 * @(#)NumericFilter.java    1.0  9 February 2001
 *
 * Copyright 2004
 * 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.filter;

import edu.neu.ccs.*;
import java.beans.*;
import java.io.Serializable;
import java.text.ParseException;
import javax.swing.event.SwingPropertyChangeSupport;

/**
 * <P>Abstract superclass for filters
 * that apply to numeric values.</P>
 *
 * @author  Jeff Raab
 * @version 2.2
 * @since   1.0
 */
public abstract class NumericFilter 
    implements StringableFilter, Cloneable, Serializable 
{
    /** Helper object for property change API. */
    protected SwingPropertyChangeSupport changeAdapter =
        new SwingPropertyChangeSupport(this);
    
    //////////////////
    // Constructors //
    //////////////////
    
    /**
     * Constructs a numeric filter.
     */
    public NumericFilter() {}

    //////////////////////
    // StringableFilter //
    //////////////////////
    
    /**
     * Returns the given object if it is 
     * an instance of type <CODE>XNumber</CODE>.
     *
     * @param obj the object to be filtered
     * @throws FilterException if the object
     *      is not a numeric type.
     */
    public Stringable filterStringable(Stringable obj) throws FilterException {
        if (obj instanceof XNumber)
            return obj;
        
        throw new FilterException(
            obj, "Not a number: " + obj.toString());
    }

    ////////////////
    // Public API //
    ////////////////

    /**
     * Registers the given object 
     * to listen for property change events 
     * generated by this object.
     *
     * @param listener the listener to be registered
     */
    public void addPropertyChangeListener(
        PropertyChangeListener listener) 
    {
        changeAdapter.addPropertyChangeListener(listener);
    }

    /**
     * Registers the given object 
     * to listen for property change events
     * generated by this object
     * with the given property name.
     *
     * @param propertyName the name of the desired property 
     * @param listener the listener to be registered
     */
    public void addPropertyChangeListener(
        String propertyName,
        PropertyChangeListener listener) 
    {
        changeAdapter.addPropertyChangeListener(
            propertyName, 
            listener);
    }

    /**
     * Deregisters the given object 
     * from listening for property change events 
     * generated by this object.
     *
     * @param listener the listener to be deregistered
     */
    public void removePropertyChangeListener(
        PropertyChangeListener listener) 
    {
        changeAdapter.removePropertyChangeListener(listener);
    }

    /**
     * Deregisters the given object 
     * from listening for property change events 
     * generated by this object
     * with the provided property name.
     *
     * @param propertyName the name of the desired property 
     * @param listener the listener to be deregistered
     */
    public void removePropertyChangeListener(
        String propertyName,
        PropertyChangeListener listener) 
    {
        changeAdapter.removePropertyChangeListener(
            propertyName, 
            listener);
    }
}
