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. public class DefaultMutableDisplayable implements MutableDisplayable
  49.   {
  50.     // private static final long serialVersionUID = 45345436345634734L;

  51.     @Nonnull
  52.     private final String toStringName;

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

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

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

  59.     private final Locale defaultLocale = Locale.ENGLISH;

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

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

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

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

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

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

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

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

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

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