package 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. import com.google.gwt.core.client.Scheduler; /** A coalescing scheduler that works with a repeating command. */ public final class CoalescingSchedulerR extends CoalescingScheduler { /** Constructs a CoalescingSchedulerR based on the {@linkplain Scheduler#get() default * scheduler}. * * @see #phaser() * @see #command() */ public CoalescingSchedulerR( Phaser _phaser, Scheduler.RepeatingCommand _command ) { this( Scheduler.get(), _phaser, _command ); } /** Constructs a CoalescingSchedulerR. * * @see #baseScheduler() * @see #phaser() * @see #command() */ public CoalescingSchedulerR( Scheduler _baseScheduler, Phaser _phaser, Scheduler.RepeatingCommand _command ) { super( _baseScheduler ); command = _command; phaser = _phaser; } /** Partially constructs a CoalescingSchedulerS, to be completed by calling * {@linkplain #init(Scheduler.RepeatingCommand) init}(command). * * @see #baseScheduler() * @see #phaser() */ protected CoalescingSchedulerR( 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.RepeatingCommand _command ) { command = _command; } // ------------------------------------------------------------------------------------ /** The command to be scheduled. */ public Scheduler.RepeatingCommand command() { return command; } private Scheduler.RepeatingCommand command; /** A phaser that schedules commands to run in the event loop prior to * GWT-generated code. * * @see Scheduler#scheduleEntry(Scheduler.RepeatingCommand) */ public static final Phaser ENTRY = new Phaser() { public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) { s.scheduleEntry( c ); } }; /** A phaser that schedules commands to run in the event loop subsequent to * GWT-generated code. * * @see Scheduler#scheduleFinally(Scheduler.RepeatingCommand) */ public static final Phaser FINALLY = new Phaser() { public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) { s.scheduleFinally( c ); } }; /** A phaser that schedules commands to perform incremental work. * * @see Scheduler#scheduleIncremental(Scheduler.RepeatingCommand) */ public static final Phaser INCREMENTAL = new Phaser() { public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) { s.scheduleIncremental( c ); } }; /** The scheduling phaser, one of: */ public 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 void schedule() { if( isScheduled ) return; phaser.schedule( baseScheduler, commandWrapper ); isScheduled = true; } // ==================================================================================== /** A phaser that schedules commands with a constant intervening delay. * * @see Scheduler#scheduleFixedDelay(Scheduler.RepeatingCommand,int) */ public static final class FixedDelay extends Phaser { public FixedDelay( int _delayMs ) { delayMs = _delayMs; } private final int delayMs; public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) { s.scheduleFixedDelay( c, delayMs ); } } // ==================================================================================== /** A phaser that schedules commands with a constant period. * * @see Scheduler#scheduleFixedPeriod(Scheduler.RepeatingCommand,int) */ public static final class FixedPeriod extends Phaser { public FixedPeriod( int _periodMs ) { periodMs = _periodMs; } private final int periodMs; public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) { s.scheduleFixedPeriod( c, periodMs ); } } // ==================================================================================== /** An adapter that schedules commands for a particular phase only. */ public static abstract class Phaser { Phaser() {} abstract void schedule( Scheduler s, Scheduler.RepeatingCommand c ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Scheduler.RepeatingCommand commandWrapper = new Scheduler.RepeatingCommand() { public boolean execute() { isScheduled = command.execute(); return isScheduled; } }; }