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.translator; 016 017 import java.util.Locale; 018 019 import org.apache.hivemind.HiveMind; 020 import org.apache.tapestry.IMarkupWriter; 021 import org.apache.tapestry.IRequestCycle; 022 import org.apache.tapestry.form.AbstractFormComponentContributor; 023 import org.apache.tapestry.form.FormComponentContributorContext; 024 import org.apache.tapestry.form.IFormComponent; 025 import org.apache.tapestry.form.ValidationMessages; 026 import org.apache.tapestry.valid.ValidatorException; 027 028 /** 029 * Abstract {@link Translator} implementation that provides default behavior for trimming, null 030 * object, and empty text handling. 031 * 032 * @author Paul Ferraro 033 * @since 4.0 034 */ 035 public abstract class AbstractTranslator extends AbstractFormComponentContributor implements 036 Translator 037 { 038 private boolean _trim; 039 040 private String _message; 041 042 public AbstractTranslator() 043 { 044 } 045 046 // Needed until HIVEMIND-134 fix is available 047 public AbstractTranslator(String initializer) 048 { 049 super(initializer); 050 } 051 052 /** 053 * @see org.apache.tapestry.form.translator.Translator#format(org.apache.tapestry.form.IFormComponent, 054 * Locale, java.lang.Object) 055 */ 056 public String format(IFormComponent field, Locale locale, Object object) 057 { 058 if (object == null) 059 return ""; 060 061 return formatObject(field, locale, object); 062 } 063 064 /** 065 * @see org.apache.tapestry.form.translator.Translator#parse(org.apache.tapestry.form.IFormComponent, 066 * ValidationMessages, java.lang.String) 067 */ 068 public Object parse(IFormComponent field, ValidationMessages messages, String text) 069 throws ValidatorException 070 { 071 String value = _trim ? text.trim() : text; 072 073 return HiveMind.isBlank(value) ? getEmpty() : parseText(field, messages, value); 074 } 075 076 protected abstract String formatObject(IFormComponent field, Locale locale, Object object); 077 078 protected abstract Object parseText(IFormComponent field, ValidationMessages messages, 079 String text) throws ValidatorException; 080 081 protected Object getEmpty() 082 { 083 return null; 084 } 085 086 protected String buildMessage(ValidationMessages messages, IFormComponent field, String key) 087 { 088 String label = field.getDisplayName(); 089 090 Object[] parameters = getMessageParameters(messages.getLocale(), label); 091 092 return messages.formatValidationMessage(_message, key, parameters); 093 } 094 095 protected Object[] getMessageParameters(Locale locale, String label) 096 { 097 return new Object[] 098 { label }; 099 } 100 101 /** 102 * @see org.apache.tapestry.form.FormComponentContributor#renderContribution(org.apache.tapestry.IRequestCycle, 103 * org.apache.tapestry.form.IFormComponent) 104 */ 105 public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, 106 FormComponentContributorContext context, IFormComponent field) 107 { 108 super.renderContribution(writer, cycle, context, field); 109 110 if (_trim) 111 context.addSubmitHandler("function (event) { Tapestry.trim_field_value('" + field.getClientId() + "'); }"); 112 } 113 114 public boolean isTrim() 115 116 { 117 return _trim; 118 } 119 120 public void setTrim(boolean trim) 121 { 122 _trim = trim; 123 } 124 125 public String getMessage() 126 { 127 return _message; 128 } 129 130 public void setMessage(String message) 131 { 132 _message = message; 133 } 134 }