001    //  Copyright 2008 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.tapestry5.internal.services;
016    
017    import org.apache.tapestry5.*;
018    import org.apache.tapestry5.ioc.MessageFormatter;
019    import org.apache.tapestry5.services.FormSupport;
020    
021    public class FieldTranslatorImpl<T> implements FieldTranslator<T>
022    {
023        private final Field field;
024        private final Translator<T> translator;
025        private final MessageFormatter formatter;
026        private final FormSupport formSupport;
027    
028        public FieldTranslatorImpl(Field field, Translator<T> translator, MessageFormatter formatter,
029                                   FormSupport formSupport)
030        {
031            this.field = field;
032            this.translator = translator;
033            this.formatter = formatter;
034            this.formSupport = formSupport;
035        }
036    
037        public T parse(String input) throws ValidationException
038        {
039            return translator.parseClient(field, input, formatMessage());
040        }
041    
042        private String formatMessage()
043        {
044            return formatter.format(field.getLabel());
045        }
046    
047        public void render(MarkupWriter writer)
048        {
049            translator.render(field, formatMessage(), writer, formSupport);
050        }
051    
052        public String toClient(T value)
053        {
054            return translator.toClient(value);
055        }
056    
057        public Class<T> getType()
058        {
059            return translator.getType();
060        }
061    }