BoundProperty.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/steelblue-src
  22.  * git clone https://github.com/tidalwave-it/steelblue-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.ui.core;

  27. import jakarta.annotation.Nonnull;
  28. import java.beans.PropertyChangeSupport;
  29. import it.tidalwave.ui.core.role.Changeable;
  30. import it.tidalwave.ui.core.role.impl.MutableListenerSupport;
  31. import lombok.AllArgsConstructor;
  32. import lombok.EqualsAndHashCode;
  33. import lombok.NoArgsConstructor;
  34. import lombok.ToString;
  35. import lombok.experimental.Delegate;

  36. /***************************************************************************************************************************************************************
  37.  *
  38.  * @since   2.0-ALPHA-1
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **************************************************************************************************************************************************************/
  42. // FIXME: weak listeners
  43. @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(exclude={"pcs", "mls"}) @ToString(exclude={"pcs", "mls"}) @SuppressWarnings("this-escape")
  44. public class BoundProperty<T> implements ChangingSource<T>, Changeable<T>
  45.   {
  46.     @Delegate
  47.     private final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);

  48.     @Delegate
  49.     private final transient MutableListenerSupport mls = new MutableListenerSupport(pcs);

  50.     public static final String PROP_VALUE = "value";

  51.     private T value;

  52.     /***********************************************************************************************************************************************************
  53.      * Creates a new {@code BoundProperty} with the given initial value.
  54.      *
  55.      * @param <T>     the type of the property
  56.      * @param value   the initial value
  57.      * @return        the property
  58.      * @since         3.2-ALPHA-2
  59.      **********************************************************************************************************************************************************/
  60.     @Nonnull
  61.     public static <T> BoundProperty<T> of (@Nonnull final T value)
  62.       {
  63.         return new BoundProperty<>(value);
  64.       }

  65.     /***********************************************************************************************************************************************************
  66.      * {@inheritDoc}
  67.      *  This method fires a property change event associated to {@link #PROP_VALUE}.
  68.      **********************************************************************************************************************************************************/
  69.     @Override
  70.     public void set (final T value)
  71.       {
  72.         final var oldValue = this.value;
  73.         this.value = value;
  74.         pcs.firePropertyChange(PROP_VALUE, oldValue, value);
  75.       }

  76.     /***********************************************************************************************************************************************************
  77.      * {@inheritDoc}
  78.      **********************************************************************************************************************************************************/
  79.     @Override
  80.     public T get()
  81.       {
  82.         return value;
  83.       }

  84.     /***********************************************************************************************************************************************************
  85.      * Binds this property to a {@link ChangingSource}. Every change in the value of the source will be synchronized to
  86.      * the value of this property. If the source is a {@link Changeable} binding will be bidirectional, that is the
  87.      * object will be set to the current value of this property and will be kept in sync.
  88.      *
  89.      * @param     source    the source
  90.      **********************************************************************************************************************************************************/
  91.     public void bind (@Nonnull final ChangingSource<T> source)
  92.       {
  93.         source.addPropertyChangeListener(event -> set((T)event.getNewValue()));

  94.         if (source instanceof Changeable)
  95.           {
  96.             final var changeable = (Changeable<T>)source;
  97.             changeable.set(value);
  98.             this.addPropertyChangeListener(event -> changeable.set((T)event.getNewValue()));
  99.           }
  100.       }

  101.     /***********************************************************************************************************************************************************
  102.      * {@inheritDoc}
  103.      **********************************************************************************************************************************************************/
  104.     @Override
  105.     public void unbindAll()
  106.       {
  107.         for (final var listener : pcs.getPropertyChangeListeners().clone())
  108.           {
  109.             pcs.removePropertyChangeListener(listener);
  110.           }

  111.         mls.removeAllListeners();
  112.       }
  113.   }