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 @@ -42,18 +42,77 @@ 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";

/**
* Display mode representing the built-in display modes in LocaleComboBox
* <p>
* These enums can be used in {@link #setDisplayMode(DisplayMode)} to easily switch between the
* built-in display modes.
*/
public enum DisplayMode {

/**
* Default display mode.
* <p>
* In this mode, the Locale names are displayed using the default locale's display format.
*/
DEFAULT,

/**
* Selected display mode.
* <p>
* In this mode, the Locale names are displayed using the formatting of the currently selected
* locale.
*/
SELECTED,

/**
* Custom display mode.
* <p>
* 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(
Arrays.stream(Locale.getAvailableLocales()).filter(loc -> loc.getCountry().length() == 2)
.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.
* <p>
* 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<Locale> getLocaleRenderer() {
return LitRenderer
.<Locale>of(
Expand All @@ -65,8 +124,23 @@ private LitRenderer<Locale> 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<ComboBox<Locale>, Locale> event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

}