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.ui.core.Mutable;
  31. import it.tidalwave.ui.core.role.impl.DefaultPresentationModel;
  32. import org.apiguardian.api.API;
  33. import it.tidalwave.util.As;
  34. import it.tidalwave.util.NamedCallback;
  35. import it.tidalwave.util.Parameters;
  36. import it.tidalwave.role.Aggregate;
  37. import it.tidalwave.role.SimpleComposite;
  38. import static it.tidalwave.ui.core.role.Presentable._Presentable_;
  39. import static org.apiguardian.api.API.Status.EXPERIMENTAL;
  40. import static it.tidalwave.util.Parameters.r;

  41. /***************************************************************************************************************************************************************
  42.  *
  43.  * TODO: As the NetBeans Node, it should allow children, have event listeners for children added/removed/changed.
  44.  * This class so becomes the true M in MVC.
  45.  *
  46.  * @stereotype  Role
  47.  * @since       2.0-ALPHA-1
  48.  * @author      Fabrizio Giudici
  49.  *
  50.  **************************************************************************************************************************************************************/
  51. public interface PresentationModel extends As, Mutable
  52.   {
  53.     /** Shortcut for {@link it.tidalwave.util.As}. */
  54.     public static final Class<PresentationModel> _PresentationModel_ = PresentationModel.class;

  55.     /** Shortcut for {@link it.tidalwave.util.As}. */
  56.     public static final As.Type<SimpleComposite<PresentationModel>> _CompositeOfPresentationModel_ = As.type(SimpleComposite.class);

  57.     /** Shortcut for {@link it.tidalwave.util.As}. */
  58.     public static final As.Type<Aggregate<PresentationModel>> _AggregateOfPresentationModel_ = new As.Type<>(Aggregate.class);

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

  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 P_VERBOSE_TOSTRING = PresentationModel.class.getCanonicalName() + ".verboseToString";

  64.     public static final String PARAM_OWNER = "owner";
  65.     public static final String PARAM_ROLE = "role";

  66.     /***********************************************************************************************************************************************************
  67.      * Disposes this object.
  68.      **********************************************************************************************************************************************************/
  69.     public void dispose();

  70.     /***********************************************************************************************************************************************************
  71.      * {@return a new instance given an owner and no roles}.
  72.      * @param   owner   the owner
  73.      * @since           3.2-ALPHA-3
  74.      **********************************************************************************************************************************************************/
  75.     @Nonnull
  76.     public static PresentationModel of (@Nonnull final Object owner)
  77.       {
  78.         Parameters.mustNotBeArrayOrCollection(owner, PARAM_OWNER);
  79.         return of(owner, Collections.emptyList());
  80.       }

  81.     /***********************************************************************************************************************************************************
  82.      * {@return a new instance given an owner and a single role}.
  83.      * @param   owner   the owner
  84.      * @param   role    the role (or a {@link it.tidalwave.util.RoleFactory})
  85.      * @since           3.2-ALPHA-3
  86.      **********************************************************************************************************************************************************/
  87.     @Nonnull
  88.     public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Object role)
  89.       {
  90.         Parameters.mustNotBeArrayOrCollection(owner, PARAM_OWNER);
  91.         Parameters.mustNotBeArrayOrCollection(role, PARAM_ROLE);
  92.         return of(owner, r(role));
  93.       }

  94.     /***********************************************************************************************************************************************************
  95.      * {@return a new instance given an owner and multiple roles}.
  96.      * @param   owner   the owner
  97.      * @param   roles   roles or {@link it.tidalwave.util.RoleFactory} instances
  98.      * @since           3.2-ALPHA-1
  99.      * @since           3.2-ALPHA-3 (refactored)
  100.      **********************************************************************************************************************************************************/
  101.     @Nonnull
  102.     public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
  103.       {
  104.         Parameters.mustNotBeArrayOrCollection(owner, PARAM_OWNER);
  105.         return new DefaultPresentationModel(owner, roles);
  106.       }

  107.     /***********************************************************************************************************************************************************
  108.      * {@return an empty instance (no roles, with the exception of a dummy {@link Displayable})}.
  109.      * @since           3.2-ALPHA-3
  110.      **********************************************************************************************************************************************************/
  111.     @Nonnull
  112.     public static PresentationModel empty()
  113.       {
  114.         // TODO: cache a singleton, but don't do eager initialization (e.g. a final static), as it would deadlock
  115.         return of("", Displayable.of("<empty presentation model>"));
  116.       }

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

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

  145.     /***********************************************************************************************************************************************************
  146.      * Sets the verbose mode for {@link java.lang.Object#toString} implementations of {@code PresentationModel}. Looking at the implementation of
  147.      * {@link javafx.scene.control.cell.DefaultTreeCell}, the code always calls {@code toString()} during an update, even though the text value is later
  148.      * overridden; hence, this method should be as fast as possible. By default, the shortened class name and system id of the owner object is returned;
  149.      * set verbosity to have the owner object {@code toString()} called instead.
  150.      * It is also possible to set system the property {@code it.tidalwave.ui.core.role.PresentationModel.verboseToString}.
  151.      * @param           verbose   the verbosity
  152.      * @since           2.0-ALPHA-4
  153.      * @see             #isVerboseToString()
  154.      **********************************************************************************************************************************************************/
  155.     public static void setVerboseToString (final boolean verbose)
  156.       {
  157.         DefaultPresentationModel.setVerboseToString(verbose);
  158.       }

  159.     /***********************************************************************************************************************************************************
  160.      * {@return the verbose mode for {@link java.lang.Object#toString} implementations of {@code PresentationModel}}.
  161.      * @since           2.0-ALPHA-4
  162.      * @see             #setVerboseToString(boolean)
  163.      **********************************************************************************************************************************************************/
  164.     public static boolean isVerboseToString()
  165.       {
  166.         return DefaultPresentationModel.isVerboseToString();
  167.       }
  168.   }