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: LabelTag.java,v $
31   * Revision 1.5  2005/04/11 15:41:55  colinmacleod
32   * Added after text so you can use the label tag
33   * for things like list headers too.
34   *
35   * Revision 1.4  2005/04/09 18:04:20  colinmacleod
36   * Changed copyright text to GPL v2 explicitly.
37   *
38   * Revision 1.3  2005/03/10 10:45:38  colinmacleod
39   * Fixes for tomcat4. Changed setters and getters
40   * to both use the same types.
41   *
42   * Revision 1.2  2005/01/19 13:14:04  colinmacleod
43   * Renamed CausedByException to SystemException.
44   *
45   * Revision 1.1  2005/01/06 23:10:06  colinmacleod
46   * Moved up a version number.
47   * Changed copyright notices to 2005.
48   * Updated the documentation:
49   *   - started working on multiproject:site docu.
50   *   - changed the logo.
51   * Added checkstyle and fixed LOADS of style issues.
52   * Added separate thirdparty subproject.
53   * Added struts (in web), util and webgui (in webtheme) from ivata op.
54   *
55   * Revision 1.5  2004/12/31 19:32:36  colinmacleod
56   * Cosmetic indentation improvements.
57   *
58   * Revision 1.4  2004/11/03 16:12:09  colinmacleod
59   * Changed fieldPath to resourceFieldPath.
60   *
61   * Revision 1.3  2004/07/13 19:48:10  colinmacleod
62   * Moved project to POJOs from EJBs.
63   * Applied PicoContainer to services layer (replacing session EJBs).
64   * Applied Hibernate to persistence layer (replacing entity EJBs).
65   *
66   * Revision 1.2  2004/03/21 21:16:35  colinmacleod
67   * Shortened name to ivata op.
68   *
69   * Revision 1.1.1.1  2004/01/27 20:59:39  colinmacleod
70   * Moved ivata op to SourceForge.
71   *
72   * Revision 1.2  2003/10/16 15:43:03  jano
73   * Fixes problems with building and some problems with splitting to subprojects
74   *
75   * Revision 1.1.1.1  2003/10/13 20:49:24  colin
76   * Restructured portal into subprojects
77   *
78   * Revision 1.2  2003/03/04 00:19:00  colin
79   * added resourceFieldPath to label directly
80   *
81   * Revision 1.1  2003/03/03 16:57:12  colin
82   * converted localization to automatic paths
83   * added labels
84   * added mandatory fieldName attribute
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