All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdesktop.swingx.binding.Aggregator Maven / Gradle / Ivy

/*
 * Copyright 2009 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.jdesktop.swingx.binding;

import org.jdesktop.beans.AbstractBean;
import org.jdesktop.beansbinding.Property;
import org.jdesktop.beansbinding.PropertyStateEvent;
import org.jdesktop.beansbinding.PropertyStateListener;

/**
 * An {@code Aggregator} is a binding mechanism to convert a collection of source values into one output value. 
 * Typically, these case convert source values into {@link java.util.Collection collections}, 
 * {@link java.util.Map maps}, or arrays; however, it is possible to aggregate properties of a single object. 
 * This allows multiple input sources to create (or modify) a single object.
 * 
 * @author Karl George Schaefer
 * 
 * @param 
 *            the source value; one of the sources being aggregated
 * @param 
 *            the aggregate value; what the sources are converted to
 */
public abstract class Aggregator extends AbstractBean {
	
    private class PSL implements PropertyStateListener {

        /**
         * {@inheritDoc}
         */
        public void propertyStateChanged(PropertyStateEvent pse) {
            fireValueChanged();
        }
    }
    
    private final PropertyStateListener psl = new PSL();
    private final Class sourceType;
    private AV value;
    
    /**
     * ctor
     * @param sourceType Class of source value
     */
    protected Aggregator(Class sourceType) {
        sourceType.getClass(); // null check
        
        this.sourceType = sourceType;
    }

    private void fireValueChanged() {
        AV oldValue = getValue();
        value = aggregateSourceValues();
        firePropertyChange("value", oldValue, getValue());
    }
    
    /**
     * addSourceImpl
     * @param  Source Type
     * @param object Source object
     * @param property the Property
     */
    protected abstract  void addSourceImpl(S object, Property property);
    
    /**
     * addSource
     * @param  Source type
     * @param object Source object
     * @param property Property
     */
    public  void addSource(S object, Property property) {
        property.addPropertyStateListener(object, psl);
        
        addSourceImpl(object, property);
        
        fireValueChanged();
    }

    /**
     * removeSource
     * @param  Source type
     * @param object Source object
     * @param property Property
     */
    public  void removeSource(S object, Property property) {
        property.removePropertyStateListener(object, psl);
        fireValueChanged();
    }
    
    /**
     * aggregateSourceValues
     * @return the aggregate value
     */
    protected abstract AV aggregateSourceValues();
    
    /**
     * getter
     * @return sourceType
     */
    public final Class getSourceType() {
        return sourceType;
    }
    
    /**
     * getter
     * @return value
     */
    public final AV getValue() {
        return value;
    }
    
}