001    /*****************************************************************************
002    
003        jBudget.
004        Copyright (C)  Dmitri Petrov
005    
006        This program is free software: you can redistribute it and/or modify
007        it under the terms of the GNU General Public License as published by
008        the Free Software Foundation, either version 3 of the License, or
009        (at your option) any later version.
010    
011        This program is distributed in the hope that it will be useful,
012        but WITHOUT ANY WARRANTY; without even the implied warranty of
013        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014        GNU General Public License for more details.
015    
016        You should have received a copy of the GNU General Public License
017        along with this program.  If not, see <http://www.gnu.org/licenses/>.
018     
019    *****************************************************************************/
020    
021    package org.jbudget.gui;
022    
023    import java.util.LinkedList;
024    import java.util.List;
025    import org.jbudget.Core.TransactionFilter;
026    
027    
028    /** A singleton class that is used to deliver updates to all registered
029     * components when a new TransactionFilter is selected. 
030     * 
031     * @author petrov
032     */
033    public class FilterChangeDispatcher implements FilterChangeListener
034    {
035        //=========================== Data ===============================//
036        
037        private final List<FilterChangeListener> 
038            listeners = new LinkedList<FilterChangeListener>();
039        
040        static private final FilterChangeDispatcher 
041            instance = new FilterChangeDispatcher();
042        
043        /** Constructor is private. Use getInstance() to get an instance of this
044         * class. */
045        private FilterChangeDispatcher()
046        {
047        }
048        
049        
050        //=========================== Methods ===============================//
051        
052        /** Get an instance of the FilterChangeDispatcer class. */
053        static public FilterChangeDispatcher getInstance() {
054            return instance;
055        }
056        
057        /** Register listener. */
058        public void addListener(FilterChangeListener listener) {
059            if (listeners.contains(listener))
060                return;
061            listeners.add(listener);
062        }
063        
064        /** Removes a listener. */
065        public void removeListener(FilterChangeListener listener){
066            listeners.remove(listener);
067        }
068        
069        /** Transmits the new filter information to all registered listeners. */
070        public void filterUpdated(TransactionFilter newFilter, Object initiator)
071        {
072            for (FilterChangeListener listener : listeners)
073                listener.filterUpdated(newFilter, initiator);
074        }
075    }