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