KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > valid > AbstractNumericValidator


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.valid;
16
17 import org.apache.tapestry.form.IFormComponent;
18
19 /**
20  * Base class for a number of implementations of {@link org.apache.tapestry.valid.IValidator},
21  * meant to replace the awkward {@link org.apache.tapestry.valid.NumberValidator}.
22  *
23  * @author Howard M. Lewis Ship
24  */

25 public abstract class AbstractNumericValidator extends BaseValidator
26 {
27     private boolean _zeroIsNull;
28
29     public AbstractNumericValidator()
30     {
31         super();
32     }
33
34     public AbstractNumericValidator(String JavaDoc initializer)
35     {
36         super(initializer);
37     }
38
39     public AbstractNumericValidator(boolean required)
40     {
41         super(required);
42     }
43
44     /**
45      * If true, then when rendering, a zero is treated as a non-value, and null is returned. If
46      * false, the default, then zero is rendered as zero.
47      */

48
49     public boolean getZeroIsNull()
50     {
51         return _zeroIsNull;
52     }
53
54     public void setZeroIsNull(boolean zeroIsNull)
55     {
56         _zeroIsNull = zeroIsNull;
57     }
58
59     private String JavaDoc _scriptPath =
60         getDefaultScriptPath();
61
62     
63     private String JavaDoc _invalidNumericFormatMessage;
64
65     private String JavaDoc _invalidIntegerFormatMessage;
66
67     private String JavaDoc _numberTooSmallMessage;
68
69     private String JavaDoc _numberTooLargeMessage;
70
71     private String JavaDoc _numberRangeMessage;
72
73     /**
74      * @since 2.2
75      */

76     public String JavaDoc getScriptPath()
77     {
78         return _scriptPath;
79     }
80
81     /**
82      * Allows a developer to use the existing validation logic with a different client-side script.
83      * This is often sufficient to allow application-specific error presentation (perhaps by using
84      * DHTML to update the content of a <span> tag, or to use a more sophisticated pop-up
85      * window than <code>window.alert()</code>).
86      *
87      * @since 2.2
88      */

89     public void setScriptPath(String JavaDoc scriptPath)
90     {
91         _scriptPath = scriptPath;
92     }
93
94     /** @since 3.0 */
95     public String JavaDoc getInvalidNumericFormatMessage()
96     {
97         return _invalidNumericFormatMessage;
98     }
99
100     /** @since 3.0 */
101     public String JavaDoc getInvalidIntegerFormatMessage()
102     {
103         return _invalidIntegerFormatMessage;
104     }
105
106     /** @since 3.0 */
107     public String JavaDoc getNumberRangeMessage()
108     {
109         return _numberRangeMessage;
110     }
111
112     /** @since 3.0 */
113     public String JavaDoc getNumberTooLargeMessage()
114     {
115         return _numberTooLargeMessage;
116     }
117
118     /** @since 3.0 */
119     public String JavaDoc getNumberTooSmallMessage()
120     {
121         return _numberTooSmallMessage;
122     }
123
124     /**
125      * Overrides the <code>invalid-numeric-format</code> bundle key. Parameter {0} is the display
126      * name of the field.
127      *
128      * @since 3.0
129      */

130     public void setInvalidNumericFormatMessage(String JavaDoc string)
131     {
132         _invalidNumericFormatMessage = string;
133     }
134
135     /**
136      * Overrides the <code>invalid-int-format</code> bundle key. Parameter {0} is the display name
137      * of the field.
138      *
139      * @since 3.0
140      */

141     public void setInvalidIntegerFormatMessage(String JavaDoc string)
142     {
143         _invalidIntegerFormatMessage = string;
144     }
145
146     /**
147      * Overrides the <code>number-range</code> bundle key. Parameter [0} is the display name of
148      * the field. Parameter {1} is the minimum value. Parameter {2} is the maximum value.
149      *
150      * @since 3.0
151      */

152     public void setNumberRangeMessage(String JavaDoc string)
153     {
154         _numberRangeMessage = string;
155     }
156
157     /**
158      * Overrides the <code>number-too-large</code> bundle key. Parameter {0} is the display name
159      * of the field. Parameter {1} is the maximum allowed value.
160      *
161      * @since 3.0
162      */

163     public void setNumberTooLargeMessage(String JavaDoc string)
164     {
165         _numberTooLargeMessage = string;
166     }
167
168     /**
169      * Overrides the <code>number-too-small</code> bundle key. Parameter {0} is the display name
170      * of the field. Parameter {1} is the minimum allowed value.
171      *
172      * @since 3.0
173      */

174     public void setNumberTooSmallMessage(String JavaDoc string)
175     {
176         _numberTooSmallMessage = string;
177     }
178
179     /** @since 3.0 */
180     protected String JavaDoc buildInvalidNumericFormatMessage(IFormComponent field)
181     {
182         String JavaDoc pattern = getPattern(
183                 getInvalidNumericFormatMessage(),
184                 "invalid-numeric-format",
185                 field.getPage().getLocale());
186
187         return formatString(pattern, field.getDisplayName());
188     }
189
190     protected String JavaDoc buildNumberTooSmallMessage(IFormComponent field, Number JavaDoc minimum)
191     {
192         String JavaDoc pattern = getPattern(getNumberTooSmallMessage(), "number-too-small", field.getPage()
193                 .getLocale());
194
195         return formatString(pattern, field.getDisplayName(), minimum);
196     }
197
198     /** @since 3.0 */
199     protected String JavaDoc buildInvalidIntegerFormatMessage(IFormComponent field)
200     {
201         String JavaDoc pattern = getPattern(getInvalidIntegerFormatMessage(), "invalid-int-format", field
202                 .getPage().getLocale());
203     
204         return formatString(pattern, field.getDisplayName());
205     }
206
207     /**
208      * @since 3.0
209      */

210     protected String JavaDoc buildNumberTooLargeMessage(IFormComponent field, Number JavaDoc maximum)
211     {
212         String JavaDoc pattern = getPattern(getNumberTooLargeMessage(), "number-too-large", field.getPage()
213                 .getLocale());
214     
215         return formatString(pattern, field.getDisplayName(), maximum);
216     }
217
218     protected String JavaDoc buildNumberRangeMessage(IFormComponent field, Number JavaDoc mininum, Number JavaDoc maximum)
219     {
220         String JavaDoc pattern = getPattern(getNumberRangeMessage(), "number-range", field.getPage()
221                 .getLocale());
222     
223         return formatString(pattern, new Object JavaDoc[]
224         { field.getDisplayName(), mininum, maximum });
225     }
226
227     protected String JavaDoc buildRangeMessage(IFormComponent field, Number JavaDoc minimum, Number JavaDoc maximum)
228     {
229         if (minimum != null && maximum != null)
230             return buildNumberRangeMessage(field, minimum, maximum);
231     
232         if (maximum != null)
233             return buildNumberTooLargeMessage(field, maximum);
234     
235         return buildNumberTooSmallMessage(field, minimum);
236     }
237     
238     protected abstract String JavaDoc getDefaultScriptPath();
239 }
Popular Tags