| 1 |
|
|
| 2 |
|
package net.wotonomy.web; |
| 3 |
|
|
| 4 |
|
import net.wotonomy.foundation.NSArray; |
| 5 |
|
import net.wotonomy.foundation.NSDictionary; |
| 6 |
|
import net.wotonomy.foundation.NSKeyValueCodingAdditions; |
| 7 |
|
|
| 8 |
|
public class WOPopUpButton extends WOInput { |
| 9 |
|
|
| 10 |
|
public WOPopUpButton() { |
| 11 |
0 |
super(); |
| 12 |
0 |
} |
| 13 |
|
|
| 14 |
|
public WOPopUpButton(String aName, NSDictionary assocs, WOElement template) { |
| 15 |
0 |
super(aName, assocs, template); |
| 16 |
0 |
} |
| 17 |
|
|
| 18 |
|
protected String inputType() { |
| 19 |
0 |
return "SELECT"; |
| 20 |
|
} |
| 21 |
|
|
| 22 |
|
protected int inputSize() { |
| 23 |
0 |
return 1; |
| 24 |
|
} |
| 25 |
|
|
| 26 |
|
protected NSArray list(WOContext c) { |
| 27 |
0 |
NSArray l = (NSArray)valueForProperty("list", c.component()); |
| 28 |
0 |
if (l == null) |
| 29 |
0 |
l = NSArray.EmptyArray; |
| 30 |
0 |
return l; |
| 31 |
|
} |
| 32 |
|
|
| 33 |
|
protected void setItem(Object v, WOContext c) { |
| 34 |
0 |
if (associations.objectForKey("item") == null) |
| 35 |
0 |
return; |
| 36 |
0 |
setValueForProperty("item", v, c.component()); |
| 37 |
0 |
} |
| 38 |
|
protected Object item(WOContext c) { |
| 39 |
0 |
return valueForProperty("item", c.component()); |
| 40 |
|
} |
| 41 |
|
|
| 42 |
|
protected void setSelection(Object v, WOContext c) { |
| 43 |
0 |
if (associations.objectForKey("selection") == null) |
| 44 |
0 |
return; |
| 45 |
0 |
setValueForProperty("selection", v, c.component()); |
| 46 |
0 |
} |
| 47 |
|
protected Object selection(WOContext c) { |
| 48 |
0 |
return valueForProperty("selection", c.component()); |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
public Object value(WOContext c) { |
| 52 |
0 |
return null; |
| 53 |
|
} |
| 54 |
|
|
| 55 |
|
public void appendToResponse(WOResponse r, WOContext c) { |
| 56 |
0 |
r.appendContentString("<SELECT NAME=\""); |
| 57 |
0 |
r.appendContentString(inputName(c)); |
| 58 |
0 |
r.appendContentString("\" SIZE="); |
| 59 |
0 |
r.appendContentString(Integer.toString(inputSize())); |
| 60 |
0 |
r.appendContentString(">"); |
| 61 |
0 |
java.util.Enumeration numerador = list(c).objectEnumerator(); |
| 62 |
0 |
String displayKey = stringForProperty("displayString", c.component()); |
| 63 |
0 |
String valueKey = stringForProperty("value", c.component()); |
| 64 |
0 |
Object sel = selection(c); |
| 65 |
0 |
if (sel == null) |
| 66 |
0 |
sel = item(c); |
| 67 |
0 |
int pos = 0; |
| 68 |
0 |
while (numerador.hasMoreElements()) { |
| 69 |
0 |
Object item = numerador.nextElement(); |
| 70 |
0 |
setItem(item, c); |
| 71 |
0 |
r.appendContentString("<OPTION "); |
| 72 |
|
|
| 73 |
0 |
if (sel != null && item.equals(sel)) |
| 74 |
0 |
r.appendContentString("SELECTED "); |
| 75 |
0 |
r.appendContentString("VALUE=\""); |
| 76 |
|
|
| 77 |
0 |
if (valueKey != null && item instanceof NSKeyValueCodingAdditions) { |
| 78 |
0 |
Object val = ((NSKeyValueCodingAdditions)item).valueForKeyPath(valueKey); |
| 79 |
0 |
if (val == null) |
| 80 |
0 |
val = "null"; |
| 81 |
0 |
r.appendContentString(val.toString()); |
| 82 |
0 |
} else |
| 83 |
0 |
r.appendContentString(Integer.toString(pos)); |
| 84 |
0 |
r.appendContentString("\">"); |
| 85 |
|
|
| 86 |
0 |
if (displayKey != null && item instanceof NSKeyValueCodingAdditions) { |
| 87 |
0 |
Object ds = ((NSKeyValueCodingAdditions)item).valueForKeyPath(displayKey); |
| 88 |
0 |
if (ds == null) |
| 89 |
0 |
ds = ""; |
| 90 |
0 |
r.appendContentString(ds.toString()); |
| 91 |
0 |
} else |
| 92 |
0 |
r.appendContentString(item.toString()); |
| 93 |
0 |
r.appendContentString("\n"); |
| 94 |
0 |
pos++; |
| 95 |
0 |
} |
| 96 |
0 |
r.appendContentString("</SELECT>"); |
| 97 |
0 |
} |
| 98 |
|
|
| 99 |
|
protected void select(Object v, WOContext c) { |
| 100 |
0 |
if (associations.objectForKey("selection") != null) { |
| 101 |
0 |
setSelection(v, c); |
| 102 |
0 |
return; |
| 103 |
|
} |
| 104 |
0 |
if (associations.objectForKey("item") != null) { |
| 105 |
0 |
setItem(v,c); |
| 106 |
|
} |
| 107 |
0 |
} |
| 108 |
|
public void takeValuesFromRequest(WORequest r, WOContext c) { |
| 109 |
0 |
Object val = r.formValueForKey(inputName(c)); |
| 110 |
0 |
if (val == null) |
| 111 |
0 |
return; |
| 112 |
0 |
NSArray list = list(c); |
| 113 |
0 |
String valueKey = stringForProperty("value", c.component()); |
| 114 |
|
|
| 115 |
0 |
if (valueKey == null) { |
| 116 |
0 |
int pos = Integer.parseInt(val.toString()); |
| 117 |
0 |
val = list.objectAtIndex(pos); |
| 118 |
0 |
select(val, c); |
| 119 |
0 |
return; |
| 120 |
|
} |
| 121 |
|
|
| 122 |
0 |
java.util.Enumeration numerador = list.objectEnumerator(); |
| 123 |
0 |
while (numerador.hasMoreElements()) { |
| 124 |
0 |
Object o = numerador.nextElement(); |
| 125 |
0 |
if (o instanceof NSKeyValueCodingAdditions) { |
| 126 |
0 |
Object x = ((NSKeyValueCodingAdditions)o).valueForKeyPath(valueKey); |
| 127 |
0 |
if (x != null && x.equals(val)) { |
| 128 |
0 |
select(o, c); |
| 129 |
0 |
return; |
| 130 |
|
} |
| 131 |
|
} |
| 132 |
0 |
} |
| 133 |
0 |
} |
| 134 |
|
|
| 135 |
|
} |