java.lang.Object
io.github.palexdev.materialfx.bindings.base.AbstractBindingHelper<T>
io.github.palexdev.materialfx.bindings.BindingHelper<T>
- Type Parameters:
T- the properties' value type
Binding helper for unidirectional bindings.
Bindings are syntactical sugar, because basically it's a listener attached to an observable value
which acts as a 'source', when it changes the target is updated.
The issue though is that this 'syntactical sugar' mechanism is managed by JavaFX, some functionalities are
private, unchangeable. For example, JavaFX properties are not settable when they are bound because an internal
flag doesn't allow it. To replicate this behavior the only way is to override the property's 'isBound' method
to use the
BindingManager, an example:
BindingManager bindingManager = BindingManager.instance();
IntegerProperty property = new SimpleIntegerProperty() {
@Override
public boolean isBound() {
return bindingManager.isBound(this) && !bindingManager.isIgnoreBinding(this);
}
};
IntegerProperty source = new SimpleIntegerProperty();
bindingManager.bind(property).to(source).create();
source.set(8);
The 'ignoreBinding' flag is necessary because when the updateTarget is triggered we must tell
the property to allow the modification because it is caused by the binding helper.-
Field Summary
FieldsFields inherited from class io.github.palexdev.materialfx.bindings.base.AbstractBindingHelper
sourceListener, target, targetUpdater -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbind(ObservableValue<? extends T> target) Sets the target to the specified one.voiddispose()Callsunbind()and in addition to that also the target is set to null.voidCauses the target to update with the current source's value.booleanisBound()Asks theBindingManagerto check if this helper's target is bound.booleanChecks if the helper has been disposed before.booleanChecks if the binding should be ignored.to(ObservableValue<? extends T> source) Sets the binding source to the given one.voidunbind()Removes the sourceListener from the source, then sets the source to null.protected voidupdateTarget(ObservableValue<? extends T> source, T oldValue, T newValue) Invoked by the sourceListener, it's responsible for updating the specified target using the specified targetUpdaterBiConsumer.with(BiConsumer<T, T> targetUpdater) Sets the targetUpdaterBiConsumer.Methods inherited from class io.github.palexdev.materialfx.bindings.base.AbstractBindingHelper
afterBind, afterUnbind, afterUpdateTarget, beforeBind, beforeUnbind, beforeUpdateTarget
-
Field Details
-
ignoreBinding
protected boolean ignoreBinding
-
-
Constructor Details
-
BindingHelper
public BindingHelper()
-
-
Method Details
-
bind
Sets the target to the specified one.- Specified by:
bindin classAbstractBindingHelper<T>
-
with
Sets the targetUpdaterBiConsumer.- Specified by:
within classAbstractBindingHelper<T>
-
to
Sets the binding source to the given one.Also calls
AbstractBindingHelper.beforeBind()andAbstractBindingHelper.afterBind(). -
updateTarget
Invoked by the sourceListener, it's responsible for updating the specified target using the specified targetUpdaterBiConsumer.Also calls
Sets the 'ignoreBinding' flag to true then calls the super method.AbstractBindingHelper.beforeUpdateTarget()andAbstractBindingHelper.afterUpdateTarget().The whole process is wrapped in a try-finally block since it's as important that the flag is reset at the end.
- Overrides:
updateTargetin classAbstractBindingHelper<T>- Parameters:
source- the source propertyoldValue- the source's oldValuenewValue- the source's newValue
-
invalidate
public void invalidate()Causes the target to update with the current source's value. This is necessary to 'simulate' the JavaFX's eager evaluation of bindings.- Specified by:
invalidatein classAbstractBindingHelper<T>
-
unbind
public void unbind()Removes the sourceListener from the source, then sets the source to null.This means that the helper won't be usable anymore until
Also callsto(ObservableValue)is called again.AbstractBindingHelper.beforeUnbind(),AbstractBindingHelper.afterUnbind(). -
dispose
public void dispose()Callsunbind()and in addition to that also the target is set to null.This means that the helper won't be usable anymore until
bind(ObservableValue)andto(ObservableValue)are called again.- Specified by:
disposein classAbstractBindingHelper<T>
-
isBound
public boolean isBound()Asks theBindingManagerto check if this helper's target is bound. -
isIgnoreBinding
public boolean isIgnoreBinding()Checks if the binding should be ignored. -
isDispose
public boolean isDispose()Checks if the helper has been disposed before.- Specified by:
isDisposein classAbstractBindingHelper<T>
-