001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: FieldLabelTag.java,v $
031: * Revision 1.9 2005/10/02 14:02:00 colinmacleod
032: * Added/improved log4j logging.
033: *
034: * Revision 1.8 2005/09/14 13:23:58 colinmacleod
035: * Added serialVersionUID.
036: *
037: * Revision 1.7 2005/04/09 18:04:19 colinmacleod
038: * Changed copyright text to GPL v2 explicitly.
039: *
040: * Revision 1.6 2005/03/10 10:47:21 colinmacleod
041: * Added label key prefix.
042: *
043: * Revision 1.5 2005/01/06 23:10:01 colinmacleod
044: * Moved up a version number.
045: * Changed copyright notices to 2005.
046: * Updated the documentation:
047: * - started working on multiproject:site docu.
048: * - changed the logo.
049: * Added checkstyle and fixed LOADS of style issues.
050: * Added separate thirdparty subproject.
051: * Added struts (in web), util and webgui (in webtheme) from ivata op.
052: *
053: * Revision 1.4 2004/12/30 20:28:48 colinmacleod
054: * Added red, bold text when the message resource is not defined.
055: *
056: * Revision 1.3 2004/12/23 21:28:32 colinmacleod
057: * Modifications to add ivata op compatibility.
058: *
059: * Revision 1.2 2004/11/11 13:48:40 colinmacleod
060: * Added subField.
061: *
062: * Revision 1.1.1.1 2004/05/16 20:40:33 colinmacleod
063: * Ready for 0.1 release
064: * -----------------------------------------------------------------------------
065: */
066: package com.ivata.mask.web.tag;
067:
068: import org.apache.log4j.Logger;
069:
070: import java.text.MessageFormat;
071: import javax.servlet.jsp.JspException;
072: import javax.servlet.jsp.tagext.TagSupport;
073: import org.apache.struts.Globals;
074: import org.apache.struts.taglib.TagUtils;
075: import com.ivata.mask.field.Field;
076:
077: /**
078: * <p>
079: * Creates an input field, or displays the value for a field.
080: * </p>
081: *
082: * @since ivata masks 0.1 (2004-05-11)
083: * @author Colin MacLeod
084: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
085: * @version $Revision: 1.9 $
086: * TODO: replace this class with <code>LabelTag</code> from the HTML package.
087: */
088: public class FieldLabelTag extends TagSupport {
089: /**
090: * Logger for this class.
091: */
092: private static final Logger logger = Logger
093: .getLogger(FieldLabelTag.class);
094:
095: /**
096: * Serialization version (for <code>Serializable</code> interface).
097: */
098: private static final long serialVersionUID = 1L;
099: /**
100: * <p>
101: * By default, this is the prefix used to create label keys. The field name
102: * is appended to this key.
103: * </p>
104: */
105: private static final String LABEL_KEY_PREFIX = "field.label.";
106: /**
107: * Text to display when the localized message is unavailable.
108: */
109: private static final String NULL_STRING = "<b><font color='red'>MISSING LABEL {0}</font></b>";
110: /**
111: * <p>
112: * Struts message resources bundle.
113: * </p>
114: */
115: private String bundle;
116: /**
117: * <p>
118: * Stores the identifier of the field within this group to be displayed.
119: * </p>
120: */
121: private Field field;
122: /**
123: * <p>
124: * Sub-field within the field to be displayed.
125: * </p>
126: */
127: private Field subField;
128:
129: /**
130: * <p>
131: * Called when the tag is first encountered. Simply diplays the field for
132: * now.
133: * </p>
134: *
135: * @see javax.servlet.jsp.tagext.Tag#doStartTag()
136: */
137: public int doStartTag() throws JspException {
138: if (logger.isDebugEnabled()) {
139: logger.debug("doStartTag() - start");
140: }
141:
142: Field displayField;
143: if (subField == null) {
144: displayField = field;
145: } else {
146: displayField = subField;
147: }
148: TagUtils tagUtils = TagUtils.getInstance();
149: String label = tagUtils.message(pageContext, bundle,
150: Globals.LOCALE_KEY, LABEL_KEY_PREFIX
151: + displayField.getName(), null);
152: // if the label is empty, change the label to a warning text
153: if (label == null) {
154: MessageFormat format = new MessageFormat(NULL_STRING);
155: label = format.format(new Object[] { LABEL_KEY_PREFIX
156: + displayField.getName() });
157: }
158: tagUtils.write(pageContext, label);
159: int returnint = super .doStartTag();
160: if (logger.isDebugEnabled()) {
161: logger.debug("doStartTag() - end - return value = "
162: + returnint);
163: }
164: return returnint;
165: }
166:
167: /**
168: * <p>
169: * Struts message resources bundle.
170: * </p>
171: *
172: * @return Struts message resources bundle.
173: */
174: public final String getBundle() {
175: if (logger.isDebugEnabled()) {
176: logger.debug("getBundle() - start");
177: }
178:
179: if (logger.isDebugEnabled()) {
180: logger
181: .debug("getBundle() - end - return value = "
182: + bundle);
183: }
184: return bundle;
185: }
186:
187: /**
188: * <p>
189: * Field to be displayed.
190: * </p>
191: *
192: * @return field to be displayed.
193: */
194: public Field getField() {
195: if (logger.isDebugEnabled()) {
196: logger.debug("getField() - start");
197: }
198:
199: if (logger.isDebugEnabled()) {
200: logger.debug("getField() - end - return value = " + field);
201: }
202: return field;
203: }
204:
205: /**
206: * <p>
207: * Sub-field within the field to be displayed.
208: * </p>
209: *
210: * @return Sub-field within the field to be displayed.
211: */
212: public Field getSubField() {
213: if (logger.isDebugEnabled()) {
214: logger.debug("getSubField() - start");
215: }
216:
217: if (logger.isDebugEnabled()) {
218: logger.debug("getSubField() - end - return value = "
219: + subField);
220: }
221: return subField;
222: }
223:
224: /**
225: * <p>
226: * Struts message resources bundle.
227: * </p>
228: *
229: * @param string
230: * Struts message resources bundle.
231: */
232: public final void setBundle(final String string) {
233: if (logger.isDebugEnabled()) {
234: logger.debug("setBundle(String string = " + string
235: + ") - start");
236: }
237:
238: bundle = string;
239:
240: if (logger.isDebugEnabled()) {
241: logger.debug("setBundle(String) - end");
242: }
243: }
244:
245: /**
246: * <p>
247: * Field to be displayed.
248: * </p>
249: *
250: * @param fieldParam
251: * field to be displayed.
252: */
253: public void setField(final Field fieldParam) {
254: if (logger.isDebugEnabled()) {
255: logger.debug("setField(Field fieldParam = " + fieldParam
256: + ") - start");
257: }
258:
259: this .field = fieldParam;
260:
261: if (logger.isDebugEnabled()) {
262: logger.debug("setField(Field) - end");
263: }
264: }
265:
266: /**
267: * <p>
268: * Sub-field within the field to be displayed.
269: * </p>
270: *
271: * @param fieldParam
272: * Sub-field within the field to be displayed.
273: */
274: public void setSubField(final Field fieldParam) {
275: if (logger.isDebugEnabled()) {
276: logger.debug("setSubField(Field fieldParam = " + fieldParam
277: + ") - start");
278: }
279:
280: subField = fieldParam;
281:
282: if (logger.isDebugEnabled()) {
283: logger.debug("setSubField(Field) - end");
284: }
285: }
286: }
|