package 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. import com.google.gwt.dom.client.*; import com.google.gwt.i18n.client.NumberFormat; import com.google.gwt.user.client.ui.*; import com.google.web.bindery.event.shared.HandlerRegistration; import votorola.a.count.gwt.*; import votorola.g.lang.*; import votorola.g.util.Holder; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; /** A numeric view of a tracked count node showing the volume of {@linkplain * SacRegisterJS_v#receiveVolume() votes received}. The view is empty when the volume is * unknown or zero, but still takes up some space. */ public final class FlowVolumeV extends MajorV { /** Constructs a FlowVolumeV. * * @param _nodeHolder the holder of the count node on which the view is * modelled. * @see MajorV#trackV() * @param y the outermost HTML element of which the view is composed. * @see MajorV#place() */ FlowVolumeV( Holder _nodeHolder, final VoteTrackV trackV, final TableCellElement y, final votorola.a.count.XCastRelation place ) { super( place, trackV ); nodeHolder = _nodeHolder; // View. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - setElement( y ); final Element strong = DocumentX.createElement( "strong", Document.get() ); strong.appendChild( textH ); y.appendChild( strong ); y.appendChild( textL ); // Controllers. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - new Modeller( trackV ); } // ------------------------------------------------------------------------------------ /** Sets the high digits of the volume on textH, and the low digits on * textL. */ public static void set( final long volume, final Text textH, final Text textL ) { final String s = decimalFormatter.format( volume ); int c = s.length() - 2; // skip last char, assumed to be digit for( ; c >= 0 ; --c ) { final char ch = s.charAt( c ); if( !Character.isDigit( ch )) break; } final int cLastSeparator = c; final String sH; final String sL; if( cLastSeparator < 0 ) // no separator { sH = ""; sL = s; } else { sH = s.substring( 0, cLastSeparator ); sL = s.substring( cLastSeparator ); } textH.setData( sH ); textL.setData( sL ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static final NumberFormat decimalFormatter = NumberFormat.getDecimalFormat(); private final Holder nodeHolder; private final Text textH = Document.get().createTextNode( "" ); // high digits private final Text textL = Document.get().createTextNode( "" ); // low digits // ==================================================================================== private final class Modeller extends SuspendedModeller { Modeller( final VoteTrackV trackV ) { super( trackV, spool() ); remodelUnlessMoving(); // init state } final @Warning("init call") void remodel() { final CountNodeJS node = nodeHolder.get(); final long volume; if( node == null ) volume = 0L; else volume = node.voteRegister().receiveVolume(); set( volume, textH, textL ); setVisible( volume > 0L ); } } }