Incremental updates from Tasks
Richard Bair
richard.bair at oracle.com
Fri Jan 6 15:40:10 PST 2012
I tried the following. I created a TaskBase, and moved almost everything from Task to TaskBase. Task now extends TaskBase, and adds:
updateValue(V value);
abstract V call() throws Exception;
It is entirely compatible with the previous version and all the unit tests still pass. The only difference is that the method we expose (call) returns a value and is declared on Task instead of TaskBase. Also, the updateValue is only on Task.
ObservableListTask also extends TaskBase, and adds:
protected abstract void call() throws Exception;
protected void publish(E... items);
protected void publish(Collection<E> items);
I just used the SwingWorker name "publish", but there is no process. Whatever you publish gets added. We could add a process with a default implementation if we wanted to (allowing some users to filter results or whatnot, but I'm not sure there is a reason you want to do that on the FX thread instead of the background thread, so I would propose leaving it off for now).
This call() method returns no value, just the way it should be. You just call publish to put add items. Since the Task is a one-shot deal, there isn't really a compelling reason to add other methods like clear() (you could also do your own runLater() if you needed to do something tricky).
What do you think? Here is an example of usage (complete with fading rectangles :-)):
ObservableListTask<Rectangle> task = new ObservableListTask<Rectangle>() {
@Override
protected void call() throws Exception {
for (int i=0; i<900; i++) {
Rectangle r = new Rectangle(10, 10);
r.setFill(Color.color(Math.random(), Math.random(), Math.random()));
r.setOpacity(0);
FadeTransition tx = new FadeTransition(Duration.seconds(1));
tx.setToValue(1.0);
tx.setNode(r);
tx.play();
publish(r);
Thread.sleep(20);
updateProgress(i, 900);
}
}
};
Richard
More information about the openjfx-dev
mailing list