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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87 package com.ivata.mask.web.tag.html;
88
89 import org.apache.log4j.Logger;
90
91 import java.io.IOException;
92 import java.util.List;
93
94 import javax.servlet.jsp.JspException;
95 import javax.servlet.jsp.JspWriter;
96 import org.apache.struts.taglib.html.BaseHandlerTag;
97 import com.ivata.mask.util.StringHandling;
98 import com.ivata.mask.web.format.HTMLFormatter;
99 /***
100 * <p>
101 * Specifies a label for a field. This tag must be contained within a form tag,
102 * and will take the <code>resourceFieldPath</code> and <code>bundle</code>
103 * from there to locate the correct message for this field.
104 * </p>
105 *
106 * @since ivata masks 0.4 (2003-03-01)
107 * @author Colin MacLeod
108 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
109 * @version $Revision: 1.5 $
110 */
111 public class LabelTag extends BaseHandlerTag {
112 /***
113 * Logger for this class.
114 */
115 private static final Logger logger = Logger.getLogger(LabelTag.class);
116 /***
117 * Text to appear after the label. You probably don't want this for a list.
118 */
119 private String afterText = ":";
120 /***
121 * <p>
122 * Usually, the 'for' attribute is the same as the fieldName but, in the
123 * case where mutliple fields have the same name, it is helpful to specify a
124 * unique <code>styleId</code> for each one an hence a unique value for
125 * the <code>for</code> attribute in the label.
126 * </p>
127 */
128 private String forField = null;
129 /***
130 * <p>
131 * Stores the label which is displayed.
132 * </p>
133 */
134 private String label;
135 /***
136 * <p>
137 * Stores and maintains <strong>ivata op </strong> specific mask properties.
138 * </p>
139 */
140 private MaskProperties maskProperties = new MaskProperties();
141 /***
142 * <p>
143 * Resource path of the field within the application resources. Resources
144 * for fields are retrieved by appending the field name to this path. This
145 * attribute is used when the label is not surrounded by a form tag.
146 * </p>
147 */
148 private String resourceFieldPath = null;
149 /***
150 * <p>
151 * Overridden to reset mask properties.
152 * </p>
153 *
154 * @return <code>EVAL_PAGE</code> since we always want to evaluate the
155 * page after this tag.
156 *
157 * @throws JspException
158 * if there is an error in the mask properties.
159 */
160 public int doEndTag() throws JspException {
161 int returnValue = super.doEndTag();
162 maskProperties.reset(this);
163 return returnValue;
164 }
165 /***
166 * <p>
167 * Overridden to call <code>MaskProperties</code> initialization.
168 * </p>
169 *
170 * @return <code>SKIP_BODY</code> as this tag has no body.
171 *
172 * @throws JspException
173 * if there is a problem writing to the page.
174 */
175 public int doStartTag() throws JspException {
176 maskProperties.doStartTag(this, pageContext);
177 JspWriter out = pageContext.getOut();
178 try {
179 StringBuffer attributes = new StringBuffer();
180 String fieldNameParam = maskProperties.getFieldName();
181 if (StringHandling.isNullOrEmpty(forField)
182 && !StringHandling.isNullOrEmpty(fieldNameParam)) {
183 forField = fieldNameParam;
184 }
185 attributes.append(HTMLFormatter.getAttributeNotNull("class",
186 getStyleClass()));
187 attributes.append(HTMLFormatter
188 .getAttributeNotNull("for", forField));
189 attributes.append(HTMLFormatter.getAttributeNotNull("id",
190 fieldNameParam + "Label"));
191 attributes.append(HTMLFormatter.getAttributeNotNull("onblur",
192 getOnblur()));
193 attributes.append(HTMLFormatter.getAttributeNotNull("onclick",
194 getOnclick()));
195 attributes.append(HTMLFormatter.getAttributeNotNull("ondblclick",
196 getOndblclick()));
197 attributes.append(HTMLFormatter.getAttributeNotNull("onfocus",
198 getOnfocus()));
199 attributes.append(HTMLFormatter.getAttributeNotNull("onkeydown",
200 getOnkeydown()));
201 attributes.append(HTMLFormatter.getAttributeNotNull("onkeypress",
202 getOnkeypress()));
203 attributes.append(HTMLFormatter.getAttributeNotNull("onmousedown",
204 getOnmousedown()));
205 attributes.append(HTMLFormatter.getAttributeNotNull("onmousemove",
206 getOnmousemove()));
207 attributes.append(HTMLFormatter.getAttributeNotNull("onmouseout",
208 getOnmouseout()));
209 attributes.append(HTMLFormatter.getAttributeNotNull("onmouseover",
210 getOnmouseover()));
211 attributes.append(HTMLFormatter.getAttributeNotNull("onmouseup",
212 getOnmouseup()));
213 attributes.append(HTMLFormatter.getAttributeNotNull("style",
214 getStyle()));
215 attributes.append(HTMLFormatter.getAttributeNotNull("title",
216 maskProperties.getTitle()));
217 if (!StringHandling.isNullOrEmpty(forField)) {
218 out.print("<label ");
219 out.print(attributes.toString());
220 out.print(">");
221 }
222 out.print(label);
223 out.print(afterText);
224 if (!StringHandling.isNullOrEmpty(forField)) {
225 out.print("</label>");
226 }
227 } catch (IOException e) {
228 throw new JspException(e);
229 }
230 return SKIP_BODY;
231 }
232 /***
233 * Text to appear after the label. You probably don't want this for a list.
234 * @return Returns the after text.
235 */
236 public String getAfterText() {
237 return afterText;
238 }
239 public final List getArgs() {
240 return maskProperties.getLabelArgs();
241 }
242 /***
243 * <p>
244 * Calls {@link MaskProperties#getBundleMaskProperites.getBundle}.
245 * </p>
246 *
247 * @return the current value of bundle.
248 */
249 public final String getBundle() {
250 return maskProperties.getBundle();
251 }
252 /***
253 * If you are using the label to display the correct label to show for a
254 * given class (rather than a field), this returns the name of that class.
255 *
256 * @return Returns the class name.
257 */
258 public String getClassName() {
259 return maskProperties.getClassName();
260 }
261 /***
262 * <p>
263 * Name of the field to which this label refers. This is used in
264 * conjunction with the <code>resourceFieldPath</code> to retrieve the
265 * correct message resources path.
266 * </p>
267 *
268 * @return the current value of fieldName.
269 */
270 public String getFieldName() {
271 return maskProperties.getFieldName();
272 }
273 /***
274 * <p>
275 * Usually, the 'for' attribute is the same as the fieldName but, in the
276 * case where mutliple fields have the same name, it is helpful to specify a
277 * unique <code>styleId</code> for each one an hence a unique value for
278 * the <code>for</code> attribute in the label.
279 * </p>
280 *
281 * @return the current value of forField.
282 */
283 public String getForField() {
284 return forField;
285 }
286 /***
287 * <p>
288 * Some fields have multiple label keys, depending on circumstances. This
289 * suffix is appended to the key when retrieving the localized text from the
290 * application resources file.
291 * </p>
292 *
293 * @return the current value of keySuffix.
294 */
295 public String getKeySuffix() {
296 return maskProperties.getLabelKeySuffix();
297 }
298 /***
299 * Get the current text value of the label.
300 *
301 * @return Returns the label.
302 */
303 public String getLabel() {
304 return label;
305 }
306 /***
307 * <p>
308 * Stores and maintains <strong>ivata op </strong> specific mask properties.
309 * </p>
310 *
311 * @return the current value of maskProperties.
312 */
313 public MaskProperties getMaskProperties() {
314 return maskProperties;
315 }
316 /***
317 * <p>
318 * Resource path of the field within the application resources. Resources
319 * for fields are retrieved by appending the field name to this path. This
320 * attribute is used when the label is not surrounded by a form tag.
321 * </p>
322 *
323 * @return the current value of resourceFieldPath.
324 */
325 public String getResourceFieldPath() {
326 return resourceFieldPath;
327 }
328 public final List getTitleArgs() {
329 return maskProperties.getTitleArgs();
330 }
331 /***
332 * Refer to {@link #getAfterText}.
333 * @param afterTextParam Refer to {@link #getAfterText}.
334 */
335 public void setAfterText(String afterTextParam) {
336 if (logger.isDebugEnabled()) {
337 logger.debug("Setting afterText. Before '" + afterText
338 + "', after '" + afterTextParam + "'");
339 }
340 afterText = afterTextParam;
341 }
342 /***
343 * Refer to {@link MaskProperties#setLabelArgs}.
344 *
345 * @param args Refer to {@link MaskProperties#setLabelArgs}.
346 */
347 public final void setArgs(final List args) {
348 maskProperties.setLabelArgs(args);
349 }
350 /***
351 * Refer to {@link MaskProperties#setBundle}.
352 *
353 * @param bundleParam Refer to {@link MaskProperties#setBundle}.
354 */
355 public void setBundle(final String bundleParam) {
356 maskProperties.setBundle(bundleParam);
357 }
358 /***
359 * Refer to {@link #getClassName}.
360 * @param classNameParam Refer to {@link #getClassName}.
361 */
362 public void setClassName(String classNameParam) {
363 if (logger.isDebugEnabled()) {
364 logger.debug("Setting className. Before '" + getClassName()
365 + "', after '" + classNameParam + "'");
366 }
367 maskProperties.setClassName(classNameParam);
368 }
369 /***
370 * <p>
371 * >Name of the field to which this tag refers. This is used in conjunction
372 * with the <code>resourceFieldPath</code> to retrieve the correct message
373 * resources path.
374 * </p>
375 *
376 * @param fieldNameParam
377 * the new value of fieldName.
378 */
379 public void setFieldName(final String fieldNameParam) {
380 maskProperties.setFieldName(fieldNameParam);
381 }
382 /***
383 * <p>
384 * Usually, the 'for' attribute is the same as the fieldName but, in the
385 * case where multiple fields have the same name, it is helpful to specify a
386 * unique <code>styleId</code> for each one an hence a unique value for
387 * the <code>for</code> attribute in the label.
388 * </p>
389 *
390 * @param forFieldParam
391 * the new value of forField.
392 */
393 public void setForField(final String forFieldParam) {
394 this.forField = forFieldParam;
395 }
396 /***
397 * <p>
398 * Some fields have multiple label keys, depending on circumstances. This
399 * suffix is appended to the key when retrieving the localized text from the
400 * applicaiton resources file.
401 * </p>
402 *
403 * @param keySuffix
404 * the new value of keySuffix.
405 */
406 public void setKeySuffix(final String keySuffix) {
407 maskProperties.setLabelKeySuffix(keySuffix);
408 }
409 /***
410 * <p>
411 * Called by <code>MaskProperties</code> to set the label.
412 * </p>
413 *
414 * @param labelParam new text to be displayed as the label.
415 */
416 public void setLabel(final String labelParam) {
417 this.label = labelParam;
418 }
419 /***
420 * <p>
421 * Stores and maintains <strong>ivata op </strong> specific mask properties.
422 * </p>
423 *
424 * @param maskPropertiesParam
425 * the new value of maskProperties.
426 */
427 public void setMaskProperties(final MaskProperties maskPropertiesParam) {
428 this.maskProperties = maskPropertiesParam;
429 }
430 /***
431 * <p>
432 * Resource path of the field within the application resources. Resources
433 * for fields are retrieved by appending the field name to this path. This
434 * attribute is used when the label is not surrounded by a form tag.
435 * </p>
436 *
437 * @param resourceFieldPathParam
438 * the new value of resourceFieldPath.
439 */
440 public void setResourceFieldPath(final String resourceFieldPathParam) {
441 this.resourceFieldPath = resourceFieldPathParam;
442 }
443 /***
444 * Refer to {@link MaskProperties#setTitleArgs}.
445 *
446 * @param args Refer to {@link MaskProperties#setTitleArgs}.
447 */
448 public final void setTitleArgs(final List args) {
449 maskProperties.setTitleArgs(args);
450 }
451 /***
452 * <p>
453 * Set the value of the key used to localize the title tag attribute.
454 * </p>
455 *
456 * @param titleKey
457 * the new value of titleKey.
458 */
459 public final void setTitleKey(final String titleKey) {
460 maskProperties.setTitleKey(titleKey);
461 }
462 }
463