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 28 import javax.annotation.Nonnull; 29 import java.beans.PropertyChangeListener; 30 import java.beans.PropertyChangeSupport; 31 import java.util.Collections; 32 import java.util.HashMap; 33 import java.util.Locale; 34 import java.util.Map; 35 import java.util.SortedSet; 36 import java.util.TreeSet; 37 import it.tidalwave.role.ui.MutableLocalizedDisplayable; 38 39 /*************************************************************************************************************************************************************** 40 * 41 * A default implementation of {@link MutableLocalizedDisplayable} starting which a single display name in 42 * {@code Locale.ENGLISH} language. 43 * 44 * This is no more a public class; use 45 * 46 * @author Fabrizio Giudici 47 * @it.tidalwave.javadoc.stable 48 * 49 **************************************************************************************************************************************************************/ 50 public class DefaultMutableDisplayable implements MutableLocalizedDisplayable 51 { 52 // private static final long serialVersionUID = 45345436345634734L; 53 54 @Nonnull 55 private final String toStringName; 56 57 @Nonnull 58 private final Map<Locale, String> displayNameMap = new HashMap<>(); 59 60 private final PropertyChangeSupport pcs = new PropertyChangeSupport(this); 61 62 private final Locale defaultLocale = Locale.ENGLISH; 63 64 /*********************************************************************************************************************************************************** 65 * Creates an instance with an initial given display name in {@code Locale.ENGLISH}. 66 * 67 * @param displayName the display name 68 **********************************************************************************************************************************************************/ 69 public DefaultMutableDisplayable (@Nonnull final String displayName) 70 { 71 this(displayName, "???"); 72 } 73 74 /*********************************************************************************************************************************************************** 75 * Creates an instance with an initial given display name in {@code Locale.ENGLISH} and an explicit identifier for 76 * {@code toString()}. 77 * 78 * @param displayName the display name 79 * @param toStringName the name to be rendered when {@code toString()} is called 80 **********************************************************************************************************************************************************/ 81 public DefaultMutableDisplayable (@Nonnull final String displayName, @Nonnull final String toStringName) 82 { 83 this.toStringName = toStringName; 84 displayNameMap.put(defaultLocale, displayName); 85 } 86 87 /*********************************************************************************************************************************************************** 88 * {@inheritDoc} 89 **********************************************************************************************************************************************************/ 90 @Override @Nonnull 91 public String getDisplayName() 92 { 93 return getDisplayName(defaultLocale); 94 } 95 96 /*********************************************************************************************************************************************************** 97 * {@inheritDoc} 98 **********************************************************************************************************************************************************/ 99 @Override @Nonnull 100 public String getDisplayName (@Nonnull final Locale locale) 101 { 102 return displayNameMap.get(locale); 103 } 104 105 /*********************************************************************************************************************************************************** 106 * {@inheritDoc} 107 **********************************************************************************************************************************************************/ 108 @Override @Nonnull 109 public SortedSet<Locale> getLocales() 110 { 111 return new TreeSet<>(displayNameMap.keySet()); 112 } 113 114 /*********************************************************************************************************************************************************** 115 * {@inheritDoc} 116 **********************************************************************************************************************************************************/ 117 @Override @Nonnull 118 public Map<Locale, String> getDisplayNames() 119 { 120 return Collections.unmodifiableMap(displayNameMap); 121 } 122 123 /*********************************************************************************************************************************************************** 124 * {@inheritDoc} 125 **********************************************************************************************************************************************************/ 126 @Override 127 public void setDisplayName (@Nonnull final String displayName) 128 { 129 final var oldDisplayName = getDisplayName(defaultLocale); 130 setDisplayName(displayName, defaultLocale); 131 pcs.firePropertyChange(PROP_DISPLAY_NAME, oldDisplayName, displayName); 132 } 133 134 /*********************************************************************************************************************************************************** 135 * {@inheritDoc} 136 **********************************************************************************************************************************************************/ 137 @Override 138 public void setDisplayName (@Nonnull final String displayName, @Nonnull final Locale locale) 139 { 140 final Map<Locale, String> oldDisplayNameMap = new HashMap<>(displayNameMap); 141 displayNameMap.put(locale, displayName); 142 pcs.firePropertyChange(PROP_DISPLAY_NAMES, oldDisplayNameMap, displayNameMap); 143 } 144 145 /*********************************************************************************************************************************************************** 146 * {@inheritDoc} 147 **********************************************************************************************************************************************************/ 148 @Override 149 public void setDisplayNames (@Nonnull final Map<Locale, String> displayNames) 150 { 151 final Map<Locale, String> oldDisplayNameMap = new HashMap<>(displayNameMap); 152 displayNameMap.putAll(displayNames); 153 pcs.firePropertyChange(PROP_DISPLAY_NAMES, oldDisplayNameMap, displayNameMap); 154 } 155 156 /*********************************************************************************************************************************************************** 157 * {@inheritDoc} 158 **********************************************************************************************************************************************************/ 159 @Override 160 public void addPropertyChangeListener (@Nonnull final PropertyChangeListener listener) 161 { 162 pcs.addPropertyChangeListener(listener); 163 } 164 165 /*********************************************************************************************************************************************************** 166 * {@inheritDoc} 167 **********************************************************************************************************************************************************/ 168 @Override 169 public void removePropertyChangeListener (@Nonnull final PropertyChangeListener listener) 170 { 171 pcs.removePropertyChangeListener(listener); 172 } 173 174 /*********************************************************************************************************************************************************** 175 * {@inheritDoc} 176 **********************************************************************************************************************************************************/ 177 @Override @Nonnull 178 public String toString() 179 { 180 return String.format("%s@%x$MutableDisplayable[]", toStringName, System.identityHashCode(this)); 181 } 182 }