DefaultPresentationModel.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.impl;

  27. import jakarta.annotation.Nonnull;
  28. import java.beans.PropertyChangeSupport;
  29. import java.util.Collection;
  30. import java.util.Optional;
  31. import it.tidalwave.util.As;
  32. import it.tidalwave.util.AsException;
  33. import it.tidalwave.util.Callback;
  34. import it.tidalwave.util.NamedCallback;
  35. import it.tidalwave.ui.core.role.PresentationModel;
  36. import lombok.ToString;
  37. import lombok.experimental.Delegate;
  38. import lombok.extern.slf4j.Slf4j;

  39. /***************************************************************************************************************************************************************
  40.  *
  41.  * A default implementation of {@link PresentationModel}.
  42.  *
  43.  * @author  Fabrizio Giudici
  44.  *
  45.  **************************************************************************************************************************************************************/
  46. @ToString(exclude = {"as", "pcs", "mls"}) @Slf4j @SuppressWarnings("this-escape")
  47. public class DefaultPresentationModel implements PresentationModel
  48.   {
  49.     @Nonnull
  50.     private final Object owner;

  51.     @Delegate
  52.     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

  53.     @Delegate
  54.     private final MutableListenerSupport mls = new MutableListenerSupport(pcs);

  55.     private final As as;

  56.     /***********************************************************************************************************************************************************
  57.      *
  58.      **********************************************************************************************************************************************************/
  59.     public DefaultPresentationModel (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
  60.       {
  61.         this.owner = owner;
  62.         as = As.forObject(owner, roles);
  63.       }

  64.     /***********************************************************************************************************************************************************
  65.      * {@inheritDoc}
  66.      **********************************************************************************************************************************************************/
  67.     @Override @Nonnull
  68.     public <T> T as (@Nonnull final Class<? extends T> roleType)
  69.       {
  70.         return maybeAs(roleType).orElseThrow(() -> new AsException(roleType));
  71.       }

  72.     /***********************************************************************************************************************************************************
  73.      * {@inheritDoc}
  74.      **********************************************************************************************************************************************************/
  75.     @SuppressWarnings("ConstantValue")
  76.     @Override @Nonnull
  77.     public <T> Optional<T> maybeAs (@Nonnull final Class<? extends T> roleType)
  78.       {
  79.         // Undocumented feature: for instance Zephyr needs to fire property events
  80.         if (roleType.equals(PropertyChangeSupport.class))
  81.           {
  82.             return Optional.of(roleType.cast(pcs));
  83.           }

  84.         final Optional<T> t = as.maybeAs(roleType);

  85.         if (t.isPresent())
  86.           {
  87.             return t;
  88.           }

  89.         if (owner instanceof As)
  90.           {
  91.             try
  92.               {
  93.                 final var role = ((As)owner).as(roleType);

  94.                 if (role != null) // do check it for improper implementations or partial mocks
  95.                   {
  96.                     return Optional.of(role);
  97.                   }
  98.               }
  99.             catch (AsException ignore)
  100.               {
  101.                 // fallback
  102.               }
  103.           }

  104.         return Optional.empty();
  105.       }

  106.     /***********************************************************************************************************************************************************
  107.      * {@inheritDoc}
  108.      **********************************************************************************************************************************************************/
  109.     @Override @Nonnull
  110.     public <T> Collection<T> asMany (@Nonnull final Class<? extends T> roleType)
  111.       {
  112.         final Collection<T> result = as.asMany(roleType);

  113.         // The problem here is that we want only to add local roles in owner; but calling owner.as() will also
  114.         // find again the global roles that were discovered by AsSupport.
  115.         if (roleType.isAssignableFrom(owner.getClass()))
  116.           {
  117.             result.add(roleType.cast(owner));
  118.           }

  119.         if (owner instanceof As)
  120.           {
  121.             result.addAll(((As)owner).asMany(roleType));
  122.           }

  123.         return result;
  124.       }

  125.     /***********************************************************************************************************************************************************
  126.      * {@inheritDoc}
  127.      **********************************************************************************************************************************************************/
  128.     @Override
  129.     public void dispose()
  130.       {
  131.         for (final var listener : pcs.getPropertyChangeListeners().clone())
  132.           {
  133.             pcs.removePropertyChangeListener(listener);
  134.           }

  135.         mls.removeAllListeners();
  136.         asMany(NamedCallback.class).stream()
  137.                                    .filter(c -> c.getName().equals(CALLBACK_DISPOSE))
  138.                                    .forEach(callback -> wrap(callback, "While calling 'dispose' callbacks"));
  139.       }

  140.     /***********************************************************************************************************************************************************
  141.      *
  142.      **********************************************************************************************************************************************************/
  143.     private static void wrap (@Nonnull final Callback callback, @Nonnull final String logMessage)
  144.       {
  145.         try
  146.           {
  147.             callback.call();
  148.           }
  149.         catch (Throwable t)
  150.           {
  151.             log.error(logMessage, t);
  152.           }
  153.       }
  154.   }