View Javadoc
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  
28  import jakarta.annotation.Nonnull;
29  import java.beans.PropertyChangeSupport;
30  import it.tidalwave.ui.core.role.Changeable;
31  import lombok.AllArgsConstructor;
32  import lombok.EqualsAndHashCode;
33  import lombok.NoArgsConstructor;
34  import lombok.ToString;
35  import lombok.experimental.Delegate;
36  
37  /***************************************************************************************************************************************************************
38   *
39   * @since   2.0-ALPHA-1
40   * @author  Fabrizio Giudici
41   *
42   **************************************************************************************************************************************************************/
43  // FIXME: weak listeners
44  @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(exclude="pcs") @ToString(exclude="pcs")
45  public class BoundProperty<T> implements ChangingSource<T>, Changeable<T>
46    {
47      @Delegate
48      private final transient PropertyChangeSupport pcs = new PropertyChangeSupport(this);
49  
50      public static final String PROP_VALUE = "value";
51  
52      private T value;
53  
54      /***********************************************************************************************************************************************************
55       * Creates a new {@code BoundProperty} with the given initial value.
56       *
57       * @param <T>     the type of the property
58       * @param value   the initial value
59       * @return        the property
60       * @since         3.2-ALPHA-2
61       **********************************************************************************************************************************************************/
62      @Nonnull
63      public static <T> BoundProperty<T> of (@Nonnull final T value)
64        {
65          return new BoundProperty<>(value);
66        }
67  
68      /***********************************************************************************************************************************************************
69       * {@inheritDoc}
70       *  This method fires a property change event associated to {@link #PROP_VALUE}.
71       **********************************************************************************************************************************************************/
72      @Override
73      public void set (final T value)
74        {
75          final var oldValue = this.value;
76          this.value = value;
77          pcs.firePropertyChange(PROP_VALUE, oldValue, value);
78        }
79  
80      /***********************************************************************************************************************************************************
81       * {@inheritDoc}
82       **********************************************************************************************************************************************************/
83      @Override
84      public T get()
85        {
86          return value;
87        }
88  
89      /***********************************************************************************************************************************************************
90       * Binds this property to a {@link ChangingSource}. Every change in the value of the source will be synchronized to
91       * the value of this property. If the source is a {@link Changeable} binding will be bidirectional, that is the
92       * object will be set to the current value of this property and will be kept in sync.
93       *
94       * @param     source    the source
95       **********************************************************************************************************************************************************/
96      public void bind (@Nonnull final ChangingSource<T> source)
97        {
98          source.addPropertyChangeListener(event -> set((T)event.getNewValue()));
99  
100         if (source instanceof Changeable)
101           {
102             final var changeable = (Changeable<T>)source;
103             changeable.set(value);
104             this.addPropertyChangeListener(event -> changeable.set((T)event.getNewValue()));
105           }
106       }
107 
108     /***********************************************************************************************************************************************************
109      * {@inheritDoc}
110      **********************************************************************************************************************************************************/
111     @Override
112     public void unbindAll()
113       {
114         for (final var listener : pcs.getPropertyChangeListeners().clone())
115           {
116             pcs.removePropertyChangeListener(listener);
117           }
118       }
119   }