JavaFX: Using Patterns & Clean Code
The Controllers
We're not going to see a classic Controller here; actually, we're slightly departing from the "pure" MVC. The code I'm showing you is more a "Presentation Model", a pattern described by Martin Fowler as:
The essence of a Presentation Model is of a fully self-contained class that represents all the data and behavior of the UI window, but without any of the controls used to render that UI on the screen. A view then simply projects the state of the presentation model onto the glass.
This class is basically an hybrid between a classic model and a controller. It is also a façade between the view and the domain model.
package it.tidalwave.javafxstuff.contactlist.model;
import it.tidalwave.javafxstuff.contactlist.model.ContactRegistry;
import it.tidalwave.javafxstuff.contactlist.model.ContactRegistryMock;
public class PresentationModel
{
public var searchText : String;
public var selectedIndex : Integer;
// The Business Delegate
def contactRegistry = ContactRegistryMock{} as ContactRegistry;
def allContacts = bind contactRegistry.items();
// The contacts filtered according the contents of the search field
public-read def contacts = bind allContacts[contact | "{contact.firstName} {contact.lastName}".startsWith(searchText)];
// The selected contact; the code also triggers a notification at each change
public-read def selectedContact = bind contacts[selectedIndex] on replace previousContact
{
onSelectedContactChange();
};
// Notifies a change in the current Contact selection
public-init var onSelectedContactChange = function()
{
}
}
Note the extreme compactness brought by functional programming. There's almost no imperative programming as everything is achieved by properly using the binding feature. The only imperative part is the 'onSelectedContactChange' function, which is just a listener to notify selection changes to some external code - it will be used only for triggering the animation.
BTW, I'd like to remove it from here, but I wasn't able to. Maybe it's a JavaFX thing that I've not understood yet, but I'm keeping it for another post.
Now, everything about the animation goes encapsulated in a specific class, which only exposes two properties controlling the animation: the effect and the rotation angle. A single play() function is provided to start the animation.
package it.tidalwave.javafxstuff.contactlist.view;
import javafx.animation.Interpolator;
import javafx.animation.Timeline;
import javafx.scene.effect.GaussianBlur;
public class AnimationController
{
public-read var rotation = 0;
public-read def effect = GaussianBlur{}
def timeline = Timeline
{
repeatCount: 1
keyFrames:
[
at(0s) { effect.radius => 20; rotation => 45 }
at(300ms) { effect.radius => 0 tween Interpolator.EASEBOTH;
rotation => 0 tween Interpolator.EASEBOTH }
]
}
public function play()
{
timeline.playFromStart();
}
}
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)






Comments
Juha Vainikka replied on Tue, 2009/05/12 - 3:30am
Great article Fabrizio!
I also think it's very important (especially for newbies) to see technical demos that aren't essentially "hacks".
Looking forward to read your next piece.
/juha
Camilo Arango replied on Tue, 2009/05/12 - 10:23am
Fabrizio Giudici replied on Tue, 2009/05/12 - 1:52pm
It's one of the things I'll try in future. For now, the only thing I've done is to run javap on a compiled JavaFX value object, and - as expected - I saw it's very different from a regular JavaBean. Basically every attribute its an independent object, I presume with its own get()/set() and binding support. But there are also lots of other stuff that I don't understand at a glance.
Fabrizio Giudici replied on Wed, 2009/05/13 - 10:18am
John Smith replied on Wed, 2009/05/13 - 12:29pm
Fabrizio Giudici replied on Wed, 2009/05/13 - 1:51pm
The fact that you do things in JavaFX "better" than Java is subjective, at this time, and related to a number of things. For me, I don't see compelling reasons for turning my Java desktop stuff into JavaFX _now_. If I had some compelling graphics requirements, at the point to throw a graphic designer into the team, JavaFX would make the difference. I expect the two different perspectives to get closer and closer in the next years.
Objectively, JavaFX is more coincise and binding is the key trick. Try to write the same example I'm talking of in Java. Of course, the same functional binding could be introduced in Java (thinking of it...) thus even Java programs could be much shorter. But with Java binding can't be as natural as is with JavaFX.
Raghu Semburakk... replied on Thu, 2009/05/14 - 4:01pm
Fabrizio Giudici replied on Fri, 2009/05/15 - 2:14am
Krzysztof Kula replied on Mon, 2009/06/01 - 12:54pm
BTW, there is JavaFX 1.2 with Linux support.
Regards,
Krzysztof Kula
Fabrizio Giudici replied on Tue, 2009/06/02 - 6:16am
Krzysztof, no, I didn't see problems with that handful of names. BTW, I have another application where a similar trick is used and it works with about 1.000 items, with only some slightly noticeable delay in some cases. Of course, with such a big number of items you probably have to do some smarter filtering rather than using subsequences.
Fabrizio Giudici replied on Thu, 2009/06/04 - 10:53am
Krzysztof Kula replied on Fri, 2009/06/05 - 4:56am
Then I must start serching for a reason in my computer platform. Regards, Krzysztof Kula
Fabrizio Giudici replied on Sun, 2009/06/07 - 11:04am
I've posted here a screencast of the application running with about 1.000 elements. The screencast is neither cut nor accelerated, you can see the real speed on a MacBook Pro.
For what concerns my original demo, the only problem I see on JavaFX 1.2 is some parts of the layout screwed up.
Pablo Oliveira replied on Sat, 2011/06/04 - 5:24pm
Matt Coleman replied on Tue, 2012/07/31 - 1:45am
in response to:
Camilo Arango
web designer buffalo