001package votorola.s.gwt.stage.vote; // Copyright 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.
002
003import com.google.gwt.dom.client.*;
004import votorola.g.lang.*;
005import votorola.g.web.gwt.*;
006import votorola.s.gwt.stage.*;
007import votorola.s.gwt.stage.light.*;
008
009
010/** A stage light to indicate the dart sector and cast relation of the {@linkplain
011  * NodeV.Sensor node sensor} pointed by the user.
012  */
013final class DartLight implements Light<NodeV.Sensor>
014{
015
016
017    /** Creates the {@linkplain #i() single instance} of DartLight.
018      */
019    DartLight()
020    {
021        Stage.i().lightBank().addLight( DartLight.this ); // no need to remove, co-extant
022    }
023
024
025
026    /** The single instance of DartLight.
027      */
028    static DartLight i() { return instance; }
029
030
031        private static DartLight instance;
032
033        {
034            if( instance != null ) throw new IllegalStateException();
035
036            instance = DartLight.this;
037        }
038
039
040
041   // - L i g h t ------------------------------------------------------------------------
042
043
044    public Actuator<NodeV.Sensor> assignActuator( final NodeV.Sensor sensor )
045    {
046        return new ActuatorD( sensor );
047    }
048
049
050
051    public final NodeV.Sensor tryCast( final Sensor sensor )
052    {
053        return sensor instanceof NodeV.Sensor? (NodeV.Sensor)sensor: null;
054    }
055
056
057
058
059//// P r i v a t e ///////////////////////////////////////////////////////////////////////
060
061
062    private ActuatorD actuatorLit;
063
064
065
066    private String bodyClassLit;
067
068
069
070   // ====================================================================================
071
072
073    private final class ActuatorD implements Actuator<NodeV.Sensor>
074    {
075
076        ActuatorD( final NodeV.Sensor sensor )
077        {
078            nodeV = sensor.view();
079            resetBodyClass();
080        }
081
082
083        private String bodyClass; // for the assigned sensor
084
085
086            private @Warning("init call") void resetBodyClass()
087            {
088                bodyClass = nodeV.isTightCycler()? "voDartLight-y":
089                    // tight cycle, to light both voter and candidate as they are same person
090                  "voDartLight-" + nodeV.dartLightClassName();
091            }
092
093
094        private final NodeV nodeV;
095
096
097        private void relight( final String bodyClassNew )
098        {
099            if( ObjectX.nullEquals( bodyClassLit, bodyClassNew )) return;
100
101            ElementX.replaceNullClassName( bodyClassLit, bodyClassNew,
102              Document.get().getBody() );
103            bodyClassLit = bodyClassNew;
104        }
105
106
107       // - A c t u a t o r --------------------------------------------------------------
108
109
110        public void changed( NodeV.Sensor sensor_ )
111        {
112            resetBodyClass();
113            if( actuatorLit == ActuatorD.this ) relight( bodyClass );
114        }
115
116
117        public Light<NodeV.Sensor> light() { return DartLight.this; }
118
119
120        public void out( final NodeV.Sensor sensor )
121        {
122            if( actuatorLit == ActuatorD.this ) actuatorLit = null;
123            relight( null );
124        }
125
126
127        public void over( final NodeV.Sensor sensor )
128        {
129            actuatorLit = ActuatorD.this;
130            relight( bodyClass );
131        }
132
133    }
134
135
136}