package 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. import org.apache.wicket.feedback.*; import org.apache.wicket.markup.html.basic.*; import org.apache.wicket.markup.html.form.ValidationErrorFeedback; import org.apache.wicket.markup.html.list.*; import org.apache.wicket.markup.html.panel.*; import org.apache.wicket.model.*; import org.apache.wicket.validation.ValidationError; import votorola.g.lang.*; import votorola.g.web.wic.ConversionExceptionLM; /** A reusable list of feedback messages, each rendered as a paragraph. To automatically * scroll to the list whenever there are messages, add id='feedback' to the * markup, and have the component override renderHead(): * *
  *     public void renderHead( IHeaderResponse r )
  *     {
  *         if( !getSession().getFeedbackMessages().isEmpty() )
  *         {
  *             r.renderOnLoadJavaScript( "location.hash = 'feedback'" );
  *         }
  *     }
* * @see WC_Feedback.html */ public @ThreadRestricted("wicket") final class WC_Feedback extends Panel { /** Constructs a WC_Feedback. */ public WC_Feedback( String id ) { this( id, IFeedbackMessageFilter.ALL ); } /** Constructs a WC_Feedback, with a message filter. */ public WC_Feedback( final String id, IFeedbackMessageFilter _filter ) { super( id ); filter = _filter; final FeedbackMessagesModel messagesModel = new FeedbackMessagesModel( WC_Feedback.this ); messagesModel.setFilter( filter ); final ListView repeating = new ListView( "feedback-repeating", messagesModel ) { protected void populateItem( final ListItem listItem ) { final FeedbackMessage f = listItem.getModelObject(); if( f.isRendered() ) { listItem.setVisible( false ); return; } String messageString = null; { final Object m = f.getMessage(); if( m instanceof ValidationErrorFeedback ) { final ValidationError e = (ValidationError) ((ValidationErrorFeedback)m).getError(); final Object x = e.getVariables().get( "exception" ); // as set by FormComponent.reportValidationError(), called during convertInput() if( x instanceof ConversionExceptionLM ) { messageString = ((ConversionExceptionLM)x).getMessage(); // per ConversionExceptionLM } } } if( messageString == null ) messageString = f.getMessage().toString(); final Label span = new Label( "feedback-repeating-body", messageString ); if( INVISIBLE_MESSAGE_STRING.equals( messageString )) listItem.setVisible( false ); else if( f.isWarning() ) VPageHTML.appendStyleClass( span, "invalid" ); else if( f.isInfo() ) VPageHTML.appendStyleClass( span, "changed" ); listItem.add( span ); f.markRendered(); // ? I'm just copying from FeedbackPanel } }; add( repeating ); } // ------------------------------------------------------------------------------------ /** Feedback messages with this string value will be invisible in the rendered list. */ public static final String INVISIBLE_MESSAGE_STRING = "[this message ought to be invisible]"; // - C o m p o n e n t ---------------------------------------------------------------- /** Answers whether this panel is visible. Overridden to return false * when there are no feedback messages to show. */ public boolean isVisible() { final int messageCount = getPage().getSession().getFeedbackMessages().size( filter ); return super.isVisible() && messageCount > 0; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final IFeedbackMessageFilter filter; }