On Sun, Dec 15, 2013 at 8:30 PM, John Hendrikx <hj...@xs4all.nl> wrote:
> Since you are only allowed to modify properties on the JavaFX thread (in
> most cases), I've been using Platform.runLater() to make sure I observe only
> complete changes.
>
> Basically I register an InvalidationListener on the properties that are
> relevant, and when one gets triggered I set a boolean and trigger a Runnable
> that
> will be run on the JavaFX thread later (and after the change completes).
> The Runnable checks the relevant values and acts on them, then resets the
> boolean.

Hi John,

this approach works fine if the effects of the Runnable run later are
not observable to the client code.
In most cases though, it is reasonable to expect the following
assertion to hold:

obj.setWidth(w);
obj.setHeight(h);
assert obj.getArea() == w*h;

Regards,
Tomas

>
> I use this mechanism for example when I'm observing Objects with many
> properties that need to be stored in a database.  To make sure I only store
> consistent objects, I only observe the values of the properties when my own
> little piece of observer code runs on the JavaFX thread.  Since nothing can
> modify the properties except on the JavaFX thread, this is almost like a
> form of transactions, ensuring that every change has completed before
> observing the results.
>
> --John
>
>
> On 15/12/2013 18:39, Scott Palmer wrote:
>>
>> Interesting idea.
>>
>> There is a case I have been curious about and wonder what the best
>> practices are for it.  Suppose you have a case when you are changing
>> multiple different properties that will be used in a single calculation.
>>   You want to deal with a single change to all of them in one go.  E.g.
>> imagine you have an "area" property that is bound to both "width" and
>> "height".  You want to write code like:
>>
>> obj.setWidth(w);
>> obj.setHeight(h);
>>
>> and have only ONE recalculation of the area property happen.  Currently
>> the
>> way bindings work the area will be calculated twice.  The intermediate
>> calculation is really not a value that you ever want to observe.
>>
>> Are there helpers for this sort of situation?  Are there guidelines in the
>> JavaFX docs somewhere?
>>
>> Regards,
>>
>> Scott
>>
>>
>> On Sat, Dec 14, 2013 at 11:54 PM, Tomas
>> Mikula<tomas.mik...@gmail.com>wrote:
>>
>>> Hello,
>>>
>>> I just published a small extension of javafx bindings and properties
>>> that can help you reduce redundant recalculations.
>>>
>>> They provide two additional methods:
>>>
>>> public void block();
>>> public void release();
>>>
>>> Call p.block() when you suspect your actions will lead to multiple
>>> invalidations of p, and call p.release() when you are done and want to
>>> deliver a single invalidation notification to p's observers.
>>>
>>> https://github.com/TomasMikula/InhiBeans
>>>
>>> Regards,
>>> Tomas
>>>
>

Reply via email to