diff options
Diffstat (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOInput.java')
| -rw-r--r-- | projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOInput.java | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOInput.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOInput.java new file mode 100644 index 0000000..a8c7daa --- /dev/null +++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOInput.java @@ -0,0 +1,102 @@ + +package net.wotonomy.web; + +import java.text.DecimalFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; + +import net.wotonomy.foundation.NSDictionary; +import net.wotonomy.foundation.NSMutableArray; + +public abstract class WOInput extends WODynamicElement { + + public WOInput() { + super(); + } + + public WOInput(String aName, NSDictionary assocs, WOElement template) { + super(aName, assocs, template); + } + + protected abstract String inputType(); + protected abstract Object value(WOContext c); + + protected NSMutableArray additionalAttributes() { + return new NSMutableArray(new Object[]{ + "disabled", "type", "value", "name" + }); + } + + public String inputName(WOContext c) { + String x = (String)valueForProperty("name", c.component()); + if (x != null) + return x; + return c.elementID(); + } + + protected boolean disabled(WOContext c) { + return booleanForProperty("disabled", c.component()); + } + + protected void appendExtras(WOResponse r, WOContext c) { + } + + public void appendToResponse(WOResponse r, WOContext c) { + r.appendContentString("<INPUT TYPE=\""); + r.appendContentString(inputType()); + r.appendContentString("\" NAME=\""); + r.appendContentString(inputName(c)); + r.appendContentString("\" VALUE=\""); + r.appendContentString(value(c).toString()); + r.appendContentString("\""); + String moreFields = additionalHTMLProperties(c.component(), additionalAttributes()); + if (moreFields != null && moreFields.length() > 0) + r.appendContentString(moreFields); + appendExtras(r, c); + if (disabled(c)) { + r.appendContentString(" DISABLED"); + } + r.appendContentString(">"); + } + + public void takeValuesFromRequest(WORequest r, WOContext c) { + if (disabled(c)) + return; + Object val = r.formValueForKey(inputName(c)); + WOAssociation va = (WOAssociation)associations.objectForKey("value"); + if (val != null && va != null && va.isValueSettable()) + setValueForProperty("value", val, c.component()); + } + + /** Formats a value as a date or number. Checks for + * numberformat or dateformat associations; if one of them + * exists, the value is formatter using the specified pattern. + * @param value The value to format. + * @return The original object, or a date or number if the + * receiver has a numberformat or dateformat association. + */ + protected Object formattedValue(Object value, WOComponent c) { + //Format the value in case of number + String pattern = (String)valueForProperty("numberformat", c); + if (pattern != null) { + DecimalFormat fmt = new DecimalFormat(pattern); + try { + return fmt.parse(value.toString()); + } catch (ParseException e) { + return value; + } + } + //Format the value in case of date + pattern = (String)valueForProperty("dateformat", c); + if (pattern != null) { + SimpleDateFormat fmt = new SimpleDateFormat(pattern); + try { + return fmt.parse(value.toString()); + } catch (ParseException e) { + return value; + } + } + return value; + } + +} |
