001 package net.sf.tacos.form.translator; 002 003 import java.util.Locale; 004 005 import org.apache.tapestry.form.IFormComponent; 006 import org.apache.tapestry.form.ValidationMessages; 007 import org.apache.tapestry.form.AbstractFormComponentContributor; 008 import org.apache.tapestry.form.FormComponentContributorContext; 009 import org.apache.tapestry.form.translator.Translator; 010 import org.apache.tapestry.valid.ValidatorException; 011 import org.apache.tapestry.valid.ValidationConstants; 012 import org.apache.tapestry.IMarkupWriter; 013 import org.apache.tapestry.IRequestCycle; 014 import org.apache.tapestry.json.JSONObject; 015 import org.apache.hivemind.HiveMind; 016 017 /** 018 * The generics version of Tapestry's AbstractTranslator. 019 */ 020 public abstract class AbstractTranslator<T> extends AbstractFormComponentContributor implements Translator 021 { 022 private boolean _trim; 023 024 private String _message; 025 026 public AbstractTranslator() 027 { 028 } 029 030 // Needed until HIVEMIND-134 fix is available 031 public AbstractTranslator(String initializer) 032 { 033 super(initializer); 034 } 035 036 /** 037 * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent, 038 * Locale, java.lang.Object) 039 */ 040 public String format(IFormComponent field, Locale locale, Object object) 041 { 042 if (object == null) 043 return ""; 044 045 return formatObject(field, locale, (T) object); 046 } 047 048 /** 049 * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent, 050 * ValidationMessages, java.lang.String) 051 */ 052 public Object parse(IFormComponent field, ValidationMessages messages, String text) 053 throws ValidatorException 054 { 055 String value = text == null ? null : (_trim ? text.trim() : text); 056 057 return HiveMind.isBlank(value) ? getValueForEmptyInput() : parseText(field, messages, value); 058 } 059 060 protected abstract String formatObject(IFormComponent field, Locale locale, T object); 061 062 protected abstract T parseText(IFormComponent field, ValidationMessages messages, String text) 063 throws ValidatorException; 064 065 /** 066 * The value to be used when the value supplied in the request is blank (null or empty). The 067 * default value is null, but some subclasses may override. 068 * 069 * @see #parse(IFormComponent, ValidationMessages, String) 070 * @return null, subclasses may override 071 */ 072 protected Object getValueForEmptyInput() 073 { 074 return null; 075 } 076 077 protected String buildMessage(ValidationMessages messages, IFormComponent field, String key) 078 { 079 String label = field.getDisplayName(); 080 081 Object[] parameters = getMessageParameters(messages.getLocale(), label); 082 083 return messages.formatValidationMessage(_message, key, parameters); 084 } 085 086 protected Object[] getMessageParameters(Locale locale, String label) 087 { 088 return new Object[] { label }; 089 } 090 091 /** 092 * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IMarkupWriter , org.apache.tapestry.IRequestCycle , org.apache.tapestry.form.FormComponentContributorContext , IFormComponent) 093 */ 094 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 095 FormComponentContributorContext context, IFormComponent field) 096 { 097 super.renderContribution(writer, cycle, context, field); 098 099 if (_trim) { 100 JSONObject profile = context.getProfile(); 101 102 accumulateProperty(profile, ValidationConstants.TRIM, field.getClientId()); 103 } 104 } 105 106 public boolean isTrim() 107 { 108 return _trim; 109 } 110 111 public void setTrim(boolean trim) 112 { 113 _trim = trim; 114 } 115 116 public String getMessage() 117 { 118 return _message; 119 } 120 121 public void setMessage(String message) 122 { 123 _message = message; 124 } 125 }