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: TextFieldWriter.java,v $
031: * Revision 1.11 2005/10/11 18:54:06 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.10 2005/10/09 09:55:17 colinmacleod
035: * Merged changes from ivata masks v0.6.2 into main trunk.
036: *
037: * Revision 1.9 2005/10/03 10:17:25 colinmacleod
038: * Fixed some style and javadoc issues.
039: *
040: * Revision 1.8 2005/10/02 14:06:33 colinmacleod
041: * Added/improved log4j logging.
042: *
043: * Revision 1.7 2005/09/29 12:14:09 colinmacleod
044: * Split off the code for setting the value attribute into a new method which
045: * can be overridden (in the password field writer).
046: *
047: * Revision 1.6.2.1 2005/10/08 10:52:09 colinmacleod
048: * Added automatic hidden field when the field is disabled.
049: *
050: * Revision 1.6 2005/04/09 18:04:17 colinmacleod
051: * Changed copyright text to GPL v2 explicitly.
052: *
053: * Revision 1.5 2005/01/19 12:50:34 colinmacleod
054: * Added attribute delegate methods.
055: *
056: * Revision 1.4 2005/01/07 08:08:23 colinmacleod
057: * Moved up a version number.
058: * Changed copyright notices to 2005.
059: * Updated the documentation:
060: * - started working on multiproject:site docu.
061: * - changed the logo.
062: * Added checkstyle and fixed LOADS of style issues.
063: * Added separate thirdparty subproject.
064: * Added struts (in web), util and webgui (in webtheme) from ivata op.
065: *
066: * Revision 1.3 2004/12/30 20:20:42 colinmacleod
067: * Set style class if mandatory.
068: *
069: * Revision 1.2 2004/11/11 13:39:12 colinmacleod
070: * Added HTMLFormatter, and AttributesWriter.
071: *
072: * Revision 1.1.1.1 2004/05/16 20:40:32 colinmacleod
073: * Ready for 0.1 release
074: * -----------------------------------------------------------------------------
075: */
076: package com.ivata.mask.web.field.text;
077:
078: import org.apache.log4j.Logger;
079:
080: import javax.servlet.jsp.PageContext;
081: import com.ivata.mask.field.Field;
082: import com.ivata.mask.field.FieldValueConvertor;
083: import com.ivata.mask.valueobject.ValueObject;
084: import com.ivata.mask.web.field.AttributesWriter;
085: import com.ivata.mask.web.field.FieldWriter;
086: import com.ivata.mask.web.format.HTMLFormatter;
087:
088: /**
089: * <p>
090: * This writer is used to display fields as
091: * <code><input type="text"...></code> fields.
092: * </p>
093: *
094: * @since ivata masks 0.1 (2004-05-14)
095: * @author Colin MacLeod
096: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
097: * @version $Revision: 1.11 $
098: */
099: public class TextFieldWriter implements FieldWriter {
100: /**
101: * Logger for this class.
102: */
103: private static final Logger logger = Logger
104: .getLogger(TextFieldWriter.class);
105:
106: /**
107: * <p>
108: * Stores all of the field's attributes and values, then writes them out
109: * later.
110: * </p>
111: */
112: private AttributesWriter attributesWriter;
113: /**
114: * <p>
115: * Used to convert objects to strings.
116: * </p>
117: */
118: private FieldValueConvertor convertor;
119: /**
120: * <p>
121: * Field to be displayed.
122: * </p>
123: */
124: private Field field;
125: /**
126: * <p>
127: * Used to format the returned text.
128: * </p>
129: */
130: private HTMLFormatter formatter;
131:
132: /**
133: * <p>
134: * Construct a field writer.
135: * </p>
136: *
137: * @param fieldParam
138: * defines the field to be displayed.
139: * @param convertorParam
140: * converts objects into strings for display.
141: * @param formatterParam
142: * <copyDoc>Refer to {@link #getFormatter}.</copyDoc>
143: */
144: public TextFieldWriter(final Field fieldParam,
145: final FieldValueConvertor convertorParam,
146: final HTMLFormatter formatterParam) {
147: super ();
148: this .field = fieldParam;
149: attributesWriter = new AttributesWriter(fieldParam);
150: attributesWriter.setAttribute("type", "text");
151: if (fieldParam.isMandatory()) {
152: attributesWriter.appendAttribute("class", "mandatory");
153: }
154: this .convertor = convertorParam;
155: this .formatter = formatterParam;
156: }
157:
158: /**
159: * <p>
160: * Access the attributes writer, which is responsible for converting the
161: * field attributes into text.
162: * </p>
163: *
164: * @return attributes writer.
165: */
166: protected final AttributesWriter getAttributesWriter() {
167: if (logger.isDebugEnabled()) {
168: logger.debug("getAttributesWriter() - start");
169: }
170:
171: if (logger.isDebugEnabled()) {
172: logger
173: .debug("getAttributesWriter() - end - return value = "
174: + attributesWriter);
175: }
176: return attributesWriter;
177: }
178:
179: /**
180: * <p>
181: * Access the field to be displayed.
182: * </p>
183: *
184: * @return field to be displayed.
185: */
186: protected final Field getField() {
187: if (logger.isDebugEnabled()) {
188: logger.debug("getField() - start");
189: }
190:
191: if (logger.isDebugEnabled()) {
192: logger.debug("getField() - end - return value = " + field);
193: }
194: return field;
195: }
196:
197: /**
198: * Used to format the displayed, usually ensuring line breaks are converted
199: * into HTML.
200: *
201: * @return Returns the formatter.
202: */
203: protected final HTMLFormatter getFormatter() {
204: if (logger.isDebugEnabled()) {
205: logger.debug("getFormatter() - start");
206: }
207:
208: if (logger.isDebugEnabled()) {
209: logger.debug("getFormatter() - end - return value = "
210: + formatter);
211: }
212: return formatter;
213: }
214:
215: /**
216: * {@inheritDoc}
217: *
218: * @param name {@inheritDoc}
219: */
220: public void removeAttribute(final String name) {
221: if (logger.isDebugEnabled()) {
222: logger.debug("removeAttribute(String name = " + name
223: + ") - start");
224: }
225:
226: attributesWriter.remove(name);
227:
228: if (logger.isDebugEnabled()) {
229: logger.debug("removeAttribute(String) - end");
230: }
231: }
232:
233: /**
234: * {@inheritDoc}
235: *
236: * @param name {@inheritDoc}
237: * @param value {@inheritDoc}
238: */
239: public void setAttribute(final String name, final String value) {
240: if (logger.isDebugEnabled()) {
241: logger.debug("setAttribute(String name = " + name
242: + ", String value = " + value + ") - start");
243: }
244:
245: attributesWriter.setAttribute(name, value);
246:
247: if (logger.isDebugEnabled()) {
248: logger.debug("setAttribute(String, String) - end");
249: }
250: }
251:
252: /**
253: * Override this method to change how the field writer sets the value
254: * attribute before writing.
255: *
256: * @param valueObject The value object whose value should be written.
257: * @return The string value to be displayed.
258: */
259: protected String setValue(final ValueObject valueObject) {
260: if (logger.isDebugEnabled()) {
261: logger.debug("setValue(ValueObject valueObject = "
262: + valueObject + ") - start");
263: }
264:
265: // clear the value attribute if there is no value
266: String stringValue = convertor.getStringValue(valueObject,
267: field.getPath(), field.getDefaultValue());
268: if (stringValue == null) {
269: attributesWriter.remove("value");
270: stringValue = "";
271: } else {
272: attributesWriter.setAttribute("value", stringValue);
273: }
274:
275: if (logger.isDebugEnabled()) {
276: logger
277: .debug("setValue(ValueObject) - end - return value = "
278: + stringValue);
279: }
280: return stringValue;
281: }
282:
283: /**
284: * {@inheritDoc}
285: *
286: * @param pageContext {@inheritDoc}
287: * @param valueObject {@inheritDoc}
288: * @param displayOnly {@inheritDoc}
289: * @return Text for the input field to be displayed.
290: */
291: public final String write(final PageContext pageContext,
292: final ValueObject valueObject, final boolean displayOnly) {
293: if (logger.isDebugEnabled()) {
294: logger.debug("write(PageContext pageContext = "
295: + pageContext + ", ValueObject valueObject = "
296: + valueObject + ", boolean displayOnly = "
297: + displayOnly + ") - start");
298: }
299:
300: String stringValue = setValue(valueObject);
301: // if the value should only be displayed, return the appropriate value.
302: if (displayOnly) {
303: String returnString = formatter.format(stringValue);
304: if (logger.isDebugEnabled()) {
305: logger.debug("write - end - return value = "
306: + returnString);
307: }
308: return returnString;
309: }
310: StringBuffer buffer = new StringBuffer();
311: buffer.append("<input");
312: buffer.append(attributesWriter.toString());
313: buffer.append("/>");
314: // if the field is disabled, you also need a hidden field
315: // (disabled fields are not submitted)
316: if ("true".equals(attributesWriter.getAttribute("disabled"))) {
317: String type = attributesWriter.getAttribute("type");
318: attributesWriter.setAttribute("type", "hidden");
319: attributesWriter.remove("disabled");
320: buffer.append("<input");
321: buffer.append(attributesWriter.toString());
322: buffer.append("/>");
323: attributesWriter.setAttribute("type", type);
324: attributesWriter.setAttribute("disabled", "true");
325: }
326: String returnString = buffer.toString();
327: if (logger.isDebugEnabled()) {
328: logger
329: .debug("write - end - return value = "
330: + returnString);
331: }
332: return returnString;
333: }
334: }
|