package votorola.s.gwt.stage.talk; // 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. import java.util.Date; 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.voter.IDPair; import votorola.a.web.gwt.App; import votorola.g.lang.*; import votorola.g.web.gwt.event.Change; import votorola.g.web.gwt.event.ChangeHandler; import votorola.s.gwt.stage.*; import votorola.s.gwt.stage.light.*; import static com.google.gwt.dom.client.Style.Unit.PX; /** A projection around the mouse cursor showing the personal name (above) and display * title (below) of the cued node. */ final class HeadsUpDisplay extends Composite implements Actuator, Light, ChangeHandler { /** Creates a HeadsUpDisplay. Create at most one for the entire life of the trackV registry, * as currently it does not unregister its listeners there. */ HeadsUpDisplay( final TalkTrackV trackV ) { Stage.i().lightBank().addLight( HeadsUpDisplay.this ); new Shuttler( trackV ); redisplay(); } // - C h a n g e H a n d l e r -------------------------------------------------------- @Override public void onChange(Change e) { 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 TalkV.Sensor sensor ) { if( sensor == litSensor ) redisplay(); } public Light light() { return HeadsUpDisplay.this; } public void out( final TalkV.Sensor sensor ) { if( sensor != litSensor ) return; // already out litSensor = null; redisplay(); } public void over( final TalkV.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 TalkV.Sensor sensor ) { return HeadsUpDisplay.this; } public final TalkV.Sensor tryCast( final Sensor sensor ) { return sensor instanceof TalkV.Sensor? (TalkV.Sensor)sensor: null; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// @UiField @Warning("non-API") SpanElement bottomField; @UiField @Warning("non-API") DivElement bottomRail; @UiField @Warning("non-API") SpanElement bottomShuttle; // foot of the bubble @UiField @Warning("non-API") DivElement bottomFootRail; @UiField @Warning("non-API") SpanElement bottomFootShuttle; private final String bubbleFootLeftSrc = App.getServletContextLocation() + "/stage/talk/bubbleFootLeft.png"; private final String bubbleFootRightSrc = App.getServletContextLocation() + "/stage/talk/bubbleFootRight.png"; @UiField @Warning("non-API") ImageElement bottomBubbleFoot; { bottomBubbleFoot.setSrc(bubbleFootRightSrc); bottomBubbleFoot.setSrc(bubbleFootLeftSrc); } private final Text bottomText = Document.get().createTextNode( "" ); { bottomField.appendChild( bottomText ); } private TalkV.Sensor litSensor; private void redisplay() { final Style style = getStyleElement().getStyle(); if( litSensor == null ) { style.setOpacity( 0 ); } // hide the display else { DiffMessageJS msg = litSensor.view().getMsg(); final String senderMne = IDPair.buildUserMnemonic(msg.sender(), new StringBuilder()).toString(); final String addresseeMne = IDPair.buildUserMnemonic(msg.addressee(), new StringBuilder()).toString(); leftText.setData(relativeAge((long)msg.sentDate())); senderText.setData(senderMne); addresseeText.setData(addresseeMne); bottomText.setData( msg.message().content() ); style.clearOpacity(); // to default, opaque and thus visible } } private String relativeAge(long sentDate) { final long day = 1000 * 60 * 60 * 24; // ms final long week = day * 7; final long month= day * 30; final long year= day * 365; final long difference = new Date().getTime()-sentDate; if (difference>year) { final long years = difference/year; if(years == 1) { return "1 year"; } else { return years + " years"; } } else if(difference>month) { final long months = difference/month; if(months == 1) { return "1 month"; } else { return months + " months"; } } else if(difference>week) { final long weeks = difference/week; if(weeks == 1) { return "1 week"; } else { return weeks + " weeks"; } } else { final long days = difference/day; if(days == 1) { return "1 day"; } else { return days + " days"; } } } @UiField @Warning("non-API") SpanElement leftField; private final Text leftText = Document.get().createTextNode( "" ); { leftField.appendChild( leftText ); } @UiField @Warning("non-API") SpanElement rightField; @UiField @Warning("non-API") SpanElement senderField; private final Text senderText = Document.get().createTextNode( "" ); { senderField.appendChild( senderText ); } @UiField @Warning("non-API") SpanElement addresseeField; private final Text addresseeText = Document.get().createTextNode( "" ); { addresseeField.appendChild( addresseeText ); } @UiField @Warning("non-API") DivElement topRail; @UiField @Warning("non-API") DivElement topShuttle; // ==================================================================================== /** A controller to position this display. */ private final class Shuttler implements MouseMoveHandler, MouseOverHandler { Shuttler( final TalkTrackV 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 stageWidth = StageV.i().getOffsetWidth(); final int left = e.getX() - /*aesthetic adjust*/1; final int widthTop = topShuttle.getClientWidth(); final int widthBottom = bottomShuttle.getClientWidth(); final int xTop = (left + widthTop/2 > stageWidth) ? stageWidth - widthTop : left - widthTop/2; final boolean rightBordered = left + widthBottom/2 > stageWidth; final int xBottom = rightBordered ? stageWidth - widthBottom : left - widthBottom/2; bottomBubbleFoot.setSrc(rightBordered ? bubbleFootRightSrc : bubbleFootLeftSrc); bottomFootShuttle.getStyle().setLeft( rightBordered ? left - bottomBubbleFoot.getClientWidth(): left, PX ); // if( left < 0 ) left = 0; // keep left field visible topShuttle.getStyle().setLeft( xTop, PX ); bottomShuttle.getStyle().setLeft( xBottom, 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: topRail.getStyle().setTop( y, PX ); bottomFootRail.getStyle().setTop( y, PX ); bottomRail.getStyle().setTop( y - 3, 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 ); } } }