Class CustomBinding

java.lang.Object
org.jhotdraw8.fxbase.binding.CustomBinding

public class CustomBinding extends Object
Provides bindings with conversion functions.

Slightly adapted code from example shown at stackoverflow.com.

Reference:

Stackoverflow. Custom Bidirectional Bindings in JavaFX, Copyright BlackLabrador, CC BY-SA 4.0 license
stackoverflow.com
Author:
BlackLabrador (as stated in the reference)
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T, M> void
    bind(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediatorB, Function<M,javafx.beans.value.ObservableValue<T>> propertyB)
    Binds property 'a' to property 'b'.
    static <T, M> void
    bind(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediatorB, Function<M,javafx.beans.value.ObservableValue<T>> propertyB, T unboundValue)
    Binds property 'a' to property 'b'.
    static <T, M> void
    bind(javafx.beans.value.ObservableValue<M> mediatorA, Function<M,javafx.beans.property.Property<T>> propertyA, javafx.beans.value.ObservableValue<T> propertyB, T unboundValue)
    Binds property 'a' to property 'b'.
    static <T, M> void
    bindBidirectional(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediator, Function<M,javafx.beans.property.Property<T>> propertyB)
    Binds property 'a' to property 'b'.
    static <T, S> void
    bindBidirectional(javafx.beans.property.StringProperty propertyA, javafx.beans.property.Property<S> mediator, Function<S,javafx.beans.property.Property<T>> propertyB, javafx.util.StringConverter<T> stringConverter)
    Binds property 'a' to property 'b'.
    static <A, B, PROPERTY_A extends javafx.beans.value.WritableValue<A> & javafx.beans.value.ObservableValue<A>, PROPERTY_B extends javafx.beans.value.WritableValue<B> & javafx.beans.value.ObservableValue<B>>
    void
    bindBidirectionalAndConvert(PROPERTY_A propertyA, PROPERTY_B propertyB, Function<A,B> convertAtoB, Function<B,A> convertBtoA)
    Creates a bidirectional binding for properties A and B using the provided conversion functions.
    static <T, M> void
    bindBidirectionalStrongly(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediator, Function<M,javafx.beans.property.Property<T>> propertyB)
    Binds property 'a' to property 'b'.
    static <T, M> void
    bindBidirectionalStrongly2(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediator, Function<M,javafx.beans.property.Property<T>> propertyB)
     
    static <D, S> void
    bindContent(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableList<S> src, Function<S,D> toDest)
    Binds list dest to list source.
    static <D, S> void
    bindContent(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableList<S> src, Function<S,D> toDest, @Nullable Consumer<D> destOnRemove)
    Binds list dest to list source.
    static <D, S> void
    bindContentBidirectional(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableList<S> src, Function<S,D> toDest, @Nullable Consumer<D> destOnRemove, Function<D,S> toSource, @Nullable Consumer<S> sourceOnRemove)
    Binds list dest to list source bidirectionally.
    static <E, T> void
    bindElements(javafx.collections.ObservableList<E> list, BiConsumer<E,T> setter, T value)
    Sets the specified value to all elements of the list.
    static <E, T> void
    bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.binding.Binding<T> binding)
    Binds the specified property of all list elements to the given binding.
    static <E, T> void
    bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.binding.Binding<T> binding, T unboundValue)
    Binds the specified property of all list elements to the given binding.
    static <E, T> void
    bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.property.Property<T> property)
    Binds the specified property of all list elements to the given property.
    static <E, T> void
    bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.property.Property<T> property, T unboundValue)
    Binds the specified property of all list elements to the given property.
    static <D, S> void
    bindListContentToSet(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableSet<S> src, Function<S,D> toDest)
    Binds list dest to set source.
    static <D, S> void
    bindListContentToSet(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableSet<S> src, Function<S,D> toDest, Consumer<D> disposeDest)
     
    static <E> void
    bindMembershipToBoolean(javafx.collections.ObservableSet<E> set, E element, javafx.beans.value.ObservableValue<Boolean> value)
    Adds or removes an element from a set depending on the bound boolean value.
    static <T> javafx.beans.binding.ObjectBinding<T>
    compute(Supplier<T> op, javafx.beans.value.ObservableValue<?>... dependencies)
    Creates a binding with a computed value.
    static javafx.beans.binding.DoubleBinding
    computeDouble(DoubleSupplier op, javafx.beans.value.ObservableValue<?>... dependendies)
    Creates a binding with a computed value.
    static <A, B> javafx.beans.binding.ObjectBinding<B>
    convert(javafx.beans.value.ObservableValue<A> a, Function<A,B> convert)
    Creates a binding with a converted value.
    static javafx.beans.binding.StringExpression
    formatted(String format, Object... args)
    Returns a string expression which uses java.test.MessageFormat to format the text.
    static <D, S> void
    unbindListContentToSet(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableSet<S> src)
    Unbinds list dest from set source.
    static <A, B> javafx.beans.value.ObservableValue<B>
    via(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> step)
    Creates a binding to get a property a.b.
    static <A, B, C> javafx.beans.value.ObservableValue<C>
    via(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.value.ObservableValue<C>> stepC)
    Creates a binding to get an observable value a.b.c.
    static <A, B, C, D>
    javafx.beans.value.ObservableValue<D>
    via(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.value.ObservableValue<C>> stepC, Function<C,javafx.beans.value.ObservableValue<D>> stepD)
    Creates a binding to get an observable value a.b.c.d.
    static <A, B> javafx.beans.property.Property<B>
    viaBidirectional(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.property.Property<B>> step)
    Creates a binding to get a property a.b.
    static <A, B, C> javafx.beans.property.Property<C>
    viaBidirectional(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.property.Property<C>> stepC)
    Creates a binding to get a property a.b.c.
    static <A, B, C, D>
    javafx.beans.property.Property<D>
    viaBidirectional(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.value.ObservableValue<C>> stepC, Function<C,javafx.beans.property.Property<D>> stepD)
    Creates a binding to get a property a.b.c.d.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • bindBidirectionalStrongly

      public static <T, M> void bindBidirectionalStrongly(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediator, Function<M,javafx.beans.property.Property<T>> propertyB)
      Binds property 'a' to property 'b'. Property b is provided by 'mediator'.

      This method keeps a strong reference to propertyB.

      Type Parameters:
      T - the type of properties 'a' and 'b'
      M - the type of the mediator property
      Parameters:
      propertyA - property 'a'
      mediator - the mediator property 'm'
      propertyB - property 'b', this can be a new instance on each call
    • bindBidirectionalStrongly2

      public static <T, M> void bindBidirectionalStrongly2(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediator, Function<M,javafx.beans.property.Property<T>> propertyB)
    • bindBidirectional

      public static <T, M> void bindBidirectional(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediator, Function<M,javafx.beans.property.Property<T>> propertyB)
      Binds property 'a' to property 'b'. Property b is provided by 'mediator'.
      Type Parameters:
      T - the type of properties 'a' and 'b'
      M - the type of the mediator property
      Parameters:
      propertyA - property 'a'
      mediator - the mediator property 'm'
      propertyB - property 'b', must return the same instance each time when called for the same object 'm'.
    • bind

      public static <T, M> void bind(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediatorB, Function<M,javafx.beans.value.ObservableValue<T>> propertyB)
      Binds property 'a' to property 'b'. Property 'b' is provided by 'mediatorB'.

      When 'a' is unbound from 'b', its value is set to the value that 'a' had, when this method was invoked.

      Type Parameters:
      T - the type of properties 'a' and 'b'
      M - the type of the mediator property
      Parameters:
      propertyA - property 'a'
      mediatorB - the mediator property
      propertyB - property 'b'
    • bind

      public static <T, M> void bind(javafx.beans.property.Property<T> propertyA, javafx.beans.property.Property<M> mediatorB, Function<M,javafx.beans.value.ObservableValue<T>> propertyB, T unboundValue)
      Binds property 'a' to property 'b'. Property 'b' is provided by 'mediatorB'.

      When 'a' is unbound from 'b', its value is set to 'unboundValue'.

      Type Parameters:
      T - the type of properties 'a' and 'b'
      M - the type of the mediator property
      Parameters:
      propertyA - property 'a'
      mediatorB - the mediator property
      propertyB - property 'b'
      unboundValue - the value to be set on 'a' when 'a' is unbound from 'b'.
    • bind

      public static <T, M> void bind(javafx.beans.value.ObservableValue<M> mediatorA, Function<M,javafx.beans.property.Property<T>> propertyA, javafx.beans.value.ObservableValue<T> propertyB, T unboundValue)
      Binds property 'a' to property 'b'. Property 'a' is provided by 'mediatorA'.

      When 'a' is unbound from 'b', its value is set to 'unboundValue'.

      Type Parameters:
      T - the type of properties 'a' and 'b'
      M - the type of the mediator property
      Parameters:
      mediatorA - the mediator property
      propertyA - property 'a'
      propertyB - property 'b'
      unboundValue - the value to be set on 'a' when 'a' is unbound from 'b'.
    • bindBidirectional

      public static <T, S> void bindBidirectional(javafx.beans.property.StringProperty propertyA, javafx.beans.property.Property<S> mediator, Function<S,javafx.beans.property.Property<T>> propertyB, javafx.util.StringConverter<T> stringConverter)
      Binds property 'a' to property 'b'. Property b is provided by 'mediator'.
      Type Parameters:
      T - the value type of property 'b'
      S - the value type of the mediator property
      Parameters:
      propertyA - property 'a'
      mediator - the mediator property
      propertyB - property 'b'
      stringConverter - the converter
    • bindBidirectionalAndConvert

      public static <A, B, PROPERTY_A extends javafx.beans.value.WritableValue<A> & javafx.beans.value.ObservableValue<A>, PROPERTY_B extends javafx.beans.value.WritableValue<B> & javafx.beans.value.ObservableValue<B>> void bindBidirectionalAndConvert(PROPERTY_A propertyA, PROPERTY_B propertyB, Function<A,B> convertAtoB, Function<B,A> convertBtoA)
      Creates a bidirectional binding for properties A and B using the provided conversion functions.
      Type Parameters:
      A - the type of value A
      B - the type of value B
      PROPERTY_A - the type of property A
      PROPERTY_B - the type of property B
      Parameters:
      propertyA - property A
      propertyB - property B
      convertAtoB - converts a value from A to B
      convertBtoA - converts a value from B to A
    • formatted

      public static javafx.beans.binding.StringExpression formatted(String format, Object... args)
      Returns a string expression which uses java.test.MessageFormat to format the text. See MessageStringFormatter for special treatment of boolean values.
      Parameters:
      format - The format string.
      args - The arguments.
      Returns:
      The string expression
    • bindContent

      public static <D, S> void bindContent(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableList<S> src, Function<S,D> toDest)
      Binds list dest to list source.
      Type Parameters:
      D - the type of list dest
      S - the type of list source
      Parameters:
      dest - list dest
      src - list source
      toDest - mapping function to dest
    • bindContent

      public static <D, S> void bindContent(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableList<S> src, Function<S,D> toDest, @Nullable Consumer<D> destOnRemove)
      Binds list dest to list source.
      Type Parameters:
      D - the type of list dest
      S - the type of list source
      Parameters:
      dest - list dest
      src - list source
      toDest - mapping function to dest
      destOnRemove - this consumer is called when an element is removed from the dest list
    • bindContentBidirectional

      public static <D, S> void bindContentBidirectional(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableList<S> src, Function<S,D> toDest, @Nullable Consumer<D> destOnRemove, Function<D,S> toSource, @Nullable Consumer<S> sourceOnRemove)
      Binds list dest to list source bidirectionally.
      Type Parameters:
      D - the type of list dest
      S - the type of list source
      Parameters:
      dest - list dest
      src - list source
      toDest - mapping function to dest
      destOnRemove - this consumer is called when an element is removed from the dest list
    • bindListContentToSet

      public static <D, S> void bindListContentToSet(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableSet<S> src, Function<S,D> toDest)
      Binds list dest to set source.
      Type Parameters:
      D - the type of list dest
      S - the type of list source
      Parameters:
      dest - list dest
      src - list source
      toDest - mapping function to dest
    • bindListContentToSet

      public static <D, S> void bindListContentToSet(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableSet<S> src, Function<S,D> toDest, Consumer<D> disposeDest)
    • unbindListContentToSet

      public static <D, S> void unbindListContentToSet(javafx.collections.ObservableList<D> dest, javafx.collections.ObservableSet<S> src)
      Unbinds list dest from set source.
      Type Parameters:
      D - the type of list dest
      S - the type of list source
      Parameters:
      dest - list dest
      src - list source
    • bindElements

      public static <E, T> void bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.property.Property<T> property)
      Binds the specified property of all list elements to the given property.

      If an element is added, its property is bound.

      If an element is removed, its property is unbound and the property value is set to null.

      Type Parameters:
      E - the element type
      T - the property type
      Parameters:
      list - the list
      getter - the getter for the element property
      property - the property to which the element properties shall be bound
    • bindElements

      public static <E, T> void bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.property.Property<T> property, T unboundValue)
      Binds the specified property of all list elements to the given property.

      If an element is added, its property is bound.

      If an element is removed, its property is unbound and the property value is set to unboundValue.

      Type Parameters:
      E - the element type
      T - the property type
      Parameters:
      list - the list
      getter - the getter for the element property
      property - the property to which the element properties shall be bound
      unboundValue - the value to that is set when the property is unbound.
    • bindElements

      public static <E, T> void bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.binding.Binding<T> binding)
      Binds the specified property of all list elements to the given binding.

      If an element is added, its property is bound.

      If an element is removed, its property is unbound and the property value is set to null.

      Type Parameters:
      E - the element type
      T - the property type
      Parameters:
      list - the list
      getter - the getter for the element property
      binding - the binding to which the element properties shall be bound
    • bindElements

      public static <E, T> void bindElements(javafx.collections.ObservableList<E> list, Function<E,javafx.beans.property.Property<T>> getter, javafx.beans.binding.Binding<T> binding, T unboundValue)
      Binds the specified property of all list elements to the given binding.

      If an element is added, its property is bound.

      If an element is removed, its property is unbound and the property value is set to unboundValue.

      Type Parameters:
      E - the element type
      T - the property type
      Parameters:
      list - the list
      getter - the getter for the element property
      binding - the binding to which the element properties shall be bound
    • bindElements

      public static <E, T> void bindElements(javafx.collections.ObservableList<E> list, BiConsumer<E,T> setter, T value)
      Sets the specified value to all elements of the list.

      If an element is added, the specified value is set.

      If an element is removed, the null value is set.

      Type Parameters:
      E - the element type
      T - the value type
      Parameters:
      list - the list
      setter - the setter for the value on the element
      value - the value
    • bindMembershipToBoolean

      public static <E> void bindMembershipToBoolean(javafx.collections.ObservableSet<E> set, E element, javafx.beans.value.ObservableValue<Boolean> value)
      Adds or removes an element from a set depending on the bound boolean value.
      Type Parameters:
      E - the element type
      Parameters:
      set - a set
      element - an element that is added on true and removed on false
      value - the boolean value
    • computeDouble

      public static javafx.beans.binding.DoubleBinding computeDouble(DoubleSupplier op, javafx.beans.value.ObservableValue<?>... dependendies)
      Creates a binding with a computed value.

      If the value of one of the dependencies changes, the binding is marked as invalid.

      Parameters:
      op - the operation that computes the value
      dependendies - the depencies that invalidate the computed value
      Returns:
      a new binding
    • compute

      public static <T> javafx.beans.binding.ObjectBinding<T> compute(Supplier<T> op, javafx.beans.value.ObservableValue<?>... dependencies)
      Creates a binding with a computed value.

      If the value of one of the dependencies changes, the binding is marked as invalid.

      Type Parameters:
      T - the type of the value
      Parameters:
      op - the operation that computes the value
      dependencies - the depencies that invalidate the computed value
      Returns:
      a new binding
    • convert

      public static <A, B> javafx.beans.binding.ObjectBinding<B> convert(javafx.beans.value.ObservableValue<A> a, Function<A,B> convert)
      Creates a binding with a converted value.

      If the value of one of the dependencies changes, the binding is marked as invalid.

      Type Parameters:
      A - the type of the value
      B - the type of the converted binding
      Parameters:
      a - the property A
      convert - the conversion function from A to B
      Returns:
      a new binding
    • via

      public static <A, B> javafx.beans.value.ObservableValue<B> via(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> step)
      Creates a binding to get a property a.b.
      Type Parameters:
      A - the type of a
      B - the type of b
      Parameters:
      root - observable value a
      step - the function that gets a.b
      Returns:
      the binding a.b.
    • via

      public static <A, B, C> javafx.beans.value.ObservableValue<C> via(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.value.ObservableValue<C>> stepC)
      Creates a binding to get an observable value a.b.c.
      Type Parameters:
      A - the type of a
      B - the type of b
      C - the type of c
      Parameters:
      root - observable value a
      stepB - the function that gets a.b
      stepC - the function that gets b.c
      Returns:
      the binding a.b.c.
    • via

      public static <A, B, C, D> javafx.beans.value.ObservableValue<D> via(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.value.ObservableValue<C>> stepC, Function<C,javafx.beans.value.ObservableValue<D>> stepD)
      Creates a binding to get an observable value a.b.c.d.
      Type Parameters:
      A - the type of a
      B - the type of b
      C - the type of c
      D - the type of d
      Parameters:
      root - observable value a
      stepB - the function that gets a.b
      stepC - the function that gets b.c
      stepD - the function that gets c.d
      Returns:
      the binding a.b.c.
    • viaBidirectional

      public static <A, B> javafx.beans.property.Property<B> viaBidirectional(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.property.Property<B>> step)
      Creates a binding to get a property a.b.
      Type Parameters:
      A - the type of a
      B - the type of b
      Parameters:
      root - observable value a
      step - the function that gets a.b
      Returns:
      the binding a.b.
    • viaBidirectional

      public static <A, B, C> javafx.beans.property.Property<C> viaBidirectional(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.property.Property<C>> stepC)
      Creates a binding to get a property a.b.c.
      Type Parameters:
      A - the type of a
      B - the type of b
      C - the type of c
      Parameters:
      root - observable value a
      stepB - the function that gets a.b
      stepC - the function that gets b.c
      Returns:
      the binding a.b.c.
    • viaBidirectional

      public static <A, B, C, D> javafx.beans.property.Property<D> viaBidirectional(javafx.beans.value.ObservableValue<A> root, Function<A,javafx.beans.value.ObservableValue<B>> stepB, Function<B,javafx.beans.value.ObservableValue<C>> stepC, Function<C,javafx.beans.property.Property<D>> stepD)
      Creates a binding to get a property a.b.c.d.
      Type Parameters:
      A - the type of a
      B - the type of b
      C - the type of c
      D - the type of d
      Parameters:
      root - observable value a
      stepB - the function that gets a.b
      stepC - the function that gets b.c
      stepD - the function that gets c.d
      Returns:
      the binding a.b.c.