Skip to content

Method: fireIndexedPropertyChange(String, int, boolean, boolean)

1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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
12: * the License. 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
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.role.ui;
28:
29: import javax.annotation.Nonnull;
30: import java.beans.PropertyChangeListener;
31: import java.beans.PropertyChangeSupport;
32: import lombok.AllArgsConstructor;
33: import lombok.EqualsAndHashCode;
34: import lombok.NoArgsConstructor;
35: import lombok.ToString;
36: import lombok.experimental.Delegate;
37:
38: /***********************************************************************************************************************
39: *
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: *
56: * Creates a new {@code BoundProperty} with the given initial value.
57: *
58: * @param <T> the type of the property
59: * @param value the initial value
60: * @return the property
61: * @since 3.2-ALPHA-2
62: *
63: ******************************************************************************************************************/
64: @Nonnull
65: public static <T> BoundProperty<T> of (@Nonnull final T value)
66: {
67: return new BoundProperty<>(value);
68: }
69:
70: /*******************************************************************************************************************
71: *
72: * {@inheritDoc}
73: * This method fires a property change event associated to {@link #PROP_VALUE}.
74: *
75: ******************************************************************************************************************/
76: @Override
77: public void set (final T value)
78: {
79: final T oldValue = this.value;
80: this.value = value;
81: pcs.firePropertyChange(PROP_VALUE, oldValue, value);
82: }
83:
84: /*******************************************************************************************************************
85: *
86: * {@inheritDoc}
87: *
88: ******************************************************************************************************************/
89: @Override
90: public T get()
91: {
92: return value;
93: }
94:
95: /*******************************************************************************************************************
96: *
97: * Binds this property to a {@link ChangingSource}. Every change in the value of the source will be synchronized to
98: * the value of this property. If the source is a {@link Changeable} binding will be bidirectional, that is the
99: * object will be set to the current value of this property and will be kept in sync.
100: *
101: * @param source the source
102: *
103: ******************************************************************************************************************/
104: public void bind (@Nonnull final ChangingSource<T> source)
105: {
106: source.addPropertyChangeListener(event -> set((T)event.getNewValue()));
107:
108: if (source instanceof Changeable)
109: {
110: final Changeable<T> changeable = (Changeable<T>)source;
111: changeable.set(value);
112: this.addPropertyChangeListener(event -> changeable.set((T)event.getNewValue()));
113: }
114: }
115:
116: /*******************************************************************************************************************
117: *
118: * {@inheritDoc}
119: *
120: ******************************************************************************************************************/
121: @Override
122: public void unbindAll()
123: {
124: for (final PropertyChangeListener listener : pcs.getPropertyChangeListeners().clone())
125: {
126: pcs.removePropertyChangeListener(listener);
127: }
128: }
129: }