DefaultMutableDisplayable.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.Collections;
  30. import java.util.HashMap;
  31. import java.util.Locale;
  32. import java.util.Map;
  33. import java.util.SortedSet;
  34. import java.util.TreeSet;
  35. import it.tidalwave.ui.core.role.MutableDisplayable;
  36. import lombok.experimental.Delegate;

  37. /***************************************************************************************************************************************************************
  38.  *
  39.  * A default implementation of {@link MutableDisplayable} starting which a single display name in
  40.  * {@code Locale.ENGLISH} language.
  41.  *
  42.  * This is no more a public class; use
  43.  *
  44.  * @author Fabrizio Giudici
  45.  * @it.tidalwave.javadoc.stable
  46.  *
  47.  **************************************************************************************************************************************************************/
  48. @SuppressWarnings("this-escape")
  49. public class DefaultMutableDisplayable implements MutableDisplayable
  50.   {
  51.     // private static final long serialVersionUID = 45345436345634734L;

  52.     @Nonnull
  53.     private final String toStringName;

  54.     @Nonnull
  55.     private final Map<Locale, String> displayNameMap = new HashMap<>();

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

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

  60.     private final Locale defaultLocale = Locale.ENGLISH;

  61.     /***********************************************************************************************************************************************************
  62.      * Creates an instance with an initial given display name in {@code Locale.ENGLISH}.
  63.      *
  64.      * @param  displayName   the display name
  65.      **********************************************************************************************************************************************************/
  66.     public DefaultMutableDisplayable (@Nonnull final String displayName)
  67.       {
  68.         this(displayName, "???");
  69.       }

  70.     /***********************************************************************************************************************************************************
  71.      * Creates an instance with an initial given display name in {@code Locale.ENGLISH} and an explicit identifier for
  72.      * {@code toString()}.
  73.      *
  74.      * @param  displayName   the display name
  75.      * @param  toStringName  the name to be rendered when {@code toString()} is called
  76.      **********************************************************************************************************************************************************/
  77.     public DefaultMutableDisplayable (@Nonnull final String displayName, @Nonnull final String toStringName)
  78.       {
  79.         this.toStringName = toStringName;
  80.         displayNameMap.put(defaultLocale, displayName);
  81.       }

  82.     /***********************************************************************************************************************************************************
  83.      * {@inheritDoc}
  84.      **********************************************************************************************************************************************************/
  85.     @Override @Nonnull
  86.     public String getDisplayName()
  87.       {
  88.         return getDisplayName(defaultLocale);
  89.       }

  90.     /***********************************************************************************************************************************************************
  91.      * {@inheritDoc}
  92.      **********************************************************************************************************************************************************/
  93.     @Override @Nonnull
  94.     public String getDisplayName (@Nonnull final Locale locale)
  95.       {
  96.         return displayNameMap.get(locale);
  97.       }

  98.     /***********************************************************************************************************************************************************
  99.      * {@inheritDoc}
  100.      **********************************************************************************************************************************************************/
  101.     @Override @Nonnull
  102.     public SortedSet<Locale> getLocales()
  103.       {
  104.         return new TreeSet<>(displayNameMap.keySet());
  105.       }

  106.     /***********************************************************************************************************************************************************
  107.      * {@inheritDoc}
  108.      **********************************************************************************************************************************************************/
  109.     @Override @Nonnull
  110.     public Map<Locale, String> getDisplayNames()
  111.       {
  112.         return Collections.unmodifiableMap(displayNameMap);
  113.       }

  114.     /***********************************************************************************************************************************************************
  115.      * {@inheritDoc}
  116.      **********************************************************************************************************************************************************/
  117.     @Override
  118.     public void setDisplayName (@Nonnull final String displayName)
  119.       {
  120.         final var oldDisplayName = getDisplayName(defaultLocale);
  121.         setDisplayName(displayName, defaultLocale);
  122.         pcs.firePropertyChange(PROP_DISPLAY_NAME, oldDisplayName, displayName);
  123.       }

  124.     /***********************************************************************************************************************************************************
  125.      * {@inheritDoc}
  126.      **********************************************************************************************************************************************************/
  127.     @Override
  128.     public void setDisplayName (@Nonnull final String displayName, @Nonnull final Locale locale)
  129.       {
  130.         final Map<Locale, String> oldDisplayNameMap = new HashMap<>(displayNameMap);
  131.         displayNameMap.put(locale, displayName);
  132.         pcs.firePropertyChange(PROP_DISPLAY_NAMES, oldDisplayNameMap, displayNameMap);
  133.       }

  134.     /***********************************************************************************************************************************************************
  135.      * {@inheritDoc}
  136.      **********************************************************************************************************************************************************/
  137.     @Override
  138.     public void setDisplayNames (@Nonnull final Map<Locale, String> displayNames)
  139.       {
  140.         final Map<Locale, String> oldDisplayNameMap = new HashMap<>(displayNameMap);
  141.         displayNameMap.putAll(displayNames);
  142.         pcs.firePropertyChange(PROP_DISPLAY_NAMES, oldDisplayNameMap, displayNameMap);
  143.       }

  144.     /***********************************************************************************************************************************************************
  145.      * {@inheritDoc}
  146.      **********************************************************************************************************************************************************/
  147.     @Override @Nonnull
  148.     public String toString()
  149.       {
  150.         return String.format("%s@%x$MutableDisplayable[]", toStringName, System.identityHashCode(this));
  151.       }
  152.   }