001package votorola.a.web.wic; // Copyright 2008, 2010, 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 org.apache.wicket.feedback.*;
004import org.apache.wicket.markup.html.basic.*;
005import org.apache.wicket.markup.html.form.ValidationErrorFeedback;
006import org.apache.wicket.markup.html.list.*;
007import org.apache.wicket.markup.html.panel.*;
008import org.apache.wicket.model.*;
009import org.apache.wicket.validation.ValidationError;
010import votorola.g.lang.*;
011import votorola.g.web.wic.ConversionExceptionLM;
012
013
014/** A reusable list of feedback messages, each rendered as a paragraph.  To automatically
015  * scroll to the list whenever there are messages, add <code>id='feedback'</code> to the
016  * markup, and have the component override renderHead():
017  *
018  * <pre>
019  *     public void renderHead( IHeaderResponse r )
020  *     {
021  *         if( !getSession().getFeedbackMessages().isEmpty() )
022  *         {
023  *             r.renderOnLoadJavaScript( "location.hash = 'feedback'" );
024  *         }
025  *     }</pre>
026  *
027  *     @see <a href='../../../../../../a/web/wic/WC_Feedback.html'>WC_Feedback.html</a>
028  */
029public @ThreadRestricted("wicket") final class WC_Feedback extends Panel
030{
031
032
033    /** Constructs a WC_Feedback.
034      */
035    public WC_Feedback( String id ) { this( id, IFeedbackMessageFilter.ALL ); }
036
037
038
039    /** Constructs a WC_Feedback, with a message filter.
040      */
041    public WC_Feedback( final String id, IFeedbackMessageFilter _filter )
042    {
043        super( id );
044        filter = _filter;
045
046        final FeedbackMessagesModel messagesModel = new FeedbackMessagesModel( WC_Feedback.this );
047        messagesModel.setFilter( filter );
048        final ListView<FeedbackMessage> repeating = new ListView<FeedbackMessage>(
049          "feedback-repeating", messagesModel )
050        {
051            protected void populateItem( final ListItem<FeedbackMessage> listItem )
052            {
053                final FeedbackMessage f = listItem.getModelObject();
054                if( f.isRendered() )
055                {
056                    listItem.setVisible( false );
057                    return;
058                }
059
060                String messageString = null;
061                {
062                    final Object m = f.getMessage();
063                    if( m instanceof ValidationErrorFeedback )
064                    {
065                        final ValidationError e = (ValidationError)
066                          ((ValidationErrorFeedback)m).getError();
067                        final Object x = e.getVariables().get( "exception" ); // as set by FormComponent.reportValidationError(), called during convertInput()
068                        if( x instanceof ConversionExceptionLM )
069                        {
070                            messageString = ((ConversionExceptionLM)x).getMessage(); // per ConversionExceptionLM
071                        }
072                    }
073                }
074                if( messageString == null ) messageString = f.getMessage().toString();
075
076                final Label span = new Label( "feedback-repeating-body", messageString );
077                if( INVISIBLE_MESSAGE_STRING.equals( messageString )) listItem.setVisible( false );
078                else if( f.isWarning() ) VPageHTML.appendStyleClass( span, "invalid" );
079                else if( f.isInfo() ) VPageHTML.appendStyleClass( span, "changed" );
080
081                listItem.add( span );
082                f.markRendered(); // ? I'm just copying from FeedbackPanel
083            }
084        };
085        add( repeating );
086    }
087
088
089
090   // ------------------------------------------------------------------------------------
091
092
093    /** Feedback messages with this string value will be invisible in the rendered list.
094      */
095    public static final String INVISIBLE_MESSAGE_STRING = "[this message ought to be invisible]";
096
097
098
099   // - C o m p o n e n t ----------------------------------------------------------------
100
101
102    /** Answers whether this panel is visible.  Overridden to return false
103      * when there are no feedback messages to show.
104      */
105    public boolean isVisible()
106    {
107        final int messageCount = getPage().getSession().getFeedbackMessages().size( filter );
108        return super.isVisible() && messageCount > 0;
109    }
110
111
112
113//// P r i v a t e ///////////////////////////////////////////////////////////////////////
114
115
116    private final IFeedbackMessageFilter filter;
117
118
119
120}