summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java
diff options
context:
space:
mode:
authorBenjamin Culkin <scorpress@gmail.com>2024-05-19 17:56:33 -0400
committerBenjamin Culkin <scorpress@gmail.com>2024-05-19 17:56:33 -0400
commitaedc34d55462a75e329bbf342251ff6504cd117e (patch)
treebcc8f1f2352582717b484df302aeea6696b8f000 /projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java
Initial import from SVN
Diffstat (limited to 'projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java')
-rw-r--r--projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java135
1 files changed, 135 insertions, 0 deletions
diff --git a/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java
new file mode 100644
index 0000000..a73636c
--- /dev/null
+++ b/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java
@@ -0,0 +1,135 @@
+
+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("<SELECT NAME=\"");
+ r.appendContentString(inputName(c));
+ r.appendContentString("\" SIZE=");
+ r.appendContentString(Integer.toString(inputSize()));
+ r.appendContentString(">");
+ java.util.Enumeration numerador = list(c).objectEnumerator();
+ String displayKey = stringForProperty("displayString", c.component());
+ String valueKey = stringForProperty("value", c.component());
+ Object sel = selection(c);
+ if (sel == null)
+ sel = item(c);
+ int pos = 0;
+ while (numerador.hasMoreElements()) {
+ Object item = numerador.nextElement();
+ setItem(item, c);
+ r.appendContentString("<OPTION ");
+ //Append the "SELECTED" attribute if it's the selected item
+ if (sel != null && item.equals(sel))
+ r.appendContentString("SELECTED ");
+ r.appendContentString("VALUE=\"");
+ //Append the value
+ if (valueKey != null && item instanceof NSKeyValueCodingAdditions) {
+ Object val = ((NSKeyValueCodingAdditions)item).valueForKeyPath(valueKey);
+ if (val == null)
+ val = "null";
+ r.appendContentString(val.toString());
+ } else
+ r.appendContentString(Integer.toString(pos));
+ r.appendContentString("\">");
+ //Append display string
+ if (displayKey != null && item instanceof NSKeyValueCodingAdditions) {
+ Object ds = ((NSKeyValueCodingAdditions)item).valueForKeyPath(displayKey);
+ if (ds == null)
+ ds = "";
+ r.appendContentString(ds.toString());
+ } else
+ r.appendContentString(item.toString());
+ r.appendContentString("\n");
+ pos++;
+ }
+ r.appendContentString("</SELECT>");
+ }
+
+ 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;
+ }
+ }
+ }
+ }
+
+}