Dienstag, 8. Oktober 2013

complex PropertyBindings

Today I had to deal with different dependent input-fields.
Let´s say you have (only) two TextFields, leftTextField and rightTextField. If you writing something into the left TextField you have to calculate something (Logic A) and write it to the right TextField. If you are writing to the right one you have to calculate something (Logic B) and write it to the left TextField.
This example is quite simple, but if you have to deal with a lot of fields with different deps based on a transient context you have to solve:
- cycles between the different components.
- in this case, the transient context that was changing the logic (here Logic A and Logic B).

First the component called PairedTextField.
Now, how to bind both TextFields to get the result? If you are binding bidirectional you will get a Stackoverflow. To solve this you can do something like the following:
This simple solution is working well. Binding if you need it, unbind after this. Thanks to JavaFX PropertyBinding ;-) Finally we have to deal with the different implementations of the logic-part for every request.
The class Bindings will give you the method createStringBinding(Callable, Property), with this you can create the Bindings (leftTextFieldBinding, rigthTextFieldBinding). What you need now is the implementation of the Callable. If you are doing it in the "normal" way you have to write it like the following
With this solution you are not able to switch the implementation of the attribute "demoLogic". Nicer would be something like
With every request you will get the possibility to change the implementation of DemoLogic. The Producer will decide wich implementation will be used.

Now, putting all together:

The importand parts are: