package votorola.s.gwt.stage.vote; // 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. import com.google.gwt.core.client.*; import com.google.gwt.dom.client.*; import com.google.gwt.user.client.ui.*; import com.google.web.bindery.event.shared.HandlerRegistration; import java.util.*; import org.vectomatic.dom.svg.*; import org.vectomatic.dom.svg.utils.*; import votorola.a.count.gwt.*; import votorola.g.lang.*; import votorola.g.util.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; import static votorola.a.count.CountNode.DART_SECTOR_MAX; /** A view of a candidate rendered as an HTML table cell containing an SVG drawing. The * drawing comprises a single {@linkplain BreakableNodeV breakable node view} for * the {@linkplain VoteTrack#candidate() candidate}. It is visible only if the candidate * is non-null and the {@linkplain VoteTrack#downstream() downstream node} (if any) has * been fetched. (The downstream node and its voters are required for the sake of * correctly sizing the view.) * *

Acknowledgement: Thomas von der Elbe contributed to the design of the * size visualization. See the thread Vote * track.

*/ final class CandidateV extends NodeV.Box { /** Constructs a CandidateV. * * @param element the outermost HTML element of which the view is composed. */ CandidateV( final VoteTrackV trackV, final TableCellElement element ) { super( votorola.a.count.XCastRelation.CANDIDATE, trackV ); track = trackV.track(); // HTML view. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - setElement( element ); // SVG view. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - final OMSVGDocument svgDoc = OMSVGParser.createDocument(); svg = svgDoc.createSVGSVGElement(); element.appendChild( svg.getElement() ); svg.appendChild( new BreakableNodeV( CandidateV.this, /*any dart sector*/0, track )); // Controllers. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - painter = new Painter( trackV ); new Modeller( trackV ); } // - C o u n t - N o d e . B o x ------------------------------------------------------ public Iterable nodeViews() { return nodeViews; } private final Iterable nodeViews = new Iterable() { public Iterator iterator() { return new votorola.g.util.IteratorA() { private NodeV nextV = (NodeV)svg.getFirstChild(); public boolean hasNext() { return nextV != null; } public NodeV next() { if( nextV == null ) throw new NoSuchElementException(); final NodeV nodeV = nextV; nextV = null; // just the one return nodeV; } }; } }; public Object painter() { return painter; } private final Painter painter; // - W i d g e t ---------------------------------------------------------------------- protected @Override void onLoad() { if( !spool().isUnwinding() ) painter.init( spool() ); // after load, when size known [but unclear how init depends on size] super.onLoad(); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// CountNodeJS downstream; private final BoardV.Palette palette = new BoardV.Palette(); /** The SVG component of this view. */ private final OMSVGSVGElement svg; private final VoteTrack track; // ==================================================================================== private final class Modeller extends SuspendedModeller { Modeller( final VoteTrackV trackV ) { super( trackV, spool() ); if( trackV == VoteTrackV.iBottomFixed() ) { Scheduler.get().scheduleFinally( new Scheduler.ScheduledCommand() { public void execute() { remodelUnlessMoving(); } // init state // later for bottom-fixed view, or length may init to zero }); } else remodelUnlessMoving(); // init state } final @Warning("init call") void remodel() { CountNodeJS candidate = track.candidate(); final BreakableNodeV child = (BreakableNodeV)svg.getFirstChild(); boolean wasChanged = child.setCountNode( candidate ); final CountNodeJS downstreamOld = downstream; downstream = track.downstream(); if( candidate == null || candidate.isVoter() && downstream == null ) { setVisible( false ); } else { if( !wasChanged && !ObjectX.nullEquals(downstreamOld,downstream) ) wasChanged = true; palette.occupantCount = 0; palette.outflowTotal = 0; palette.receiveTotal = 0; final JsArray coNodes = downstream == null? candidate.count().baseCandidates(): downstream.voters(); for( int n = 0; n < DART_SECTOR_MAX; ++n ) { final CountNodeJS coNode = coNodes.get( n ); if( coNode == null ) continue; ++palette.occupantCount; final SacRegisterJS_v reg = coNode.voteRegister(); palette.outflowTotal += reg.carryVolume() + reg.castVolume(); palette.receiveTotal += reg.receiveVolume(); } setVisible( true ); if( painter != null && wasChanged ) painter.repaint(); } } } // ==================================================================================== private final class Painter extends NodeVPainter { Painter( final VoteTrackV trackV ) { super( CandidateV.this ); peerBoardContainer = trackV.peersBoardV().painter().container(); } private final UIObject peerBoardContainer; // standard for sizing @Override void repaint( final int width, final float protrusion, final int y, final float halfThickness ) { if( !isVisible() ) return; // await call from remodel final BreakableNodeV child = (BreakableNodeV)svg.getFirstChild(); final CountNodeJS candidate = child.getCountNode(); palette.recalibrate( peerBoardContainer.getOffsetWidth(), protrusion, /*isEndBoard*/!candidate.isVoter() ); /* The question is whether it would be an end board when staged, because the point is to determine its size when staged in the peer board. !isVoter is a sufficient test. We already know it's a candidate. So, if it's not voting, then it must be a root candidate (end board). Otherwise it's cyclic and will never stage as an end board. */ final float length = palette.calculateLength( candidate ); final float lengthMax = width - protrusion; // --lengthMax; /* correct overflow that blunts arrow (Firefox 9). Setting // overflow style 'visible' on svg element is no help. */ /// no longer needed, Firefox 16 child.repaint( /*x*/0, length, protrusion, y, halfThickness, lengthMax ); GWTX.i().bus().fireEventFromSource( new Change(), Painter.this ); } } }