package votorola.s.gwt.wic; // Copyright 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.http.client.UrlBuilder; import com.google.gwt.user.client.Window; import java.util.*; import votorola.a.count.*; import votorola.g.lang.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; import votorola.s.gwt.stage.*; /** A controller that loads a new page when the poll changes on stage. */ class PollPager implements PropertyChangeHandler { /** Creates the single instance of PollPager. */ PollPager( final Stage stage ) // called by CountIn { pollName0 = stage.getPollName(); GWTX.i().bus().addHandlerToSource( PropertyChange.TYPE, /*source*/stage.prompter(), PollPager.this ); // no need to unregister, registry does not outlive this handler } /** The single instance of PollPager. */ private static PollPager instance; { if( instance != null ) throw new IllegalStateException(); instance = PollPager.this; } // ------------------------------------------------------------------------------------ /** The initial poll name. */ String pollName0; // matching (it is assumed) query parameter 'p' /** Returns true if the named parameter was set to the specified value, false if it * had already been set. */ static boolean setParameter( final String name, final String value, final UrlBuilder u ) { final String oldValue = Window.Location.getParameter( name ); final boolean wasSet; if( value == null ) { if( null == oldValue ) wasSet = false; else { u.removeParameter( name ); wasSet = true; } } else { if( value.equals( oldValue )) wasSet = false; else { u.setParameter( name, value ); wasSet = true; } } return wasSet; } // - P r o p e r t y - C h a n g e - H a n d l e r ------------------------------------ public void onPropertyChange( final PropertyChange e ) { // cf. PositionPager.onPropertyChange final String propertyName = e.propertyName(); if( !propertyName.startsWith( "pollName" ) ) return; final Stage stage = Stage.i(); final String pollName = stage.getPollName(); if( ObjectX.nullEquals( pollName, pollName0 )) { // no actual change. Either event is from setPollName below (page load // pending) or another change that was reverted before the event got here. In // either case, it is safe to suppress to lighten the burden // stage.prompter().setSuppressed( true ); //// not actually safe, as an intermediate value may possibly have been read somewhere return; } final UrlBuilder u = Window.Location.createUrlBuilder(); if( !setParameter( "p", Poll.U.toQuery(pollName), u )) { // parameters are already set correctly, so apparently pollName0 wasn't assert false: "pollName0 to 'p'"; pollName0 = pollName; // ensure it's now correct return; } stage.setPollName( pollName0 ); // back to original values, keep state clean in BFCache stage.prompter().setSuppressed( true ); // suppress event, which is now redundant Window.Location.assign( u.buildString() ); } }