Class AbstractComponentValue<C extends JComponent,T>

java.lang.Object
is.codion.common.reactive.observer.AbstractObserver<T>
is.codion.common.reactive.value.AbstractValue<T>
is.codion.swing.common.ui.component.value.AbstractComponentValue<C,T>
Type Parameters:
C - the component type
T - the value type
All Implemented Interfaces:
Observable<T>, Observer<T>, Value<T>, ComponentValue<C,T>
Direct Known Subclasses:
AbstractTextComponentValue

public abstract class AbstractComponentValue<C extends JComponent,T> extends AbstractValue<T> implements ComponentValue<C,T>

An abstract base implementation of ComponentValue.

A change made in the component, by the user for example, is validated before it is notified, see validateChanges(). A rejected change is not notified, the component being restored to the last accepted value once the event making the change has completed, and the exception rethrown on the Event Dispatch Thread.

  • Constructor Details

    • AbstractComponentValue

      protected AbstractComponentValue(C component)
      Instantiates a new nullable AbstractComponentValue
      Parameters:
      component - the component
      Throws:
      NullPointerException - in case component is null
    • AbstractComponentValue

      protected AbstractComponentValue(C component, @Nullable T nullValue)
      Instantiates a new AbstractComponentValue
      Parameters:
      component - the component
      nullValue - the value to use instead of null
      Throws:
      NullPointerException - in case component is null
  • Method Details

    • component

      public final C component()
      Specified by:
      component in interface ComponentValue<C extends JComponent,T>
      Returns:
      the input component representing the value
    • getValue

      protected final @Nullable T getValue()
      Returns the actual internal value
      Returns:
      the value
    • setValue

      protected final void setValue(@Nullable T value)
      Sets the actual internal value
      Parameters:
      value - the value
    • shouldNotify

      protected final boolean shouldNotify(@Nullable T value)
      Called by notifyObserver() before the listeners are notified, with the value they are to receive, the listeners only notified in case this method returns true. Allows a value whose state changes outside Value.set(Object), held by a component for example, to validate a change before it is notified.

      Returns true by default.

      Parameters:
      value - the value the listeners are to receive
      Returns:
      true if the listeners should be notified
    • validateChanges

      protected boolean validateChanges()

      Specifies whether a change made in the component is validated before it is notified, a rejected change being restored. Override to return false for a component edited in steps passing through intermediate values, such as text being typed, where validating each step would reject an edit in progress.

      Returns true by default.

      Returns:
      true if a change made in the component is validated before it is notified
    • getComponentValue

      protected abstract @Nullable T getComponentValue()
      Returns the value from the underlying component
      Returns:
      the value from the underlying component
      See Also:
    • setComponentValue

      protected abstract void setComponentValue(@Nullable T value)
      Sets the given value in the underlying component. Note that this method is called on the Event Dispatch Thread.
      Parameters:
      value - the value to display in the underlying component
      See Also:
    • observable

      public Observable<T> observable()
      Specified by:
      observable in interface Value<T>
      Returns:
      a read-only Observable instance for this value
    • changed

      public final Observer<Change<T>> changed()
      Description copied from interface: Observable
      The default implementation returns a new Observer instance on each call, but subclasses are free to return the same instance.

      Note that when a new instance is returned per call, a listener added through one changed() instance can only be removed through that same instance; retain it if the listener must later be removed.

      Specified by:
      changed in interface Observable<T>
      Returns:
      an Observer instance notified when this observable has changed
    • locked

      public final Value.Locked locked()
      Description copied from interface: Value

      Locking a value prevents it from being changed, it does not prevent it from being set.

      Note that locking a value does not prevent linked values from being changed.

      A locked value in a linked value chain, may cause it to go out of sync, since any subsequent linked values will not be updated after a locked value is encountered.

      Specified by:
      locked in interface Value<T>
      Returns:
      the Value.Locked instance controlling whether this value is locked
    • isNullable

      public final boolean isNullable()
      Description copied from interface: Observable
      If false then get() is guaranteed to never return null.
      Specified by:
      isNullable in interface Observable<T>
      Returns:
      true if this observable can be null
    • link

      public final void link(Value<T> originalValue)
      Description copied from interface: Value

      Creates a bidirectional link between this and the given original value, so that changes in one are reflected in the other.

      Note that after a call to this method this value is the same as originalValue.

      The two behave as one logical value: either end validates against both ends' validators, before either is written, so the pair can not diverge or come to hold a value one end rejects. This holds for any Value, Value.validate(Object) being the gate.

      Cycle detection reaches as far as the framework's own link bookkeeping, which means every value extending AbstractValue. A Value implemented from scratch keeps its own links, if it has any, so a cycle running through one is not detected - the reciprocal update guard still keeps a single link from cycling back on itself.

      Specified by:
      link in interface Value<T>
      Parameters:
      originalValue - the original value to link this value to
    • unlink

      public final void unlink(Value<T> originalValue)
      Description copied from interface: Value
      Unlinks this value from the given original value
      Specified by:
      unlink in interface Value<T>
      Parameters:
      originalValue - the original value to unlink from this one
    • link

      public final void link(Observable<T> observable)
      Description copied from interface: Value
      Creates a unidirectional link between this value and the given observable, so that changes in the observable are reflected in this one. Note that after a call to this method the value of this value is the same as the observable.
      Specified by:
      link in interface Value<T>
      Parameters:
      observable - the observable to link this value to
    • unlink

      public final void unlink(Observable<T> observable)
      Description copied from interface: Value
      Unlinks this value from the given observable
      Specified by:
      unlink in interface Value<T>
      Parameters:
      observable - the observable to unlink
    • addValidator

      public final boolean addValidator(Value.Validator<? super T> validator)
      Description copied from interface: Value
      Adds a validator to this Value. Adding the same validator again has no effect.
      Specified by:
      addValidator in interface Value<T>
      Parameters:
      validator - the validator
      Returns:
      true if this value did not already contain the specified validator
    • removeValidator

      public final boolean removeValidator(Value.Validator<? super T> validator)
      Description copied from interface: Value
      Removes the given validator from this value
      Specified by:
      removeValidator in interface Value<T>
      Parameters:
      validator - the validator
      Returns:
      true if this value contained the specified validator
    • validate

      public final void validate(T value)
      Description copied from interface: Value
      Validates the given value using all validators, as Value.set(Object) does, the null value standing in for null in case of a non-null value.
      Specified by:
      validate in interface Value<T>
      Parameters:
      value - the value to validate
    • notifyObserver

      protected final void notifyObserver()
      Notifies the underlying observer that the underlying value has changed or at least that it may have changed, in case shouldNotify(Object) allows it.
    • createObservable

      protected Observable<T> createObservable()
      Returns:
      a new Observable instance representing this value