package votorola.s.gwt.stage.poll; // 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.core.client.*; import com.google.gwt.user.client.rpc.AsyncCallback; import java.util.*; import votorola.a.count.*; import votorola.a.web.gwt.*; import votorola.g.hold.*; import votorola.g.lang.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; import votorola.s.gwt.stage.*; /** A track for listing {@linkplain votorola.s.wap.PollspaceWAP compiled polls}. Changes * to track state are signalled by {@linkplain Change change events} on the {@linkplain * GWTX#bus() bus}, which are fired from {@linkplain #eventSource eventSource}. * * @see PolltrackV */ public final class Polltrack extends AbstractList implements AsyncCallback, Track { /** Constructs a Polltrack. */ public Polltrack() { Stage.i().addInitializer( new TheatreInitializer() // auto-removed { // cf. s.gwt.stage.vote.VoteTrack // Early referrer resolution. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public void initTo( Stage _s ) {} // public void initTo( final Stage s, final TheatrePage r ) { initR( s, r ); } /// but let init complete for sake of initR guards public void initTo( final Stage s, final TheatrePage r ) { initToReferrer = r; } private TheatrePage initToReferrer; public void initToComplete( final Stage s, final boolean rPending ) { final TheatrePage r = initToReferrer; if( r == null ) return; // no referrer state to transfer assert !rPending; if( s.getPollName() != null ) return; // set by user or prop, do not clobber initR( s, r ); } // Late referrer resolution. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - public void initFrom( Stage _s, boolean _rPending ) {} public void initFromComplete( final Stage s, final boolean rPending ) { if( rPending ) initFromPollName = s.getPollName(); // continues at initUltimately } private String initFromPollName; // as restored from single page persistence public void initUltimately( final Stage s, final TheatrePage r ) { if( r == null ) return; // no referrer state to transfer if( !ObjectX.nullEquals( s.getPollName(), initFromPollName )) return; // changed by user or prop, do not clobber initR( s, r ); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - private void initR( final Stage s, final TheatrePage r ) { final String rPollName = r.getPollName(); if( rPollName == null ) return; // no poll on referrer if( s.getDefaultPollName() != null ) return; // set by scene, do not mask s.setPollName( rPollName ); } }); final StringBuilder c = GWTX.stringBuilderClear(); c.append( App.getServletContextLocation() ); c.append( "/wap?wCall=psPollspace" ); requestLoc = c.toString(); App.i().jsonpWAP().requestObject( requestLoc, Polltrack.this ); // continues at onFailure or onSuccess } /** Returns the {@linkplain Stage#tracks() staged instance} of Polltrack, or null if * none is staged. */ public static Polltrack i( final Stage stage ) { for( Track t: stage.tracks() ) if( t instanceof Polltrack ) return (Polltrack)t; return null; } // ------------------------------------------------------------------------------------ /** The source of change events for the track. */ final Object eventSource = new Object(); // workaround, grep CAES // - A s y n c - C a l l b a c k ------------------------------------------------------ public void onFailure( final Throwable x ) { Stage.i().addWarning( App.i().mesS().gwt_stage_poll_Polltrack_requestFail( x, requestLoc )); } public void onSuccess( final JavaScriptObject response ) { buffer = response._get( "ps" ); GWTX.i().bus().fireEventFromSource( new Change(), eventSource ); } // - L i s t -------------------------------------------------------------------------- public Poll get( final int p ) { return buffer.get( p ); } public int size() { return buffer.length(); } // - T r a c k ------------------------------------------------------------------------ public PolltrackV newView( StageV _stageV ) { return new PolltrackV( Polltrack.this ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private JsArray buffer = JavaScriptObject.createArray().cast(); // empty placeholder private String requestLoc; // final after initialized }