package votorola.a.count.gwt; // 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.*; import com.google.gwt.event.logical.shared.*; import com.google.gwt.regexp.shared.*; import com.google.gwt.user.client.Window; import votorola.g.lang.*; //import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; /** A controller to maintain a superaccount selection in sync with the value of an 'a' * switch. * * @see SacSelection#aSacSwitch() */ public final class SacSetter extends CoalescingSchedulerS implements Scheduler.ScheduledCommand, ValueChangeHandler { /** Constructs the SacSetter for a given selection model. Construct at most one for * the entire life of the selection model, as currently it does not unregister its * handlers. * * @param _sacSelection the superaccount selection model in which this controller * operates. This is also the source for all events. * @param _phaser the scheduling phaser for state changes, which is exposed as * {@linkplain #phaser phaser}(). * @param eventsPhaser the scheduling phaser for the dispatch of events * consequent on state changes. */ public SacSetter( SacSelection _sacSelection, Phaser _phaser, final Phaser eventsPhaser ) { super( Scheduler.get(), _phaser ); sacSelection = _sacSelection; init( SacSetter.this ); gun = new CoalescingSelectionChangeGun( sacSelection, eventsPhaser ); sacSelection.aSacSwitch().addHandler( SacSetter.this ); // no need to unregister, registry does not outlive this listener syncFromSwitch(); // init state } // ------------------------------------------------------------------------------------ /** The superaccount that is currently set, which is also to be used as the return * value in the implementation of SacSelection.{@linkplain SacSelection#getSac() * getSac}(). */ public SacJS sac() { return sac; } private SacJS sac; /** Sets the superaccount without firing an event and returns true if this * resulted in a change, false otherwise. * * @see #sac() */ private boolean setSac( final SacJS newSac ) { if( ObjectX.nullEquals( newSac, sac )) return false; sac = newSac; return true; } // - 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 ---------------------------- /** Synchronizes the superaccount setting from the 'a' switch, and schedules a * selection change event for any state change that results. */ public void execute() { if( syncFromSwitch() ) gun.schedule(); } // - V a l u e - C h a n g e - H a n d l e r ------------------------------------------ public void onValueChange( ValueChangeEvent _e ) { schedule(); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final CoalescingSelectionChangeGun gun; private final SacSelection sacSelection; /** Synchronizes the superaccount from the 'a' switch without firing an event and * returns true if this resulted in a change, false otherwise. * * @see #sac() * @see SacSelection#aSacSwitch() */ private final @Warning("init call") boolean syncFromSwitch() { final CountJS count = sacSelection.count(); if( count == null ) return setSac( null ); final String a = sacSelection.aSacSwitch().get(); if( a == null ) return setSac( count.voteSuperaccount() ); final MatchResult m = SacSelection.A_PATTERN.exec( a ); if( a == null ) { Window.alert( "Malformed superaccount switch: a=" + a ); return setSac( count.nullSuperaccount() ); } final CountingMethodJS.SwitchMnemonic countingMethodMnemonic; { final String name = m.getGroup( 1 ); try{ countingMethodMnemonic = CountingMethodJS.SwitchMnemonic.valueOf( name ); } catch( final IllegalArgumentException x ) { Window.alert( "Malformed superaccount switch, no such counting method \"" + name + "\": a=" + a ); return setSac( count.nullSuperaccount() ); } } final String accountName = m.getGroup( 2 ); final SacJS newSac = count.superaccount( countingMethodMnemonic.fullName(), accountName ); if( newSac == null ) { Window.alert( "Current count has no such superaccount: a=" + a ); return setSac( count.nullSuperaccount() ); } return setSac( newSac ); } }