package votorola.s.gwt.stage.vote; // 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.event.logical.shared.ResizeEvent; import com.google.gwt.event.logical.shared.ResizeHandler; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.*; import com.google.web.bindery.event.shared.HandlerRegistration; import votorola.g.hold.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; /** A device to calculate the dimensions and drawing parameters of node views. This is * complicated because the rendered size of the SVG element is unknown, its offset * dimensions always being reported as zero (at least with Firefox 9). As a workaround, * the calculations are based instead on the rendered size of the HTML container. For * this to work correctly, it is crucial that the sizes of the two match. SVG overflow * cannot be * made visible in Firefox; so if the SVG element happens to be smaller than the * container, then its overflow will end up being truncated ("hidden"). Ideally the * element should be set to 100% of the container size in vote/track.css. */ abstract class NodeVPainter implements PropertyChangeHandler, ResizeHandler { /** Partially creates a NodeVPainter for {@linkplain #init(Spool) init} to * finish. * * @see #container() */ NodeVPainter( MajorV _container ) { container = _container; } /** Completes the creation of this NodeVPainter and activates it. Call once * only, preferably after the {@linkplain #container() HTML container} is rendered. * * @param _spool the spool for the release of associated holds. When unwound it * releases the holds of the painter and thereby disables it. */ final void init( Spool _spool ) { spool = _spool; GWTX.i().bus().addHandlerToSource( PropertyChange.TYPE, /*source*/container, NodeVPainter.this ); // no need to unregister, registry does not outlive the handler // svg.addResizeHandler( NodeVPainter.this ); /// no effect, as size of OMSVGSVGElement is stable, apparently related to logical size of drawing spool.add( new Hold() { final HandlerRegistration hR = Window.addResizeHandler( NodeVPainter.this ); public void release() { hR.removeHandler(); } }); } // ------------------------------------------------------------------------------------ /** The HTML container whose rendered size determines the dimensions of the node * views. */ final UIObject container() { return container; } private final UIObject container; /** The margin to respect below the node view in pixels. */ static final int MARGIN_BOTTOM = 3/*apparent*/ - NodeV.STROKE_WIDTH / 2; /** The margin to respect above the node view in pixels. */ static final int MARGIN_TOP = 2/*apparent*/ - NodeV.STROKE_WIDTH / 2; /** Forces a recalibration. */ final void repaint() { repaint( container.getOffsetWidth() ); } /** Effects a recalibration and redraws the node views accordingly. * * @param width the rendered width of the {@linkplain #container() HTML * container}. * @see NodeV#repaint(float,float,float,int,float) */ abstract void repaint( int width, float protrusion, int y, float halfThickness ); // - P r o p e r t y - C h a n g e - H a n d l e r ------------------------------------ public void onPropertyChange( final PropertyChange e ) { if( spool.isUnwinding() ) return; if( e.getSource() == container && e.propertyName().equals("visible") && container.isVisible() ) // becomes visible { if( widthCalibrated == -1 ) repaint(); // width changed meantime, so repaint } } // - R e s i z e - H a n d l e r -------------------------------------------------- public final void onResize( ResizeEvent _e ) { if( spool.isUnwinding() ) return; final int width = container.getOffsetWidth(); if( width == widthCalibrated ) return; repaint( width ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private void repaint( final int width ) { if( !container.isVisible() ) { widthCalibrated = -1; // skipping calibration, therefore no width is calibrated return; } widthCalibrated = width; final int height = container.getOffsetHeight(); final float halfThickness = (height - MARGIN_TOP - MARGIN_BOTTOM) / 2f; final float protrusion = halfThickness; repaint( width, protrusion, /*y*/MARGIN_TOP, halfThickness ); } private Spool spool; // final after init private int widthCalibrated = -1; /* guard against redundant resize events, assumes that window resize cannot affect container height, only width */ }