/* 
@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;
    }

}

@@ -250,46 +250,8 @@
 on;%0A
-import com.vaadin.terminal.Extension;%0A
 impo
@@ -2100,615 +2100,6 @@
  %7D%0A%0A
-    private static JavaScript getJavascript(Root root) %7B%0A        // TODO Add caching to avoid iterating collection every time%0A        // Caching should use weak references to avoid memory leaks -%3E cache%0A        // should be transient to avoid serialization problems%0A        for (Extension extension : root.getExtensions()) %7B%0A            if (extension instanceof JavaScript) %7B%0A                return (JavaScript) extension;%0A            %7D%0A        %7D%0A%0A        // Extend root if it isn't yet done%0A        JavaScript javascript = new JavaScript();%0A        javascript.extend(root);%0A        return javascript;%0A    %7D%0A%0A
 %7D%0A

Remove method accidentally left behind (#6730)