View Javadoc

1   /**
2    * This file is part of the equanda project.
3    *
4    * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7    *
8    * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9    * ANY KIND, either express or implied. See the License for the specific language governing rights and
10   * limitations under the License.
11   *
12   * Alternatively, the contents of this file may be used under the terms of
13   * either the GNU General Public License Version 2 or later (the "GPL"), or
14   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15   * in which case the provisions of the GPL or the LGPL are applicable instead
16   * of those above. If you wish to allow use of your version of this file only
17   * under the terms of either the GPL or the LGPL, and not to allow others to
18   * use your version of this file under the terms of the MPL, indicate your
19   * decision by deleting the provisions above and replace them with the notice
20   * and other provisions required by the GPL or the LGPL. If you do not delete
21   * the provisions above, a recipient may use your version of this file under
22   * the terms of any one of the MPL, the GPL or the LGPL.
23   */
24  
25  package org.equanda.tapestry5.components;
26  
27  import org.apache.tapestry5.*;
28  import org.apache.tapestry5.annotations.*;
29  import org.apache.tapestry5.corelib.mixins.DiscardBody;
30  import org.apache.tapestry5.dom.Element;
31  import org.apache.tapestry5.ioc.annotations.Inject;
32  import org.apache.tapestry5.ioc.services.SymbolSource;
33  import org.apache.tapestry5.ioc.services.TypeCoercer;
34  
35  /**
36   * Text Truncate Component
37   *
38   * @author <a href="mailto:vladimir.tkachenko@gmail.com">Vladimir Tkachenko</a>
39   */
40  @IncludeStylesheet( "classpath:/org/equanda/tapestry5/resources/truncate.css" )
41  @IncludeJavaScriptLibrary( { "${tapestry.scriptaculous}/prototype.js",
42                               "classpath:/org/equanda/tapestry5/resources/t5equanda.js" } )
43  @SupportsInformalParameters
44  public class Truncate
45      implements ClientElement
46  {
47      /** The text to be truncated */
48      @Parameter( required = true, defaultPrefix = BindingConstants.PROP )
49      private String text;
50  
51      /** The max length of the text */
52      @Parameter( defaultPrefix = BindingConstants.LITERAL )
53      private Integer length;
54  
55      /** The suffix */
56      @Parameter( defaultPrefix = BindingConstants.LITERAL )
57      private String suffix;
58  
59      /** Injects SymbolSource object */
60      @Inject
61      private SymbolSource symbolSource;
62  
63      /** Injects TypeCoercer object */
64      @Inject
65      private TypeCoercer typeCoercer;
66  
67      /**
68       * Default binding for length parameter
69       *
70       * @return default length
71       */
72      public Integer defaultLength()
73      {
74          return typeCoercer.coerce( symbolSource.valueForSymbol( "equanda.truncate.default.length" ), Integer.class );
75      }
76  
77      /**
78       * Default binding for suffix parameter
79       *
80       * @return default suffix
81       */
82      public String defaultSuffix()
83      {
84          return symbolSource.valueForSymbol( "equanda.truncate.default.suffix" );
85      }
86  
87      /** Mixin that discards body rendering */
88      @Mixin
89      @SuppressWarnings( "unused" )
90      private DiscardBody discardBody;
91  
92      @Environmental
93      private RenderSupport renderSupport;
94  
95      @Inject
96      private ComponentResources resources;
97  
98      private String assignedClientId;
99  
100     /**
101      * Truncates text to specified length
102      *
103      * @return truncated text
104      */
105     public String getTruncatedText()
106     {
107         int len = length;
108         return text != null && text.length() > len ? ( text.substring( 0, len - suffix.length() ) + suffix ) : text;
109     }
110 
111     public String getClientId()
112     {
113         return assignedClientId;
114     }
115 
116     @SetupRender
117     void setupRender()
118     {
119         assignedClientId = renderSupport.allocateClientId( resources.getId() ).toLowerCase();
120     }
121 
122     /**
123      * Renders truncated text
124      *
125      * @param writer MarkupWriter used to render truncated text tag
126      */
127     @BeginRender
128     void doBeginRender( MarkupWriter writer )
129     {
130         String text = getTruncatedText();
131         if ( null == text ) text = "";
132         writer.element( resources.getElementName() != null ? resources.getElementName() : "span", "id",
133                         assignedClientId ).text( text );
134         resources.renderInformalParameters( writer );
135         writer.end();
136     }
137 
138     /**
139      * Renders full text tag
140      *
141      * @param writer MarkupWriter used to render full text tag
142      */
143     @AfterRender
144     void doAfterRender( MarkupWriter writer )
145     {
146         if ( text != null && text.length() > length )
147         {
148             Element root = writer.getDocument().getRootElement();
149             Element body = null;
150             if ( root != null )
151             {
152                 if ( root.getName().equals( "html" ) )
153                 {
154                     body = root.find( "body" );
155                 }
156             }
157             String[] args = new String[]{ "class", "eqTrunCt", "id", assignedClientId + "_text",
158                                           "style", "visibility: hidden; top: -1000px; left: 0;" };
159             if ( body != null )
160             {
161                 Element element = body.element( "div", args );
162                 element.text( text );
163             }
164             else
165             {
166                 Element element = writer.element( "div", (Object[]) args );
167                 element.text( text );
168                 writer.end();
169             }
170             renderSupport.addScript( "eqTruncInit('%s'); ", assignedClientId );
171         }
172     }
173 
174 }