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