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
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><input type="text"...></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
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
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
207
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