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
28 import javax.annotation.Nonnull;
29 import java.beans.PropertyChangeListener;
30 import java.util.Collection;
31 import java.util.Collections;
32 import it.tidalwave.util.As;
33 import it.tidalwave.util.NamedCallback;
34 import it.tidalwave.util.Parameters;
35 import it.tidalwave.role.SimpleComposite;
36 import it.tidalwave.role.ui.impl.DefaultPresentationModel;
37 import static it.tidalwave.util.Parameters.r;
38 import static it.tidalwave.role.ui.Presentable._Presentable_;
39
40 /***************************************************************************************************************************************************************
41 *
42 * TODO: As the NetBeans Node, it should allow children, have event listeners for children added/removed/changed.
43 * This class so becomes the true M in MVC.
44 *
45 * @stereotype Role
46 *
47 * @author Fabrizio Giudici
48 *
49 **************************************************************************************************************************************************************/
50 public interface PresentationModel extends As
51 {
52 public static final Class<PresentationModel> PresentationModel = PresentationModel.class;
53
54 public static final As.Type<SimpleComposite<PresentationModel>> _SimpleCompositeOfPresentationModel_ =
55 As.type(SimpleComposite.class);
56
57 public static final String PROPERTY_CHILDREN = "children";
58
59 /** This is an undocumented feature. If you add a {@link NamedCallback} with this name as a role in this object, it
60 * will be called back when {@link #dispose()} is called. */
61 public static final String CALLBACK_DISPOSE = "dispose";
62
63 /***********************************************************************************************************************************************************
64 * Disposes this object.
65 **********************************************************************************************************************************************************/
66 public void dispose();
67
68 /***********************************************************************************************************************************************************
69 * Adds a {@link PropertyChangeListener}.
70 *
71 * @param listener the listener
72 **********************************************************************************************************************************************************/
73 public void addPropertyChangeListener (@Nonnull PropertyChangeListener listener);
74
75 /***********************************************************************************************************************************************************
76 * Adds a {@link PropertyChangeListener} for the given property.
77 *
78 * @param propertyName the name of the property
79 * @param listener the listener
80 **********************************************************************************************************************************************************/
81 public void addPropertyChangeListener (@Nonnull String propertyName, @Nonnull PropertyChangeListener listener);
82
83 /***********************************************************************************************************************************************************
84 * Removes a {@link PropertyChangeListener}.
85 *
86 * @param listener the listener
87 **********************************************************************************************************************************************************/
88 public void removePropertyChangeListener (@Nonnull PropertyChangeListener listener);
89
90 /***********************************************************************************************************************************************************
91 * Removes a {@link PropertyChangeListener} for the given property.
92 *
93 * @param propertyName the name of the property
94 * @param listener the listener
95 **********************************************************************************************************************************************************/
96 public void removePropertyChangeListener (@Nonnull String propertyName, @Nonnull PropertyChangeListener listener);
97
98 /***********************************************************************************************************************************************************
99 * Checks whether the given property has been bound to listeners.
100 *
101 * @param propertyName the name of the property
102 * @return {@code true} if the property is bound
103 **********************************************************************************************************************************************************/
104 public boolean hasListeners (@Nonnull String propertyName);
105
106 /***********************************************************************************************************************************************************
107 * Returns all the bound {@link PropertyChangeListener}s.
108 *
109 * @return the listeners
110 **********************************************************************************************************************************************************/
111 @Nonnull
112 public PropertyChangeListener[] getPropertyChangeListeners();
113
114 /***********************************************************************************************************************************************************
115 * Returns the bound {@link PropertyChangeListener}s for the given property.
116 *
117 * @param propertyName the name of the property
118 * @return the listeners
119 **********************************************************************************************************************************************************/
120 @Nonnull
121 public PropertyChangeListener[] getPropertyChangeListeners (@Nonnull String propertyName);
122
123 /***********************************************************************************************************************************************************
124 * Creates an instance given an owner and no roles.
125 *
126 * @param owner the owner
127 * @return the new instance
128 * @since 3.2-ALPHA-3
129 **********************************************************************************************************************************************************/
130 @Nonnull
131 public static PresentationModel of (@Nonnull final Object owner)
132 {
133 Parameters.mustNotBeArrayOrCollection(owner, "owner");
134 return of(owner, Collections.emptyList());
135 }
136
137 /***********************************************************************************************************************************************************
138 * Creates an instance given an owner and a single role.
139 *
140 * @param owner the owner
141 * @param role the role (or a {@link it.tidalwave.util.RoleFactory})
142 * @return the new instance
143 * @since 3.2-ALPHA-3
144 **********************************************************************************************************************************************************/
145 @Nonnull
146 public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Object role)
147 {
148 Parameters.mustNotBeArrayOrCollection(owner, "owner");
149 Parameters.mustNotBeArrayOrCollection(role, "role");
150 return of(owner, r(role));
151 }
152
153 /***********************************************************************************************************************************************************
154 * Creates an instance given an owner and multiple roles.
155 *
156 * @param owner the owner
157 * @param roles roles or {@link it.tidalwave.util.RoleFactory} instances
158 * @return the new instance
159 * @since 3.2-ALPHA-1
160 * @since 3.2-ALPHA-3 (refactored)
161 **********************************************************************************************************************************************************/
162 @Nonnull
163 public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
164 {
165 Parameters.mustNotBeArrayOrCollection(owner, "owner");
166 return new DefaultPresentationModel(owner, roles);
167 }
168
169 /***********************************************************************************************************************************************************
170 * Returns an empty instance (no roles, with the exception of a dummy {@link Displayable}).
171 *
172 * @return the empty instance
173 * @since 3.2-ALPHA-3
174 **********************************************************************************************************************************************************/
175 @Nonnull
176 public static PresentationModel empty()
177 {
178 // TODO: cache a singleton, but don't do eager initialization (e.g. a final static), as it would deadlock with
179 // SteelBlue.
180 return of("", Displayable.of("<empty presentation model>"));
181 }
182
183 /***********************************************************************************************************************************************************
184 * Creates an instance from an owner which might have the {@link Presentable} role. If it is present, it is called
185 * to create the {@code PresentationModel}; otherwise a default one is created. Additional roles are added.
186 *
187 * @param owner the owner
188 * @param roles roles or {@link it.tidalwave.util.RoleFactory} instances
189 * @return the new instance
190 * @since 3.2-ALPHA-8
191 * @it.tidalwave.javadoc.experimental TODO: perhaps it could be merged to of().
192 **********************************************************************************************************************************************************/
193 @Nonnull
194 public static PresentationModel ofMaybePresentable (@Nonnull final As owner, @Nonnull final Collection<Object> roles)
195 {
196 Parameters.mustNotBeArrayOrCollection(owner, "owner");
197 return owner.maybeAs(_Presentable_)
198 .map(p -> p.createPresentationModel(roles))
199 .orElseGet(() -> of(owner, roles));
200 }
201
202 /***********************************************************************************************************************************************************
203 * Creates an instance from an owner which might have the {@link Presentable} role. If it is present, it is called
204 * to create the {@code PresentationModel}; otherwise a default one is created.
205 *
206 * @param owner the owner
207 * @return the new instance
208 * @since 3.2-ALPHA-8
209 * @it.tidalwave.javadoc.experimental TODO: perhaps it could be merged to of().
210 **********************************************************************************************************************************************************/
211 @Nonnull
212 public static PresentationModel ofMaybePresentable (@Nonnull final As owner)
213 {
214 return ofMaybePresentable(owner, Collections.emptyList());
215 }
216 }