1 15 package org.apache.tapestry.form.translator; 16 17 import java.text.DecimalFormat ; 18 import java.text.DecimalFormatSymbols ; 19 import java.text.Format ; 20 import java.util.Locale ; 21 22 import org.apache.tapestry.IForm; 23 import org.apache.tapestry.IMarkupWriter; 24 import org.apache.tapestry.IRequestCycle; 25 import org.apache.tapestry.form.FormComponentContributorContext; 26 import org.apache.tapestry.form.IFormComponent; 27 import org.apache.tapestry.valid.ValidationConstraint; 28 import org.apache.tapestry.valid.ValidationStrings; 29 30 36 public class NumberTranslator extends FormatTranslator 37 { 38 39 public NumberTranslator() 40 { 41 } 42 43 public NumberTranslator(String initializer) 45 { 46 super(initializer); 47 } 48 49 52 protected String defaultScript() 53 { 54 return "/org/apache/tapestry/form/translator/NumberTranslator.js"; 55 } 56 57 60 protected String defaultPattern() 61 { 62 return "#"; 63 } 64 65 68 protected Format getFormat(Locale locale) 69 { 70 return getDecimalFormat(locale); 71 } 72 73 public DecimalFormat getDecimalFormat(Locale locale) 74 { 75 return new DecimalFormat (getPattern(), new DecimalFormatSymbols (locale)); 76 } 77 78 81 protected String getMessageKey() 82 { 83 return ValidationStrings.INVALID_NUMBER; 84 } 85 86 90 protected Object [] getMessageParameters(Locale locale, String label) 91 { 92 String pattern = getDecimalFormat(locale).toLocalizedPattern(); 93 94 return new Object [] 95 { label, pattern }; 96 } 97 98 102 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, FormComponentContributorContext context, IFormComponent field) 103 { 104 super.renderContribution(writer, cycle, context, field); 105 106 String message = buildMessage(field, getMessageKey()); 107 IForm form = field.getForm(); 108 109 addSubmitHandler(form, "validate_number(event, document." + form.getName() + "." 110 + field.getName() + ",'" + message + "')"); 111 } 112 113 116 protected ValidationConstraint getConstraint() 117 { 118 return ValidationConstraint.NUMBER_FORMAT; 119 } 120 } 121 | Popular Tags |