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 * Expects the object to be a number, and checks that the value not smaller than a specified value. 036 * 037 * @author Howard Lewis Ship 038 * @since 4.0 039 */ 040 public class Min extends BaseValidator 041 { 042 private double _min; 043 044 public Min() 045 { 046 } 047 048 public Min(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 (_min > value.doubleValue()) 063 throw new ValidatorException(buildMessage(messages, field), ValidationConstraint.TOO_SMALL); 064 } 065 066 private String getStringValue(Locale locale, IFormComponent field) 067 { 068 String ret = null; 069 NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); 070 071 if (translator != null) 072 ret = translator.format(field, locale, new Double(_min)); 073 else 074 ret = String.valueOf(_min); 075 076 return ret; 077 } 078 079 private String buildMessage(ValidationMessages messages, IFormComponent field) 080 { 081 return messages.formatValidationMessage( 082 getMessage(), 083 ValidationStrings.VALUE_TOO_SMALL, 084 new Object[] { 085 field.getDisplayName(), getStringValue(messages.getLocale(), field) 086 }); 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 String minString = getStringValue(context.getLocale(), field); 100 String grouping = ""; 101 102 DecimalFormatSymbols symbols = null; 103 NumberTranslator translator = (NumberTranslator)super.getFieldTranslator(field, NumberTranslator.class); 104 105 if (translator != null) { 106 DecimalFormat format = translator.getDecimalFormat(context.getLocale()); 107 108 if (format.isGroupingUsed()) { 109 110 grouping += ",separator:" + JSONObject.quote(format.getDecimalFormatSymbols().getGroupingSeparator()); 111 grouping += ",groupSize:" + format.getGroupingSize(); 112 } else { 113 114 grouping += ",separator:\"\""; 115 } 116 117 symbols = format.getDecimalFormatSymbols(); 118 } else { 119 120 symbols = new DecimalFormatSymbols(context.getLocale()); 121 } 122 123 accumulateProperty(cons, field.getClientId(), 124 new JSONLiteral("[tapestry.form.validation.greaterThanOrEqual," 125 + JSONObject.quote(minString) 126 + ",{" 127 + "decimal:" + JSONObject.quote(symbols.getDecimalSeparator()) 128 + grouping 129 + "}]")); 130 131 accumulateProfileProperty(field, profile, ValidationConstants.CONSTRAINTS, buildMessage(context, field)); 132 } 133 134 public void setMin(double min) 135 { 136 _min = min; 137 } 138 139 public double getMin() 140 { 141 return _min; 142 } 143 }