I would like to use the JavaFx WebEngine in combination with the JavaScript library d3.js. I am already able to access the JavaScript library from within Java. That works using JSObjects as shown in [1]:
JSObject d3Object = (JSObject) webEngine.executeScript("d3");
JSObject chart= (JSObject) d3Object.call("select", ".chart");
As you see there is a general purpose method "call" and I have to pass the name of the JavaScript method that I want to invoke as a String. In order to use the JavaScript library d3.js more comfortably, I would like to have a Java wrapper for it, e.g.
JSObject d3Object = (JSObject) webEngine.executeScript("d3");
D3Wrapper d3 = new D3Wrapper(d3Object);
Selection chart = d3.select(".chart");
What is the recommended way to create such a JavaScript wrapper? I found out that there is already a wrapper for D3 that targets GWT applications. The main wrapper for the d3 object is located here:
https://github.com/gwtd3/gwt-d3/blob/master/gwt-d3-api/src/main/java/com/github/gwtd3/api/D3.java
It seems to be based on JSNI [2] and an example method definition is
public static final native Selection select(String selector)/*-{
return $wnd.d3.select(selector);
}-*/;
The native code in the comment will be executed when calling the method from within Java. How can I take advantage of that existing wrapper code in my JavaFx desktop application? I never used GWT before and I do not want to run an extra web server. Do I really need to dive into GWT and run a full GWT application in the JavaFx WebEngine (if that is possible at all)? Or can I somehow only use the JSNI part of GWT together with the JavaFx WebEngine?
Here is an article on the debug mode for GWT and it seems to be quite complex: http://www.quora.com/How-does-GWT-live-javascript-debugging-in-Eclipse-work-and-how-could-it-be-applied-to-Clojure So I am afraid that there is no easy way to plug the WebEngine into such a work flow? Do I really need to manually rewrite each of the wrapping classes "already provides by gwt-d3"? If the native JavaScript code would be given by Annotations I could reuse the wrapper classes from gwt-d3 and process them by reflection.
If combining JSNI with JavaFx does not make any sense, what would be an elegant way to write an own wrapper for d3.js? Is there a Java library that helps me writing a JavaScript wrapper?
It turned out that JSNI is no (practicable) option. I decided to start with the existing wrapper code from gwt-d3 and transform it with search and replace operations ... and do some manual fine tuning. This way it wont work for GWT any more but for JavaFx. I will create a new GitHub project for the transformed code.
My wrapper classes inherit from an abstract base class JavaScriptObject that talks to the JavaFx WebEngine and handles the JSObject operations. A related discussion with the author of gwt-d3 can be found here: https://github.com/gwtd3/gwt-d3/issues/124
Edit
I just created a new GitHub project:
https://github.com/stefaneidelloth/javafx-d3
Please feel free to join in and help fixing the remaining bugs.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments