001package votorola.g.web.gwt.event; // Copyright 2011, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import com.google.gwt.core.client.Scheduler;
004import com.google.gwt.event.shared.GwtEvent;
005import com.google.gwt.event.shared.HasHandlers;
006import java.util.*;
007
008
009/** A scheduler for the delayed dispatch of events.  Use it to both delay the dispatch of
010  * events and to group multiple events into a single, atomic dispatch.
011  */
012public class DelayedEventGun extends CoalescingSchedulerS implements Scheduler.ScheduledCommand
013{
014
015
016    /** Constructs a new DelayedEventGun.
017      *
018      *     @see #source()
019      *     @see #phaser()
020      */
021    public DelayedEventGun( HasHandlers _source, Phaser _phaser )
022    {
023        super( Scheduler.get(), _phaser );
024        source = _source;
025        init( /*command*/DelayedEventGun.this );
026    }
027
028
029
030   // ------------------------------------------------------------------------------------
031
032
033    /** Immediately dispatches all scheduled events.
034      */
035    public final void flush() { execute(); }
036
037
038
039    /** Adds the specified event to the dispatch list and ensures that the dispatcher is
040      * scheduled to execute.
041      *
042      *     @see #schedule()
043      */
044    public void schedule( final GwtEvent<?> e )
045    {
046        eList.add( e );
047        schedule();
048    }
049
050
051
052    /** The source from which the events are to be fired.
053      */
054    public final HasHandlers source() { return source; }
055
056
057        private final HasHandlers source;
058
059
060
061   // - S c h e d u l e r . S c h e d u l e d - C o m m a n d ----------------------------
062
063
064    public final void execute()
065    {
066        final GwtEvent<?>[] events = eList.toArray( new GwtEvent<?>[eList.size()] );
067        eList.clear(); // atomic copy/clear, simpler than concurrent modification via iterator
068        for( final GwtEvent<?> e: events ) source.fireEvent( e );
069    }
070
071
072
073//// P r i v a t e ///////////////////////////////////////////////////////////////////////
074
075
076    private final ArrayList<GwtEvent<?>> eList = new ArrayList<GwtEvent<?>>( /*initial capacity*/4 );
077
078
079}