001package 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.
002
003import com.google.gwt.http.client.UrlBuilder;
004import com.google.gwt.regexp.shared.*;
005import com.google.gwt.user.client.Window;
006import java.util.*;
007import votorola.a.count.*;
008import votorola.g.lang.*;
009import votorola.g.web.gwt.event.*;
010import votorola.s.gwt.stage.*;
011
012
013/** A controller that loads a new page when the position (actor + poll) changes on stage.
014  */
015final class PositionPager extends PollPager
016{
017
018
019    /** Creates the single instance of PositionPager.
020      */
021    PositionPager( final Stage stage ) // called by CountIn
022    {
023        super( stage );
024        actorName0 = stage.getActorName();
025    }
026
027
028
029   // ------------------------------------------------------------------------------------
030
031
032    /** The initial actor name.
033      */
034    String actorName0; // matching (it is assumed) query parameter 'u' or 'v'
035
036
037
038   // - P r o p e r t y - C h a n g e - H a n d l e r ------------------------------------
039
040
041    public @Override void onPropertyChange( final PropertyChange e )
042    {
043        // cf. PollPager.onPropertyChange
044
045        final String propertyName = e.propertyName();
046        if( !propertyName.startsWith("actorName") && !propertyName.startsWith("pollName") ) return;
047
048        final Stage stage = Stage.i();
049        final String actorName = stage.getActorName();
050        final String pollName = stage.getPollName();
051        if( ObjectX.nullEquals(actorName,actorName0) && ObjectX.nullEquals(pollName,pollName0) )
052        {
053            // no actual change, per PollPager.onPropertyChange
054            return;
055        }
056
057        final UrlBuilder u = Window.Location.createUrlBuilder();
058        if( !setParameter("u",actorName,u) & // unconditional AND
059            !setParameter("p",Poll.U.toQuery(pollName),u) )
060        {
061            // parameters are already set correctly, so apparently actorName0 or pollName0 weren't
062            assert false: "actorName0 init to page param 'u' or 'v', and pollName0 to 'p'";
063            actorName0 = actorName; // ensure they are now correct
064            pollName0 = pollName;
065            return;
066        }
067
068        u.removeParameter( "v" ); // in case it was set, it's illegal to specify both 'u' and 'v'
069        final Map<String,List<String>> paramMap = Window.Location.getParameterMap();
070        for( final String paramName: paramMap.keySet() )
071        {
072            // Find and remove the page version parameter if there is one.  Currently only
073            // the "Rank" page has it.  If it remains, Wicket (1.5.4) will serve the same
074            // page regardless of other parameter changes.
075            if( !PAGE_VERSION_PARAM_NAME_PATTERN.test( paramName )) continue;
076
077            u.removeParameter( paramName );
078            break;
079        }
080
081        Stage.setActorName( actorName0 ); // back to original values, keep state clean in BFCache
082        stage.setPollName( pollName0 );
083        stage.prompter().setSuppressed( true ); // suppress event, which is now redundant
084        Window.Location.assign( u.buildString() );
085    }
086
087
088
089//// P r i v a t e ///////////////////////////////////////////////////////////////////////
090
091
092    private static final RegExp PAGE_VERSION_PARAM_NAME_PATTERN = RegExp.compile( "^[0-9]+$" );
093
094
095}