001package votorola.s.gwt.stage.vote; // 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.dom.client.*;
004import com.google.gwt.i18n.client.NumberFormat;
005import com.google.gwt.user.client.ui.*;
006import com.google.web.bindery.event.shared.HandlerRegistration;
007import votorola.a.count.gwt.*;
008import votorola.g.lang.*;
009import votorola.g.util.Holder;
010import votorola.g.web.gwt.*;
011import votorola.g.web.gwt.event.*;
012
013
014/** A numeric view of a tracked count node showing the volume of {@linkplain
015  * SacRegisterJS_v#receiveVolume() votes received}.  The view is empty when the volume is
016  * unknown or zero, but still takes up some space.
017  */
018public final class FlowVolumeV extends MajorV
019{
020
021
022    /** Constructs a FlowVolumeV.
023      *
024      *     @param _nodeHolder the holder of the count node on which the view is
025      *       modelled.
026      *     @see MajorV#trackV()
027      *     @param y the outermost HTML element of which the view is composed.
028      *     @see MajorV#place()
029      */
030    FlowVolumeV( Holder<CountNodeJS> _nodeHolder, final VoteTrackV trackV,
031      final TableCellElement y, final votorola.a.count.XCastRelation place )
032    {
033        super( place, trackV );
034        nodeHolder = _nodeHolder;
035
036      // View.
037      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
038        setElement( y );
039        final Element strong = DocumentX.createElement( "strong", Document.get() );
040        strong.appendChild( textH );
041        y.appendChild( strong );
042        y.appendChild( textL );
043
044      // Controllers.
045      // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
046        new Modeller( trackV );
047    }
048
049
050
051   // ------------------------------------------------------------------------------------
052
053
054    /** Sets the high digits of the volume on <code>textH</code>, and the low digits on
055      * <code>textL</code>.
056      */
057    public static void set( final long volume, final Text textH, final Text textL )
058    {
059        final String s = decimalFormatter.format( volume );
060        int c = s.length() - 2; // skip last char, assumed to be digit
061        for( ; c >= 0 ; --c )
062        {
063            final char ch = s.charAt( c );
064            if( !Character.isDigit( ch )) break;
065        }
066
067        final int cLastSeparator = c;
068        final String sH;
069        final String sL;
070        if( cLastSeparator < 0 ) // no separator
071        {
072            sH = "";
073            sL = s;
074        }
075        else
076        {
077            sH = s.substring( 0, cLastSeparator );
078            sL = s.substring( cLastSeparator );
079        }
080        textH.setData( sH );
081        textL.setData( sL );
082    }
083
084
085
086//// P r i v a t e ///////////////////////////////////////////////////////////////////////
087
088
089    private static final NumberFormat decimalFormatter = NumberFormat.getDecimalFormat();
090
091
092
093    private final Holder<CountNodeJS> nodeHolder;
094
095
096
097    private final Text textH = Document.get().createTextNode( "" ); // high digits
098
099
100
101    private final Text textL = Document.get().createTextNode( "" ); // low digits
102
103
104
105   // ====================================================================================
106
107
108    private final class Modeller extends SuspendedModeller
109    {
110
111        Modeller( final VoteTrackV trackV )
112        {
113            super( trackV, spool() );
114            remodelUnlessMoving(); // init state
115        }
116
117
118        final @Warning("init call") void remodel()
119        {
120            final CountNodeJS node = nodeHolder.get();
121            final long volume;
122            if( node == null ) volume = 0L;
123            else volume = node.voteRegister().receiveVolume();
124            set( volume, textH, textL );
125            setVisible( volume > 0L );
126        }
127
128    }
129
130
131}