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: FieldTag.java,v $
31   * Revision 1.7.2.1  2005/10/08 10:54:51  colinmacleod
32   * Added temporary workarounds for bugs in ivata groupware v0.11.x
33   *
34   * Revision 1.7  2005/04/09 18:04:19  colinmacleod
35   * Changed copyright text to GPL v2 explicitly.
36   *
37   * Revision 1.6  2005/01/19 13:14:04  colinmacleod
38   * Renamed CausedByException to SystemException.
39   *
40   * Revision 1.5  2005/01/06 23:10:02  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.4  2004/12/23 21:28:32  colinmacleod
51   * Modifications to add ivata op compatibility.
52   *
53   * Revision 1.3  2004/11/12 15:10:42  colinmacleod
54   * Moved persistence classes from ivata op as a replacement for
55   * ValueObjectLocator.
56   *
57   * Revision 1.2  2004/11/11 13:48:40  colinmacleod
58   * Added subField.
59   *
60   * Revision 1.1.1.1  2004/05/16 20:40:33  colinmacleod
61   * Ready for 0.1 release
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         // make sure we have a field writer factory
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             // TODO: temporary workaround for ivata groupware
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