001// Copyright 2007, 2008, 2010 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 private final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale); 043 044 public String getConvertedEnumValue() 045 { 046 Enum value = (Enum) context.getPropertyValue(); 047 048 if (value == null) return null; 049 050 return TapestryInternalUtils.getLabelForEnum(context.getMessages(), value); 051 } 052 053 public DateFormat getDateFormat() 054 { 055 return dateFormat; 056 } 057 058 public Date getCalendarDate() 059 { 060 Calendar calendar = (Calendar) context.getPropertyValue(); 061 062 if(calendar == null) 063 return null; 064 065 return calendar.getTime(); 066 } 067 068 069 public PropertyOutputContext getContext() 070 { 071 return context; 072 } 073 074 public Renderable getPasswordRenderer() 075 { 076 return new Renderable() 077 { 078 public void render(MarkupWriter writer) 079 { 080 081 Object value = context.getPropertyValue(); 082 083 int length = value == null ? 0 : value.toString().length(); 084 085 for (int i = 0; i < length; i++) writer.write("*"); 086 } 087 }; 088 } 089}