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.role.impl;
27
28 import jakarta.annotation.Nonnull;
29 import java.beans.PropertyChangeSupport;
30 import java.util.IdentityHashMap;
31 import it.tidalwave.ui.core.MutableListener;
32 import it.tidalwave.util.annotation.VisibleForTesting;
33 import lombok.RequiredArgsConstructor;
34
35 /***************************************************************************************************************************************************************
36 *
37 * @author Fabrizio Giudici
38 *
39 **************************************************************************************************************************************************************/
40 @RequiredArgsConstructor
41 public class MutableListenerSupport
42 {
43 @Nonnull
44 private final PropertyChangeSupport pcs;
45
46 @VisibleForTesting final IdentityHashMap<MutableListener<?>, MutableListenerAdapter<?>> adapterMap = new IdentityHashMap<>();
47
48 /***********************************************************************************************************************************************************
49 * Registers a {@link MutableListener}.
50 * @param listener the listener
51 **********************************************************************************************************************************************************/
52 public <T> void addListener (@Nonnull final MutableListener<T> listener)
53 {
54 final var adapter = new MutableListenerAdapter<>(listener);
55 adapterMap.put(listener, adapter);
56 pcs.addPropertyChangeListener(adapter);
57 }
58
59 /***********************************************************************************************************************************************************
60 * Registers a {@link MutableListener}.
61 * @param listener the listener
62 * @param propertyName the property name to which the listener will be associated
63 **********************************************************************************************************************************************************/
64 public <T> void addListener (@Nonnull final String propertyName, @Nonnull final MutableListener<T> listener)
65 {
66 final var adapter = new MutableListenerAdapter<>(listener);
67 adapterMap.put(listener, adapter);
68 pcs.addPropertyChangeListener(propertyName, adapter);
69 }
70
71 /***********************************************************************************************************************************************************
72 * Unregisters a {@link MutableListener}.
73 * @param listener the listener
74 **********************************************************************************************************************************************************/
75 public <T> void removeListener (@Nonnull final MutableListener<T> listener)
76 {
77 final var adapter = adapterMap.remove(listener);
78
79 if (adapter != null)
80 {
81 pcs.removePropertyChangeListener(adapter);
82 }
83 }
84
85 /***********************************************************************************************************************************************************
86 * Unregisters a {@link MutableListener}.
87 * @param listener the listener
88 * @param propertyName the property name to which the listener will be associated
89 **********************************************************************************************************************************************************/
90 public <T> void removeListener (@Nonnull final String propertyName, @Nonnull final MutableListener<T> listener)
91 {
92 final var adapter = adapterMap.remove(listener);
93
94 if (adapter != null)
95 {
96 pcs.removePropertyChangeListener(propertyName, adapter);
97 }
98 }
99
100 /***********************************************************************************************************************************************************
101 * Unregisters all the {@link MutableListener}s.
102 **********************************************************************************************************************************************************/
103 public void removeAllListeners()
104 {
105 adapterMap.clear();
106 }
107
108 public <T> void fireChange (final T oldValue, final T newValue)
109 {
110 pcs.firePropertyChange("", oldValue, newValue);
111 }
112
113 public <T> void fireChange (@Nonnull final String propertyName, final T oldValue, final T newValue)
114 {
115 pcs.firePropertyChange(propertyName, oldValue, newValue);
116 }
117 }