View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: TextTag.java,v $
31   * Revision 1.3  2005/04/11 15:31:13  colinmacleod
32   * Removed extra gt sign from getFieldName comment.
33   *
34   * Revision 1.2  2005/04/09 18:04:20  colinmacleod
35   * Changed copyright text to GPL v2 explicitly.
36   *
37   * Revision 1.1  2005/01/06 23:10:07  colinmacleod
38   * Moved up a version number.
39   * Changed copyright notices to 2005.
40   * Updated the documentation:
41   *   - started working on multiproject:site docu.
42   *   - changed the logo.
43   * Added checkstyle and fixed LOADS of style issues.
44   * Added separate thirdparty subproject.
45   * Added struts (in web), util and webgui (in webtheme) from ivata groupware.
46   *
47   * Revision 1.4  2004/11/03 16:12:09  colinmacleod
48   * Changed fieldPath to resourceFieldPath.
49   *
50   * Revision 1.3  2004/07/13 19:48:10  colinmacleod
51   * Moved project to POJOs from EJBs.
52   * Applied PicoContainer to services layer (replacing session EJBs).
53   * Applied Hibernate to persistence layer (replacing entity EJBs).
54   *
55   * Revision 1.2  2004/03/21 21:16:35  colinmacleod
56   * Shortened name to ivata groupware.
57   *
58   * Revision 1.1.1.1  2004/01/27 20:59:40  colinmacleod
59   * Moved ivata groupware to SourceForge.
60   *
61   * Revision 1.2  2003/10/16 15:43:03  jano
62   * Fixes problems with building and some problems with splitting to subprojects
63   *
64   * Revision 1.1.1.1  2003/10/13 20:49:27  colin
65   * Restructured portal into subprojects
66   *
67   * Revision 1.2  2003/03/03 16:57:12  colin
68   * converted localization to automatic paths
69   * added labels
70   * added mandatory fieldName attribute
71   *
72   * Revision 1.1  2003/02/24 19:33:33  colin
73   * Moved to new subproject.
74   *
75   * Revision 1.5  2003/02/13 08:51:32  colin
76   * moved initialization from doStartTag to MaskProperties.doStartTag
77   *
78   * Revision 1.4  2003/02/04 17:43:51  colin
79   * copyright notice
80   *
81   * Revision 1.3  2003/01/30 07:52:44  colin
82   * bugfixes in mandatory handling
83   *
84   * Revision 1.2  2003/01/27 08:26:21  colin
85   * SSLEXT & i18n changes
86   *
87   * Revision 1.1  2003/01/18 20:32:08  colin
88   * added html struts override tags and help
89   * -----------------------------------------------------------------------------
90   */
91  package com.ivata.mask.web.tag.html;
92  import java.io.IOException;
93  import javax.servlet.jsp.JspException;
94  import javax.servlet.jsp.JspWriter;
95  import com.ivata.mask.util.StringHandling;
96  import com.ivata.mask.web.format.HTMLFormatter;
97  /***
98   * <p>
99   * Overrides an HTML <code>&lt;input type=text&gt;</code> tag, by overriding
100  * the class from <strong>Struts </strong>.
101  * </p>
102  *
103  * <p>
104  * <b>Tag attributes: </b> <br/><table cellpadding='2' cellspacing='5'
105  * border='0' align='center' width='85%'>
106  * <tr class='TableHeadingColor'>
107  * <th>attribute</th>
108  * <th>reqd.</th>
109  * <th>param. class</th>
110  * <th width='100%'>description</th>
111  * </tr>
112  * <tr>
113  * <td>formatter</td>
114  * <td>false</td>
115  * <td><code>boolean</code></td>
116  * <td>Specifies a formatter to format the text if it is read-only.</td>
117  * </tr>
118  * <tr>
119  * <td>fieldName</td>
120  * <td>true</td>
121  * <td><code>java.lang.String</code></td>
122  * <td>The field name is used to default the following attributes of the tag:
123  * <br/>
124  * <ul>
125  * <li><code>styleId</code></li>
126  * <li><code>titleKey</code></li>
127  * <li><code>valueKey</code></li>
128  * </ul>.</td>
129  * </tr>
130  * <tr>
131  * <td>mandatory</td>
132  * <td>false</td>
133  * <td><code>boolean</code></td>
134  * <td>Specifies data for this tag must be entered when the form is submitted.
135  * </td>
136  * </tr>
137  * <tr>
138  * <td>readOnly</td>
139  * <td>false</td>
140  * <td><code>boolean</code></td>
141  * <td>Specifies the form elements should be displayed only and cannot be
142  * altered.</td>
143  * </tr>
144  * </table>
145  * </p>
146  *
147  * <p>
148  * <b>Note: </b> all the tag attributes from {@link
149  * org.apache.struts.taglib.TextTag Stuts} are also included.
150  * </p>
151  *
152  * @since ivata masks 0.4 (2003-01-15)
153  * @author Colin MacLeod
154  * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
155  * @version $Revision: 1.3 $
156  */
157 public class TextTag extends org.apache.struts.taglib.html.TextTag {
158     /***
159      * <p>
160      * Stores and maintains <strong>ivata groupware</strong> specific mask properties.
161      * </p>
162      */
163     private MaskProperties maskProperties = new MaskProperties();
164     /***
165      * <p>
166      * Formatter used to format read-only text.
167      * </p>
168      */
169     private HTMLFormatter formatter = new HTMLFormatter();
170     /***
171      * <p>
172      * Specifies whether or not the input values can be altered.
173      * </p>
174      *
175      * @param readOnly
176      *            <code>true</code> if the input of this field can be changed,
177      *            otherwise <code>false</code> to only display the current
178      *            value.
179      */
180     public void setReadOnly(final boolean readOnly) {
181         maskProperties.setReadOnly(readOnly);
182     }
183     /***
184      * <p>
185      * Set whether or not the value this control must be entered.
186      * </p>
187      *
188      * @param mandatory
189      *            <code>true</code> if this field must be entered, otherwise
190      *            <code>false</code> if the field is optional.
191      */
192     public void setMandatory(final boolean mandatory) {
193         maskProperties.setMandatory(mandatory);
194     }
195     /***
196      * <p>
197      * Overridden to provide mandatory/readony fuctionality.
198      * </p>
199      *
200      * @return value returned by superclass method, or <code>SKIP_BODY</code>
201      *         if this tag is read-only.
202      *
203      * @throws JspException
204      *             if either <code>valueKey</code> or <code>titleKey</code>
205      *             have not been set, or as thrown by superclass method.
206      */
207     public int doStartTag() throws JspException {
208         maskProperties.doStartTag(this, pageContext);
209         if (maskProperties.getReadOnly()) {
210             return SKIP_BODY;
211         } else {
212             // if it is not read-only, use the standard struts implementation
213             return super.doStartTag();
214         }
215     }
216     /***
217      * <p>
218      * Overridden to check whether or not the field is read only, and to apply
219      * mandatory field attributes.
220      * </p>
221      *
222      * @return <code>EVAL_PAGE</code> since we always want to evaluate the
223      *         page after this tag.
224      *
225      * @throws JspException
226      *             if there is an error wrting to <code>out.print()</code>
227      *
228      */
229     public int doEndTag() throws JspException {
230         // if it is read only, just put out the current text contents, formatted
231         if (maskProperties.getReadOnly()) {
232             try {
233                 JspWriter out = pageContext.getOut();
234                 out.print(formatter.format(StringHandling.getNotNull(
235                         getValue(), "")));
236                 maskProperties.reset(this);
237                 return EVAL_PAGE;
238             } catch (IOException e) {
239                 throw new JspException(e);
240             }
241         }
242         // if it is not read-only, use the standard struts implementation
243         int returnValue = super.doEndTag();
244         maskProperties.reset(this);
245         return super.doEndTag();
246     }
247     /***
248      * <p>
249      * Set a formatter to use if the tag is read-only.
250      * </p>
251      *
252      * @param formatterParam
253      *            new value of formatter.
254      */
255     public void setFormatter(final HTMLFormatter formatterParam) {
256         this.formatter = formatterParam;
257     }
258     /***
259      * <p>
260      * Stores and maintains <strong>ivata groupware</strong> specific mask
261      * properties.
262      * </p>
263      *
264      * @return the current value of maskProperties.
265      */
266     public MaskProperties getMaskProperties() {
267         return maskProperties;
268     }
269     /***
270      * <p>
271      * Stores and maintains <strong>ivata groupware</strong> specific mask
272      * properties.
273      * </p>
274      *
275      * @param maskPropertiesParam
276      *            the new value of maskProperties.
277      */
278     public void setMaskProperties(final MaskProperties maskPropertiesParam) {
279         this.maskProperties = maskPropertiesParam;
280     }
281     /***
282      * <p>
283      * Name of the field to which this label refers. This is used in
284      * conjunction with the <code>resourceFieldPath</code> to retrieve the
285      * correct message resources path.
286      * </p>
287      *
288      * @return value of fieldName.
289      */
290     public String getFieldName() {
291         return maskProperties.getFieldName();
292     }
293     /***
294      * <p>
295      * Name of the field to which this label refers. This is used in
296      * conjunction with the <code>resourceFieldPath</code> to retrieve the
297      * correct message resources path.
298      * </p>
299      *
300      * @param fieldName
301      *            the new value of fieldName.
302      */
303     public void setFieldName(final String fieldName) {
304         maskProperties.setFieldName(fieldName);
305     }
306 }
307