DefaultMutableDisplayable.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 2024 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/thesefoolishthings-src
  22.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.role.ui.impl;

  27. import javax.annotation.Nonnull;
  28. import java.beans.PropertyChangeListener;
  29. import java.beans.PropertyChangeSupport;
  30. import java.util.Collections;
  31. import java.util.HashMap;
  32. import java.util.Locale;
  33. import java.util.Map;
  34. import java.util.SortedSet;
  35. import java.util.TreeSet;
  36. import it.tidalwave.role.ui.MutableLocalizedDisplayable;

  37. /***************************************************************************************************************************************************************
  38.  *
  39.  * A default implementation of {@link MutableLocalizedDisplayable} 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 MutableLocalizedDisplayable
  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.     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

  56.     private final Locale defaultLocale = Locale.ENGLISH;

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

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

  78.     /***********************************************************************************************************************************************************
  79.      * {@inheritDoc}
  80.      **********************************************************************************************************************************************************/
  81.     @Override @Nonnull
  82.     public String getDisplayName()
  83.       {
  84.         return getDisplayName(defaultLocale);
  85.       }

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

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

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

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

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

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

  140.     /***********************************************************************************************************************************************************
  141.      * {@inheritDoc}
  142.      **********************************************************************************************************************************************************/
  143.     @Override
  144.     public void addPropertyChangeListener (@Nonnull final PropertyChangeListener listener)
  145.       {
  146.         pcs.addPropertyChangeListener(listener);
  147.       }

  148.     /***********************************************************************************************************************************************************
  149.      * {@inheritDoc}
  150.      **********************************************************************************************************************************************************/
  151.     @Override
  152.     public void removePropertyChangeListener (@Nonnull final PropertyChangeListener listener)
  153.       {
  154.         pcs.removePropertyChangeListener(listener);
  155.       }

  156.     /***********************************************************************************************************************************************************
  157.      * {@inheritDoc}
  158.      **********************************************************************************************************************************************************/
  159.     @Override @Nonnull
  160.     public String toString()
  161.       {
  162.         return String.format("%s@%x$MutableDisplayable[]", toStringName, System.identityHashCode(this));
  163.       }
  164.   }