001    /*
002     * Apache License
003     * Version 2.0, January 2004
004     * http://www.apache.org/licenses/
005     *
006     * Copyright 2008-2010 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    package org.chenillekit.tapestry.core.components;
016    
017    import org.apache.tapestry5.ComponentResources;
018    import org.apache.tapestry5.MarkupWriter;
019    import org.apache.tapestry5.annotations.Parameter;
020    import org.apache.tapestry5.annotations.SupportsInformalParameters;
021    import org.apache.tapestry5.ioc.annotations.Inject;
022    
023    /**
024     * Replace from <em>maxLength</em> all text with parameter value <em>suffix</em>.
025     *
026     * @version $Id: TrimmedString.java 645 2010-03-11 13:05:54Z homburgs $
027     */
028    @SupportsInformalParameters
029    public class TrimmedString
030    {
031        public static final String TRIM_LEFT = "left";
032        public static final String TRIM_RIGHT = "right";
033    
034        /**
035         * the string value
036         */
037        @Parameter(required = true, defaultPrefix = "prop")
038        private String value;
039    
040        /**
041         * max. length of string value
042         */
043        @Parameter(required = false, defaultPrefix = "literal", value = "20")
044        private int maxLength;
045    
046        /**
047         * the suffix for strings that longer then <em>maxLength</em>
048         */
049        @Parameter(required = false, defaultPrefix = "literal", value = "...")
050        private String suffix;
051    
052        /**
053         * trim the string at the <code>left</code> or <code>right</code> site.
054         * Default is <code>right</code>.
055         */
056        @Parameter(required = false, defaultPrefix = "literal", value = TRIM_RIGHT)
057        private String trimPos;
058    
059        /**
060         * ComponentResources. For blocks, messages, crete actionlink, trigger event
061         */
062        @Inject
063        private ComponentResources _resources;
064    
065    
066        boolean beginRender(MarkupWriter writer)
067        {
068            String tmpValue = value;
069    
070            if (tmpValue == null)
071                return true;
072    
073            if (tmpValue.length() > maxLength)
074            {
075                if (trimPos.equalsIgnoreCase(TRIM_LEFT))
076                    tmpValue = suffix + value.substring(value.length() - maxLength);
077                else
078                    tmpValue = value.substring(0, maxLength) + suffix;
079            }
080    
081            writer.element("span", "title", value);
082            _resources.renderInformalParameters(writer);
083            writer.write(tmpValue);
084            writer.end();
085    
086            return false;
087        }
088    }