summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.web/src/main/java/net/wotonomy/web/WOCheckBox.java
blob: 5d22d36e5a3851b618f1cdc7334194dda2acfac8 (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

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

}