summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/ui/JavaScript.java
blob: dcd8540947ce2f8dec7cc0cf621a0e4cf493fe4a (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
/* 
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.ui;

import java.util.HashMap;
import java.util.Map;

import com.vaadin.external.json.JSONArray;
import com.vaadin.external.json.JSONException;
import com.vaadin.terminal.AbstractExtension;
import com.vaadin.terminal.Extension;
import com.vaadin.terminal.gwt.client.communication.ServerRpc;
import com.vaadin.terminal.gwt.client.extensions.javascriptmanager.ExecuteJavaScriptRpc;
import com.vaadin.terminal.gwt.client.extensions.javascriptmanager.JavascriptManagerState;

public class JavaScript extends AbstractExtension {
    private Map<String, JavascriptCallback> callbacks = new HashMap<String, JavascriptCallback>();

    // Can not be defined in client package as this JSONArray is not available
    // in GWT
    public interface JavascriptCallbackRpc extends ServerRpc {
        public void call(String name, JSONArray arguments);
    }

    public JavaScript() {
        registerRpc(new JavascriptCallbackRpc() {
            public void call(String name, JSONArray arguments) {
                JavascriptCallback callback = callbacks.get(name);
                // TODO handle situation if name is not registered
                try {
                    callback.call(arguments);
                } catch (JSONException e) {
                    throw new IllegalArgumentException(e);
                }
            }
        });
    }

    @Override
    public JavascriptManagerState getState() {
        return (JavascriptManagerState) super.getState();
    }

    public void addCallback(String name, JavascriptCallback callback) {
        callbacks.put(name, callback);
        if (getState().getNames().add(name)) {
            requestRepaint();
        }
    }

    public void removeCallback(String name) {
        callbacks.remove(name);
        if (getState().getNames().remove(name)) {
            requestRepaint();
        }
    }

    public void execute(String script) {
        getRpcProxy(ExecuteJavaScriptRpc.class).executeJavaScript(script);
    }

    public static JavaScript getCurrent() {
        return Root.getCurrentRoot().getJavaScript();
    }

    private static JavaScript getJavascript(Root root) {
        // TODO Add caching to avoid iterating collection every time
        // Caching should use weak references to avoid memory leaks -> cache
        // should be transient to avoid serialization problems
        for (Extension extension : root.getExtensions()) {
            if (extension instanceof JavaScript) {
                return (JavaScript) extension;
            }
        }

        // Extend root if it isn't yet done
        JavaScript javascript = new JavaScript();
        javascript.extend(root);
        return javascript;
    }

}