-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3525905
commit 87a6fb8
Showing
16 changed files
with
163 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,10 @@ | |
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.flowingcode.vaadin.addons</groupId> | ||
<artifactId>template-addon</artifactId> | ||
<artifactId>locale-combo-box-addon</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<name>Template Add-on</name> | ||
<description>Template Add-on for Vaadin Flow</description> | ||
<name>LocaleComboBox</name> | ||
<description>LocaleComboBox Add-on for Vaadin Flow</description> | ||
<url>https://www.flowingcode.com/en/open-source/</url> | ||
|
||
<properties> | ||
|
@@ -39,9 +39,9 @@ | |
</licenses> | ||
|
||
<scm> | ||
<url>https://github.com/FlowingCode/AddonStarter24</url> | ||
<connection>scm:git:git://github.com/FlowingCode/AddonStarter24.git</connection> | ||
<developerConnection>scm:git:ssh://[email protected]:/FlowingCode/AddonStarter24.git</developerConnection> | ||
<url>https://github.com/FlowingCode/LocaleComboBox</url> | ||
<connection>scm:git:git://github.com/FlowingCode/LocaleComboBox.git</connection> | ||
<developerConnection>scm:git:ssh://[email protected]:/FlowingCode/LocaleComboBox.git</developerConnection> | ||
<tag>master</tag> | ||
</scm> | ||
|
||
|
79 changes: 79 additions & 0 deletions
79
src/main/java/com/flowingcode/vaadin/addons/localecombobox/LocaleComboBox.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/*- | ||
* #%L | ||
* LocaleComboBox Add-on | ||
* %% | ||
* Copyright (C) 2024 Flowing Code | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
|
||
package com.flowingcode.vaadin.addons.localecombobox; | ||
|
||
import com.vaadin.flow.component.combobox.ComboBox; | ||
import com.vaadin.flow.component.dependency.CssImport; | ||
import com.vaadin.flow.component.dependency.NpmPackage; | ||
import com.vaadin.flow.component.html.Span; | ||
import com.vaadin.flow.data.renderer.LitRenderer; | ||
import java.util.Arrays; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Vaadin ComboBox extension that allows to choose between multiple locales. | ||
* | ||
* @author Tomas Peiretti / Flowing Code | ||
*/ | ||
@SuppressWarnings("serial") | ||
@NpmPackage(value = "flag-icons", version = "7.2.3") | ||
@CssImport("flag-icons/css/flag-icons.min.css") | ||
@CssImport("styles/locale-combo-box.css") | ||
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"; | ||
|
||
/** | ||
* Creates a new instance of LocaleComboBox with all the installed locales. | ||
*/ | ||
public LocaleComboBox() { | ||
setItemLabelGenerator(Locale::getDisplayName); | ||
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()); | ||
} | ||
|
||
private LitRenderer<Locale> getLocaleRenderer() { | ||
return LitRenderer | ||
.<Locale>of("<vaadin-horizontal-layout class=\"" + ITEM_LAYOUT_CLASS_NAME + "\">" | ||
+ "<span class=\"fi fi-${item.countryCode} " + ITEM_FLAG_CLASS_NAME | ||
+ "\" alt=\"${item.countryName}\'s flag\"></span>" + "<span>${item.displayName}</span>" | ||
+ "</vaadin-horizontal-layout>") | ||
.withProperty("countryCode", loc -> loc.getCountry().toLowerCase()) | ||
.withProperty("countryName", loc -> loc.getDisplayCountry()) | ||
.withProperty("displayName", loc -> loc.getDisplayName()); | ||
} | ||
|
||
private void onValueChange(ComponentValueChangeEvent<ComboBox<Locale>, Locale> event) { | ||
Locale newValue = event.getValue(); | ||
if (newValue == null) { | ||
setPrefixComponent(null); | ||
return; | ||
} | ||
Span flagIcon = new Span(); | ||
flagIcon.addClassNames("fi", "fi-" + newValue.getCountry().toLowerCase()); | ||
setPrefixComponent(flagIcon); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/resources/META-INF/frontend/styles/locale-combo-box.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/*- | ||
* #%L | ||
* LocaleComboBox Add-on | ||
* %% | ||
* Copyright (C) 2024 Flowing Code | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* #L% | ||
*/ | ||
vaadin-horizontal-layout.fc-locale-combo-box-item-layout { | ||
align-items: center; | ||
justify-content: start; | ||
gap: 0.5em; | ||
} | ||
|
||
span.fc-locale-combo-box-item-flag { | ||
flex-shrink: 0; | ||
} |
1 change: 0 additions & 1 deletion
1
src/main/resources/META-INF/frontend/styles/static_addon_styles
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/*- | ||
* #%L | ||
* Template Add-on | ||
* LocaleComboBox Add-on | ||
* %% | ||
* Copyright (C) 2024 Flowing Code | ||
* %% | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.