package votorola.s.gwt.wic; // Copyright 2012-2013, 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.regexp.shared.*; import com.google.gwt.user.client.Window; import java.util.*; import votorola.a.count.*; import votorola.g.lang.*; import votorola.g.web.gwt.event.*; import votorola.s.gwt.stage.*; /** A controller that loads a new page when the position (actor + poll) changes on stage. */ final class PositionPager extends PollPager { /** Creates the single instance of PositionPager. */ PositionPager( final Stage stage ) // called by CountIn { super( stage ); actorName0 = stage.getActorName(); } // ------------------------------------------------------------------------------------ /** The initial actor name. */ String actorName0; // matching (it is assumed) query parameter 'u' or 'v' // - P r o p e r t y - C h a n g e - H a n d l e r ------------------------------------ public @Override void onPropertyChange( final PropertyChange e ) { // cf. PollPager.onPropertyChange final String propertyName = e.propertyName(); if( !propertyName.startsWith("actorName") && !propertyName.startsWith("pollName") ) return; final Stage stage = Stage.i(); final String actorName = stage.getActorName(); final String pollName = stage.getPollName(); if( ObjectX.nullEquals(actorName,actorName0) && ObjectX.nullEquals(pollName,pollName0) ) { // no actual change, per PollPager.onPropertyChange return; } final UrlBuilder u = Window.Location.createUrlBuilder(); if( !setParameter("u",actorName,u) & // unconditional AND !setParameter("p",Poll.U.toQuery(pollName),u) ) { // parameters are already set correctly, so apparently actorName0 or pollName0 weren't assert false: "actorName0 init to page param 'u' or 'v', and pollName0 to 'p'"; actorName0 = actorName; // ensure they are now correct pollName0 = pollName; return; } u.removeParameter( "v" ); // in case it was set, it's illegal to specify both 'u' and 'v' final Map> paramMap = Window.Location.getParameterMap(); for( final String paramName: paramMap.keySet() ) { // Find and remove the page version parameter if there is one. Currently only // the "Rank" page has it. If it remains, Wicket (1.5.4) will serve the same // page regardless of other parameter changes. if( !PAGE_VERSION_PARAM_NAME_PATTERN.test( paramName )) continue; u.removeParameter( paramName ); break; } Stage.setActorName( actorName0 ); // back to original values, keep state clean in BFCache stage.setPollName( pollName0 ); stage.prompter().setSuppressed( true ); // suppress event, which is now redundant Window.Location.assign( u.buildString() ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static final RegExp PAGE_VERSION_PARAM_NAME_PATTERN = RegExp.compile( "^[0-9]+$" ); }