KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > validator > Max


1 // Copyright 2005 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.tapestry.form.validator;
16
17 import org.apache.tapestry.IMarkupWriter;
18 import org.apache.tapestry.IRequestCycle;
19 import org.apache.tapestry.form.FormComponentContributorContext;
20 import org.apache.tapestry.form.IFormComponent;
21 import org.apache.tapestry.form.ValidationMessages;
22 import org.apache.tapestry.valid.ValidationConstraint;
23 import org.apache.tapestry.valid.ValidationStrings;
24 import org.apache.tapestry.valid.ValidatorException;
25
26 /**
27  * Validates that the input value is not larger than a particular maximum value.
28  *
29  * @author Howard Lewis Ship
30  * @since 4.0
31  */

32 public class Max extends BaseValidator
33 {
34     private double _max;
35
36     public Max()
37     {
38     }
39
40     public Max(String JavaDoc initializer)
41     {
42         super(initializer);
43     }
44
45     /**
46      * Does comparison based on the {@link Number#doubleValue()}.
47      */

48
49     public void validate(IFormComponent field, ValidationMessages messages, Object JavaDoc object)
50             throws ValidatorException
51     {
52         Number JavaDoc value = (Number JavaDoc) object;
53
54         if (value.doubleValue() > _max)
55             throw new ValidatorException(buildMessage(messages, field),
56                     ValidationConstraint.TOO_LARGE);
57     }
58
59     private String JavaDoc buildMessage(ValidationMessages messages, IFormComponent field)
60     {
61         return messages.formatValidationMessage(
62                 getMessage(),
63                 ValidationStrings.VALUE_TOO_LARGE,
64                 new Object JavaDoc[]
65                 { field.getDisplayName(), new Double JavaDoc(_max) });
66     }
67
68     public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
69             FormComponentContributorContext context, IFormComponent field)
70     {
71         context.includeClasspathScript("/org/apache/tapestry/form/validator/NumberValidator.js");
72
73         String JavaDoc message = buildMessage(context, field);
74
75         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("function(event) { validate_max_number(event, ");
76         buffer.append(context.getFieldDOM());
77         buffer.append(", ");
78         buffer.append(_max);
79         buffer.append(", '");
80         buffer.append(message);
81         buffer.append("'); }");
82
83         context.addSubmitListener(buffer.toString());
84     }
85
86     public void setMax(double max)
87     {
88         _max = max;
89     }
90
91 }
92
Popular Tags