package net.wotonomy.web; import net.wotonomy.foundation.NSArray; import net.wotonomy.foundation.NSDictionary; import net.wotonomy.foundation.NSMutableArray; public class WOCheckBox extends WOInput { protected boolean checked = false; public WOCheckBox() { super(); } public WOCheckBox(String aName, NSDictionary assocs, WOElement template) { super(aName, assocs, template); } protected String inputType() { return "CHECKBOX"; } protected Object value(WOContext c) { Object val = null; boolean checked = false; if (associations.objectForKey("value") != null) { val = valueForProperty("value", c.component()); Object sel = valueForProperty("selection", c.component()); if (sel != null && val != null && sel.equals(val)) checked = true; } if (val == null) { val = c.elementID(); } return val; } protected void appendExtras(WOResponse r, WOContext c) { checked |= booleanForProperty("checked", c.component()); if (checked) r.appendContentString(" CHECKED"); } protected NSMutableArray additionalAttributes() { NSMutableArray a = super.additionalAttributes(); a.addObject("checked"); a.addObject("selection"); return a; } public void appendToResponse(WOResponse r, WOContext c) { checked = false; super.appendToResponse(r, c); } public void takeValuesFromRequest(WORequest r, WOContext c) { if (disabled(c)) return; NSArray values = r.formValuesForKey(inputName(c)); Object val = valueForProperty("value", c.component()); if (val == null) val = c.elementID(); java.util.Enumeration enumerator = values.objectEnumerator(); checked = false; while (enumerator.hasMoreElements()) { Object nextval = enumerator.nextElement(); if (nextval.equals(val)) checked = true; } if (associations.objectForKey("value") != null && associations.objectForKey("selection") != null) { if (checked) setValueForProperty("selection", val, c.component()); else if (valueForProperty("selection", c.component()) != null) setValueForProperty("selection", null, c.component()); } if (associations.objectForKey("checked") != null) setValueForProperty("checked", checked ? Boolean.TRUE : Boolean.FALSE, c.component()); } }