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.Getter;
  37. import lombok.Setter;
  38. import lombok.experimental.Delegate;
  39. import lombok.extern.slf4j.Slf4j;
  40. import static it.tidalwave.util.ShortNames.shortId;

  41. /***************************************************************************************************************************************************************
  42.  *
  43.  * A default implementation of {@link PresentationModel}.
  44.  *
  45.  * @author  Fabrizio Giudici
  46.  *
  47.  **************************************************************************************************************************************************************/
  48. @Slf4j @SuppressWarnings("this-escape")
  49. public class DefaultPresentationModel implements PresentationModel
  50.   {
  51.     @Getter @Setter
  52.     private static boolean verboseToString = Boolean.getBoolean(P_VERBOSE_TOSTRING);

  53.     @Nonnull
  54.     private final Object owner;

  55.     @Delegate
  56.     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

  57.     @Delegate
  58.     private final MutableListenerSupport mls = new MutableListenerSupport(pcs);

  59.     private final As as;

  60.     /***********************************************************************************************************************************************************
  61.      *
  62.      **********************************************************************************************************************************************************/
  63.     public DefaultPresentationModel (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
  64.       {
  65.         this.owner = owner;
  66.         as = As.forObject(owner, roles);
  67.       }

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

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

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

  89.         if (t.isPresent())
  90.           {
  91.             return t;
  92.           }

  93.         if (owner instanceof As)
  94.           {
  95.             try
  96.               {
  97.                 final var role = ((As)owner).as(roleType);

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

  108.         return Optional.empty();
  109.       }

  110.     /***********************************************************************************************************************************************************
  111.      * {@inheritDoc}
  112.      **********************************************************************************************************************************************************/
  113.     @Override @Nonnull
  114.     public <T> Collection<T> asMany (@Nonnull final Class<? extends T> roleType)
  115.       {
  116.         final Collection<T> result = as.asMany(roleType);

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

  123.         if (owner instanceof As)
  124.           {
  125.             result.addAll(((As)owner).asMany(roleType));
  126.           }

  127.         return result;
  128.       }

  129.     /***********************************************************************************************************************************************************
  130.      * {@inheritDoc}
  131.      **********************************************************************************************************************************************************/
  132.     @Override
  133.     public void dispose()
  134.       {
  135.         for (final var listener : pcs.getPropertyChangeListeners().clone())
  136.           {
  137.             pcs.removePropertyChangeListener(listener);
  138.           }

  139.         mls.removeAllListeners();
  140.         asMany(NamedCallback.class).stream()
  141.                                    .filter(c -> c.getName().equals(CALLBACK_DISPOSE))
  142.                                    .forEach(callback -> wrap(callback, "While calling 'dispose' callbacks"));
  143.       }

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

  158.     /***********************************************************************************************************************************************************
  159.      * {@inheritDoc}
  160.      * This is unavoidably called by TreeCell renderers, even though the value is later overridden; so this method should be really fast.
  161.      **********************************************************************************************************************************************************/
  162.     @Override @Nonnull
  163.     public String toString()
  164.       {
  165.         return "DefaultPresentationModel(" + (verboseToString ? owner : shortId(owner)) + ")";
  166.       }
  167.   }