PresentationModel.java

  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. import jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import java.util.Collections;
  30. import it.tidalwave.util.As;
  31. import it.tidalwave.util.NamedCallback;
  32. import it.tidalwave.util.Parameters;
  33. import it.tidalwave.role.SimpleComposite;
  34. import it.tidalwave.ui.core.Mutable;
  35. import it.tidalwave.ui.core.role.impl.DefaultPresentationModel;
  36. import org.apiguardian.api.API;
  37. import static it.tidalwave.util.Parameters.r;
  38. import static it.tidalwave.ui.core.role.Presentable._Presentable_;
  39. import static org.apiguardian.api.API.Status.EXPERIMENTAL;

  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.  * @since       2.0-ALPHA-1
  47.  * @author      Fabrizio Giudici
  48.  *
  49.  **************************************************************************************************************************************************************/
  50. public interface PresentationModel extends As, Mutable
  51.   {
  52.     public static final Class<PresentationModel> _PresentationModel_ = PresentationModel.class;

  53.     public static final As.Type<SimpleComposite<PresentationModel>> _SimpleCompositeOfPresentationModel_ = As.type(SimpleComposite.class);

  54.     public static final String PROPERTY_CHILDREN = "children";

  55.     /** 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
  56.         {@link #dispose()} is called. */
  57.     public static final String CALLBACK_DISPOSE = "dispose";

  58.     /***********************************************************************************************************************************************************
  59.      * Disposes this object.
  60.      **********************************************************************************************************************************************************/
  61.     public void dispose();

  62.     /***********************************************************************************************************************************************************
  63.      * {@return a new instance given an owner and no roles}.
  64.      * @param   owner   the owner
  65.      * @since           3.2-ALPHA-3
  66.      **********************************************************************************************************************************************************/
  67.     @Nonnull
  68.     public static PresentationModel of (@Nonnull final Object owner)
  69.       {
  70.         Parameters.mustNotBeArrayOrCollection(owner, "owner");
  71.         return of(owner, Collections.emptyList());
  72.       }

  73.     /***********************************************************************************************************************************************************
  74.      * {@return a new instance given an owner and a single role}.
  75.      * @param   owner   the owner
  76.      * @param   role    the role (or a {@link it.tidalwave.util.RoleFactory})
  77.      * @since           3.2-ALPHA-3
  78.      **********************************************************************************************************************************************************/
  79.     @Nonnull
  80.     public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Object role)
  81.       {
  82.         Parameters.mustNotBeArrayOrCollection(owner, "owner");
  83.         Parameters.mustNotBeArrayOrCollection(role, "role");
  84.         return of(owner, r(role));
  85.       }

  86.     /***********************************************************************************************************************************************************
  87.      * {@return a new instance given an owner and multiple roles}.
  88.      * @param   owner   the owner
  89.      * @param   roles   roles or {@link it.tidalwave.util.RoleFactory} instances
  90.      * @since           3.2-ALPHA-1
  91.      * @since           3.2-ALPHA-3 (refactored)
  92.      **********************************************************************************************************************************************************/
  93.     @Nonnull
  94.     public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
  95.       {
  96.         Parameters.mustNotBeArrayOrCollection(owner, "owner");
  97.         return new DefaultPresentationModel(owner, roles);
  98.       }

  99.     /***********************************************************************************************************************************************************
  100.      * {@return an empty instance (no roles, with the exception of a dummy {@link Displayable})}.
  101.      * @since           3.2-ALPHA-3
  102.      **********************************************************************************************************************************************************/
  103.     @Nonnull
  104.     public static PresentationModel empty()
  105.       {
  106.         // TODO: cache a singleton, but don't do eager initialization (e.g. a final static), as it would deadlock
  107.         return of("", Displayable.of("<empty presentation model>"));
  108.       }

  109.     /***********************************************************************************************************************************************************
  110.      * {@return a new instance from an owner which might have the {@link Presentable} role}. If it is present, it is called to create the
  111.      * {@code PresentationModel}; otherwise a default one is created. Additional roles are added.
  112.      * @param   owner   the owner
  113.      * @param   roles   roles or {@link it.tidalwave.util.RoleFactory} instances
  114.      * @since           3.2-ALPHA-8
  115.      **********************************************************************************************************************************************************/
  116.     @API(status = EXPERIMENTAL) // TODO: perhaps it could be merged to of().
  117.     @Nonnull
  118.     public static PresentationModel ofMaybePresentable (@Nonnull final As owner, @Nonnull final Collection<Object> roles)
  119.       {
  120.         Parameters.mustNotBeArrayOrCollection(owner, "owner");
  121.         return owner.maybeAs(_Presentable_)
  122.                     .map(p -> p.createPresentationModel(roles))
  123.                     .orElseGet(() -> of(owner, roles));
  124.       }

  125.     /***********************************************************************************************************************************************************
  126.      * {@return a new instance from an owner which might have the {@link Presentable} role}. If it is present, it is called to create the
  127.      * {@code PresentationModel}; otherwise a default one is created.
  128.      * @param   owner   the owner
  129.      * @since           3.2-ALPHA-8
  130.      **********************************************************************************************************************************************************/
  131.     @API(status = EXPERIMENTAL) // TODO: perhaps it could be merged to of().
  132.     @Nonnull
  133.     public static PresentationModel ofMaybePresentable (@Nonnull final As owner)
  134.       {
  135.         return ofMaybePresentable(owner, Collections.emptyList());
  136.       }
  137.   }