1 15 package org.apache.tapestry.form.translator; 16 17 import java.text.Format ; 18 import java.text.ParseException ; 19 import java.util.Locale ; 20 21 import org.apache.tapestry.form.IFormComponent; 22 import org.apache.tapestry.valid.ValidationConstraint; 23 import org.apache.tapestry.valid.ValidatorException; 24 25 31 public abstract class FormatTranslator extends AbstractTranslator 32 { 33 private String _pattern = defaultPattern(); 34 35 protected abstract String defaultPattern(); 36 37 41 protected String formatObject(IFormComponent field, Object object) 42 { 43 46 Format format = getFormat(field.getPage().getLocale()); 47 48 return format.format(object); 49 } 50 51 55 protected Object parseText(IFormComponent field, String text) throws ValidatorException 56 { 57 Format format = getFormat(field.getPage().getLocale()); 58 59 try 60 { 61 return format.parseObject(text); 62 } 63 catch (ParseException e) 64 { 65 throw new ValidatorException(buildMessage(field, getMessageKey()), getConstraint()); 66 } 67 } 68 69 protected abstract ValidationConstraint getConstraint(); 70 71 protected abstract Format getFormat(Locale locale); 72 73 protected abstract String getMessageKey(); 74 75 public String getPattern() 76 { 77 return _pattern; 78 } 79 80 public void setPattern(String pattern) 81 { 82 _pattern = pattern; 83 } 84 85 public FormatTranslator() 86 { 87 } 88 89 public FormatTranslator(String initializer) 91 { 92 super(initializer); 93 } 94 } 95 | Popular Tags |