001package 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.
002
003import com.google.gwt.core.client.*;
004import com.google.gwt.user.client.rpc.AsyncCallback;
005import java.util.*;
006import votorola.a.count.*;
007import votorola.a.web.gwt.*;
008import votorola.g.hold.*;
009import votorola.g.lang.*;
010import votorola.g.web.gwt.*;
011import votorola.g.web.gwt.event.*;
012import votorola.s.gwt.stage.*;
013
014
015/** A track for listing {@linkplain votorola.s.wap.PollspaceWAP compiled polls}.  Changes
016  * to track state are signalled by {@linkplain Change change events} on the {@linkplain
017  * GWTX#bus() bus}, which are fired from {@linkplain #eventSource eventSource}.
018  *
019  *     @see PolltrackV
020  */
021public final class Polltrack extends AbstractList<Poll>
022  implements AsyncCallback<JavaScriptObject>, Track
023{
024
025
026    /** Constructs a Polltrack.
027      */
028    public Polltrack()
029    {
030        Stage.i().addInitializer( new TheatreInitializer() // auto-removed
031        {
032            // cf. s.gwt.stage.vote.VoteTrack
033
034          // Early referrer resolution.
035          // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
036            public void initTo( Stage _s ) {}
037         // public void initTo( final Stage s, final TheatrePage r ) { initR( s, r ); }
038         /// but let init complete for sake of initR guards
039            public void initTo( final Stage s, final TheatrePage r ) { initToReferrer = r; }
040            private TheatrePage initToReferrer;
041            public void initToComplete( final Stage s, final boolean rPending )
042            {
043                final TheatrePage r = initToReferrer;
044                if( r == null ) return; // no referrer state to transfer
045
046                assert !rPending;
047                if( s.getPollName() != null ) return; // set by user or prop, do not clobber
048
049                initR( s, r );
050            }
051
052          // Late referrer resolution.
053          // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
054            public void initFrom( Stage _s, boolean _rPending ) {}
055            public void initFromComplete( final Stage s, final boolean rPending )
056            {
057                if( rPending ) initFromPollName = s.getPollName(); // continues at initUltimately
058            }
059            private String initFromPollName; // as restored from single page persistence
060            public void initUltimately( final Stage s, final TheatrePage r )
061            {
062                if( r == null ) return; // no referrer state to transfer
063
064                if( !ObjectX.nullEquals( s.getPollName(), initFromPollName )) return;
065                  // changed by user or prop, do not clobber
066
067                initR( s, r );
068            }
069
070          // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
071            private void initR( final Stage s, final TheatrePage r )
072            {
073                final String rPollName = r.getPollName();
074                if( rPollName == null ) return; // no poll on referrer
075
076                if( s.getDefaultPollName() != null ) return; // set by scene, do not mask
077
078                s.setPollName( rPollName );
079            }
080        });
081        final StringBuilder c = GWTX.stringBuilderClear();
082        c.append( App.getServletContextLocation() );
083        c.append( "/wap?wCall=psPollspace" );
084        requestLoc = c.toString();
085        App.i().jsonpWAP().requestObject( requestLoc, Polltrack.this );
086        // continues at onFailure or onSuccess
087    }
088
089
090
091    /** Returns the {@linkplain Stage#tracks() staged instance} of Polltrack, or null if
092      * none is staged.
093      */
094    public static Polltrack i( final Stage stage )
095    {
096        for( Track t: stage.tracks() ) if( t instanceof Polltrack ) return (Polltrack)t;
097
098        return null;
099    }
100
101
102
103   // ------------------------------------------------------------------------------------
104
105
106    /** The source of change events for the track.
107      */
108    final Object eventSource = new Object(); // workaround, grep CAES
109
110
111
112   // - A s y n c - C a l l b a c k ------------------------------------------------------
113
114
115    public void onFailure( final Throwable x )
116    {
117        Stage.i().addWarning( App.i().mesS().gwt_stage_poll_Polltrack_requestFail( x, requestLoc ));
118    }
119
120
121
122    public void onSuccess( final JavaScriptObject response )
123    {
124        buffer = response._get( "ps" );
125        GWTX.i().bus().fireEventFromSource( new Change(), eventSource );
126    }
127
128
129
130   // - L i s t --------------------------------------------------------------------------
131
132
133    public Poll get( final int p ) { return buffer.get( p ); }
134
135
136
137    public int size() { return buffer.length(); }
138
139
140
141   // - T r a c k ------------------------------------------------------------------------
142
143
144    public PolltrackV newView( StageV _stageV ) { return new PolltrackV( Polltrack.this ); }
145
146
147
148//// P r i v a t e ///////////////////////////////////////////////////////////////////////
149
150
151    private JsArray<PollJS> buffer = JavaScriptObject.createArray().cast(); // empty placeholder
152
153
154
155    private String requestLoc; // final after initialized
156
157
158}