package votorola.s.gwt.stage; // 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.dom.client.*; import com.google.gwt.event.dom.client.*; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.ui.*; import com.google.web.bindery.event.shared.HandlerRegistration; import java.util.*; import votorola.a.web.gwt.*; import votorola.g.hold.*; import votorola.g.lang.*; import votorola.g.web.gwt.*; import votorola.g.web.gwt.event.*; /** A warning view rendered as a small red triangle. The view is ordinarily invisible, * but appears whenever one or more {@linkplain Stage#warnings() warnings} are issued. * Clicking on it displays a summary of the actual warnings in a separate popup window. */ public final class WarnV extends Image { /** Constructs a WarnV. */ public WarnV() { super( App.i().staticContextLocation() + "/stage/warn.png" ); addStyleName( "WarnV" ); new Enabler(); new Popper(); } // - W i d g e t ---------------------------------------------------------------------- public @Override void removeFromParent() { if( getParent() != null ) throw new UnsupportedOperationException(); } protected @Override void onUnload() { super.onUnload(); spool.unwind(); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Spool spool = new Spool1(); // ==================================================================================== private final class Enabler implements PropertyChangeHandler { Enabler() { spool.add( new Hold() { final HandlerRegistration hR = GWTX.i().bus().addHandlerToSource( PropertyChange.TYPE, /*source*/Stage.i(), Enabler.this ); public void release() { hR.removeHandler(); } }); reEnable(); // init } public final void onPropertyChange( final PropertyChange e ) { if( "warnings".equals(e.propertyName()) && !spool.isUnwinding() ) reEnable(); } private void reEnable() { final Style style = getElement().getStyle(); if( Stage.i().warnings().size() > 0 ) style.clearDisplay(); else style.setDisplay( Style.Display.NONE ); } } // ==================================================================================== private final class Popper implements ClickHandler { Popper() { addClickHandler( Popper.this ); } // no need to unregister, registry does not outlive this listener public void onClick( ClickEvent _e ) { final StringBuilder b = GWTX.stringBuilderClear(); final List warnings = Stage.i().warnings(); final int wN = warnings.size(); if( wN > 0 ) { int w = 0; for( ;; ) { final String warning = warnings.get( w ); b.append( warning ); ++w; if( w >= wN ) break; b.append( '\n' ); b.append( '\n' ); } } else assert false; Window.alert( b.toString() ); } } }