001    // Copyright 2006, 2007, 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.corelib.components;
016    
017    import org.apache.tapestry5.CSSClassConstants;
018    import org.apache.tapestry5.MarkupWriter;
019    import org.apache.tapestry5.ValidationTracker;
020    import org.apache.tapestry5.annotations.Environmental;
021    import org.apache.tapestry5.annotations.Parameter;
022    import org.apache.tapestry5.corelib.internal.InternalMessages;
023    import org.apache.tapestry5.services.FormSupport;
024    
025    import java.util.List;
026    
027    /**
028     * Standard validation error presenter. Must be enclosed by a {@link org.apache.tapestry5.corelib.components.Form}
029     * component. If errors are present, renders a div element around a banner message and around an unnumbered list of
030     * error messages. Renders nothing if the {@link org.apache.tapestry5.ValidationTracker} shows no errors.
031     */
032    public class Errors
033    {
034        /**
035         * The banner message displayed above the errors. The default value is "You must correct the following errors before
036         * you may continue.".
037         */
038        @Parameter("message:default-banner")
039        private String banner;
040    
041        /**
042         * The CSS class for the div element rendered by the component. The default value is "t-error".
043         */
044        @Parameter(name = "class")
045        private String className = CSSClassConstants.ERROR;
046    
047        // Allow null so we can generate a better error message if missing
048        @Environmental(false)
049        private ValidationTracker tracker;
050    
051        @Environmental
052        private FormSupport formSupport;
053    
054        void beginRender(MarkupWriter writer)
055        {
056            if (tracker == null) throw new RuntimeException(InternalMessages.encloseErrorsInForm());
057    
058            if (!tracker.getHasErrors()) return;
059    
060            writer.element("div", "class", className);
061    
062            // Inner div for the banner text
063            writer.element("div");
064            writer.write(banner);
065            writer.end();
066    
067            List<String> errors = tracker.getErrors();
068    
069            if (!errors.isEmpty())
070            {
071                // Only write out the <UL> if it will contain <LI> elements. An empty <UL> is not
072                // valid XHTML.
073    
074                writer.element("ul");
075    
076                for (String message : errors)
077                {
078                    writer.element("li");
079                    writer.write(message);
080                    writer.end();
081                }
082    
083                writer.end(); // ul
084            }
085    
086            writer.end(); // div
087    
088        }
089    }