package votorola.s.gwt.stage.talk; // Copyright 2012, Michael Allan. 2013 Christian Weilbach. 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.event.dom.client.*; import com.google.gwt.user.client.ui.*; import com.google.web.bindery.event.shared.HandlerRegistration; import votorola.a.count.*; import votorola.a.diff.DiffLookJS; import votorola.a.diff.DiffLook; import votorola.a.web.gwt.*; import votorola.g.hold.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; import votorola.s.gwt.stage.*; import votorola.s.gwt.stage.light.*; /** A talk view implemented as an HTML image. */ final class TalkV extends Image { /** Constructs a PollV. * * @param spool the spool for the release of associated holds. When unwound it * releases the holds of the view and thereby disables it. */ TalkV( final Spool spool ) { addStyleName("talkV"); sensor = new Sensor( spool ); imager = new Imager( spool ); new Stager(); } private final Imager imager; private final static String selectedLoc = App.getServletContextLocation() + "/stage/talk/selected.png"; private final static String cuedLoc = App.getServletContextLocation() + "/stage/talk/cued.png"; private final static String normalLoc = App.getServletContextLocation() + "/stage/talk/normal.png"; static { prefetch( selectedLoc ); prefetch( cuedLoc ); prefetch( normalLoc ); } // ------------------------------------------------------------------------------------ /** The difference-message on which this view is modelled, or null if none is modelled. * * @see #setMsg(DiffMessageJS) */ DiffMessageJS getMsg() { return msg; } private DiffMessageJS msg = null; /** Sets the difference-message on which this view is modelled. * * @see #getMsg() */ void setMsg( final DiffMessageJS msgNew ) { if( msgNew == null ) { if( msg == null ) return; // very common, nothing to do msg = null; } else { msg = msgNew; } enabler.reEnable(); imager.reImage(); sensor.relight(); } // ==================================================================================== private final class Imager implements MouseOutHandler, MouseOverHandler, PropertyChangeHandler { Imager( Spool _spool ) { spool = _spool; addMouseOutHandler( Imager.this ); // no need to unregister, registry does not outlive the handler addMouseOverHandler( Imager.this ); // " spool.add( new Hold() { final HandlerRegistration hR = GWTX.i().bus().addHandlerToSource( PropertyChange.TYPE, /*source*/Stage.i(), Imager.this ); public void release() { hR.removeHandler(); } }); reImage(); } private final Spool spool; boolean isCued; private void reImage() { if(isStaged()) { setUrl( selectedLoc ); } else if(isCued) { setUrl( cuedLoc ); } else { setUrl( normalLoc ); } } // - M o u s e - O u t - H a n d l e r -------------------------------------------- public void onMouseOut( MouseOutEvent _e ) { isCued = false; reImage(); } // - M o u s e - O v e r - H a n d l e r ------------------------------------------ public void onMouseOver( MouseOverEvent _e ) { isCued = true; reImage(); } // - P r o p e r t y - C h a n g e - H a n d l e r -------------------------------- public final void onPropertyChange( final PropertyChange e ) { if( ( "message".equals(e.propertyName()) || "difference".equals(e.propertyName()) ) && !spool.isUnwinding() ) reImage(); } } // ==================================================================================== /** A lighting sensor for a poll view. */ final class Sensor extends Sensor1 implements MouseOutHandler, MouseOverHandler { private Sensor( final Spool spool ) { Stage.i().lightBank().addSensor( Sensor.this ); spool.add( new Hold() { public void release() { Stage.i().lightBank().removeSensor( Sensor.this ); } }); addMouseOutHandler( Sensor.this ); // no need to unregister, registry does not outlive the handler addMouseOverHandler( Sensor.this ); // " } private void relight() { changed(); } /** The talk view associated with this lighting sensor. */ TalkV view() { return TalkV.this; } // - M o u s e - O u t - H a n d l e r -------------------------------------------- public void onMouseOut( MouseOutEvent _e ) { out(); } // - M o u s e - O v e r - H a n d l e r ------------------------------------------ public void onMouseOver( MouseOverEvent _e ) { over(); } } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Enabler enabler = new Enabler(); private final Sensor sensor; // ==================================================================================== private final class Enabler { Enabler() { setVisible( false ); } private boolean isEnabled; void reEnable() { final boolean toEnable = msg != null; if( toEnable ) { if( !isEnabled ) { isEnabled = true; setVisible( true ); } } else if( isEnabled ) { isEnabled = false; setVisible( false ); } } } // ==================================================================================== private final class Stager implements ClickHandler { Stager() { addClickHandler( Stager.this ); } // no need to unregister, registry does not outlive this listener private final Stage stage = Stage.i(); public void onClick( ClickEvent _e ) { Message newMsg = msg.message(); DiffLookJS newDiff = msg.difference(); if( isStaged() ) { newMsg = null; newDiff = null; } stage.setMessage( newMsg ); stage.setDifference( newDiff ); } } private boolean isStaged() { if( msg == null ) { return false; } final Message msgS = Stage.i().getMessage(); final DiffLook diffS = Stage.i().getDifference(); return MessageJS.EQUATOR.equals(msg.message(), msgS) && DiffLookJS.EQUATOR.equals(msg.difference(), diffS); } }