001package 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.
002
003import com.google.gwt.dom.client.*;
004import com.google.gwt.event.dom.client.*;
005import com.google.gwt.user.client.Window;
006import com.google.gwt.user.client.ui.*;
007import com.google.web.bindery.event.shared.HandlerRegistration;
008import java.util.*;
009import votorola.a.web.gwt.*;
010import votorola.g.hold.*;
011import votorola.g.lang.*;
012import votorola.g.web.gwt.*;
013import votorola.g.web.gwt.event.*;
014
015
016/** A warning view rendered as a small red triangle.  The view is ordinarily invisible,
017  * but appears whenever one or more {@linkplain Stage#warnings() warnings} are issued.
018  * Clicking on it displays a summary of the actual warnings in a separate popup window.
019  */
020public final class WarnV extends Image
021{
022
023
024    /** Constructs a WarnV.
025      */
026    public WarnV()
027    {
028        super( App.i().staticContextLocation() + "/stage/warn.png" );
029        addStyleName( "WarnV" );
030        new Enabler();
031        new Popper();
032    }
033
034
035
036   // - W i d g e t ----------------------------------------------------------------------
037
038
039    public @Override void removeFromParent()
040    {
041        if( getParent() != null ) throw new UnsupportedOperationException();
042    }
043
044
045
046    protected @Override void onUnload()
047    {
048        super.onUnload();
049        spool.unwind();
050    }
051
052
053
054//// P r i v a t e ///////////////////////////////////////////////////////////////////////
055
056
057    private final Spool spool = new Spool1();
058
059
060
061   // ====================================================================================
062
063
064    private final class Enabler implements PropertyChangeHandler
065    {
066
067        Enabler()
068        {
069            spool.add( new Hold()
070            {
071                final HandlerRegistration hR = GWTX.i().bus().addHandlerToSource(
072                  PropertyChange.TYPE, /*source*/Stage.i(), Enabler.this );
073                public void release() { hR.removeHandler(); }
074            });
075            reEnable(); // init
076        }
077
078
079        public final void onPropertyChange( final PropertyChange e )
080        {
081            if( "warnings".equals(e.propertyName()) && !spool.isUnwinding() ) reEnable();
082        }
083
084
085        private void reEnable()
086        {
087            final Style style = getElement().getStyle();
088            if( Stage.i().warnings().size() > 0 ) style.clearDisplay();
089            else style.setDisplay( Style.Display.NONE );
090        }
091
092
093    }
094
095
096
097   // ====================================================================================
098
099
100    private final class Popper implements ClickHandler
101    {
102
103        Popper() { addClickHandler( Popper.this ); } // no need to unregister, registry does not outlive this listener
104
105
106        public void onClick( ClickEvent _e )
107        {
108            final StringBuilder b = GWTX.stringBuilderClear();
109            final List<String> warnings = Stage.i().warnings();
110            final int wN = warnings.size();
111            if( wN > 0 )
112            {
113                int w = 0;
114                for( ;; )
115                {
116                    final String warning = warnings.get( w );
117                    b.append( warning );
118                    ++w;
119                    if( w >= wN ) break;
120
121                    b.append( '\n' );
122                    b.append( '\n' );
123                }
124            }
125            else assert false;
126            Window.alert( b.toString() );
127        }
128
129    }
130
131
132}