package votorola.s.gwt.stage.poll; // Copyright 2012, 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.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.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 poll view implemented as an HTML image. */ final class PollV 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. */ PollV( final Spool spool ) { imager = new Imager( spool ); sensor = new Sensor( spool ); new Stager(); } static { final String loc = App.i().staticContextLocation(); prefetch( loc, "Issue" ); prefetch( src( loc, "IssueStaged" )); prefetch( loc, "Norm" ); prefetch( loc, "Law" ); prefetch( loc, "Plan" ); prefetch( loc, "Policy" ); prefetch( loc, "Office" ); prefetch( loc, "Assembly seat" ); prefetch( loc, "Executive office" ); } // ------------------------------------------------------------------------------------ /** The poll on which this view is modelled, or null if none is modelled. * * @see #setPoll(Poll) */ Poll getPoll() { return poll; } private Poll poll; /** Sets the poll on which this view is modelled. * * @see #getPoll() */ void setPoll( final Poll pollNew ) { if( pollNew == null ) { if( poll == null ) return; // very common, nothing to do poll = null; pollNameLeft = ""; pollNameRight = ""; } else { poll = pollNew; final String name = poll.name(); int c = name.lastIndexOf( '/' ); if( c == -1 ) { pollNameLeft = ""; pollNameRight = name; } else { ++c; pollNameLeft = name.substring( 0, c ); pollNameRight = name.substring( c ); } } enabler.reEnable(); imager.reImage(); sensor.relight(); } /** The left portion of the poll name extending to the final slash character '/' * inclusive, or the empty string if there is no slash character or no poll. */ String pollNameLeft() { return pollNameLeft; } String pollNameLeft = ""; /** The right portion of the poll name immediately following the left, or the empty * string if there is no poll. */ String pollNameRight() { return pollNameRight; } String pollNameRight = ""; // ==================================================================================== /** 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 poll view associated with this lighting sensor. */ PollV view() { return PollV.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 Imager imager; private final Sensor sensor; private static void prefetch( final String staticContextLocation, final String issueType ) { prefetch( src( staticContextLocation, issueType )); prefetch( src( staticContextLocation, issueType + "Stub" )); } private static String src( final String staticContextLocation, String issueType ) { issueType = issueType.replace( ' ', '_' ); return staticContextLocation + "/stage/poll/i" + issueType + ".png"; } // ==================================================================================== private final class Enabler { Enabler() { setVisible( false ); } private boolean isEnabled; void reEnable() { final boolean toEnable = poll != null; if( toEnable ) { if( !isEnabled ) { isEnabled = true; setVisible( true ); } } else if( isEnabled ) { isEnabled = false; setVisible( false ); } } } // ==================================================================================== 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(); } boolean isCued; private void reImage() { if( poll == null ) return; final String issueType = poll.issueType(); final String suffix; if( issueType.equals( "Issue" )) { if( poll.name().equals(Stage.i().getPollName()) ) suffix = "Staged"; // extra bright else if( isCued ) suffix = ""; else suffix = "Stub"; } else { if( isCued || poll.name().equals(Stage.i().getPollName()) ) suffix = ""; else suffix = "Stub"; } setUrl( src( App.i().staticContextLocation(), issueType + suffix )); } private final Spool spool; // - 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( "pollName".equals(e.propertyName()) && !spool.isUnwinding() ) reImage(); } } // ==================================================================================== private final class Stager implements ClickHandler { Stager() { addClickHandler( Stager.this ); } // no need to unregister, registry does not outlive this listener public void onClick( ClickEvent _e ) { if( poll == null ) return; // though likely impossible final Stage stage = Stage.i(); final String nameS = stage.getPollName(); String name = poll.name(); if( name.equals( nameS )) name = null; // unstage stage.setPollName( name ); } } }