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: TextFieldWriter.java,v $
31   * Revision 1.6.2.1  2005/10/08 10:52:09  colinmacleod
32   * Added automatic hidden field when the field is disabled.
33   *
34   * Revision 1.6  2005/04/09 18:04:17  colinmacleod
35   * Changed copyright text to GPL v2 explicitly.
36   *
37   * Revision 1.5  2005/01/19 12:50:34  colinmacleod
38   * Added attribute delegate methods.
39   *
40   * Revision 1.4  2005/01/07 08:08:23  colinmacleod
41   * Moved up a version number.
42   * Changed copyright notices to 2005.
43   * Updated the documentation:
44   *   - started working on multiproject:site docu.
45   *   - changed the logo.
46   * Added checkstyle and fixed LOADS of style issues.
47   * Added separate thirdparty subproject.
48   * Added struts (in web), util and webgui (in webtheme) from ivata op.
49   *
50   * Revision 1.3  2004/12/30 20:20:42  colinmacleod
51   * Set style class if mandatory.
52   *
53   * Revision 1.2  2004/11/11 13:39:12  colinmacleod
54   * Added HTMLFormatter, and AttributesWriter.
55   *
56   * Revision 1.1.1.1  2004/05/16 20:40:32  colinmacleod
57   * Ready for 0.1 release
58   * -----------------------------------------------------------------------------
59   */
60  package com.ivata.mask.web.field.text;
61  import javax.servlet.jsp.PageContext;
62  import com.ivata.mask.field.Field;
63  import com.ivata.mask.field.FieldValueConvertor;
64  import com.ivata.mask.valueobject.ValueObject;
65  import com.ivata.mask.web.field.AttributesWriter;
66  import com.ivata.mask.web.field.FieldWriter;
67  import com.ivata.mask.web.format.HTMLFormatter;
68  /***
69   * <p>
70   * This writer is used to display fields as
71   * <code>&lt;input type=&quot;text&quot;...&gt;</code> fields.
72   * </p>
73   *
74   * @since ivata masks 0.1 (2004-05-14)
75   * @author Colin MacLeod
76   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
77   * @version $Revision: 1.6.2.1 $
78   */
79  public class TextFieldWriter implements FieldWriter {
80      /***
81       * <p>
82       * Stores all of the field's attributes and values, then writes them out
83       * later.
84       * </p>
85       */
86      private AttributesWriter attributesWriter;
87      /***
88       * <p>
89       * Used to convert objects to strings.
90       * </p>
91       */
92      private FieldValueConvertor convertor;
93      /***
94       * <p>
95       * Field to be displayed.
96       * </p>
97       */
98      private Field field;
99      /***
100      * <p>
101      * Used to format the returned text.
102      * </p>
103      */
104     private HTMLFormatter formatter;
105     /***
106      * <p>
107      * Construct a field writer.
108      * </p>
109      *
110      * @param fieldParam
111      *            defines the field to be displayed.
112      * @param convertorParam
113      *            converts objects into strings for display.
114      * @param formatterParam
115      *            Refer to {@link #getFormatter}.
116      */
117     public TextFieldWriter(final Field fieldParam,
118             final FieldValueConvertor convertorParam,
119             final HTMLFormatter formatterParam) {
120         super();
121         this.field = fieldParam;
122         attributesWriter = new AttributesWriter(fieldParam);
123         attributesWriter.setAttribute("type", "text");
124         if (fieldParam.isMandatory()) {
125             attributesWriter.appendAttribute("class", "mandatory");
126         }
127         this.convertor = convertorParam;
128         this.formatter = formatterParam;
129     }
130     /***
131      * <p>
132      * Access the attributes writer, which is responsible for converting the
133      * field attributes into text.
134      * </p>
135      *
136      * @return attributes writer.
137      */
138     protected final AttributesWriter getAttributesWriter() {
139         return attributesWriter;
140     }
141     /***
142      * <p>
143      * Access the field to be displayed.
144      * </p>
145      *
146      * @return field to be displayed.
147      */
148     protected final Field getField() {
149         return field;
150     }
151     /***
152      * Used to format the displayed, usually ensuring line breaks are converted
153      * into HTML.
154      *
155      * @return Returns the formatter.
156      */
157     protected final HTMLFormatter getFormatter() {
158         return formatter;
159     }
160     /***
161      * Refer to {@link FieldWriter#clearAttribute}.
162      *
163      * @param name Refer to {@link FieldWriter#clearAttribute}.
164      */
165     public void removeAttribute(final String name) {
166         attributesWriter.remove(name);
167     }
168     /***
169      * Refer to {@link FieldWriter#setAttribute}.
170      *
171      * @param name Refer to {@link FieldWriter#setAttribute}.
172      * @param value Refer to {@link FieldWriter#setAttribute}.
173      */
174     public void setAttribute(final String name, final String value) {
175         attributesWriter.setAttribute(name, value);
176     }
177     /***
178      * Refer to {@link com.ivata.mask.web.field.FieldWriter#write}.
179      *
180      * @param pageContext
181      *            Refer to {@link com.ivata.mask.web.field.FieldWriter#write}.
182      * @param valueObject
183      *            Refer to {@link com.ivata.mask.web.field.FieldWriter#write}.
184      * @param displayOnly
185      *            Refer to {@link com.ivata.mask.web.field.FieldWriter#write}.
186      * @return Refer to {@link com.ivata.mask.web.field.FieldWriter#write}.
187      */
188     public final String write(final PageContext pageContext,
189             final ValueObject valueObject, final boolean displayOnly) {
190         // clear the value attribute if there is no value
191         String stringValue = convertor.getStringValue(valueObject, field
192                 .getPath(), field.getDefaultValue());
193         if (stringValue == null) {
194             attributesWriter.remove("value");
195         } else {
196             attributesWriter.setAttribute("value", stringValue);
197         }
198         // if the value should only be displayed, return the appropriate value.
199         if (displayOnly) {
200             return formatter.format(stringValue);
201         }
202         StringBuffer buffer = new StringBuffer();
203         buffer.append("<input");
204         buffer.append(attributesWriter.toString());
205         buffer.append("/>");
206         // if the field is disabled, you also need a hidden field
207         // (disabled fields are not submitted)
208         if ("true".equals(attributesWriter.getAttribute("disabled"))) {
209             String type = attributesWriter.getAttribute("type");
210             attributesWriter.setAttribute("type", "hidden");
211             attributesWriter.remove("disabled");
212             buffer.append("<input");
213             buffer.append(attributesWriter.toString());
214             buffer.append("/>");
215             attributesWriter.setAttribute("type", type);
216             attributesWriter.setAttribute("disabled", "true");
217         }
218         return buffer.toString();
219     }
220 }
221