Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.data.renderer.LitRenderer;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Optional;

/**
* Vaadin ComboBox extension that allows to choose between multiple locales.
Expand All @@ -41,9 +44,10 @@ public class LocaleComboBox extends ComboBox<Locale> {

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";
private static final String DEFAULT_FLAG_CODE = "un";

/**
* Display mode representing the built-in display modes in LocaleComboBox
* Display mode representing the built-in display modes in {@link LocaleComboBox}
* <p>
* These enums can be used in {@link #setDisplayMode(DisplayMode)} to easily switch between the
* built-in display modes.
Expand Down Expand Up @@ -73,19 +77,27 @@ public enum DisplayMode {
*/
CUSTOM;
}

private DisplayMode displayMode = DisplayMode.DEFAULT;
private Locale customDisplayLocale = Locale.getDefault();

/**
* Creates a new instance of LocaleComboBox with all the installed locales.
* Creates a new instance of {@code LocaleComboBox}.
*/
public LocaleComboBox() {
setItemLabelGenerator(item -> item.getDisplayName(getLocaleForDisplay()));
setRenderer(getLocaleRenderer());
addValueChangeListener(this::onValueChange);
setItems(
Arrays.stream(Locale.getAvailableLocales()).filter(loc -> loc.getCountry().length() == 2)
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName())).toList());
}

/**
* Creates a new instance of {@code LocaleComboBox} with the desired locales
*
* @param locales the {@code Collection} of {@code Locale} to include in the combobox
*/
public LocaleComboBox(Collection<Locale> items) {
this();
setItems(items);
}

/**
Expand All @@ -107,7 +119,7 @@ public void setDisplayMode(DisplayMode displayMode) {
* 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.
* @param displayLocale the {@code Locale} to use for formatting.
*/
public void setDisplayLocale(Locale displayLocale) {
this.customDisplayLocale = displayLocale == null ? Locale.getDefault() : displayLocale;
Expand All @@ -123,7 +135,7 @@ private LitRenderer<Locale> getLocaleRenderer() {
</vaadin-horizontal-layout>""")
.withProperty("layoutClass", loc -> ITEM_LAYOUT_CLASS_NAME)
.withProperty("flagClass", loc -> ITEM_FLAG_CLASS_NAME)
.withProperty("countryCode", loc -> loc.getCountry().toLowerCase())
.withProperty("countryCode", this::getFlagCode)
.withProperty("countryName", loc -> loc.getDisplayCountry(getLocaleForDisplay()))
.withProperty("displayName", loc -> loc.getDisplayName(getLocaleForDisplay()));
}
Expand All @@ -143,6 +155,12 @@ private Locale getLocaleForDisplay() {
}
}

private String getFlagCode(Locale locale) {
String countryCode = locale.getCountry();
return LocaleCountryConverter.convertToISO3166Code(countryCode).map(String::toLowerCase)
.orElse(DEFAULT_FLAG_CODE);
}

private void onValueChange(ComponentValueChangeEvent<ComboBox<Locale>, Locale> event) {
Locale newValue = event.getValue();
if (newValue == null) {
Expand Down
Loading