01: // Copyright 2006, 2007 The Apache Software Foundation
02: //
03: // Licensed under the Apache License, Version 2.0 (the "License");
04: // you may not use this file except in compliance with the License.
05: // You may obtain a copy of the License at
06: //
07: // http://www.apache.org/licenses/LICENSE-2.0
08: //
09: // 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.tapestry.corelib.components;
16:
17: import org.apache.tapestry.MarkupWriter;
18: import org.apache.tapestry.ValidationTracker;
19: import org.apache.tapestry.annotations.Environmental;
20: import org.apache.tapestry.annotations.Parameter;
21: import org.apache.tapestry.internal.InternalConstants;
22: import org.apache.tapestry.services.FormSupport;
23:
24: /**
25: * Standard validation error presenter. Must be enclosed by a {@link Form} component. If errors are
26: * present, renders a div element around a banner message and around an unnumbered list of error
27: * messages.
28: */
29: public class Errors {
30: /**
31: * The banner message displayed above the errors. The default value is "You must correct the
32: * following errors before you may continue.".
33: */
34: @Parameter("message:default-banner")
35: private String _banner;
36:
37: /** The CSS class for the div element rendered by the component. The default value is "t-error". */
38: @Parameter
39: private String _class = InternalConstants.TAPESTRY_ERROR_CLASS;
40:
41: // Allow null so we can generate a better error message if missing
42: @Environmental(false)
43: private ValidationTracker _tracker;
44:
45: @Environmental
46: private FormSupport _formSupport;
47:
48: void beginRender(MarkupWriter writer) {
49: // TODO: Would be nice if there was a Location to report ... can we add a Location property
50: // to ComponentResources?
51:
52: if (_tracker == null)
53: throw new RuntimeException(ComponentMessages
54: .encloseErrorsInForm());
55:
56: String cssClass = _tracker.getHasErrors() ? _class : _class
57: + " t-invisible";
58:
59: writer.element("div", "class", cssClass, "id", _formSupport
60: .getClientId()
61: + ":errors");
62:
63: // Inner div for the banner text
64: writer.element("div");
65: writer.write(_banner);
66: writer.end();
67:
68: writer.element("ul");
69:
70: for (String message : _tracker.getErrors()) {
71: writer.element("li");
72: writer.write(message);
73: writer.end();
74: }
75:
76: writer.end(); // ul
77: writer.end(); // div
78:
79: }
80: }
|