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; 004 005 006/** A coalescing scheduler that works with a repeating command. 007 */ 008public final class CoalescingSchedulerR extends CoalescingScheduler 009{ 010 011 012 /** Constructs a CoalescingSchedulerR based on the {@linkplain Scheduler#get() default 013 * scheduler}. 014 * 015 * @see #phaser() 016 * @see #command() 017 */ 018 public CoalescingSchedulerR( Phaser _phaser, Scheduler.RepeatingCommand _command ) 019 { 020 this( Scheduler.get(), _phaser, _command ); 021 } 022 023 024 025 /** Constructs a CoalescingSchedulerR. 026 * 027 * @see #baseScheduler() 028 * @see #phaser() 029 * @see #command() 030 */ 031 public CoalescingSchedulerR( Scheduler _baseScheduler, Phaser _phaser, 032 Scheduler.RepeatingCommand _command ) 033 { 034 super( _baseScheduler ); 035 command = _command; 036 phaser = _phaser; 037 } 038 039 040 041 /** Partially constructs a CoalescingSchedulerS, to be completed by calling 042 * {@linkplain #init(Scheduler.RepeatingCommand) init}(command). 043 * 044 * @see #baseScheduler() 045 * @see #phaser() 046 */ 047 protected CoalescingSchedulerR( Scheduler _baseScheduler, Phaser _phaser ) 048 { 049 super( _baseScheduler ); 050 phaser = _phaser; 051 } 052 053 054 055 /** Completes the construction of this coalescing scheduler. Call once only. 056 * 057 * @see #command() 058 */ 059 protected final void init( Scheduler.RepeatingCommand _command ) { command = _command; } 060 061 062 063 // ------------------------------------------------------------------------------------ 064 065 066 /** The command to be scheduled. 067 */ 068 public Scheduler.RepeatingCommand command() { return command; } 069 070 071 private Scheduler.RepeatingCommand command; 072 073 074 075 /** A phaser that schedules commands to run in the event loop <em>prior to</em> 076 * GWT-generated code. 077 * 078 * @see Scheduler#scheduleEntry(Scheduler.RepeatingCommand) 079 */ 080 public static final Phaser ENTRY = new Phaser() 081 { 082 public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) 083 { 084 s.scheduleEntry( c ); 085 } 086 }; 087 088 089 090 /** A phaser that schedules commands to run in the event loop <em>subsequent to</em> 091 * GWT-generated code. 092 * 093 * @see Scheduler#scheduleFinally(Scheduler.RepeatingCommand) 094 */ 095 public static final Phaser FINALLY = new Phaser() 096 { 097 public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) 098 { 099 s.scheduleFinally( c ); 100 } 101 }; 102 103 104 105 /** A phaser that schedules commands to perform incremental work. 106 * 107 * @see Scheduler#scheduleIncremental(Scheduler.RepeatingCommand) 108 */ 109 public static final Phaser INCREMENTAL = new Phaser() 110 { 111 public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) 112 { 113 s.scheduleIncremental( c ); 114 } 115 }; 116 117 118 119 /** The scheduling phaser, one of:<ul> 120 * 121 * <li>{@linkplain #ENTRY ENTRY}</li> 122 * <li>{@linkplain #FINALLY FINALLY}</li> 123 * <li>{@linkplain FixedDelay FixedDelay}</li> 124 * <li>{@linkplain FixedPeriod FixedPeriod}</li> 125 * <li>{@linkplain #INCREMENTAL INCREMENTAL}</li></ul> 126 */ 127 public Phaser phaser() { return phaser; } 128 129 130 private final Phaser phaser; 131 132 133 134 // - C o a l e s c i n g - S c h e d u l e r ------------------------------------------ 135 136 137 public void schedule() 138 { 139 if( isScheduled ) return; 140 141 phaser.schedule( baseScheduler, commandWrapper ); 142 isScheduled = true; 143 } 144 145 146 147 // ==================================================================================== 148 149 150 /** A phaser that schedules commands with a constant intervening delay. 151 * 152 * @see Scheduler#scheduleFixedDelay(Scheduler.RepeatingCommand,int) 153 */ 154 public static final class FixedDelay extends Phaser 155 { 156 157 public FixedDelay( int _delayMs ) { delayMs = _delayMs; } 158 159 160 private final int delayMs; 161 162 163 public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) 164 { 165 s.scheduleFixedDelay( c, delayMs ); 166 } 167 168 } 169 170 171 172 // ==================================================================================== 173 174 175 /** A phaser that schedules commands with a constant period. 176 * 177 * @see Scheduler#scheduleFixedPeriod(Scheduler.RepeatingCommand,int) 178 */ 179 public static final class FixedPeriod extends Phaser 180 { 181 182 public FixedPeriod( int _periodMs ) { periodMs = _periodMs; } 183 184 185 private final int periodMs; 186 187 188 public void schedule( final Scheduler s, final Scheduler.RepeatingCommand c ) 189 { 190 s.scheduleFixedPeriod( c, periodMs ); 191 } 192 193 } 194 195 196 197 // ==================================================================================== 198 199 200 /** An adapter that schedules commands for a particular phase only. 201 */ 202 public static abstract class Phaser 203 { 204 205 Phaser() {} 206 207 208 abstract void schedule( Scheduler s, Scheduler.RepeatingCommand c ); 209 210 } 211 212 213 214//// P r i v a t e /////////////////////////////////////////////////////////////////////// 215 216 217 private final Scheduler.RepeatingCommand commandWrapper = new Scheduler.RepeatingCommand() 218 { 219 public boolean execute() 220 { 221 isScheduled = command.execute(); 222 return isScheduled; 223 } 224 }; 225 226 227 228}