Samstag, 11. Oktober 2014

JavaFX - finally there

For our inhouse application I once wrote a framework to make it easier to deal with all the things you have to keep in mind when developing a Swing application.
I ended with a custom binding mechanism which took care of all the threading stuff and it was quite easy to develop an application that way.
Then, JavaFX grew and it was very clear that this will become the new Java UI standard with tons of new possibilities.
But, like Swing you have to take care of things like an UI thread which should not block but must be used to update the UI.
Thanks to my framework I were able to completely convert my Swing app to JavaFX - yes, 100% JavaFX now - in light speed.

As with Swing, there is no standard JavaFX desktop application framework.
But something like this might come:

So, I wonder if my framework can be a part of this effort.
I think, the way how I bind the UI element to the model is very interesting.

A controller looks like a simple Java class using normal properties.
Lets call this a "model controller":

What JavaFX calls controller than, is for me just a description of how to bind the view to the controller.
Lets call this a "view controller":

In this "view controller" then there is a method which describes how the data of the model is bound to the UI control:

Notice how I bind a nested model property (the postalAddress) to the UI control.
AND - all this without string constants.

Using Java 8 method references to bind actions:

The data transfer between the model and the UI control happens in an request/response fashion. Means, it is not updated automatically and immediately, but, before one issues an action, the data is copied form the view to the model and afterwards it is copied back from the model to the view.

Yep, one can easily notice that I came from JSF. Theoretically my "model controller" can be used unmodified to serve the data even for JSF applications!

Well, one might ask - what is this "ServiceCenterController.p." stuff? This is how I think properties have to work in Java. I found the idea to this somewhere in the Internet yeas ago. Unhappily I can't find it just yet to link to the original blog post.

It comes down to a static class structure which describes the properties. I wrote an IntelliJ Plugin to automate this process.

The only drawback here is, that I still can not deal with recursive models. The initialize of the FirstClass structure will end as some depth.

The last bit to wire things up is the @PropertyChangeSupport annotation which is used by an Java agent to, yep, implement a special java.beans.PropertyChangeSupport which also provides javafx.beans.ObservableValues.
The use of the Java agent might be problematic for embedded devices not using a JVM (RoboVM).

... so, I wonder, if some of you find this useful and if I should try to Open-Source my framework.

What do you think?