Skip to content

Commit

Permalink
style: apply code formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-godoy authored and paodb committed Sep 23, 2024
1 parent 579495d commit 2570ef2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

/**
* Vaadin ComboBox extension that allows to choose between multiple locales.
*
*
* @author Tomas Peiretti / Flowing Code
*/
@SuppressWarnings("serial")
Expand All @@ -46,47 +46,43 @@ public class LocaleComboBox extends ComboBox<Locale> {

/**
* Represents the built-in display modes for locale names in the {@link LocaleComboBox} component.
* <p>
* These enums can be used in {@link #setDisplayMode(DisplayMode)} to easily switch between the
*
* <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.
*
* <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.
*
* <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)}.
*
* <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;

/**
* Indicates whether the flags should be displayed alongside the locale names.
*/
/** Indicates whether the flags should be displayed alongside the locale names. */
private boolean hasFlags = true;

/**
* Creates a new instance of {@code LocaleComboBox}.
*/
/** Creates a new instance of {@code LocaleComboBox}. */
public LocaleComboBox() {
setItemLabelGenerator(item -> item.getDisplayName(getLocaleForDisplay()));
setRenderer(getLocaleRenderer());
Expand All @@ -95,7 +91,7 @@ public LocaleComboBox() {

/**
* 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> locales) {
Expand All @@ -105,9 +101,8 @@ public LocaleComboBox(Collection<Locale> locales) {

/**
* Sets the display mode of the LocaleComboBox.
*
*
* @param displayMode the display mode to use
*
* @see DisplayMode
*/
public void setDisplayMode(DisplayMode displayMode) {
Expand All @@ -117,11 +112,11 @@ public void setDisplayMode(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
*
* <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 {@code Locale} to use for formatting.
*/
public void setDisplayLocale(Locale displayLocale) {
Expand All @@ -139,10 +134,10 @@ public boolean hasFlags() {

/**
* Sets whether flags should be displayed alongside locale names.
* <p>
* This method updates the internal state to reflect whether flags should be displayed and updates
* the rendering based on the new state.
*
*
* <p>This method updates the internal state to reflect whether flags should be displayed and
* updates the rendering based on the new state.
*
* @param hasFlags A {@code boolean} indicating whether flags should be displayed or not.
*/
public void setHasFlags(boolean hasFlags) {
Expand All @@ -152,8 +147,7 @@ public void setHasFlags(boolean hasFlags) {
}

private LitRenderer<Locale> getLocaleRenderer() {
return LitRenderer
.<Locale>of(
return LitRenderer.<Locale>of(
"""
<vaadin-horizontal-layout class="${item.layoutClass}">
<span class="fi fi-${item.countryCode} ${item.flagClass} alt="${item.countryName}'s flag"></span>
Expand All @@ -167,17 +161,18 @@ private LitRenderer<Locale> getLocaleRenderer() {
}

private LitRenderer<Locale> getLocaleRendererWithoutFlags() {
return LitRenderer.<Locale>of("""
return LitRenderer.<Locale>of(
"""
<vaadin-horizontal-layout class="${item.layoutClass}">
<span>${item.displayName}</span>
</vaadin-horizontal-layout>""").withProperty("layoutClass", loc -> ITEM_LAYOUT_CLASS_NAME)
</vaadin-horizontal-layout>""")
.withProperty("layoutClass", loc -> ITEM_LAYOUT_CLASS_NAME)
.withProperty("displayName", loc -> loc.getDisplayName(getLocaleForDisplay()));
}

private Locale getLocaleForDisplay() {

switch (displayMode) {

case CUSTOM:
return Optional.ofNullable(customDisplayLocale).orElseGet(this::getLocale);

Expand All @@ -191,7 +186,8 @@ private Locale getLocaleForDisplay() {

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

Expand All @@ -215,5 +211,4 @@ private void setPrefixFlag(Locale locale) {
flagIcon.addClassNames("fi", "fi-" + this.getFlagCode(locale));
setPrefixComponent(flagIcon);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@
package com.flowingcode.vaadin.addons.localecombobox;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* Utility class for converting between different formats of country codes.
*
* <p>
* The {@code LocaleCountryConverter} class provides methods to convert country codes from ISO
* <p>The {@code LocaleCountryConverter} class provides methods to convert country codes from ISO
* 3166-1 alpha-2, alpha-3, and numeric-3 formats to the 3166-1 alpha-2 format. The class uses
* static methods, so no instances are needed.
*
Expand Down Expand Up @@ -307,13 +305,11 @@ private static void add(String alpha2, String alpha3, int numeric) {
* Converts a country code to its corresponding ISO 3166-1 alpha-2 code.
*
* @param countryCode The country code to be converted. This can be in ISO 3166-1 alpha-2 format
* (e.g., "AR"), ISO 3166-1 alpha-3 format (e.g., "ARG"), or numeric-3 format (e.g.,
* "032").
* (e.g., "AR"), ISO 3166-1 alpha-3 format (e.g., "ARG"), or numeric-3 format (e.g., "032").
* @return An {@code Optional} containing the ISO 3166-1 alpha-2 code if the conversion is
* successful.
* successful.
*/
public static Optional<String> convertToISO3166Code(String countryCode) {
return Optional.ofNullable(conversions.get(countryCode.toUpperCase()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
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.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.FlexComponent.Alignment;
import com.vaadin.flow.component.orderedlayout.FlexComponent.JustifyContentMode;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;

@SuppressWarnings("serial")
public class BaseLocaleComboBoxDemo extends Div {
Expand All @@ -45,5 +45,4 @@ protected HorizontalLayout createHorizontalContainer(Component component, Locale
container.expand(combo);
return container;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ public class DisplayModeDemo extends BaseLocaleComboBoxDemo {
public DisplayModeDemo() {

List<Locale> localeList =
Arrays.stream(Locale.getAvailableLocales()).filter(loc -> !loc.getDisplayName().isBlank())
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName())).toList();
Arrays.stream(Locale.getAvailableLocales())
.filter(loc -> !loc.getDisplayName().isBlank())
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName()))
.toList();

LocaleComboBox defaultDisplayLocale = new LocaleComboBox(localeList);
LocaleComboBox koreanLocaleCombo = new LocaleComboBox();
Expand All @@ -63,5 +65,4 @@ public DisplayModeDemo() {
// show-source add(koreanLocaleCombo);
// show-source add(selectedLocaleCombo);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public class RenderingDemo extends BaseLocaleComboBoxDemo {
public RenderingDemo() {

List<Locale> localeList =
Arrays.stream(Locale.getAvailableLocales()).filter(loc -> !loc.getDisplayName().isBlank())
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName())).toList();
Arrays.stream(Locale.getAvailableLocales())
.filter(loc -> !loc.getDisplayName().isBlank())
.sorted((l1, l2) -> l1.getDisplayName().compareTo(l2.getDisplayName()))
.toList();

LocaleComboBox defaultLocaleCombo = new LocaleComboBox(localeList);
LocaleComboBox flagsLocaleCombo = new LocaleComboBox(localeList);
Expand Down

0 comments on commit 2570ef2

Please sign in to comment.