Skip to contentMethod: setDisplayNames(Map)
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:
28: import jakarta.annotation.Nonnull;
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.ui.core.role.MutableDisplayable;
37: import lombok.experimental.Delegate;
38:
39: /***************************************************************************************************************************************************************
40: *
41: * A default implementation of {@link MutableDisplayable} 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: @SuppressWarnings("this-escape")
51: public class DefaultMutableDisplayable implements MutableDisplayable
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: @Delegate
62: private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
63:
64: @Delegate
65: private final MutableListenerSupport mls = new MutableListenerSupport(pcs);
66:
67: private final Locale defaultLocale = Locale.ENGLISH;
68:
69: /***********************************************************************************************************************************************************
70: * Creates an instance with an initial given display name in {@code Locale.ENGLISH}.
71: *
72: * @param displayName the display name
73: **********************************************************************************************************************************************************/
74: public DefaultMutableDisplayable (@Nonnull final String displayName)
75: {
76: this(displayName, "???");
77: }
78:
79: /***********************************************************************************************************************************************************
80: * Creates an instance with an initial given display name in {@code Locale.ENGLISH} and an explicit identifier for
81: * {@code toString()}.
82: *
83: * @param displayName the display name
84: * @param toStringName the name to be rendered when {@code toString()} is called
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: * {@inheritDoc}
94: **********************************************************************************************************************************************************/
95: @Override @Nonnull
96: public String getDisplayName()
97: {
98: return getDisplayName(defaultLocale);
99: }
100:
101: /***********************************************************************************************************************************************************
102: * {@inheritDoc}
103: **********************************************************************************************************************************************************/
104: @Override @Nonnull
105: public String getDisplayName (@Nonnull final Locale locale)
106: {
107: return displayNameMap.get(locale);
108: }
109:
110: /***********************************************************************************************************************************************************
111: * {@inheritDoc}
112: **********************************************************************************************************************************************************/
113: @Override @Nonnull
114: public SortedSet<Locale> getLocales()
115: {
116: return new TreeSet<>(displayNameMap.keySet());
117: }
118:
119: /***********************************************************************************************************************************************************
120: * {@inheritDoc}
121: **********************************************************************************************************************************************************/
122: @Override @Nonnull
123: public Map<Locale, String> getDisplayNames()
124: {
125: return Collections.unmodifiableMap(displayNameMap);
126: }
127:
128: /***********************************************************************************************************************************************************
129: * {@inheritDoc}
130: **********************************************************************************************************************************************************/
131: @Override
132: public void setDisplayName (@Nonnull final String displayName)
133: {
134: final var oldDisplayName = getDisplayName(defaultLocale);
135: setDisplayName(displayName, defaultLocale);
136: pcs.firePropertyChange(PROP_DISPLAY_NAME, oldDisplayName, displayName);
137: }
138:
139: /***********************************************************************************************************************************************************
140: * {@inheritDoc}
141: **********************************************************************************************************************************************************/
142: @Override
143: public void setDisplayName (@Nonnull final String displayName, @Nonnull final Locale locale)
144: {
145: final Map<Locale, String> oldDisplayNameMap = new HashMap<>(displayNameMap);
146: displayNameMap.put(locale, displayName);
147: pcs.firePropertyChange(PROP_DISPLAY_NAMES, oldDisplayNameMap, displayNameMap);
148: }
149:
150: /***********************************************************************************************************************************************************
151: * {@inheritDoc}
152: **********************************************************************************************************************************************************/
153: @Override
154: public void setDisplayNames (@Nonnull final Map<Locale, String> displayNames)
155: {
156: final Map<Locale, String> oldDisplayNameMap = new HashMap<>(displayNameMap);
157: displayNameMap.putAll(displayNames);
158: pcs.firePropertyChange(PROP_DISPLAY_NAMES, oldDisplayNameMap, displayNameMap);
159: }
160:
161: /***********************************************************************************************************************************************************
162: * {@inheritDoc}
163: **********************************************************************************************************************************************************/
164: @Override @Nonnull
165: public String toString()
166: {
167: return String.format("%s@%x$MutableDisplayable[]", toStringName, System.identityHashCode(this));
168: }
169: }