001package votorola.s.gwt.stage.link; // 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 votorola.a.count.gwt.*;
004import votorola.g.web.gwt.*;
005import votorola.g.web.gwt.event.*;
006import votorola.s.gwt.stage.*;
007import votorola.s.gwt.stage.light.*;
008import votorola.s.gwt.stage.vote.*;
009
010
011/** The stage light for the {@linkplain NominalDifferenceTargeter nominal difference
012  * targeter}.
013  */
014public final class NominalDifferenceLight extends DifferenceLight<LightableDifference1>
015{
016
017
018    /** Creates the {@linkplain #i() single instance} of NominalDifferenceLight.
019      */
020    NominalDifferenceLight( final Stage stage, NominalDifferenceTargeter _targeter )
021    {
022        super( _targeter.voteTrack() );
023        targeter = _targeter;
024        new Mapper();
025        new MapActuatorRelay( baseActuator() );
026        init( stage );
027    }
028
029
030
031    /** The single instance of NominalDifferenceLight, if there is one.
032      */
033    public static NominalDifferenceLight i() { return instance; }
034
035
036        private static NominalDifferenceLight instance;
037
038        {
039            if( instance != null ) throw new IllegalStateException();
040
041            instance = NominalDifferenceLight.this;
042        }
043
044
045
046   // - D i f f e r e n c e - L i g h t --------------------------------------------------
047
048
049    protected LightableDifference1 differenceFor( final String personName, final String pollName )
050    {
051        final CountJS count = targeter.voteTrack().count();
052        final LightableDifference1 diff;
053        if( count == null || !count.pollName().equals( pollName )) diff = null;
054        else diff = targeter.diffMap().get( personName );
055        return diff;
056    }
057
058
059
060    protected boolean hasDiffLooks() { return false; }
061
062
063
064    protected void run( final LightableDifference.Runner<LightableDifference1> runner )
065    {
066        for( LightableDifference1 diff: targeter.diffMap().values() ) runner.run( diff );
067    }
068
069
070
071   // - L i g h t ------------------------------------------------------------------------
072
073
074    public @Override Actuator_DL assignActuator( final PositionSensor sensor )
075    {
076        final Actuator_DL actuator = super.assignActuator( sensor );
077        if( actuator != null ) new MapActuatorRelay( actuator );
078        return actuator;
079    }
080
081
082
083//// P r i v a t e ///////////////////////////////////////////////////////////////////////
084
085
086    private final NominalDifferenceTargeter targeter;
087
088
089
090   // ====================================================================================
091
092
093    private final class MapActuatorRelay implements ChangeHandler
094    {
095
096        MapActuatorRelay( Actuator_DL _actuator )
097        {
098            actuator = _actuator;
099            GWTX.i().bus().addHandlerToSource( Change.TYPE, /*source*/targeter.diffMapS,
100              MapActuatorRelay.this ); // no need to unregister, registry does not outlive this listener
101        }
102
103
104        private final Actuator_DL actuator;
105
106
107       // - C h a n g e - H a n d l e r - ------------------------------------------------
108
109
110        public final void onChange( Change _e ) { actuator.changed(); }
111
112    }
113
114
115
116   // ====================================================================================
117
118
119    private final class Mapper implements ChangeHandler
120    {
121
122        Mapper()
123        {
124            GWTX.i().bus().addHandlerToSource( Change.TYPE, /*source*/targeter.diffMapS,
125              Mapper.this ); // no need to unregister, registry does not outlive this listener
126            remap();
127        }
128
129
130        private void remap()
131        {
132            if( /*isMapped*/targeter.diffMap().size() > 0 )
133            {
134                if( !wasMapped )
135                {
136                    NominalDifferenceLight.i().addStageRelClasses();
137                    wasMapped = true;
138                }
139            }
140            else if( wasMapped )
141            {
142                removeStageRelClasses();
143                wasMapped = false;
144            }
145        }
146
147
148        private boolean wasMapped;
149
150
151       // - C h a n g e - H a n d l e r - ------------------------------------------------
152
153
154        public final void onChange( Change _e ) { remap(); }
155
156    }
157
158
159}