PresentationModel.java

/*
 * *************************************************************************************************************************************************************
 *
 * SteelBlue: DCI User Interfaces
 * http://tidalwave.it/projects/steelblue
 *
 * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *************************************************************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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
 * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
 *
 * *************************************************************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/steelblue-src
 * git clone https://github.com/tidalwave-it/steelblue-src
 *
 * *************************************************************************************************************************************************************
 */
package it.tidalwave.ui.core.role;

import jakarta.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import it.tidalwave.util.As;
import it.tidalwave.util.NamedCallback;
import it.tidalwave.util.Parameters;
import it.tidalwave.role.SimpleComposite;
import it.tidalwave.ui.core.Mutable;
import it.tidalwave.ui.core.role.impl.DefaultPresentationModel;
import org.apiguardian.api.API;
import static it.tidalwave.util.Parameters.r;
import static it.tidalwave.ui.core.role.Presentable._Presentable_;
import static org.apiguardian.api.API.Status.EXPERIMENTAL;

/***************************************************************************************************************************************************************
 *
 * TODO: As the NetBeans Node, it should allow children, have event listeners for children added/removed/changed.
 * This class so becomes the true M in MVC.
 *
 * @stereotype  Role
 * @since       2.0-ALPHA-1
 * @author      Fabrizio Giudici
 *
 **************************************************************************************************************************************************************/
public interface PresentationModel extends As, Mutable
  {
    public static final Class<PresentationModel> _PresentationModel_ = PresentationModel.class;

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

    public static final String PROPERTY_CHILDREN = "children";

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

    /***********************************************************************************************************************************************************
     * Disposes this object.
     **********************************************************************************************************************************************************/
    public void dispose();

    /***********************************************************************************************************************************************************
     * {@return a new instance given an owner and no roles}.
     * @param   owner   the owner
     * @since           3.2-ALPHA-3
     **********************************************************************************************************************************************************/
    @Nonnull
    public static PresentationModel of (@Nonnull final Object owner)
      {
        Parameters.mustNotBeArrayOrCollection(owner, "owner");
        return of(owner, Collections.emptyList());
      }

    /***********************************************************************************************************************************************************
     * {@return a new instance given an owner and a single role}.
     * @param   owner   the owner
     * @param   role    the role (or a {@link it.tidalwave.util.RoleFactory})
     * @since           3.2-ALPHA-3
     **********************************************************************************************************************************************************/
    @Nonnull
    public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Object role)
      {
        Parameters.mustNotBeArrayOrCollection(owner, "owner");
        Parameters.mustNotBeArrayOrCollection(role, "role");
        return of(owner, r(role));
      }

    /***********************************************************************************************************************************************************
     * {@return a new instance given an owner and multiple roles}.
     * @param   owner   the owner
     * @param   roles   roles or {@link it.tidalwave.util.RoleFactory} instances
     * @since           3.2-ALPHA-1
     * @since           3.2-ALPHA-3 (refactored)
     **********************************************************************************************************************************************************/
    @Nonnull
    public static PresentationModel of (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
      {
        Parameters.mustNotBeArrayOrCollection(owner, "owner");
        return new DefaultPresentationModel(owner, roles);
      }

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

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

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