Skip to contentMethod: of(Object, Collection)
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;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.Collection;
30: import java.util.Collections;
31: import it.tidalwave.util.As;
32: import it.tidalwave.util.NamedCallback;
33: import it.tidalwave.util.Parameters;
34: import it.tidalwave.role.SimpleComposite;
35: import it.tidalwave.ui.core.Mutable;
36: import it.tidalwave.ui.core.role.impl.DefaultPresentationModel;
37: import org.apiguardian.api.API;
38: import static it.tidalwave.util.Parameters.r;
39: import static it.tidalwave.ui.core.role.Presentable._Presentable_;
40: import static org.apiguardian.api.API.Status.EXPERIMENTAL;
41:
42: /***************************************************************************************************************************************************************
43: *
44: * TODO: As the NetBeans Node, it should allow children, have event listeners for children added/removed/changed.
45: * This class so becomes the true M in MVC.
46: *
47: * @stereotype Role
48: * @since 2.0-ALPHA-1
49: * @author Fabrizio Giudici
50: *
51: **************************************************************************************************************************************************************/
52: public interface PresentationModel extends As, Mutable
53: {
54: public static final Class<PresentationModel> _PresentationModel_ = PresentationModel.class;
55:
56: public static final As.Type<SimpleComposite<PresentationModel>> _SimpleCompositeOfPresentationModel_ = As.type(SimpleComposite.class);
57:
58: public static final String PROPERTY_CHILDREN = "children";
59:
60: /** This is an undocumented feature. If you add a {@link NamedCallback} with this name as a role in this object, it will be called back when
61: {@link #dispose()} is called. */
62: public static final String CALLBACK_DISPOSE = "dispose";
63: public static final String PARAM_OWNER = "owner";
64: public static final String PARAM_ROLE = "role";
65:
66: /***********************************************************************************************************************************************************
67: * Disposes this object.
68: **********************************************************************************************************************************************************/
69: public void dispose();
70:
71: /***********************************************************************************************************************************************************
72: * {@return a new instance given an owner and no roles}.
73: * @param owner the owner
74: * @since 3.2-ALPHA-3
75: **********************************************************************************************************************************************************/
76: @Nonnull
77: public static PresentationModel of (@Nonnull final Object owner)
78: {
79: Parameters.mustNotBeArrayOrCollection(owner, PARAM_OWNER);
80: return of(owner, Collections.emptyList());
81: }
82:
83: /***********************************************************************************************************************************************************
84: * {@return a new instance given an owner and a single role}.
85: * @param owner the owner
86: * @param role the role (or a {@link it.tidalwave.util.RoleFactory})
87: * @since 3.2-ALPHA-3
88: **********************************************************************************************************************************************************/
89: @Nonnull
90: public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Object role)
91: {
92: Parameters.mustNotBeArrayOrCollection(owner, PARAM_OWNER);
93: Parameters.mustNotBeArrayOrCollection(role, PARAM_ROLE);
94: return of(owner, r(role));
95: }
96:
97: /***********************************************************************************************************************************************************
98: * {@return a new instance given an owner and multiple roles}.
99: * @param owner the owner
100: * @param roles roles or {@link it.tidalwave.util.RoleFactory} instances
101: * @since 3.2-ALPHA-1
102: * @since 3.2-ALPHA-3 (refactored)
103: **********************************************************************************************************************************************************/
104: @Nonnull
105: public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
106: {
107: Parameters.mustNotBeArrayOrCollection(owner, PARAM_OWNER);
108: return new DefaultPresentationModel(owner, roles);
109: }
110:
111: /***********************************************************************************************************************************************************
112: * {@return an empty instance (no roles, with the exception of a dummy {@link Displayable})}.
113: * @since 3.2-ALPHA-3
114: **********************************************************************************************************************************************************/
115: @Nonnull
116: public static PresentationModel empty()
117: {
118: // TODO: cache a singleton, but don't do eager initialization (e.g. a final static), as it would deadlock
119: return of("", Displayable.of("<empty presentation model>"));
120: }
121:
122: /***********************************************************************************************************************************************************
123: * {@return a new instance from an owner which might have the {@link Presentable} role}. If it is present, it is called to create the
124: * {@code PresentationModel}; otherwise a default one is created. Additional roles are added.
125: * @param owner the owner
126: * @param roles roles or {@link it.tidalwave.util.RoleFactory} instances
127: * @since 3.2-ALPHA-8
128: **********************************************************************************************************************************************************/
129: @API(status = EXPERIMENTAL) // TODO: perhaps it could be merged to of().
130: @Nonnull
131: public static PresentationModel ofMaybePresentable (@Nonnull final As owner, @Nonnull final Collection<Object> roles)
132: {
133: Parameters.mustNotBeArrayOrCollection(owner, PARAM_OWNER);
134: return owner.maybeAs(_Presentable_)
135: .map(p -> p.createPresentationModel(roles))
136: .orElseGet(() -> of(owner, roles));
137: }
138:
139: /***********************************************************************************************************************************************************
140: * {@return a new instance from an owner which might have the {@link Presentable} role}. If it is present, it is called to create the
141: * {@code PresentationModel}; otherwise a default one is created.
142: * @param owner the owner
143: * @since 3.2-ALPHA-8
144: **********************************************************************************************************************************************************/
145: @API(status = EXPERIMENTAL) // TODO: perhaps it could be merged to of().
146: @Nonnull
147: public static PresentationModel ofMaybePresentable (@Nonnull final As owner)
148: {
149: return ofMaybePresentable(owner, Collections.emptyList());
150: }
151: }