001package 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. 002 003import com.google.gwt.core.client.Scheduler; 004 005 006/** A coalescing scheduler that works with an ordinary, non-repeating command. 007 */ 008public class CoalescingSchedulerS extends CoalescingScheduler 009{ 010 011 012 /** Constructs a CoalescingSchedulerS based on the {@linkplain Scheduler#get() default 013 * scheduler}. 014 * 015 * @see #phaser() 016 * @see #command() 017 */ 018 public CoalescingSchedulerS( Phaser _phaser, Scheduler.ScheduledCommand _command ) 019 { 020 this( Scheduler.get(), _phaser, _command ); 021 } 022 023 024 025 /** Constructs a CoalescingSchedulerS. 026 * 027 * @see #baseScheduler() 028 * @see #phaser() 029 * @see #command() 030 */ 031 public CoalescingSchedulerS( Scheduler _baseScheduler, Phaser _phaser, 032 Scheduler.ScheduledCommand _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.ScheduledCommand) init}(command). 043 * 044 * @see #baseScheduler() 045 * @see #phaser() 046 */ 047 protected CoalescingSchedulerS( 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.ScheduledCommand _command ) { command = _command; } 060 061 062 063 // ------------------------------------------------------------------------------------ 064 065 066 /** The command to be scheduled. 067 */ 068 public final Scheduler.ScheduledCommand command() { return command; } 069 070 071 private Scheduler.ScheduledCommand command; 072 073 074 075 /** A phaser that schedules commands to run after the browser event loop returns. 076 * 077 * @see Scheduler#scheduleDeferred(Scheduler.ScheduledCommand) 078 */ 079 public static final Phaser DEFERRED = new Phaser() 080 { 081 public void schedule( final Scheduler s, final Scheduler.ScheduledCommand c ) 082 { 083 s.scheduleDeferred( c ); 084 } 085 }; 086 087 088 089 /** A phaser that schedules commands to run in the event loop <em>prior to</em> 090 * GWT-generated code. 091 * 092 * @see Scheduler#scheduleEntry(Scheduler.ScheduledCommand) 093 */ 094 public static final Phaser ENTRY = new Phaser() 095 { 096 public void schedule( final Scheduler s, final Scheduler.ScheduledCommand c ) 097 { 098 s.scheduleEntry( c ); 099 } 100 }; 101 102 103 104 /** A phaser that schedules commands to run in the event loop <em>subsequent to</em> 105 * GWT-generated code. 106 * 107 * @see Scheduler#scheduleFinally(Scheduler.ScheduledCommand) 108 */ 109 public static final Phaser FINALLY = new Phaser() 110 { 111 public void schedule( final Scheduler s, final Scheduler.ScheduledCommand c ) 112 { 113 s.scheduleFinally( c ); 114 } 115 }; 116 117 118 119 /** The scheduling phaser, one of:<ul> 120 * 121 * <li>{@linkplain #DEFERRED DEFERRED}</li> 122 * <li>{@linkplain #ENTRY ENTRY}</li> 123 * <li>{@linkplain #FINALLY FINALLY}</li></ul> 124 */ 125 public final Phaser phaser() { return phaser; } 126 127 128 private final Phaser phaser; 129 130 131 132 // - C o a l e s c i n g - S c h e d u l e r ------------------------------------------ 133 134 135 public final void schedule() 136 { 137 if( isScheduled ) return; 138 139 phaser.schedule( baseScheduler, commandWrapper ); 140 isScheduled = true; 141 } 142 143 144 145 // ==================================================================================== 146 147 148 /** An adapter that schedules commands for a particular phase only. 149 */ 150 public static abstract class Phaser 151 { 152 153 Phaser() {} 154 155 156 public abstract void schedule( Scheduler s, Scheduler.ScheduledCommand c ); 157 158 } 159 160 161 162//// P r i v a t e /////////////////////////////////////////////////////////////////////// 163 164 165 private final Scheduler.ScheduledCommand commandWrapper = new Scheduler.ScheduledCommand() 166 { 167 public void execute() 168 { 169 isScheduled = false; // allow nested scheduling request 170 command.execute(); 171 } 172 }; 173 174 175 176}