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