diff --git a/src/main/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBox.java b/src/main/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBox.java index 014fd97..a1d6a4e 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBox.java +++ b/src/main/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBox.java @@ -42,11 +42,45 @@ public class LocaleComboBox extends ComboBox { private static final String ITEM_LAYOUT_CLASS_NAME = "fc-locale-combo-box-item-layout"; private static final String ITEM_FLAG_CLASS_NAME = "fc-locale-combo-box-item-flag"; + /** + * Display mode representing the built-in display modes in LocaleComboBox + *

+ * These enums can be used in {@link #setDisplayMode(DisplayMode)} to easily switch between the + * built-in display modes. + */ + public enum DisplayMode { + + /** + * Default display mode. + *

+ * In this mode, the Locale names are displayed using the default locale's display format. + */ + DEFAULT, + + /** + * Selected display mode. + *

+ * In this mode, the Locale names are displayed using the formatting of the currently selected + * locale. + */ + SELECTED, + + /** + * Custom display mode. + *

+ * In this mode, the Locale names are displayed using the formatting of the specific locale set + * by {@link #setDisplayLocale(Locale)}. + */ + CUSTOM; + } + private DisplayMode displayMode = DisplayMode.DEFAULT; + private Locale customDisplayLocale = Locale.getDefault(); + /** * Creates a new instance of LocaleComboBox with all the installed locales. */ public LocaleComboBox() { - setItemLabelGenerator(Locale::getDisplayName); + setItemLabelGenerator(item -> item.getDisplayName(getLocaleForDisplay())); setRenderer(getLocaleRenderer()); addValueChangeListener(this::onValueChange); setItems( @@ -54,6 +88,31 @@ public LocaleComboBox() { .sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName())).toList()); } + /** + * Sets the display mode of the LocaleComboBox. + * + * @param displayMode the display mode to use + * + * @see DisplayMode + */ + public void setDisplayMode(DisplayMode displayMode) { + this.displayMode = displayMode; + } + + /** + * Sets the locale used for formatting Locale names when {@link DisplayMode#CUSTOM} mode is + * active. + *

+ * This locale determines how Locale names are formatted when {@link DisplayMode#CUSTOM} is + * selected as the display mode. If the display mode is any other than {@link DisplayMode#CUSTOM}, + * this setting is ignored. + * + * @param displayLocale the locale to use for formatting. + */ + public void setDisplayLocale(Locale displayLocale) { + this.customDisplayLocale = displayLocale == null ? Locale.getDefault() : displayLocale; + } + private LitRenderer getLocaleRenderer() { return LitRenderer .of( @@ -65,8 +124,23 @@ private LitRenderer getLocaleRenderer() { .withProperty("layoutClass", loc -> ITEM_LAYOUT_CLASS_NAME) .withProperty("flagClass", loc -> ITEM_FLAG_CLASS_NAME) .withProperty("countryCode", loc -> loc.getCountry().toLowerCase()) - .withProperty("countryName", loc -> loc.getDisplayCountry()) - .withProperty("displayName", loc -> loc.getDisplayName()); + .withProperty("countryName", loc -> loc.getDisplayCountry(getLocaleForDisplay())) + .withProperty("displayName", loc -> loc.getDisplayName(getLocaleForDisplay())); + } + + private Locale getLocaleForDisplay() { + + switch (displayMode) { + + case CUSTOM: + return customDisplayLocale; + + case SELECTED: + return this.getValue() == null ? Locale.getDefault() : this.getValue(); + + default: + return Locale.getDefault(); + } } private void onValueChange(ComponentValueChangeEvent, Locale> event) { diff --git a/src/test/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBoxDemo.java b/src/test/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBoxDemo.java index a0c67d4..b8877e9 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBoxDemo.java +++ b/src/test/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBoxDemo.java @@ -21,17 +21,59 @@ package com.flowingcode.vaadin.addons.localecombobox; import com.flowingcode.vaadin.addons.demo.DemoSource; +import com.vaadin.flow.component.Text; import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.component.html.Span; +import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment; +import com.vaadin.flow.component.orderedlayout.FlexComponent.JustifyContentMode; +import com.vaadin.flow.component.orderedlayout.HorizontalLayout; import com.vaadin.flow.router.PageTitle; import com.vaadin.flow.router.Route; +import java.util.Locale; @DemoSource -@PageTitle("LocaleComboBox Add-on Demo") +@PageTitle("Display modes") @SuppressWarnings("serial") @Route(value = "demo", layout = LocaleComboBoxDemoView.class) public class LocaleComboBoxDemo extends Div { public LocaleComboBoxDemo() { - add(new LocaleComboBox()); + + LocaleComboBox defaultDisplayLocale = new LocaleComboBox(); + LocaleComboBox koreanLocaleCombo = new LocaleComboBox(); + LocaleComboBox selectedLocaleCombo = new LocaleComboBox(); + + defaultDisplayLocale.setValue(Locale.ITALY); + + koreanLocaleCombo.setDisplayLocale(Locale.KOREA); + koreanLocaleCombo.setDisplayMode(LocaleComboBox.DisplayMode.CUSTOM); + koreanLocaleCombo.setValue(Locale.ITALY); + + selectedLocaleCombo.setDisplayMode(LocaleComboBox.DisplayMode.SELECTED); + selectedLocaleCombo.setValue(Locale.ITALY); + + // #if vaadin eq 0 + add(createHorizontalContainer("Default display mode (uses default locale):", defaultDisplayLocale), + createHorizontalContainer("Display locales with Korean locale:", koreanLocaleCombo), + createHorizontalContainer("Display locales with selected locale:", selectedLocaleCombo)); + // #endif + // show-source add(defaultDisplayLocale); + // show-source add(koreanLocaleCombo); + // show-source add(selectedLocaleCombo); } + + // #if vaadin eq 0 + private HorizontalLayout createHorizontalContainer(String title, LocaleComboBox combo) { + Span titleSpan = new Span(new Text(title)); + titleSpan.setWidth("300px"); + HorizontalLayout container = new HorizontalLayout(); + container.setWidthFull(); + container.setAlignItems(Alignment.CENTER); + container.setJustifyContentMode(JustifyContentMode.BETWEEN); + container.add(titleSpan, combo); + container.expand(combo); + return container; + } + // #endif + }