001    package de.mospace.swing.event;
002    
003    /* This class is part of the de.mospace.swing library.
004     * Copyright (C) 2006 Moritz Ringler
005     * $Id: Aggregator.java 12 2007-07-27 09:48:53Z ringler $
006     *
007     * This program is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU General Public License
009     * as published by the Free Software Foundation; either version 2
010     * of the License, or (at your option) any later version.
011     *
012     * This program is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * along with this program; if not, write to the Free Software
019     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
020     */
021    
022    import java.util.Collections;
023    import java.util.HashSet;
024    import java.util.Iterator;
025    import java.util.Set;
026    import java.util.Timer;
027    import java.util.TimerTask;
028    import javax.swing.SwingUtilities;
029    
030    /**
031     * When the same runnable is invoked several times through an Aggregator's
032     * {@link #aggregatedInvokeLater aggregatedInvokeLater} method during a predefined delay
033     * it is executed only once. This facility can be used for example to
034     * reduce the frequency of costly GUI updates after changes to the
035     * displayed data.
036     *
037     * @author Moritz Ringler
038     * @version $Revision: 12 $ ($Date: 2007-07-27 11:48:53 +0200 (Fr, 27 Jul 2007) $)
039     */
040    public class Aggregator {
041        private final Timer timer = new Timer(true); //run as daemon
042        private final long aggDelay;
043        private final boolean reset;
044        private final Set currentTasks = Collections.synchronizedSet(new HashSet());
045    
046        private static class AggregatorTask extends TimerTask{
047            private final Runnable job;
048            private final Aggregator parent;
049    
050            public AggregatorTask(Runnable r, Aggregator parent){
051                this.parent = parent;
052                job = r;
053            }
054    
055            public void run(){
056                parent.stopAggregating(this);
057                SwingUtilities.invokeLater(job);
058            }
059    
060            public Runnable getRunnable(){
061                return job;
062            }
063        }
064    
065        /** Constructs a new aggregator with the specified delay and reset behavior.
066        * @param aggregationTime the period of time the aggregator waits for further
067        * calls to {@link #aggregatedInvokeLater} before an invoked runnable
068        * is executed.
069        * @param reset wether to reset the timer waiting for further calls to
070        * <code>aggregatedInvokeLater</code> when <code>aggregatedInvokeLater</code>
071        * is invoked a second time with the same argument during
072        * the aggregation period
073        */
074        public Aggregator(long aggregationTime, boolean reset){
075            aggDelay = aggregationTime;
076            this.reset = reset;
077        }
078    
079        /** Schedules the specified runnable for execution after the aggregation
080        * delay. When the same runnable is invoked another time during the
081        * aggregation delay it is executed only once.
082        * @param r the runnable to schedule
083        */
084        public void aggregatedInvokeLater(Runnable r){
085            /* Checks whether this runnable is already scheduled. */
086            AggregatorTask task = getTask(r);
087            /* If it is not then we schedule it now and we wait whether it is
088            * invoked a second time */
089            if (task == null){
090                startAggregating(r);
091            } else if (reset && task.cancel()){
092                /* it it is already scheduled and reset is true
093                 * then we try to stop the previously scheduled
094                 * task and schedule a new task executing r */
095                 stopAggregating(task);
096                 startAggregating(r);
097            }
098        }
099    
100        private void stopAggregating(AggregatorTask t){
101            currentTasks.remove(t);
102        }
103    
104        private void startAggregating(Runnable r){
105            AggregatorTask task = new AggregatorTask(r, this);
106            currentTasks.add(task);
107            timer.schedule(task, aggDelay);
108        }
109    
110    
111        private AggregatorTask getTask(Runnable r){
112            AggregatorTask result = null;
113            Iterator it = currentTasks.iterator();
114            while( it.hasNext() && (result == null) ){
115                AggregatorTask task = (AggregatorTask) it.next();
116                if(task.getRunnable().equals(r)){
117                    result = task;
118                }
119            }
120            return result;
121        }
122    }