Class SignalPropertySupport<T>

java.lang.Object
com.vaadin.flow.component.SignalPropertySupport<T>
Type Parameters:
T - the type of the property
All Implemented Interfaces:
Serializable

public class SignalPropertySupport<T> extends Object implements Serializable
Helper class for binding a Signal to a property of a Component. Not all component features delegate directly to the state in Element. For those features, this helper class ensures state management behaves consistently with Element properties.

Example of usage:

 ValueSignal<String> signal = new ValueSignal<>("");
 MyComponent component = new MyComponent();
 add(component);
 component.bindTextContent(signal);
 signal.set("Hello"); // component content showing now "Content: Hello" text
 
 @Tag(div)
 public class MyComponent extends Component {
     private final SignalPropertySupport<String> textProperty = SignalPropertySupport
             .create(this, value -> {
                 getElement().executeJs("this.textContent = 'Content: ' + $0",
                         value);
             });

     public String getText() {
         return textProperty.get();
     }

     public void setText(String text) {
         textProperty.set(text);
     }

     public void bindTextContent(Signal<String> textSignal) {
         textProperty.bind(textSignal);
     }
 }
 
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    bind(Signal<T> signal)
    Binds a Signal's value to this property support and keeps the value synchronized with the signal value while the component is in attached state.
    static <T> SignalPropertySupport<T>
    create(Component owner, SerializableConsumer<T> valueChangeConsumer)
    Creates a new instance of SignalPropertySupport for the given owner component and a value change consumer to be called when property value is updated.
    get()
    Gets the current value of this property support.
    void
    set(T value)
    Sets the value of this property support.

    Methods inherited from class java.lang.Object

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

    • create

      public static <T> SignalPropertySupport<T> create(Component owner, SerializableConsumer<T> valueChangeConsumer)
      Creates a new instance of SignalPropertySupport for the given owner component and a value change consumer to be called when property value is updated. The property value is updated either manually with set(Object), or automatically via Signal value change while the owner component is in the attached state and signal is bound with bind(Signal).
      Type Parameters:
      T - the type of the property
      Parameters:
      owner - the owner component for which the value change consumer is applied, must not be null
      valueChangeConsumer - the consumer to be called when the value changes, must not be null
      Returns:
      a new instance of SignalPropertySupport
      See Also:
    • bind

      public void bind(Signal<T> signal)
      Binds a Signal's value to this property support and keeps the value synchronized with the signal value while the component is in attached state. When the component is in detached state, signal value changes have no effect. null signal unbinds existing binding.

      While a Signal is bound to a property support, any attempt to set value manually throws BindingActiveException. Same happens when trying to bind a new Signal while one is already bound.

      Parameters:
      signal - the signal to bind or null to unbind any existing binding
      Throws:
      BindingActiveException - thrown when there is already an existing binding
    • get

      public T get()
      Gets the current value of this property support.
      Returns:
      the current value
    • set

      public void set(T value)
      Sets the value of this property support.
      Parameters:
      value - the value to set
      Throws:
      BindingActiveException - thrown when there is an existing binding