Search code examples
javascriptgwtappletjsni

GWT JSNI Applet with callback


I've spent quite a long time coding and searching this and other sites with no success. I have a GWT app that calls into JSNI which then calls into an Applet to perform some file loading. So I need to be called back when the file loading has completed. Try as I might, I can't get my JavaScript (or Java) callback to be invoked. Normally you refer to the $wnd variable something like this:

$wnd.myFunc()

If I try this (or tons of other variations I thought might work), it silently fails. I can't even call something simple like:

$wnd.alert("Made it")

I've tried using window.eval("$wnd.myFunc()");

I've also tried window.call with the same results.

I'm guessing that I'm in the wrong context (GWT places everything in an IFrame), but I can't find any option to get access to the global context where GWT is supposed to place "$wnd" variables.

IFrames are supposed to be somewhat constrained for security reasons and I'm wondering if I'm running into something that has been intentionally disabled.

Anyway, a lot of guesswork on my part, but my knowledge of JavaScript is limited, so perhaps I'm overlooking something simple.


Solution

  • I finally solved my own problem. Here’s a quick synopsis of a method that works:

    This is the applet insertion code:

        String  appletDivHTML = "<div> <applet id=\"uploadApplet\" " +
                "code=\"<mypath>.ClientFileUpload\" " +
                "archive=\"applet.jar\" width=\"100\" height=\"100\" MAYSCRIPT> </applet>" +
                "<param name=\"MAYSCRIPT\" value=\"true\"/> </div>";
        com.google.gwt.user.client.Element  div = DOM.createDiv();
        div.setInnerHTML(appletDivHTML);
        BodyElement  body = Document.get().getBody();
        body.appendChild(div);
    

    Here’s the Java call back and JSNI:

    public static void fileCallback(String result) {
        Util.showMessage("Callback reached", "Result: " + result);
    }
    
    public static native void uploadFile(String serverURL, String filePath) /*-{
        $wnd.fileCallback = $entry(@<mypath>.Applet::fileCallback(Ljava/lang/String;));
        var  fileArray = [];
        fileArray.push(filePath);
        $wnd.uploadApplet.uploadFiles(serverURL, fileArray, "fileCallback");
    }-*/;
    

    The applet code to call the Java callback:

            JSObject window = JSObject.getWindow(this);
            String[] args = new String[] {responseString};
            window.call(callbackJsMethod, args);