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  @SupportsInformalParameters
41  public class Truncate
42      implements ClientElement
43  {
44  
45      /**
46       * The text to be truncated
47       */
48      @Parameter( required = true, defaultPrefix = BindingConstants.PROP )
49      private String text;
50  
51      /**
52       * The max length of the text
53       */
54      @Parameter( defaultPrefix = BindingConstants.LITERAL )
55      private Integer length;
56  
57      /**
58       * The suffix
59       */
60      @Parameter( defaultPrefix = BindingConstants.LITERAL )
61      private String suffix;
62  
63      /**
64       * Injects SymbolSource object
65       */
66      @Inject
67      private SymbolSource symbolSource;
68  
69      /**
70       * Injects TypeCoercer object
71       */
72      @Inject
73      private TypeCoercer typeCoercer;
74  
75      /**
76       * Default binding for length parameter
77       * 
78       * @return default length
79       */
80      public Integer defaultLength()
81      {
82          return typeCoercer.coerce( symbolSource.valueForSymbol( "equanda.truncate.default.length" ), Integer.class );
83      }
84  
85      /**
86       * Default binding for suffix parameter
87       * 
88       * @return default suffix
89       */
90      public String defaultSuffix()
91      {
92          return symbolSource.valueForSymbol( "equanda.truncate.default.suffix" );
93      }
94  
95      /**
96       * Mixin that discards body rendering
97       */
98      @Mixin
99      @SuppressWarnings( "unused" )
100     private DiscardBody discardBody;
101 
102     @Environmental
103     private RenderSupport renderSupport;
104 
105     @Inject
106     private ComponentResources resources;
107 
108     @Inject
109     @Path( "classpath:/org/equanda/tapestry5/resources/truncate.js" )
110     private Asset script;
111 
112     @Inject
113     @Path( "classpath:/org/equanda/tapestry5/resources/truncate.css" )
114     private Asset style;
115     
116     private String assignedClientId;
117 
118     /**
119      * Truncates text to specified length
120      * 
121      * @return truncated text
122      */
123     public String getTruncatedText()
124     {
125         int len = length;
126         return text != null && text.length() > len ? ( text.substring( 0, len - suffix.length() ) + suffix ) : text;
127     }
128 
129     public String getClientId()
130     {
131         return assignedClientId;
132     }
133 
134     @SetupRender
135     void setupRender()
136     {
137         assignedClientId = renderSupport.allocateClientId( resources.getId() ).toLowerCase();
138     }
139 
140     /**
141      * Renders truncated text
142      * 
143      * @param writer
144      *            MarkupWriter used to render truncated text tag
145      */
146     @BeginRender
147     void doBeginRender( MarkupWriter writer )
148     {
149         renderSupport.addScriptLink( script );
150         renderSupport.addStylesheetLink( style, null );
151         String text = getTruncatedText();
152         if ( null == text ) text = "";
153         writer.element( resources.getElementName() != null ? resources.getElementName() : "span", "id",
154                 assignedClientId ).text( text );
155         resources.renderInformalParameters( writer );
156         writer.end();
157     }
158 
159     /**
160      * Renders full text tag
161      * 
162      * @param writer
163      *            MarkupWriter used to render full text tag
164      */
165     @AfterRender
166     void doAfterRender( MarkupWriter writer )
167     {
168         if ( text != null && text.length() > length )
169         {
170             Element root = writer.getDocument().getRootElement();
171             Element body = null;
172             if ( root != null )
173             {
174                 if ( root.getName().equals( "html" ) )
175                 {
176                     body = root.find( "body" );
177                 }
178             }
179             String[] args = new String[] { "class", "equanda_truncate_content", "id", assignedClientId + "_text",
180                     "style", "visibility: hidden; top: -1000px; left: 0;" };
181             if ( body != null )
182             {
183                 Element element = body.element( "div", args );
184                 element.text( text );
185             }
186             else
187             {
188                 Element element = writer.element( "div", (Object[]) args );
189                 element.text( text );
190                 writer.end();
191             }
192             renderSupport.addScript( "equandaTruncateInit('%s'); ", assignedClientId );
193         }
194     }
195 
196 }