001// Copyright 2007, 2008, 2010, 2013 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
015package org.apache.tapestry5.corelib.pages;
016
017import org.apache.tapestry5.MarkupWriter;
018import org.apache.tapestry5.Renderable;
019import org.apache.tapestry5.annotations.Environmental;
020import org.apache.tapestry5.internal.TapestryInternalUtils;
021import org.apache.tapestry5.ioc.annotations.Inject;
022import org.apache.tapestry5.services.BeanBlockSource;
023import org.apache.tapestry5.services.PropertyOutputContext;
024
025import java.text.DateFormat;
026import java.util.Calendar;
027import java.util.Date;
028import java.util.Locale;
029
030/**
031 * Contains blocks for displaying basic property types; the blocks are contributed to the
032 * {@link BeanBlockSource} service.
033 */
034public class PropertyDisplayBlocks
035{
036    @Environmental
037    private PropertyOutputContext context;
038
039    @Inject
040    private Locale locale;
041
042    public String getConvertedEnumValue()
043    {
044        Enum value = (Enum) context.getPropertyValue();
045
046        if (value == null) return null;
047
048        return TapestryInternalUtils.getLabelForEnum(context.getMessages(), value);
049    }
050
051    public DateFormat getDateFormat()
052    {
053        return DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
054    }
055
056    public Date getCalendarDate()
057    {
058        Calendar calendar = (Calendar) context.getPropertyValue();
059
060        return calendar == null ? null : calendar.getTime();
061    }
062
063
064    public PropertyOutputContext getContext()
065    {
066        return context;
067    }
068
069    public Renderable getPasswordRenderer()
070    {
071        return new Renderable()
072        {
073            public void render(MarkupWriter writer)
074            {
075
076                Object value = context.getPropertyValue();
077
078                int length = value == null ? 0 : value.toString().length();
079
080                for (int i = 0; i < length; i++) writer.write("*");
081            }
082        };
083    }
084}