package votorola.g.web.gwt.event; // Copyright 2011-2012, 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. import com.google.gwt.core.client.Scheduler; /** A coalescing scheduler that works with an ordinary, non-repeating command. */ public class CoalescingSchedulerS extends CoalescingScheduler { /** Constructs a CoalescingSchedulerS based on the {@linkplain Scheduler#get() default * scheduler}. * * @see #phaser() * @see #command() */ public CoalescingSchedulerS( Phaser _phaser, Scheduler.ScheduledCommand _command ) { this( Scheduler.get(), _phaser, _command ); } /** Constructs a CoalescingSchedulerS. * * @see #baseScheduler() * @see #phaser() * @see #command() */ public CoalescingSchedulerS( Scheduler _baseScheduler, Phaser _phaser, Scheduler.ScheduledCommand _command ) { super( _baseScheduler ); command = _command; phaser = _phaser; } /** Partially constructs a CoalescingSchedulerS, to be completed by calling * {@linkplain #init(Scheduler.ScheduledCommand) init}(command). * * @see #baseScheduler() * @see #phaser() */ protected CoalescingSchedulerS( Scheduler _baseScheduler, Phaser _phaser ) { super( _baseScheduler ); phaser = _phaser; } /** Completes the construction of this coalescing scheduler. Call once only. * * @see #command() */ protected final void init( Scheduler.ScheduledCommand _command ) { command = _command; } // ------------------------------------------------------------------------------------ /** The command to be scheduled. */ public final Scheduler.ScheduledCommand command() { return command; } private Scheduler.ScheduledCommand command; /** A phaser that schedules commands to run after the browser event loop returns. * * @see Scheduler#scheduleDeferred(Scheduler.ScheduledCommand) */ public static final Phaser DEFERRED = new Phaser() { public void schedule( final Scheduler s, final Scheduler.ScheduledCommand c ) { s.scheduleDeferred( c ); } }; /** A phaser that schedules commands to run in the event loop prior to * GWT-generated code. * * @see Scheduler#scheduleEntry(Scheduler.ScheduledCommand) */ public static final Phaser ENTRY = new Phaser() { public void schedule( final Scheduler s, final Scheduler.ScheduledCommand c ) { s.scheduleEntry( c ); } }; /** A phaser that schedules commands to run in the event loop subsequent to * GWT-generated code. * * @see Scheduler#scheduleFinally(Scheduler.ScheduledCommand) */ public static final Phaser FINALLY = new Phaser() { public void schedule( final Scheduler s, final Scheduler.ScheduledCommand c ) { s.scheduleFinally( c ); } }; /** The scheduling phaser, one of: */ public final Phaser phaser() { return phaser; } private final Phaser phaser; // - C o a l e s c i n g - S c h e d u l e r ------------------------------------------ public final void schedule() { if( isScheduled ) return; phaser.schedule( baseScheduler, commandWrapper ); isScheduled = true; } // ==================================================================================== /** An adapter that schedules commands for a particular phase only. */ public static abstract class Phaser { Phaser() {} public abstract void schedule( Scheduler s, Scheduler.ScheduledCommand c ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Scheduler.ScheduledCommand commandWrapper = new Scheduler.ScheduledCommand() { public void execute() { isScheduled = false; // allow nested scheduling request command.execute(); } }; }