aboutsummaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/terminal/gwt/client/ValueMap.java
blob: 8d14ef57ceff9117b87bae0822be65ea4753ac67 (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
/*
@VaadinApache2LicenseForJavaFiles@
 */
/**
 * 
 */
package com.vaadin.terminal.gwt.client;

import java.util.HashSet;
import java.util.Set;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsArrayString;

public final class ValueMap extends JavaScriptObject {
    protected ValueMap() {
    }

    public native double getRawNumber(final String name)
    /*-{
        return this[name];
    }-*/;

    public native int getInt(final String name)
    /*-{
        return this[name];
    }-*/;

    public native boolean getBoolean(final String name)
    /*-{
        return Boolean(this[name]);
    }-*/;

    public native String getString(String name)
    /*-{
        return this[name];
    }-*/;

    public native JsArrayString getKeyArray()
    /*-{
        var a = new Array();
        var attr = this;
        for(var j in attr) {
            // workaround for the infamous chrome hosted mode hack (__gwt_ObjectId)
            if(attr.hasOwnProperty(j))
                a.push(j);
        }
        return a;
    }-*/;

    public Set<String> getKeySet() {
        final HashSet<String> attrs = new HashSet<String>();
        JsArrayString attributeNamesArray = getKeyArray();
        for (int i = 0; i < attributeNamesArray.length(); i++) {
            attrs.add(attributeNamesArray.get(i));
        }
        return attrs;
    }

    native JsArrayString getJSStringArray(String name)
    /*-{
        return this[name];
    }-*/;

    native JsArray<ValueMap> getJSValueMapArray(String name)
    /*-{
        return this[name];
    }-*/;

    public String[] getStringArray(final String name) {
        JsArrayString stringArrayAttribute = getJSStringArray(name);
        final String[] s = new String[stringArrayAttribute.length()];
        for (int i = 0; i < stringArrayAttribute.length(); i++) {
            s[i] = stringArrayAttribute.get(i);
        }
        return s;
    }

    public int[] getIntArray(final String name) {
        JsArrayString stringArrayAttribute = getJSStringArray(name);
        final int[] s = new int[stringArrayAttribute.length()];
        for (int i = 0; i < stringArrayAttribute.length(); i++) {
            s[i] = Integer.parseInt(stringArrayAttribute.get(i));
        }
        return s;
    }

    public native boolean containsKey(final String name)
    /*-{
         return name in this;
    }-*/;

    public native ValueMap getValueMap(String name)
    /*-{
        return this[name];
    }-*/;

    native String getAsString(String name)
    /*-{
        return '' + this[name];
    }-*/;

}