package net.wotonomy.web; import net.wotonomy.foundation.NSArray; import net.wotonomy.foundation.NSDictionary; import net.wotonomy.foundation.NSKeyValueCodingAdditions; public class WOPopUpButton extends WOInput { public WOPopUpButton() { super(); } public WOPopUpButton(String aName, NSDictionary assocs, WOElement template) { super(aName, assocs, template); } protected String inputType() { return "SELECT"; } protected int inputSize() { return 1; } protected NSArray list(WOContext c) { NSArray l = (NSArray) valueForProperty("list", c.component()); if (l == null) l = NSArray.EmptyArray; return l; } protected void setItem(Object v, WOContext c) { if (associations.objectForKey("item") == null) return; setValueForProperty("item", v, c.component()); } protected Object item(WOContext c) { return valueForProperty("item", c.component()); } protected void setSelection(Object v, WOContext c) { if (associations.objectForKey("selection") == null) return; setValueForProperty("selection", v, c.component()); } protected Object selection(WOContext c) { return valueForProperty("selection", c.component()); } public Object value(WOContext c) { return null; } public void appendToResponse(WOResponse r, WOContext c) { r.appendContentString(""); } protected void select(Object v, WOContext c) { if (associations.objectForKey("selection") != null) { setSelection(v, c); return; } if (associations.objectForKey("item") != null) { setItem(v, c); } } public void takeValuesFromRequest(WORequest r, WOContext c) { Object val = r.formValueForKey(inputName(c)); if (val == null) return; NSArray list = list(c); String valueKey = stringForProperty("value", c.component()); // If no value binding, just get the index if (valueKey == null) { int pos = Integer.parseInt(val.toString()); val = list.objectAtIndex(pos); select(val, c); return; } // If value binding is present, lookup the value java.util.Enumeration numerador = list.objectEnumerator(); while (numerador.hasMoreElements()) { Object o = numerador.nextElement(); if (o instanceof NSKeyValueCodingAdditions) { Object x = ((NSKeyValueCodingAdditions) o).valueForKeyPath(valueKey); if (x != null && x.equals(val)) { select(o, c); return; } } } } }