package votorola.s.gwt.stage.vote; // Copyright 2013, Michael Allan, 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.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.a.count.gwt.*; 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 above or below the mouse cursor showing the display title or personal * name of the cued node. */ 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( VoteTrackV _trackV, final Spool spool ) { trackV = _trackV; if( trackV == VoteTrackV.iBottomFixed() ) { rail = topRail; shuttle = topShuttle; text = topText; } else { rail = bottomRail; shuttle = bottomShuttle; text = bottomText; } final Element div = rail.getParentElement(); // layout div.display.top or bottom div.addClassName( "inside" ); div.getStyle().setDisplay( Style.Display.BLOCK ); Stage.i().lightBank().addLight( HeadsUpDisplay.this ); spool.add( new Hold() { public void release() { Stage.i().lightBank().removeLight( HeadsUpDisplay.this ); } }); new Shuttler(); 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 NodalSensor sensor ) { if( sensor == litSensor ) redisplay(); } public Light light() { return HeadsUpDisplay.this; } public void out( final NodalSensor sensor ) { if( sensor != litSensor ) return; // already out litSensor = null; redisplay(); } public void over( final NodalSensor 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 NodalSensor sensor ) { return sensor.box().trackV() == trackV? HeadsUpDisplay.this: null; // actuate only for sensors in the same track view } public final NodalSensor tryCast( final Sensor sensor ) { return sensor instanceof NodalSensor? (NodalSensor)sensor: null; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private static final String BLANK_TEXT = "\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"; // non-breaking spaces @UiField @Warning("non-API") SpanElement bottomField; @UiField @Warning("non-API") DivElement bottomRail; @UiField @Warning("non-API") SpanElement bottomShuttle; private final Text bottomText = Document.get().createTextNode( "" ); { bottomField.appendChild( bottomText ); } private NodalSensor litSensor; private final DivElement rail; // top or bottom, whichever is displayed private @Warning("init call") void redisplay() { final Style style = getStyleElement().getStyle(); if( litSensor == null ) style.setOpacity( 0 ); // hide the display else { String s = litSensor.displayTitle(); if( s == null ) { s = litSensor.personName(); if( s == null ) s = BLANK_TEXT; } text.setData( s ); style.clearOpacity(); // to default, opaque and thus visible } } private final SpanElement shuttle; // top or bottom, whichever is displayed private final Text text; // top or bottom, whichever is displayed @UiField @Warning("non-API") SpanElement topField; @UiField @Warning("non-API") DivElement topRail; @UiField @Warning("non-API") SpanElement topShuttle; private final Text topText = Document.get().createTextNode( "" ); { topField.appendChild( topText ); } private final VoteTrackV trackV; // ==================================================================================== /** A controller to position this display. */ private final class Shuttler implements MouseMoveHandler, MouseOverHandler { Shuttler() { 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() - /*aesthetic adjust*/1; // if( left < 0 ) left = 0; // keep left field visible shuttle.getStyle().setLeft( left , PX ); final int y = e.getY(); // Adjusting the Y position too, so the fields behave together as a // supplementary cursor. This helps the user to guide the mouse, which is // otherwise difficult because the cursor in the vote track (unlike poll and // talk tracks) is relatively small compared to the large, moving display // fields, like a dog running alongside a pair of cattle. Here we position // the top field to abut the visible top of the node view (fill) when the // cursor is at the bottom limit of the view, and vice versa for the bottom // field. The user need only keep them from touching the edges of the view. // topRail.getStyle().setTop( y + NodeVPainter.MARGIN_TOP // + NodeV.STROKE_WIDTH, PX ); // // not sure why stroke needn't be halved here (Firefox 16) // bottomRail.getStyle().setTop( y - NodeVPainter.MARGIN_BOTTOM // - NodeV.STROKE_WIDTH / 2, PX ); // // this may be off by one pixel for very large font sizes (Firefox 16) //// but when SVG not absolutely positioned, it happens to be much simpler: rail.getStyle().setTop( y, 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 ); } } }