1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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><textarea>...</textarea></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
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