001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008 by chenillekit.org
007     *
008     * Licensed under the Apache License, Version 2.0 (the "License");
009     * you may not use this file except in compliance with the License.
010     * You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     */
015    
016    package org.chenillekit.tapestry.core.components;
017    
018    import java.text.SimpleDateFormat;
019    import java.util.Date;
020    
021    import org.apache.tapestry5.BindingConstants;
022    import org.apache.tapestry5.MarkupWriter;
023    import org.apache.tapestry5.annotations.Mixin;
024    import org.apache.tapestry5.annotations.Parameter;
025    import org.apache.tapestry5.annotations.SetupRender;
026    import org.apache.tapestry5.corelib.mixins.RenderInformals;
027    
028    /**
029     * Formats a Date object with the given <em>pattern<em>.
030     * <p/>
031     * The given html tag sourrunds the formatted date value
032     * end the optional parameter <em>bodyPosition</em> shifts the
033     * tag body to the left or right side, or discarded it.
034     *
035     * @version $Id: DateFormat.java 619 2010-02-23 23:41:40Z homburgs $
036     */
037    public class DateFormat
038    {
039        /**
040         * discard body
041         */
042        public static int BODY_POS_NONE = 0;
043    
044        /**
045         * shift body to left
046         */
047        public static int BODY_POS_LEFT = 1;
048    
049        /**
050         * shift body to right
051         */
052        public static int BODY_POS_RIGHT = 2;
053    
054        /**
055         * the date value.
056         */
057        @Parameter(required = true, defaultPrefix = BindingConstants.PROP)
058        private Date value;
059    
060        /**
061         * the format pattern.
062         */
063        @Parameter(required = false, defaultPrefix = BindingConstants.LITERAL, value = "yyyy-MM-dd")
064        private String pattern;
065    
066        /**
067         * the eelement body position.
068         * may be : BODY_POS_NONE (default) / BODY_POS_LEFT / BODY_POS_RIGHT
069         */
070        @Parameter(required = false, defaultPrefix = BindingConstants.LITERAL, value = "0")
071        private int bodyPosition;
072    
073        @Mixin
074        private RenderInformals renderInformals;
075    
076        private SimpleDateFormat sdf;
077    
078            /**
079             * Tapestry render phase method.
080             * Initialize temporary instance variables here.
081             */
082            @SetupRender
083            void setupRender()
084            {
085                    sdf = new SimpleDateFormat();
086            }
087    
088        boolean beginRender(MarkupWriter writer)
089        {
090            if (value == null)
091                return false;
092    
093            sdf.applyPattern(pattern);
094    
095            if (bodyPosition == BODY_POS_NONE || bodyPosition == BODY_POS_RIGHT)
096                writer.write(sdf.format(value));
097    
098            return bodyPosition != BODY_POS_NONE;
099        }
100    
101        void afterRender(MarkupWriter writer)
102        {
103            if (value == null)
104                return;
105    
106            if (bodyPosition == BODY_POS_LEFT)
107                writer.write(sdf.format(value));
108        }
109    }