/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: DataFieldWrapper.java 2576 2007-11-02 16:00:34Z drmlipp $
*
* $Log$
* Revision 1.12 2007/10/23 15:22:24 drmlipp
* Fixed problem with truncated input fields.
*
* Revision 1.11 2007/09/13 20:59:32 mlipp
* Added expandable display for XML data.
*
* Revision 1.10 2007/09/13 15:06:06 drmlipp
* Fixed line number calculation.
*
* Revision 1.9 2007/09/13 07:47:08 drmlipp
* Made string fields expandable.
*
* Revision 1.8 2006/09/29 12:32:11 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.7 2006/09/13 07:16:14 drmlipp
* Finished date/time input.
*
* Revision 1.6 2006/09/12 21:39:30 mlipp
* Better date/time handling.
*
* Revision 1.5 2006/09/07 15:04:43 drmlipp
* Added XML support.
*
* Revision 1.4 2006/09/06 15:26:22 drmlipp
* Added double support.
*
* Revision 1.3 2006/09/04 14:58:34 drmlipp
* Proceeding with data editing.
*
* Revision 1.2 2006/07/18 16:36:51 mlipp
* Added String and Integer data input fileds.
*
* Revision 1.1 2006/07/17 14:57:35 drmlipp
* Started data display.
*
*/
package de.danet.an.workflow.clients.mgmtportlets.process;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import javax.faces.context.FacesContext;
import de.danet.an.util.Misc;
import de.danet.an.util.jsf.PortletEnvironment;
import de.danet.an.util.sax.SAXContentBuffer;
import de.danet.an.workflow.api.SAXEventBuffer;
/**
* A wrapper for data fields.
*
* @author Michael Lipp
*/
public class DataFieldWrapper implements Serializable {
private static final long serialVersionUID = -2281633394073306794L;
protected static String L10N_MSGS
= "de.danet.an.workflow.clients.mgmtportlets.process.L10n";
private Map dataFieldAttrs;
private String name;
private Object type;
private Object originalValue;
private Object value;
/**
* Create a new instance with all atributes initialized
* to defaults or the given values.
* @param name
* @param type TODO
* @param value
*/
public DataFieldWrapper
(Map dataFieldAttrs, String name, Object type, Object value) {
super();
this.dataFieldAttrs = dataFieldAttrs;
this.name = name;
this.type = type;
this.originalValue = value;
this.value = value;
}
private Calendar getCalendar() {
FacesContext fc = FacesContext.getCurrentInstance();
PortletEnvironment portletEnv = (PortletEnvironment)fc.getApplication()
.getVariableResolver().resolveVariable(fc, "processPortletEnv");
Locale locale = fc.getApplication().getDefaultLocale();
if (locale == null) {
locale = Locale.getDefault();
}
return Calendar.getInstance (portletEnv.getTimeZone(), locale);
}
/**
* @return <code>true</code> if modified
*/
public boolean isModified () {
return !Misc.equals(originalValue, value);
}
/**
* @return Returns the value.
*/
public Object getValue() {
return value;
}
/**
* @param value The value to set.
*/
public void setValue(Object value) {
this.value = value;
}
/**
* @return value as <code>Date</code>
*/
public Date getDateValue () {
return (Date)value;
}
/**
* Sets the date part as specified by the parameter
* @param newDate
*/
public void setDateValue (Date newDate) {
if (newDate == null) {
return;
}
if (value == null) {
value = new Date ();
}
if (!(value instanceof Date)) {
throw new IllegalStateException ("Value is not a date");
}
Calendar valCal = getCalendar();
valCal.setTime((Date)value);
Calendar argCal = getCalendar();
argCal.setTime(newDate);
valCal.set(argCal.get(Calendar.YEAR), argCal.get(Calendar.MONTH),
argCal.get(Calendar.DAY_OF_MONTH));
value = valCal.getTime();
}
/**
* @return value as <code>Date</code>
*/
public Date getTimeValue () {
return (Date)value;
}
/**
* Sets the date part as specified by the parameter
* @param newDate
*/
public void setTimeValue (Date newTime) {
if (newTime == null) {
return;
}
if (value == null) {
value = new Date ();
}
if (!(value instanceof Date)) {
throw new IllegalStateException ("Value is not a date");
}
Calendar valCal = getCalendar();
valCal.setTime((Date)value);
Calendar argCal = getCalendar();
argCal.setTime(newTime);
valCal.set(Calendar.HOUR_OF_DAY, argCal.get(Calendar.HOUR_OF_DAY));
valCal.set(Calendar.MINUTE, argCal.get(Calendar.MINUTE));
valCal.set(Calendar.SECOND, argCal.get(Calendar.SECOND));
valCal.set(Calendar.MILLISECOND, argCal.get(Calendar.MILLISECOND));
value = valCal.getTime();
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* Checks if this data field is of type String.
* @return the result
*/
public boolean isString () {
return type == String.class;
}
/**
* Checks if this data field is of type Long (integer).
* @return the result
*/
public boolean isLong () {
return type == Long.class;
}
/**
* Checks if this data field is of type Double (float).
* @return the result
*/
public boolean isDouble () {
return type == Double.class;
}
/**
* Checks if this data field is of type boolean.
* @return the result
*/
public boolean isBoolean () {
return type == Boolean.class;
}
/**
* Checks if this data field is of type date.
* @return the result
*/
public boolean isDate () {
return type == Date.class;
}
/**
* Checks if this data field is of type date.
* @return the result
*/
public boolean isXml () {
return value instanceof SAXEventBuffer;
}
/**
* Checks is this data filed is of an uneditable type.
* @return the result
*/
public boolean isUneditable () {
return !isString() && !isLong() && !isDouble() && ! isBoolean()
&& !isDate() && !isXml();
}
/**
* @return Returns the expanded.
*/
public boolean isExpanded() {
return dataFieldAttrs.containsKey(name);
}
/**
* Toggle the expanded state.
* @return
*/
public String toggleExpanded () {
if (isExpanded()) {
dataFieldAttrs.remove(name);
} else {
dataFieldAttrs.put(name, null);
}
return "";
}
/**
* Return the number of lines to display, valid only if
* type is string or XML. If expanded, return at least 2.
*/
public int getLines() {
if (!(isString() || isXml())) {
return 1;
}
// if this is expanded show two lines (i.e. text area)
if (getValue() == null) {
return isExpanded() ? 2 : 1;
}
String s = "";
if (isString()) {
s = (String)getValue();
} else if (isXml()) {
s = ((SAXContentBuffer)value).toString(true);
}
return Math.max(s.split("\\n").length, isExpanded() ? 2 : 1);
}
}