View on GitHub

JsFunction-GWT

Extensive support for passing GWT Java functions to JavaScript APIs that require JavaScript functions

Download this project as a .zip file Download this project as a tar.gz file

This library used to be part of the Cesium-GWT repository, but it became too useful to other non-Cesium projects (such as my OZONE Widget Framework GWT wrappers), so I made it an independent project.

OWF required extensive enhancements to JsFunction, ultimately forcing me to cover a wide array of cases of function call interactions between GWT and JavaScript.

A key tenet of JsFunction is to provide support for the strong type checking built into Java, which not only makes the software more reliable, but it also makes development faster by ensuring the code that compiles is more likely to work the first time, and by allowing Integrated Development Environments (IDEs) like Eclipse or NetBeans to provide automated code completion and other syntax checking and highlighting. JsFunction makes extensive use of Java Generics to support developer-provided classes as well as standard JavaScript types and primitives.

The library now supports:

Examples

The following callback can be passed into a JavaScript "onclick" handler:

testWindowOnClick(new NoArgsFunction() {
  public void callback() {
    log("Made it", count++);
  }
});

To turn the Java callback into an actual JavaScript function, we call JsFunction.create(). Typically, we implement a wrapper method to hide that detail from an API user:

private void testWindowOnClick(NoArgsFunction noArgsFunction) {
  nativeWindowOnClick(JsFunction.create(noArgsFunction));
}

Then we invoke the JavaScript from GWT:

private native void nativeWindowOnClick(JsFunction callback) /*-{
  $wnd.onclick = callback
}-*/;

For more examples, review the JsFunctionTest class in the jsfunction.jsfunctiontest test case package.

API Reference Documentation is provided at this JavaDoc link.