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.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
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