2
votes

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.

2
How/where is the applet loaded? and how does it calls back into JS? - Thomas Broyer
The code below modifies the page to insert the applet: String appletDivHTML = "<div> <applet id=\"uploadApplet\" " + "code=\"<my java path>.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); - user2007196
Ignore, I can't delete this... - user2007196

2 Answers

2
votes

your GWT code

String value = JsniMethods.decryptData(data, publickey);

in JsniMethods class

public static native String decryptData(String text, String publickey) /*-{
        var encrypted = $doc.appletname.methodname(text, publickey);
        return encrypted;
    }-*/;

you applet intialization in html

<applet name="appletname" code="appletname.class"
        archive="appletname.jar" width="0" height="0" MAYSCRIPT="true"
        scriptable="true" > </applet>  

And finally

Place your appletname.jar in war folder ..

good luck

1
votes

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);