Skip to content

Commit 1e60851

Browse files
elPeirettimlopezFC
authored andcommitted
feat: add support for multiple display modes
1 parent ca84578 commit 1e60851

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

src/main/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBox.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,74 @@ public class LocaleComboBox extends ComboBox<Locale> {
4242
private static final String ITEM_LAYOUT_CLASS_NAME = "fc-locale-combo-box-item-layout";
4343
private static final String ITEM_FLAG_CLASS_NAME = "fc-locale-combo-box-item-flag";
4444

45+
/**
46+
* Constant for the default display mode.
47+
* <p>
48+
* In this mode, the Locale names are displayed using the default locale's display format.
49+
*/
50+
public static final int DISPLAY_DEFAULT = 0;
51+
/**
52+
* Constant for the locale-specific display mode.
53+
* <p>
54+
* In this mode, the Locale names are displayed using the formatting of the specific locale set by
55+
* {@link #setDisplayLocale(Locale)}.
56+
*/
57+
public static final int DISPLAY_CUSTOM = 1;
58+
/**
59+
* Constant for the selected display mode.
60+
* <p>
61+
* In this mode, the Locale names are displayed using the formatting of the currently selected
62+
* locale.
63+
*/
64+
public static final int DISPLAY_SELECTED = 2;
65+
66+
private int displayMode = DISPLAY_DEFAULT;
67+
private Locale customDisplayLocale = Locale.getDefault();
68+
4569
/**
4670
* Creates a new instance of LocaleComboBox with all the installed locales.
4771
*/
4872
public LocaleComboBox() {
49-
setItemLabelGenerator(Locale::getDisplayName);
73+
setItemLabelGenerator(item -> item.getDisplayName(getLocaleForDisplay()));
5074
setRenderer(getLocaleRenderer());
5175
addValueChangeListener(this::onValueChange);
5276
setItems(
5377
Arrays.stream(Locale.getAvailableLocales()).filter(loc -> loc.getCountry().length() == 2)
5478
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName())).toList());
5579
}
5680

81+
/**
82+
* Sets the display mode of the LocaleComboBox.
83+
* <p>
84+
* The display mode determines how the Locale names are presented in the combo box:
85+
* <ul>
86+
* <li>{@link #DISPLAY_DEFAULT} - Uses the default locale's display format to show Locale
87+
* names.</li>
88+
* <li>{@link #DISPLAY_CUSTOM} - Uses the specific locale (set by
89+
* {@link #setDisplayLocale(Locale)}) to format the Locale names.</li>
90+
* <li>{@link #DISPLAY_SELECTED} - Uses the currently selected locale to format the Locale
91+
* names.</li>
92+
* </ul>
93+
*
94+
* @param displayMode the display mode to use; must be one of {@link #DISPLAY_DEFAULT},
95+
* {@link #DISPLAY_CUSTOM}, or {@link #DISPLAY_SELECTED}.
96+
*/
97+
public void setDisplayMode(int displayMode) {
98+
this.displayMode = displayMode;
99+
}
100+
101+
/**
102+
* Sets the locale used for formatting Locale names when {@link #DISPLAY_LOCALE} mode is active.
103+
* <p>
104+
* This locale determines how Locale names are formatted when {@link #DISPLAY_LOCALE} is selected
105+
* as the display mode. If the display mode is {@link #DISPLAY_DEFAULT}, this setting is ignored.
106+
*
107+
* @param displayLocale the locale to use for formatting.
108+
*/
109+
public void setDisplayLocale(Locale displayLocale) {
110+
this.customDisplayLocale = displayLocale == null ? Locale.getDefault() : displayLocale;
111+
}
112+
57113
private LitRenderer<Locale> getLocaleRenderer() {
58114
return LitRenderer
59115
.<Locale>of(
@@ -65,8 +121,23 @@ private LitRenderer<Locale> getLocaleRenderer() {
65121
.withProperty("layoutClass", loc -> ITEM_LAYOUT_CLASS_NAME)
66122
.withProperty("flagClass", loc -> ITEM_FLAG_CLASS_NAME)
67123
.withProperty("countryCode", loc -> loc.getCountry().toLowerCase())
68-
.withProperty("countryName", loc -> loc.getDisplayCountry())
69-
.withProperty("displayName", loc -> loc.getDisplayName());
124+
.withProperty("countryName", loc -> loc.getDisplayCountry(getLocaleForDisplay()))
125+
.withProperty("displayName", loc -> loc.getDisplayName(getLocaleForDisplay()));
126+
}
127+
128+
private Locale getLocaleForDisplay() {
129+
130+
switch (displayMode) {
131+
132+
case DISPLAY_CUSTOM:
133+
return customDisplayLocale;
134+
135+
case DISPLAY_SELECTED:
136+
return this.getValue() == null ? Locale.getDefault() : this.getValue();
137+
138+
default:
139+
return Locale.getDefault();
140+
}
70141
}
71142

72143
private void onValueChange(ComponentValueChangeEvent<ComboBox<Locale>, Locale> event) {

0 commit comments

Comments
 (0)