summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOPopUpButton.java
blob: a40c828f06bffeb6fb7f60bdf429ae1bf79f38be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

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

}