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(" 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; } }