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: FieldLabelTag.java,v $
31   * Revision 1.7  2005/04/09 18:04:19  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.6  2005/03/10 10:47:21  colinmacleod
35   * Added label key prefix.
36   *
37   * Revision 1.5  2005/01/06 23:10:01  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.4  2004/12/30 20:28:48  colinmacleod
48   * Added red, bold text when the message resource is not defined.
49   *
50   * Revision 1.3  2004/12/23 21:28:32  colinmacleod
51   * Modifications to add ivata op compatibility.
52   *
53   * Revision 1.2  2004/11/11 13:48:40  colinmacleod
54   * Added subField.
55   *
56   * Revision 1.1.1.1  2004/05/16 20:40:33  colinmacleod
57   * Ready for 0.1 release
58   * -----------------------------------------------------------------------------
59   */
60  package com.ivata.mask.web.tag;
61  import java.text.MessageFormat;
62  import javax.servlet.jsp.JspException;
63  import javax.servlet.jsp.tagext.TagSupport;
64  import org.apache.struts.Globals;
65  import org.apache.struts.taglib.TagUtils;
66  import com.ivata.mask.field.Field;
67  /***
68   * <p>
69   * Creates an input field, or displays the value for a field.
70   * </p>
71   *
72   * @since ivata masks 0.1 (2004-05-11)
73   * @author Colin MacLeod
74   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
75   * @version $Revision: 1.7 $
76   * TODO: replace this class with <code>LabelTag</code> from the HTML package.
77   */
78  public class FieldLabelTag extends TagSupport {
79      /***
80       * <p>
81       * By default, this is the prefix used to create label keys. The field name
82       * is appended to this key.
83       * </p>
84       */
85      private static final String LABEL_KEY_PREFIX = "field.label.";
86      /***
87       * Text to display when the localized message is unavailable.
88       */
89      private static final String NULL_STRING =
90          "<b><font color='red'>MISSING LABEL {0}</font></b>";
91      /***
92       * <p>
93       * Struts message resources bundle.
94       * </p>
95       */
96      private String bundle;
97      /***
98       * <p>
99       * Stores the identifier of the field within this group to be displayed.
100      * </p>
101      */
102     private Field field;
103     /***
104      * <p>
105      * Sub-field within the field to be displayed.
106      * </p>
107      */
108     private Field subField;
109     /***
110      * <p>
111      * Called when the tag is first encountered. Simply diplays the field for
112      * now.
113      * </p>
114      *
115      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
116      */
117     public int doStartTag() throws JspException {
118         Field displayField;
119         if (subField == null) {
120             displayField = field;
121         } else {
122             displayField = subField;
123         }
124         TagUtils tagUtils = TagUtils.getInstance();
125         String label = tagUtils.message(pageContext, bundle,
126                 Globals.LOCALE_KEY, LABEL_KEY_PREFIX + displayField.getName(),
127                 null);
128         // if the label is empty, change the label to a warning text
129         if (label == null) {
130             MessageFormat format = new MessageFormat(NULL_STRING);
131             label = format.format(new Object[] {LABEL_KEY_PREFIX
132                     + displayField.getName()});
133         }
134         tagUtils.write(pageContext, label);
135         return super.doStartTag();
136     }
137     /***
138      * <p>
139      * Struts message resources bundle.
140      * </p>
141      *
142      * @return Struts message resources bundle.
143      */
144     public final String getBundle() {
145         return bundle;
146     }
147     /***
148      * <p>
149      * Field to be displayed.
150      * </p>
151      *
152      * @return field to be displayed.
153      */
154     public Field getField() {
155         return field;
156     }
157     /***
158      * <p>
159      * Sub-field within the field to be displayed.
160      * </p>
161      *
162      * @return Sub-field within the field to be displayed.
163      */
164     public Field getSubField() {
165         return subField;
166     }
167     /***
168      * <p>
169      * Struts message resources bundle.
170      * </p>
171      *
172      * @param string
173      *            Struts message resources bundle.
174      */
175     public final void setBundle(final String string) {
176         bundle = string;
177     }
178     /***
179      * <p>
180      * Field to be displayed.
181      * </p>
182      *
183      * @param fieldParam
184      *            field to be displayed.
185      */
186     public void setField(final Field fieldParam) {
187         this.field = fieldParam;
188     }
189     /***
190      * <p>
191      * Sub-field within the field to be displayed.
192      * </p>
193      *
194      * @param fieldParam
195      *            Sub-field within the field to be displayed.
196      */
197     public void setSubField(final Field fieldParam) {
198         subField = fieldParam;
199     }
200 }
201