Use ScenePulseListener to avoid expensive recalculations?
hjohn at xs4all.nl
Tue Nov 5 06:58:32 PST 2013
I'm considering using a ScenePulseListener to avoid expensive
recalculations when multiple bindings trigger an action.
My problem is this:
I like to build Views (Controls) that are dumb and expose properties
that can be manipulated by whatever is using the View. The View listens
to its own Properties and adjusts its state accordingly. Some of these
adjustments are related (like width + height) and can be expensive when
calculated immediately. So I would like to mark the View as "invalid"
and recalculate its state (if invalid) on the next Pulse.
My current use case I'm looking at is a View that wraps (amongst others)
a TreeView. The View exposes an ObservableList and a BooleanProperty
that decides whether the first level of the Tree should be displayed as
Tabs or as Nodes (which has an impact on what Nodes actually are added
to the TreeView, and which are added as Tabs). User code will thus
often set a new list of nodes + change the boolean to show tabs or
nodes. The View currently naively has InvalidationListeners on both of
these properties which cause TreeNodes to be created after the first
change... then discarded and recreated after the second change by the
user code, ie:
view.nodesProperty().setAll(nodes); // Recreates all Tabs/TreeNodes
with the current value of expand top level, as we donot know another
change might be incoming...
view.expandTopLevelProperty().set(false); // Recreates all
Tabs/TreeNodes again if expand top level was changed...
This specific problem might be done in a better way, but the point
remains, how do I avoid expensive calculations when multiple properties
get changed one after the other by the user code? I'm assuming that
JavaFX controls already avoid these kinds of things, and I'd like to
know whether using a ScenePulseListener is the way to go, or that it
can/should be done in a different way -- examining the code for TreeView
(and its superclasses), I couldn't find uses of ScenePulseListener...
More information about the openjfx-dev