001package 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.
002
003import com.google.gwt.dom.client.*;
004import com.google.web.bindery.event.shared.HandlerRegistration;
005import org.vectomatic.dom.svg.*;
006import votorola.a.count.gwt.*;
007import votorola.g.web.gwt.*;
008import votorola.g.web.gwt.event.*;
009import votorola.s.gwt.stage.*;
010import votorola.s.gwt.stage.vote.*;
011
012
013/** A component of the bridge/stage {@linkplain DIn tie overlay} that visually ties the
014  * other (non-anchor) drafter's link (tLink) at top left of the bridge scene to the
015  * corresponding position in the vote track.  It comprises an {@linkplain AlterLine alter
016  * line} and {@linkplain AlterBracket alter bracket}.<pre>
017  *
018  *    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  vote track
019  *                                 |                |
020  *                                 +----------------+  alter
021  *                alter line               |          bracket
022  *    tLink  +-----------------------------+</pre>
023  */
024final class AlterTie extends OMSVGGElement
025{
026
027
028    /** Creates an AlterTie.  Create at most one for the entire life of the document
029      * as currently it does not unregister its listeners or otherwise clean up after
030      * itself.
031      */
032    AlterTie( final NodeV.Box nodeBox, VoteTrack _track )
033    {
034        track = _track;
035        setVisible( false );
036        new Initializer( nodeBox );
037    }
038
039
040
041    private static AlterTie instance;
042
043        {
044            if( instance != null ) throw new IllegalStateException();
045
046            instance = AlterTie.this;
047        }
048
049
050
051   // ------------------------------------------------------------------------------------
052
053
054    /** Answers whether this tie is nominally visible.  Returns false if the display style
055      * is "none", true otherwise.
056      *
057      *     @see #setVisible(boolean)
058      */
059    final boolean isVisible() { return !"none".equals( getStyle().getDisplay() ); }
060
061
062        /** Sets the visibility of this tie.
063          *
064          *     @see #isVisible()
065          */
066        private final void setVisible( final boolean toBe )
067        {
068            if( toBe )
069            {
070                if( !isVisible() )
071                {
072                    getStyle().clearDisplay();
073                    getFirstChild().repaint();
074                }
075            }
076            else getStyle().setDisplay( Style.Display.NONE );
077        }
078
079
080
081   // - O  M - N o d e -------------------------------------------------------------------
082
083
084    public @Override AlterBracket getFirstChild() { return (AlterBracket)super.getFirstChild(); }
085
086
087
088//// P r i v a t e ///////////////////////////////////////////////////////////////////////
089
090
091    private final VoteTrack track;
092
093
094
095   // ====================================================================================
096
097
098    private final class Initializer implements ChangeHandler
099    {
100
101        Initializer( NodeV.Box _nodeBox )
102        {
103            nodeBox = _nodeBox;
104            name = DifferenceLight.sceneName();
105            if( name == null ) throw new IllegalStateException();
106
107            if( nodeBox.isVisible() ) init();
108            else // wait for it to repaint, then both visible and ready for AlterBracket.Painter
109            {
110                hR = GWTX.i().bus().addHandlerToSource( Change.TYPE, /*source*/nodeBox.painter(),
111                  Initializer.this );
112            }
113        }
114
115
116        private HandlerRegistration hR; // set if registered, cleared after unregistered
117
118
119        private final String name;
120
121
122        public final void onChange( Change _e )
123        {
124            if( hR == null ) return;
125
126            hR.removeHandler();
127            hR = null;
128            init();
129        }
130
131
132        private final NodeV.Box nodeBox;
133
134
135        private void init()
136        {
137            for( final NodeV v: nodeBox.nodeViews() )
138            {
139                final CountNodeJS node = v.getCountNode();
140                if( node != null && node.name().equals(name) )
141                {
142                    final AlterLine alterLine = new AlterLine();
143                    appendChild( new AlterBracket( nodeBox, v, alterLine ));
144                      // the requiste initial repaint is called in setVisible, via Visibilizer below
145                    appendChild( alterLine );
146                    new Visibilizer();
147                    break;
148                }
149            }
150        }
151
152    }
153
154
155
156   // ====================================================================================
157
158
159    private final class Visibilizer implements ChangeHandler
160    {
161
162        Visibilizer()
163        {
164            GWTX.i().bus().addHandlerToSource( Change.TYPE, /*source*/track, Visibilizer.this );
165              // no need to unregister, registry does not outlive this listener
166            refresh();
167        }
168
169
170        public final void onChange( Change _e ) { refresh(); }
171
172
173        private void refresh()
174        {
175            final CountJS count = track.count();
176            setVisible( count != null && count.pollName().equals( Stage.i().getDefaultPollName() ));
177              // invisible for other polls, as nodeBox and nodeV are currently fixed
178        }
179
180    }
181
182
183}