001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: DataFieldWrapper.java,v 1.11.4.1 2007/11/02 16:00:33 drmlipp Exp $
021: *
022: * $Log: DataFieldWrapper.java,v $
023: * Revision 1.11.4.1 2007/11/02 16:00:33 drmlipp
024: * Merged bug fixes from HEAD.
025: *
026: * Revision 1.12 2007/10/23 15:22:24 drmlipp
027: * Fixed problem with truncated input fields.
028: *
029: * Revision 1.11 2007/09/13 20:59:32 mlipp
030: * Added expandable display for XML data.
031: *
032: * Revision 1.10 2007/09/13 15:06:06 drmlipp
033: * Fixed line number calculation.
034: *
035: * Revision 1.9 2007/09/13 07:47:08 drmlipp
036: * Made string fields expandable.
037: *
038: * Revision 1.8 2006/09/29 12:32:11 drmlipp
039: * Consistently using WfMOpen as projct name now.
040: *
041: * Revision 1.7 2006/09/13 07:16:14 drmlipp
042: * Finished date/time input.
043: *
044: * Revision 1.6 2006/09/12 21:39:30 mlipp
045: * Better date/time handling.
046: *
047: * Revision 1.5 2006/09/07 15:04:43 drmlipp
048: * Added XML support.
049: *
050: * Revision 1.4 2006/09/06 15:26:22 drmlipp
051: * Added double support.
052: *
053: * Revision 1.3 2006/09/04 14:58:34 drmlipp
054: * Proceeding with data editing.
055: *
056: * Revision 1.2 2006/07/18 16:36:51 mlipp
057: * Added String and Integer data input fileds.
058: *
059: * Revision 1.1 2006/07/17 14:57:35 drmlipp
060: * Started data display.
061: *
062: */
063: package de.danet.an.workflow.clients.mgmtportlets.process;
064:
065: import java.io.Serializable;
066: import java.util.Calendar;
067: import java.util.Date;
068: import java.util.Locale;
069: import java.util.Map;
070:
071: import javax.faces.context.FacesContext;
072:
073: import de.danet.an.util.Misc;
074: import de.danet.an.util.jsf.PortletEnvironment;
075: import de.danet.an.util.sax.SAXContentBuffer;
076: import de.danet.an.workflow.api.SAXEventBuffer;
077:
078: /**
079: * A wrapper for data fields.
080: *
081: * @author Michael Lipp
082: */
083: public class DataFieldWrapper implements Serializable {
084:
085: private static final long serialVersionUID = -2281633394073306794L;
086:
087: protected static String L10N_MSGS = "de.danet.an.workflow.clients.mgmtportlets.process.L10n";
088:
089: private Map dataFieldAttrs;
090: private String name;
091: private Object type;
092: private Object originalValue;
093: private Object value;
094:
095: /**
096: * Create a new instance with all atributes initialized
097: * to defaults or the given values.
098: * @param name
099: * @param type TODO
100: * @param value
101: */
102: public DataFieldWrapper(Map dataFieldAttrs, String name,
103: Object type, Object value) {
104: super ();
105: this .dataFieldAttrs = dataFieldAttrs;
106: this .name = name;
107: this .type = type;
108: this .originalValue = value;
109: this .value = value;
110: }
111:
112: private Calendar getCalendar() {
113: FacesContext fc = FacesContext.getCurrentInstance();
114: PortletEnvironment portletEnv = (PortletEnvironment) fc
115: .getApplication().getVariableResolver()
116: .resolveVariable(fc, "processPortletEnv");
117: Locale locale = fc.getApplication().getDefaultLocale();
118: if (locale == null) {
119: locale = Locale.getDefault();
120: }
121: return Calendar.getInstance(portletEnv.getTimeZone(), locale);
122: }
123:
124: /**
125: * @return <code>true</code> if modified
126: */
127: public boolean isModified() {
128: return !Misc.equals(originalValue, value);
129: }
130:
131: /**
132: * @return Returns the value.
133: */
134: public Object getValue() {
135: return value;
136: }
137:
138: /**
139: * @param value The value to set.
140: */
141: public void setValue(Object value) {
142: this .value = value;
143: }
144:
145: /**
146: * @return value as <code>Date</code>
147: */
148: public Date getDateValue() {
149: return (Date) value;
150: }
151:
152: /**
153: * Sets the date part as specified by the parameter
154: * @param newDate
155: */
156: public void setDateValue(Date newDate) {
157: if (newDate == null) {
158: return;
159: }
160: if (value == null) {
161: value = new Date();
162: }
163: if (!(value instanceof Date)) {
164: throw new IllegalStateException("Value is not a date");
165: }
166: Calendar valCal = getCalendar();
167: valCal.setTime((Date) value);
168: Calendar argCal = getCalendar();
169: argCal.setTime(newDate);
170: valCal
171: .set(argCal.get(Calendar.YEAR), argCal
172: .get(Calendar.MONTH), argCal
173: .get(Calendar.DAY_OF_MONTH));
174: value = valCal.getTime();
175: }
176:
177: /**
178: * @return value as <code>Date</code>
179: */
180: public Date getTimeValue() {
181: return (Date) value;
182: }
183:
184: /**
185: * Sets the date part as specified by the parameter
186: * @param newDate
187: */
188: public void setTimeValue(Date newTime) {
189: if (newTime == null) {
190: return;
191: }
192: if (value == null) {
193: value = new Date();
194: }
195: if (!(value instanceof Date)) {
196: throw new IllegalStateException("Value is not a date");
197: }
198: Calendar valCal = getCalendar();
199: valCal.setTime((Date) value);
200: Calendar argCal = getCalendar();
201: argCal.setTime(newTime);
202: valCal.set(Calendar.HOUR_OF_DAY, argCal
203: .get(Calendar.HOUR_OF_DAY));
204: valCal.set(Calendar.MINUTE, argCal.get(Calendar.MINUTE));
205: valCal.set(Calendar.SECOND, argCal.get(Calendar.SECOND));
206: valCal.set(Calendar.MILLISECOND, argCal
207: .get(Calendar.MILLISECOND));
208: value = valCal.getTime();
209: }
210:
211: /**
212: * @return Returns the name.
213: */
214: public String getName() {
215: return name;
216: }
217:
218: /**
219: * Checks if this data field is of type String.
220: * @return the result
221: */
222: public boolean isString() {
223: return type == String.class;
224: }
225:
226: /**
227: * Checks if this data field is of type Long (integer).
228: * @return the result
229: */
230: public boolean isLong() {
231: return type == Long.class;
232: }
233:
234: /**
235: * Checks if this data field is of type Double (float).
236: * @return the result
237: */
238: public boolean isDouble() {
239: return type == Double.class;
240: }
241:
242: /**
243: * Checks if this data field is of type boolean.
244: * @return the result
245: */
246: public boolean isBoolean() {
247: return type == Boolean.class;
248: }
249:
250: /**
251: * Checks if this data field is of type date.
252: * @return the result
253: */
254: public boolean isDate() {
255: return type == Date.class;
256: }
257:
258: /**
259: * Checks if this data field is of type date.
260: * @return the result
261: */
262: public boolean isXml() {
263: return value instanceof SAXEventBuffer;
264: }
265:
266: /**
267: * Checks is this data filed is of an uneditable type.
268: * @return the result
269: */
270: public boolean isUneditable() {
271: return !isString() && !isLong() && !isDouble() && !isBoolean()
272: && !isDate() && !isXml();
273: }
274:
275: /**
276: * @return Returns the expanded.
277: */
278: public boolean isExpanded() {
279: return dataFieldAttrs.containsKey(name);
280: }
281:
282: /**
283: * Toggle the expanded state.
284: * @return
285: */
286: public String toggleExpanded() {
287: if (isExpanded()) {
288: dataFieldAttrs.remove(name);
289: } else {
290: dataFieldAttrs.put(name, null);
291: }
292: return "";
293: }
294:
295: /**
296: * Return the number of lines to display, valid only if
297: * type is string or XML. If expanded, return at least 2.
298: */
299: public int getLines() {
300: if (!(isString() || isXml())) {
301: return 1;
302: }
303: // if this is expanded show two lines (i.e. text area)
304: if (getValue() == null) {
305: return isExpanded() ? 2 : 1;
306: }
307: String s = "";
308: if (isString()) {
309: s = (String) getValue();
310: } else if (isXml()) {
311: s = ((SAXContentBuffer) value).toString(true);
312: }
313: return Math.max(s.split("\\n").length, isExpanded() ? 2 : 1);
314: }
315: }
|