package votorola.s.gwt.stage.poll; // Copyright 2012-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. import com.google.gwt.core.client.GWT; import com.google.gwt.dom.client.*; import com.google.gwt.event.dom.client.*; import com.google.gwt.uibinder.client.*; import com.google.gwt.user.client.ui.*; import votorola.g.hold.*; import votorola.g.lang.*; import votorola.s.gwt.stage.*; import votorola.s.gwt.stage.light.*; import static com.google.gwt.dom.client.Style.Unit.PX; /** A projection just above the mouse cursor showing the name of the cued poll. */ final class HeadsUpDisplay extends Composite implements Actuator, Light { /** Creates a HeadsUpDisplay. Create at most one for the entire life of the trackV * registry, as currently it does not unregister its listeners there. * * @param spool the spool for the release of associated holds. When unwound it * releases the holds of this display and thereby disables it. */ HeadsUpDisplay( final PolltrackV trackV, final Spool spool ) { Stage.i().lightBank().addLight( HeadsUpDisplay.this ); spool.add( new Hold() { public void release() { Stage.i().lightBank().removeLight( HeadsUpDisplay.this ); } }); new Shuttler( trackV ); redisplay(); } // ` e a r l y ```````````````````````````````````````````````````````````````````````` @Warning("non-API") interface UiBinderI extends UiBinder {} { final UiBinderI uiBinder = GWT.create( UiBinderI.class ); initWidget( uiBinder.createAndBindUi( HeadsUpDisplay.this )); } // - A c t u a t o r ------------------------------------------------------------------ public void changed( final PollV.Sensor sensor ) { if( sensor == litSensor ) redisplay(); } public Light light() { return HeadsUpDisplay.this; } public void out( final PollV.Sensor sensor ) { if( sensor != litSensor ) return; // already out litSensor = null; redisplay(); } public void over( final PollV.Sensor sensor ) { if( sensor == litSensor ) return; // already over litSensor = sensor; redisplay(); } // - C o m p o s i t e ---------------------------------------------------------------- protected @Override HTMLPanel getWidget() { return (HTMLPanel)super.getWidget(); } // - L i g h t ------------------------------------------------------------------------ public Actuator assignActuator( final PollV.Sensor sensor ) { return HeadsUpDisplay.this; } public final PollV.Sensor tryCast( final Sensor sensor ) { return sensor instanceof PollV.Sensor? (PollV.Sensor)sensor: null; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// /** The fixed width of the expander element that contains the left field. It is large * enough for almost any poll name. Fixing it enables the shuttle to be positioned * such that the boundary between the left and right fields is directly above the * cued poll. */ private static final int EXPANDER_WIDTH_PX = 150; // same as span.expander width in track.css @UiField @Warning("non-API") SpanElement leftField; private final Text leftText = Document.get().createTextNode( "" ); { leftField.appendChild( leftText ); } private PollV.Sensor litSensor; private @Warning("init call") void redisplay() { if( litSensor == null ) shuttle.getStyle().setOpacity( 0 ); // hide it else { final PollV litView = litSensor.view(); leftText.setData( litView.pollNameLeft() ); rightText.setData( litView.pollNameRight() ); shuttle.getStyle().clearOpacity(); // to default, opaque and thus visible } } @UiField @Warning("non-API") SpanElement rightField; private final Text rightText = Document.get().createTextNode( "" ); { rightField.appendChild( rightText ); } @UiField @Warning("non-API") DivElement shuttle; // ==================================================================================== /** A controller to position this display. */ private final class Shuttler implements MouseMoveHandler, MouseOverHandler { Shuttler( final PolltrackV trackV ) { trackV.addDomHandler( Shuttler.this, MouseMoveEvent.getType() ); // no need to unregister, registry does not outlive the handler trackV.addDomHandler( Shuttler.this, MouseOverEvent.getType() ); // " } private void reposition( final MouseEvent e ) { final int left = e.getX() - EXPANDER_WIDTH_PX - /*aesthetic adjust*/4; /// need rendered width of field to position properly final int visibleOffset = leftField.getClientWidth()- EXPANDER_WIDTH_PX + 2; if( left < visibleOffset ) shuttle.getStyle().setLeft( visibleOffset, PX ); else shuttle.getStyle().setLeft( left , PX ); } // - M o u s e - M o v e - H a n d l e r ------------------------------------------ public void onMouseMove( final MouseMoveEvent e ) { reposition( e ); } // - M o u s e - O v e r - H a n d l e r ------------------------------------------ public void onMouseOver( final MouseOverEvent e ) { reposition( e ); } } }