View Javadoc

1   /*
2    * This software was designed and created by Jason Carroll.
3    * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4    * The author can be reached at jcarroll@cowsultants.com
5    * ITracker website: http://www.cowsultants.com
6    * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7    *
8    * This program is free software; you can redistribute it and/or modify
9    * it only under the terms of the GNU General Public License as published by
10   * the Free Software Foundation; either version 2 of the License, or
11   * (at your option) any later version.
12   *
13   * This program is distributed in the hope that it will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   * GNU General Public License for more details.
17   */
18  
19  package org.itracker.web.taglib;
20  
21  import javax.servlet.http.HttpServletRequest;
22  import javax.servlet.jsp.JspException;
23  import javax.servlet.jsp.tagext.BodyTagSupport;
24  
25  import org.apache.struts.taglib.TagUtils;
26  import org.itracker.core.resources.ITrackerResources;
27  import org.itracker.web.util.LoginUtilities;
28  
29  
30  /**
31   * Truncate a string if it's longer than truncateLength
32   * 
33   * @author ranks@rosa.com
34   *
35   */
36  public class FormatDescriptionTag extends BodyTagSupport {
37      /**
38  	 * 
39  	 */
40  	private static final long serialVersionUID = 1L;
41  
42  	private String text = null;
43  
44      private String truncateKey = "itracker.web.generic.truncated";
45      private String truncateText = null;
46      private int truncateLength = 40;
47  
48      public String getTruncateKey() {
49          return truncateKey;
50      }
51  
52      public void setTruncateKey(String value) {
53          truncateKey = value;
54      }
55  
56      public int getTruncateLength() {
57          return truncateLength;
58      }
59  
60      public void setTruncateLength(int value) {
61          truncateLength = value;
62      }
63  
64      public int doStartTag() throws JspException {
65          text = null;
66          return EVAL_BODY_BUFFERED;
67      }
68  
69      public int doAfterBody() throws JspException {
70          if(bodyContent != null) {
71              String value = bodyContent.getString().trim();
72              if(value.length() > 0) {
73                  text = value;
74              }
75          }
76          return SKIP_BODY;
77      }
78  
79      public String getTruncateText() {
80      	if (null == truncateText) {
81  	    	if (pageContext.getRequest() instanceof HttpServletRequest) {
82  	    		truncateText = ITrackerResources.getString(truncateKey, LoginUtilities.getCurrentLocale((HttpServletRequest)this.pageContext.getRequest()));
83  	    	} else {
84  	    		truncateText = ITrackerResources.getString(truncateKey);
85  	    	}
86  	    	if (null != truncateText) {
87  	    		truncateText = truncateText.trim();
88  	    	} else {
89  	    		truncateText = "";
90  	    	}
91      	}
92      	return truncateText;
93      }
94      public int doEndTag() throws JspException {
95          StringBuffer results = new StringBuffer();
96          if(text != null && text.trim().length() > truncateLength - getTruncateText().length()) {
97          	results.append(text.trim().substring(0, truncateLength - getTruncateText().length()).trim());
98          	results.append(getTruncateText());
99          } else if (null == text) {
100 			results.append("");
101 		} else {
102         	results.append(text.trim());
103         }
104         TagUtils.getInstance().write(pageContext, results.toString());
105         clearState();
106         return (EVAL_PAGE);
107     }
108 
109     public void release() {
110         super.release();
111         clearState();
112     }
113 
114     private void clearState() {
115         truncateKey = "itracker.web.generic.truncated";
116         truncateLength = 40;
117         text = null;
118         truncateText = null;
119     }
120 }