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

package net.wotonomy.web;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import net.wotonomy.foundation.NSDictionary;
import net.wotonomy.foundation.NSMutableArray;

public abstract class WOInput extends WODynamicElement {

    public WOInput() {
        super();
    }

    public WOInput(String aName, NSDictionary assocs, WOElement template) {
        super(aName, assocs, template);
    }

    protected abstract String inputType();
    protected abstract Object value(WOContext c);

    protected NSMutableArray additionalAttributes() {
        return new NSMutableArray(new Object[]{
            "disabled", "type", "value", "name"
        });
    }

    public String inputName(WOContext c) {
        String x = (String)valueForProperty("name", c.component());
        if (x != null)
            return x;
        return c.elementID();
    }

    protected boolean disabled(WOContext c) {
        return booleanForProperty("disabled", c.component());
    }

    protected void appendExtras(WOResponse r, WOContext c) {
    }

    public void appendToResponse(WOResponse r, WOContext c) {
        r.appendContentString("<INPUT TYPE=\"");
        r.appendContentString(inputType());
        r.appendContentString("\" NAME=\"");
        r.appendContentString(inputName(c));
        r.appendContentString("\" VALUE=\"");
        r.appendContentString(value(c).toString());
        r.appendContentString("\"");
        String moreFields = additionalHTMLProperties(c.component(), additionalAttributes());
        if (moreFields != null && moreFields.length() > 0)
            r.appendContentString(moreFields);
        appendExtras(r, c);
        if (disabled(c)) {
            r.appendContentString(" DISABLED");
        }
        r.appendContentString(">");
    }

    public void takeValuesFromRequest(WORequest r, WOContext c) {
        if (disabled(c))
            return;
        Object val = r.formValueForKey(inputName(c));
        WOAssociation va = (WOAssociation)associations.objectForKey("value");
        if (val != null && va != null && va.isValueSettable())
            setValueForProperty("value", val, c.component());
    }

	/** Formats a value as a date or number. Checks for
	 * numberformat or dateformat associations; if one of them
	 * exists, the value is formatter using the specified pattern.
	 * @param value The value to format.
	 * @return The original object, or a date or number if the
	 * receiver has a numberformat or dateformat association.
	 */
	protected Object formattedValue(Object value, WOComponent c) {
		//Format the value in case of number
		String pattern = (String)valueForProperty("numberformat", c);
		if (pattern != null) {
			DecimalFormat fmt = new DecimalFormat(pattern);
			try {
				return fmt.parse(value.toString());
			} catch (ParseException e) {
				return value;
			}
		}
		//Format the value in case of date
		pattern = (String)valueForProperty("dateformat", c);
		if (pattern != null) {
			SimpleDateFormat fmt = new SimpleDateFormat(pattern);
			try {
				return fmt.parse(value.toString());
			} catch (ParseException e) {
				return value;
			}
		}
		return value;
	}

}