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.impl;
27
28 import javax.annotation.Nonnull;
29 import java.beans.PropertyChangeSupport;
30 import java.util.Collection;
31 import java.util.Optional;
32 import it.tidalwave.util.As;
33 import it.tidalwave.util.AsException;
34 import it.tidalwave.util.Callback;
35 import it.tidalwave.util.NamedCallback;
36 import it.tidalwave.role.ui.PresentationModel;
37 import lombok.ToString;
38 import lombok.experimental.Delegate;
39 import lombok.extern.slf4j.Slf4j;
40
41 /***************************************************************************************************************************************************************
42 *
43 * A default implementation of {@link PresentationModel}.
44 *
45 * @author Fabrizio Giudici
46 *
47 **************************************************************************************************************************************************************/
48 @ToString(exclude = {"as", "pcs"}) @Slf4j
49 public class DefaultPresentationModel implements PresentationModel
50 {
51 @Nonnull
52 private final Object owner;
53
54 @Delegate
55 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
56
57 private final As as;
58
59 /***********************************************************************************************************************************************************
60 *
61 **********************************************************************************************************************************************************/
62 public DefaultPresentationModel (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
63 {
64 this.owner = owner;
65 as = As.forObject(owner, roles);
66 }
67
68 /***********************************************************************************************************************************************************
69 * {@inheritDoc}
70 **********************************************************************************************************************************************************/
71 @Override @Nonnull
72 public <T> T as (@Nonnull final Class<? extends T> roleType)
73 {
74 return maybeAs(roleType).orElseThrow(() -> new AsException(roleType));
75 }
76
77 /***********************************************************************************************************************************************************
78 * {@inheritDoc}
79 **********************************************************************************************************************************************************/
80 @SuppressWarnings("ConstantValue")
81 @Override @Nonnull
82 public <T> Optional<T> maybeAs (@Nonnull final Class<? extends T> roleType)
83 {
84 // Undocumented feature: for instance Zephyr needs to fire property events
85 if (roleType.equals(PropertyChangeSupport.class))
86 {
87 return Optional.of(roleType.cast(pcs));
88 }
89
90 final Optional<T> t = as.maybeAs(roleType);
91
92 if (t.isPresent())
93 {
94 return t;
95 }
96
97 if (owner instanceof As)
98 {
99 try
100 {
101 final var role = ((As)owner).as(roleType);
102
103 if (role != null) // do check it for improper implementations or partial mocks
104 {
105 return Optional.of(role);
106 }
107 }
108 catch (AsException ignore)
109 {
110 // fallback
111 }
112 }
113
114 return Optional.empty();
115 }
116
117 /***********************************************************************************************************************************************************
118 * {@inheritDoc}
119 **********************************************************************************************************************************************************/
120 @Override @Nonnull
121 public <T> Collection<T> asMany (@Nonnull final Class<? extends T> roleType)
122 {
123 final Collection<T> result = as.asMany(roleType);
124
125 // The problem here is that we want only to add local roles in owner; but calling owner.as() will also
126 // find again the global roles that were discovered by AsSupport.
127 if (roleType.isAssignableFrom(owner.getClass()))
128 {
129 result.add(roleType.cast(owner));
130 }
131
132 if (owner instanceof As)
133 {
134 result.addAll(((As)owner).asMany(roleType));
135 }
136
137 return result;
138 }
139
140 /***********************************************************************************************************************************************************
141 * {@inheritDoc}
142 **********************************************************************************************************************************************************/
143 @Override
144 public void dispose()
145 {
146 for (final var listener : pcs.getPropertyChangeListeners().clone())
147 {
148 pcs.removePropertyChangeListener(listener);
149 }
150
151 asMany(NamedCallback.class).stream()
152 .filter(c -> c.getName().equals(CALLBACK_DISPOSE))
153 .forEach(callback -> wrap(callback, "While calling 'dispose' callbacks"));
154 }
155
156 /***********************************************************************************************************************************************************
157 *
158 **********************************************************************************************************************************************************/
159 private static void wrap (@Nonnull final Callback callback, @Nonnull final String logMessage)
160 {
161 try
162 {
163 callback.call();
164 }
165 catch (Throwable t)
166 {
167 log.error(logMessage, t);
168 }
169 }
170 }