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;
016    
017    import org.apache.hivemind.service.ThreadLocale;
018    import org.apache.tapestry.IMarkupWriter;
019    import org.apache.tapestry.IRequestCycle;
020    import org.apache.tapestry.valid.IValidationDelegate;
021    import org.apache.tapestry.valid.ValidatorException;
022    
023    public class TranslatedFieldSupportImpl implements TranslatedFieldSupport
024    {
025        private ThreadLocale _threadLocale;
026    
027        /**
028         * @return Returns the threadLocale.
029         */
030        public ThreadLocale getThreadLocale()
031        {
032            return _threadLocale;
033        }
034    
035        /**
036         * @param threadLocale
037         *            The threadLocale to set.
038         */
039        public void setThreadLocale(ThreadLocale threadLocale)
040        {
041            _threadLocale = threadLocale;
042        }
043    
044        public String format(TranslatedField field, Object object)
045        {
046            IValidationDelegate delegate = field.getForm().getDelegate();
047    
048            return delegate.isInError() ? delegate.getFieldInputValue() : field.getTranslator().format(
049                    field,
050                    _threadLocale.getLocale(),
051                    object);
052        }
053    
054        public Object parse(TranslatedField field, String text) throws ValidatorException
055        {
056            IValidationDelegate delegate = field.getForm().getDelegate();
057    
058            delegate.recordFieldInputValue(text);
059    
060            ValidationMessages messages = new ValidationMessagesImpl(field, _threadLocale.getLocale());
061    
062            return field.getTranslator().parse(field, messages, text);
063        }
064    
065        public void renderContributions(TranslatedField field, IMarkupWriter writer, IRequestCycle cycle)
066        {
067            if (field.getForm().isClientValidationEnabled())
068            {
069                FormComponentContributorContext context = new FormComponentContributorContextImpl(
070                        _threadLocale.getLocale(), cycle, field);
071    
072                field.getTranslator().renderContribution(writer, cycle, context, field);
073            }
074        }
075    }