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