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.form.validator;
016    
017    import org.apache.tapestry.IMarkupWriter;
018    import org.apache.tapestry.IRequestCycle;
019    import org.apache.tapestry.form.FormComponentContributorContext;
020    import org.apache.tapestry.form.IFormComponent;
021    import org.apache.tapestry.form.ValidationMessages;
022    import org.apache.tapestry.form.translator.NumberTranslator;
023    import org.apache.tapestry.json.JSONLiteral;
024    import org.apache.tapestry.json.JSONObject;
025    import org.apache.tapestry.valid.ValidationConstants;
026    import org.apache.tapestry.valid.ValidationConstraint;
027    import org.apache.tapestry.valid.ValidationStrings;
028    import org.apache.tapestry.valid.ValidatorException;
029    
030    import java.text.DecimalFormat;
031    import java.text.DecimalFormatSymbols;
032    import java.util.Locale;
033    
034    /**
035     * Validates that the input value is not larger than a particular maximum value.
036     * 
037     * @author Howard Lewis Ship
038     * @since 4.0
039     */
040    public class Max extends BaseValidator
041    {
042        private double _max;
043    
044        public Max()
045        {
046        }
047    
048        public Max(String initializer)
049        {
050            super(initializer);
051        }
052        
053        /**
054         * Does comparison based on the {@link Number#doubleValue()}.
055         */
056    
057        public void validate(IFormComponent field, ValidationMessages messages, Object object)
058                throws ValidatorException
059        {
060            Number value = (Number) object;
061    
062            if (value.doubleValue() > _max)
063                throw new ValidatorException(buildMessage(messages, field),
064                        ValidationConstraint.TOO_LARGE);
065        }
066    
067        private String getStringValue(Locale locale, IFormComponent field)
068        {
069            String ret = null;
070            NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
071    
072            if (translator != null)
073                ret = translator.format(field, locale, new Double(_max));
074            else
075                ret = String.valueOf(_max);
076    
077            return ret;
078        }
079    
080        private String buildMessage(ValidationMessages messages, IFormComponent field)
081        {
082            return messages.formatValidationMessage(
083                    getMessage(),
084                    ValidationStrings.VALUE_TOO_LARGE,
085                    new Object[]
086                            { field.getDisplayName(), getStringValue(messages.getLocale(), field) });
087        }
088    
089        public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
090                FormComponentContributorContext context, IFormComponent field)
091        {
092            JSONObject profile = context.getProfile();
093            
094            if (!profile.has(ValidationConstants.CONSTRAINTS)) {
095                profile.put(ValidationConstants.CONSTRAINTS, new JSONObject());
096            }
097            JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS);
098    
099            context.addInitializationScript(field, "dojo.require(\"dojo.i18n.number\");");
100    
101            String maxString = getStringValue(context.getLocale(), field);
102            String grouping = "";
103    
104            DecimalFormatSymbols symbols = null;
105            NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class);
106    
107            if (translator != null) {
108                DecimalFormat format = translator.getDecimalFormat(context.getLocale());
109    
110                if (format.isGroupingUsed()) {
111    
112                    grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator());
113                    grouping += ",groupSize:" + format.getGroupingSize();
114                } else {
115    
116                    grouping += ",separator:\"\"";
117                }
118    
119                symbols = format.getDecimalFormatSymbols();
120            } else {
121    
122                symbols = new DecimalFormatSymbols(context.getLocale());
123            }
124    
125            accumulateProperty(cons, field.getClientId(),
126                               new JSONLiteral("[tapestry.form.validation.lessThanOrEqual,"
127                                               + JSONObject.quote(maxString)
128                                               + ",{"
129                                               + "decimal:" + JSONObject.quote(symbols.getDecimalSeparator())
130                                               + grouping
131                                               + "}]"));
132            
133            accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field));
134        }
135    
136        public void setMax(double max)
137        {
138            _max = max;
139        }
140    
141        public double getMax()
142        {
143            return _max;
144        }
145    }