Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]

    1   // Copyright 2006, 2007, 2008 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.corelib.components;
   16   
   17   import org.apache.tapestry5.CSSClassConstants;
   18   import org.apache.tapestry5.MarkupWriter;
   19   import org.apache.tapestry5.ValidationTracker;
   20   import org.apache.tapestry5.annotations.Environmental;
   21   import org.apache.tapestry5.annotations.Parameter;
   22   import org.apache.tapestry5.corelib.internal.InternalMessages;
   23   import org.apache.tapestry5.services.FormSupport;
   24   
   25   import java.util.List;
   26   
   27   /**
   28    * Standard validation error presenter. Must be enclosed by a {@link org.apache.tapestry5.corelib.components.Form}
   29    * component. If errors are present, renders a div element around a banner message and around an unnumbered list of
   30    * error messages. Renders nothing if the {@link org.apache.tapestry5.ValidationTracker} shows no errors.
   31    */
   32   public class Errors
   33   {
   34       /**
   35        * The banner message displayed above the errors. The default value is "You must correct the following errors before
   36        * you may continue.".
   37        */
   38       @Parameter("message:default-banner")
   39       private String banner;
   40   
   41       /**
   42        * The CSS class for the div element rendered by the component. The default value is "t-error".
   43        */
   44       @Parameter(name = "class")
   45       private String className = CSSClassConstants.ERROR;
   46   
   47       // Allow null so we can generate a better error message if missing
   48       @Environmental(false)
   49       private ValidationTracker tracker;
   50   
   51       @Environmental
   52       private FormSupport formSupport;
   53   
   54       void beginRender(MarkupWriter writer)
   55       {
   56           if (tracker == null) throw new RuntimeException(InternalMessages.encloseErrorsInForm());
   57   
   58           if (!tracker.getHasErrors()) return;
   59   
   60           writer.element("div", "class", className);
   61   
   62           // Inner div for the banner text
   63           writer.element("div");
   64           writer.write(banner);
   65           writer.end();
   66   
   67           List<String> errors = tracker.getErrors();
   68   
   69           if (!errors.isEmpty())
   70           {
   71               // Only write out the <UL> if it will contain <LI> elements. An empty <UL> is not
   72               // valid XHTML.
   73   
   74               writer.element("ul");
   75   
   76               for (String message : errors)
   77               {
   78                   writer.element("li");
   79                   writer.write(message);
   80                   writer.end();
   81               }
   82   
   83               writer.end(); // ul
   84           }
   85   
   86           writer.end(); // div
   87   
   88       }
   89   }

Home » tapestry-src-5.0.19 » org.apache.tapestry5.corelib.components » [javadoc | source]