001    // Copyright 2005 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.tapestry.valid;
016    
017    import org.apache.tapestry.form.IFormComponent;
018    
019    /**
020     * Base class for a number of implementations of {@link org.apache.tapestry.valid.IValidator},
021     * meant to replace the awkward {@link org.apache.tapestry.valid.NumberValidator}.
022     * 
023     * @author Howard M. Lewis Ship
024     */
025    public abstract class AbstractNumericValidator extends BaseValidator
026    {
027        private boolean _zeroIsNull;
028    
029        private String _scriptPath = getDefaultScriptPath();
030        
031        private String _invalidNumericFormatMessage;
032    
033        private String _invalidIntegerFormatMessage;
034    
035        private String _numberTooSmallMessage;
036    
037        private String _numberTooLargeMessage;
038    
039        private String _numberRangeMessage;
040        
041        public AbstractNumericValidator()
042        {
043            super();
044        }
045    
046        public AbstractNumericValidator(String initializer)
047        {
048            super(initializer);
049        }
050    
051        public AbstractNumericValidator(boolean required)
052        {
053            super(required);
054        }
055    
056        /**
057         * If true, then when rendering, a zero is treated as a non-value, and null is returned. If
058         * false, the default, then zero is rendered as zero.
059         */
060    
061        public boolean getZeroIsNull()
062        {
063            return _zeroIsNull;
064        }
065    
066        public void setZeroIsNull(boolean zeroIsNull)
067        {
068            _zeroIsNull = zeroIsNull;
069        }
070    
071        /**
072         * @since 2.2
073         */
074        public String getScriptPath()
075        {
076            return _scriptPath;
077        }
078    
079        /**
080         * Allows a developer to use the existing validation logic with a different client-side script.
081         * This is often sufficient to allow application-specific error presentation (perhaps by using
082         * DHTML to update the content of a <span> tag, or to use a more sophisticated pop-up
083         * window than <code>window.alert()</code>).
084         * 
085         * @since 2.2
086         */
087        public void setScriptPath(String scriptPath)
088        {
089            _scriptPath = scriptPath;
090        }
091    
092        /** @since 3.0 */
093        public String getInvalidNumericFormatMessage()
094        {
095            return _invalidNumericFormatMessage;
096        }
097    
098        /** @since 3.0 */
099        public String getInvalidIntegerFormatMessage()
100        {
101            return _invalidIntegerFormatMessage;
102        }
103    
104        /** @since 3.0 */
105        public String getNumberRangeMessage()
106        {
107            return _numberRangeMessage;
108        }
109    
110        /** @since 3.0 */
111        public String getNumberTooLargeMessage()
112        {
113            return _numberTooLargeMessage;
114        }
115    
116        /** @since 3.0 */
117        public String getNumberTooSmallMessage()
118        {
119            return _numberTooSmallMessage;
120        }
121    
122        /**
123         * Overrides the <code>invalid-numeric-format</code> bundle key. Parameter {0} is the display
124         * name of the field.
125         * 
126         * @since 3.0
127         */
128        public void setInvalidNumericFormatMessage(String string)
129        {
130            _invalidNumericFormatMessage = string;
131        }
132    
133        /**
134         * Overrides the <code>invalid-int-format</code> bundle key. Parameter {0} is the display name
135         * of the field.
136         * 
137         * @since 3.0
138         */
139        public void setInvalidIntegerFormatMessage(String string)
140        {
141            _invalidIntegerFormatMessage = string;
142        }
143    
144        /**
145         * Overrides the <code>number-range</code> bundle key. Parameter [0} is the display name of
146         * the field. Parameter {1} is the minimum value. Parameter {2} is the maximum value.
147         * 
148         * @since 3.0
149         */
150        public void setNumberRangeMessage(String string)
151        {
152            _numberRangeMessage = string;
153        }
154    
155        /**
156         * Overrides the <code>number-too-large</code> bundle key. Parameter {0} is the display name
157         * of the field. Parameter {1} is the maximum allowed value.
158         * 
159         * @since 3.0
160         */
161        public void setNumberTooLargeMessage(String string)
162        {
163            _numberTooLargeMessage = string;
164        }
165    
166        /**
167         * Overrides the <code>number-too-small</code> bundle key. Parameter {0} is the display name
168         * of the field. Parameter {1} is the minimum allowed value.
169         * 
170         * @since 3.0
171         */
172        public void setNumberTooSmallMessage(String string)
173        {
174            _numberTooSmallMessage = string;
175        }
176    
177        /** @since 3.0 */
178        protected String buildInvalidNumericFormatMessage(IFormComponent field)
179        {
180            String pattern = getPattern(
181                    getInvalidNumericFormatMessage(),
182                    "invalid-numeric-format",
183                    field.getPage().getLocale());
184    
185            return formatString(pattern, field.getDisplayName());
186        }
187    
188        protected String buildNumberTooSmallMessage(IFormComponent field, Number minimum)
189        {
190            String pattern = getPattern(getNumberTooSmallMessage(), "number-too-small", field.getPage()
191                    .getLocale());
192    
193            return formatString(pattern, field.getDisplayName(), minimum);
194        }
195    
196        /** @since 3.0 */
197        protected String buildInvalidIntegerFormatMessage(IFormComponent field)
198        {
199            String pattern = getPattern(getInvalidIntegerFormatMessage(), "invalid-int-format", field
200                    .getPage().getLocale());
201        
202            return formatString(pattern, field.getDisplayName());
203        }
204    
205        /**
206         * @since 3.0
207         */
208        protected String buildNumberTooLargeMessage(IFormComponent field, Number maximum)
209        {
210            String pattern = getPattern(getNumberTooLargeMessage(), "number-too-large", field.getPage()
211                    .getLocale());
212        
213            return formatString(pattern, field.getDisplayName(), maximum);
214        }
215    
216        protected String buildNumberRangeMessage(IFormComponent field, Number mininum, Number maximum)
217        {
218            String pattern = getPattern(getNumberRangeMessage(), "number-range", field.getPage()
219                    .getLocale());
220        
221            return formatString(pattern, new Object[]
222            { field.getDisplayName(), mininum, maximum });
223        }
224    
225        protected String buildRangeMessage(IFormComponent field, Number minimum, Number maximum)
226        {
227            if (minimum != null && maximum != null)
228                return buildNumberRangeMessage(field, minimum, maximum);
229        
230            if (maximum != null)
231                return buildNumberTooLargeMessage(field, maximum);
232        
233            return buildNumberTooSmallMessage(field, minimum);
234        }
235        
236        protected abstract String getDefaultScriptPath();
237    }