package votorola.s.gwt.wic; // 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.web.bindery.event.shared.HandlerRegistration; import org.vectomatic.dom.svg.*; import votorola.a.count.gwt.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; import votorola.s.gwt.stage.*; import votorola.s.gwt.stage.vote.*; /** A component of the bridge/stage {@linkplain DIn tie overlay} that visually ties the * other (non-anchor) drafter's link (tLink) at top left of the bridge scene to the * corresponding position in the vote track. It comprises an {@linkplain AlterLine alter * line} and {@linkplain AlterBracket alter bracket}.
  *
  *    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  vote track
  *                                 |                |
  *                                 +----------------+  alter
  *                alter line               |          bracket
  *    tLink  +-----------------------------+
*/ final class AlterTie extends OMSVGGElement { /** Creates an AlterTie. Create at most one for the entire life of the document * as currently it does not unregister its listeners or otherwise clean up after * itself. */ AlterTie( final NodeV.Box nodeBox, VoteTrack _track ) { track = _track; setVisible( false ); new Initializer( nodeBox ); } private static AlterTie instance; { if( instance != null ) throw new IllegalStateException(); instance = AlterTie.this; } // ------------------------------------------------------------------------------------ /** Answers whether this tie is nominally visible. Returns false if the display style * is "none", true otherwise. * * @see #setVisible(boolean) */ final boolean isVisible() { return !"none".equals( getStyle().getDisplay() ); } /** Sets the visibility of this tie. * * @see #isVisible() */ private final void setVisible( final boolean toBe ) { if( toBe ) { if( !isVisible() ) { getStyle().clearDisplay(); getFirstChild().repaint(); } } else getStyle().setDisplay( Style.Display.NONE ); } // - O M - N o d e ------------------------------------------------------------------- public @Override AlterBracket getFirstChild() { return (AlterBracket)super.getFirstChild(); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final VoteTrack track; // ==================================================================================== private final class Initializer implements ChangeHandler { Initializer( NodeV.Box _nodeBox ) { nodeBox = _nodeBox; name = DifferenceLight.sceneName(); if( name == null ) throw new IllegalStateException(); if( nodeBox.isVisible() ) init(); else // wait for it to repaint, then both visible and ready for AlterBracket.Painter { hR = GWTX.i().bus().addHandlerToSource( Change.TYPE, /*source*/nodeBox.painter(), Initializer.this ); } } private HandlerRegistration hR; // set if registered, cleared after unregistered private final String name; public final void onChange( Change _e ) { if( hR == null ) return; hR.removeHandler(); hR = null; init(); } private final NodeV.Box nodeBox; private void init() { for( final NodeV v: nodeBox.nodeViews() ) { final CountNodeJS node = v.getCountNode(); if( node != null && node.name().equals(name) ) { final AlterLine alterLine = new AlterLine(); appendChild( new AlterBracket( nodeBox, v, alterLine )); // the requiste initial repaint is called in setVisible, via Visibilizer below appendChild( alterLine ); new Visibilizer(); break; } } } } // ==================================================================================== private final class Visibilizer implements ChangeHandler { Visibilizer() { GWTX.i().bus().addHandlerToSource( Change.TYPE, /*source*/track, Visibilizer.this ); // no need to unregister, registry does not outlive this listener refresh(); } public final void onChange( Change _e ) { refresh(); } private void refresh() { final CountJS count = track.count(); setVisible( count != null && count.pollName().equals( Stage.i().getDefaultPollName() )); // invisible for other polls, as nodeBox and nodeV are currently fixed } } }