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
61
62
63
64 package com.ivata.mask.web.tag;
65 import javax.servlet.http.HttpServletRequest;
66 import javax.servlet.jsp.JspException;
67 import javax.servlet.jsp.PageContext;
68 import javax.servlet.jsp.tagext.TagSupport;
69 import org.apache.struts.taglib.TagUtils;
70 import com.ivata.mask.Mask;
71 import com.ivata.mask.field.Field;
72 import com.ivata.mask.util.SystemException;
73 import com.ivata.mask.valueobject.ValueObject;
74 import com.ivata.mask.web.field.FieldWriter;
75 import com.ivata.mask.web.field.FieldWriterFactory;
76 import com.ivata.mask.web.struts.InputMaskForm;
77 import com.ivata.mask.web.struts.ListForm;
78 import com.ivata.mask.web.struts.MaskForm;
79 /***
80 * <p>
81 * Creates an input field, or displays the value for a field.
82 * </p>
83 *
84 * <p>
85 * <b>Note: </b> you must set a session attribute
86 *
87 * @since ivata masks 0.1 (2004-05-11)
88 * @author Colin MacLeod
89 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
90 * @version $Revision: 1.7.2.1 $
91 */
92 public class FieldTag extends TagSupport {
93 /***
94 * <p>
95 * Set this to the field writer factory we'll be using to create field
96 * writers.
97 * </p>
98 */
99 private static FieldWriterFactory fieldWriterFactory = null;
100 /***
101 * <p>
102 * Get the field writer factory we'll be using to create field writers.
103 * </p>
104 *
105 * @param pageContext current tag page context, used to lookup the factory
106 * attribute.
107 * @return field writer factory, used to create field writers.
108 * @throws JspException
109 * if the field writer factory has not been set to an
110 * application scope attribute called {@link
111 * FieldWriterFactory#APPLICATION_ATTRIBUTE
112 * FieldWriterFactory.APPLICATION_ATTRIBUTE}.
113 */
114 public static synchronized FieldWriterFactory getFieldWriterFactory(
115 final PageContext pageContext) throws JspException {
116 if (fieldWriterFactory == null) {
117 fieldWriterFactory = (FieldWriterFactory) TagUtils.getInstance()
118 .lookup(pageContext,
119 FieldWriterFactory.APPLICATION_ATTRIBUTE,
120 "application");
121 if (fieldWriterFactory == null) {
122 throw new JspException("ERROR: you must specify a valid field "
123 + "writer factory in application scope, called '"
124 + FieldWriterFactory.APPLICATION_ATTRIBUTE + "'");
125 }
126 }
127 return fieldWriterFactory;
128 }
129 /***
130 * <p>
131 * If <code>true</code>, the field will not allow user entry.
132 * </p>
133 */
134 private boolean disabled = false;
135 /***
136 * <p>
137 * Field to be displayed.
138 * </p>
139 */
140 private Field field;
141 /***
142 * <p>
143 * If <code>true</code>, overrides the hidden value of the field definition,
144 * to make this a hidden field.
145 * </p>
146 * @see Field#isHidden
147 */
148 private boolean hidden = false;
149 /***
150 * <p>
151 * Sub-field within the field to be displayed.
152 * </p>
153 */
154 private Field subField;
155 /***
156 * <p>
157 * Value object to display/edit.
158 * </p>
159 */
160 private ValueObject valueObject;
161 /***
162 * <p>
163 * Called when the tag is first encountered. Simply diplays the field for
164 * now.
165 * </p>
166 *
167 * @see javax.servlet.jsp.tagext.Tag#doStartTag()
168 */
169 public int doStartTag() throws JspException {
170
171 getFieldWriterFactory(pageContext);
172 HttpServletRequest request = (HttpServletRequest) pageContext
173 .getRequest();
174 Mask mask = null;
175 ListForm listForm = (ListForm) request
176 .getAttribute(ListForm.REQUEST_ATTRIBUTE);
177 MaskForm form;
178 boolean displayOnly;
179 if (listForm == null) {
180 InputMaskForm maskForm = (InputMaskForm) request
181 .getAttribute(InputMaskForm.REQUEST_ATTRIBUTE);
182
183 if (maskForm == null) {
184 maskForm = (InputMaskForm) request.getSession()
185 .getAttribute(InputMaskForm.REQUEST_ATTRIBUTE);
186 }
187 if (maskForm == null) {
188 throw new NullPointerException("Either ListForm or MaskForm "
189 + "must be specified in request scope.");
190 }
191 mask = maskForm.getMask();
192 displayOnly = maskForm.isDisplayOnly();
193 form = maskForm;
194 } else {
195 mask = listForm.getMask();
196 displayOnly = mask.isDisplayOnly();
197 form = listForm;
198 }
199 FieldWriter fieldWriter;
200 try {
201 fieldWriter = fieldWriterFactory.getFieldWriter(valueObject, field,
202 subField, hidden);
203 } catch (SystemException e) {
204 throw new JspException(e);
205 }
206 if (disabled) {
207 fieldWriter.setAttribute("disabled", "true");
208 } else {
209 fieldWriter.removeAttribute("disabled");
210 }
211 String value = fieldWriter.write(pageContext, valueObject, displayOnly);
212 TagUtils.getInstance().write(pageContext, value);
213 return super.doStartTag();
214 }
215 /***
216 * <p>
217 * Field to be displayed.
218 * </p>
219 *
220 * @return field to be displayed.
221 */
222 public Field getField() {
223 return field;
224 }
225 /***
226 * <p>
227 * Sub-field within the field to be displayed.
228 * </p>
229 *
230 * @return Sub-field within the field to be displayed.
231 */
232 public Field getSubField() {
233 return subField;
234 }
235 /***
236 * <p>
237 * Value object to display/edit.
238 * </p>
239 *
240 * @return value object to display/edit.
241 */
242 public ValueObject getValueObject() {
243 return valueObject;
244 }
245 /***
246 * <p>
247 * If <code>true</code>, the field will not allow user entry.
248 * </p>
249 *
250 * @return Returns whether or not the field is disabled.
251 */
252 public boolean isDisabled() {
253 return disabled;
254 }
255 /***
256 * <p>
257 * If <code>true</code>, overrides the hidden value of the field definition,
258 * to make this a hidden field.
259 * </p>
260 * @return Returns whether or not the field definition is overridden as
261 * hidden.
262 * @see Field#isHidden
263 */
264 public boolean isHidden() {
265 return hidden;
266 }
267 /***
268 * <p>
269 * If <code>true</code>, the field will not allow user entry.
270 * </p>
271 *
272 * @param disabledParam Set whether or not the field is disabled.
273 */
274 public void setDisabled(final boolean disabledParam) {
275 this.disabled = disabledParam;
276 }
277 /***
278 * <p>
279 * Stores the identifier of the field within this group to be displayed.
280 * </p>
281 *
282 * @param fieldParam definition of the field which this tag is going to
283 * display/edit.
284 */
285 public final void setField(final Field fieldParam) {
286 this.field = fieldParam;
287 }
288 /***
289 * <p>
290 * If <code>true</code>, overrides the hidden value of the field definition,
291 * to make this a hidden field.
292 * </p>
293 * @param hiddenParam Set to <code>true</code> to hide the field, overriding
294 * any setting in the field definition.
295 * @see Field#isHidden
296 */
297 public void setHidden(final boolean hiddenParam) {
298 this.hidden = hiddenParam;
299 }
300 /***
301 * <p>
302 * Sub-field within the field to be displayed.
303 * </p>
304 *
305 * @param fieldParam
306 * Sub-field within the field to be displayed.
307 */
308 public void setSubField(final Field fieldParam) {
309 subField = fieldParam;
310 }
311 /***
312 * <p>
313 * Value object to display/edit.
314 * </p>
315 *
316 * @param object
317 * value object to display/edit.
318 */
319 public void setValueObject(final ValueObject object) {
320 valueObject = object;
321 }
322 }
323