How to create a JavaScript wrapper to be used with JavaFx WebView? Is JSNI an option?

Stefan

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?

Stefan

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a wrapper object for a webview

From Dev

How to create UUID in JSNI of the input elements?

From Dev

Javascript in JavaFX WebView

From Dev

How to call a JavaScript function from a JavaFX WebView on Button click?

From Dev

odoo 10 how to create Snippets Javascript option

From Dev

How to create a JavaScript wrapper that can access a function and it's objects?

From Dev

How compatible is JavaFX Webview with Android's Webview?

From Dev

How to write a wrapper in javascript?

From Dev

How to create a "wrapper" in C++?

From Dev

How to create a "wrapper" in C++?

From Dev

JavaFX WebView load local Javascript file

From Dev

Disable javascript from running in javafx webview

From Dev

JavaFX WebView doesn't execute JavaScript

From Dev

Create C# Wrapper for Javascript Library

From Dev

How to make a wrapper function in javascript?

From Dev

how create if for option in select?

From Dev

How to track JSNI Errors in GWT?

From Dev

How to use GWTs JSNI with Xtend

From Dev

How to use GWTs JSNI with Xtend

From Dev

espresso > how to select an option value in webview?

From Dev

How do I include jquery in javafx webview?

From Dev

How disable keyboard in a webView browser (JavaFx)?

From Dev

How to display HTML document in JavaFX WebView

From Dev

JavaFX : How to process a webview in a non application thread

From Dev

What javascript engine used inside javafx?

From Dev

GWT : create DOM element using JSNI

From Dev

Create GWT jar from a JSNI class

From Dev

how to create wrapper around std::bind

From Dev

How to create Scala swing wrapper classes with SuperMixin?