BoundProperty.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 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/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.role.ui;

  27. import javax.annotation.Nonnull;
  28. import java.beans.PropertyChangeSupport;
  29. import lombok.AllArgsConstructor;
  30. import lombok.EqualsAndHashCode;
  31. import lombok.NoArgsConstructor;
  32. import lombok.ToString;
  33. import lombok.experimental.Delegate;

  34. /***************************************************************************************************************************************************************
  35.  *
  36.  * @author  Fabrizio Giudici
  37.  *
  38.  **************************************************************************************************************************************************************/
  39. // FIXME: weak listeners
  40. @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(exclude="pcs") @ToString(exclude="pcs")
  41. public class BoundProperty<T> implements ChangingSource<T>, Changeable<T>
  42.   {
  43.     @Delegate
  44.     private final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);

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

  46.     private T value;

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

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

  71.     /***********************************************************************************************************************************************************
  72.      * {@inheritDoc}
  73.      **********************************************************************************************************************************************************/
  74.     @Override
  75.     public T get()
  76.       {
  77.         return value;
  78.       }

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

  89.         if (source instanceof Changeable)
  90.           {
  91.             final var changeable = (Changeable<T>)source;
  92.             changeable.set(value);
  93.             this.addPropertyChangeListener(event -> changeable.set((T)event.getNewValue()));
  94.           }
  95.       }

  96.     /***********************************************************************************************************************************************************
  97.      * {@inheritDoc}
  98.      **********************************************************************************************************************************************************/
  99.     @Override
  100.     public void unbindAll()
  101.       {
  102.         for (final var listener : pcs.getPropertyChangeListeners().clone())
  103.           {
  104.             pcs.removePropertyChangeListener(listener);
  105.           }
  106.       }
  107.   }