01: // Copyright 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.validator;
16:
17: import static org.apache.tapestry.TapestryUtils.quote;
18:
19: import org.apache.tapestry.Field;
20: import org.apache.tapestry.MarkupWriter;
21: import org.apache.tapestry.PageRenderSupport;
22: import org.apache.tapestry.ValidationException;
23: import org.apache.tapestry.Validator;
24: import org.apache.tapestry.ioc.MessageFormatter;
25:
26: /** Enforces a maximum integer value. */
27: public class Max implements Validator<Long, Number> {
28: public Class<Long> getConstraintType() {
29: return Long.class;
30: }
31:
32: public String getMessageKey() {
33: return "max-integer";
34: }
35:
36: public Class<Number> getValueType() {
37: return Number.class;
38: }
39:
40: public boolean invokeIfBlank() {
41: return false;
42: }
43:
44: public void validate(Field field, Long constraintValue,
45: MessageFormatter formatter, Number value)
46: throws ValidationException {
47: if (value.longValue() > constraintValue)
48: throw new ValidationException(buildMessage(formatter,
49: field, constraintValue));
50: }
51:
52: private String buildMessage(MessageFormatter formatter,
53: Field field, Long constraintValue) {
54: return formatter.format(constraintValue, field.getLabel());
55: }
56:
57: public void render(Field field, Long constraintValue,
58: MessageFormatter formatter, MarkupWriter writer,
59: PageRenderSupport pageRenderSupport) {
60: pageRenderSupport.addScript(
61: "Tapestry.Field.max('%s', %d, %s);", field
62: .getClientId(), constraintValue,
63: quote(buildMessage(formatter, field, constraintValue)));
64:
65: }
66:
67: }
|