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