X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FLocaleChoice.java;h=6609f4c94718728395f9e88f46dc47111d5fcee6;hb=ad05df464e8135fe995f918589b763a171f0a6d4;hp=aaf719db4c437b84bf53501fde2ead0e3021b1e5;hpb=ad9eb24753d1486113cfbc19d8080f15ce5ff68a;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/LocaleChoice.java b/org.argeo.util/src/org/argeo/util/LocaleChoice.java index aaf719db4..6609f4c94 100644 --- a/org.argeo.util/src/org/argeo/util/LocaleChoice.java +++ b/org.argeo.util/src/org/argeo/util/LocaleChoice.java @@ -16,69 +16,58 @@ package org.argeo.util; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Locale; import javax.security.auth.callback.LanguageCallback; +import org.argeo.ArgeoException; + /** Choose in a list of locales. TODO: replace with {@link LanguageCallback} */ public class LocaleChoice { - private List availableLocales = new ArrayList(); + private final List locales; private Integer selectedIndex = null; - private Integer defaultIndex = null; + private final Integer defaultIndex; + + public LocaleChoice(List locales, Locale defaultLocale) { + Integer defaultIndex = null; + this.locales = Collections.unmodifiableList(locales); + for (int i = 0; i < locales.size(); i++) + if (locales.get(i).equals(defaultLocale)) + defaultIndex = i; - // public LocaleCallback(Integer defaultIndex, List - // availableLocales) { - // this.availableLocales = Collections - // .unmodifiableList(new ArrayList(availableLocales)); - // this.defaultIndex = defaultIndex; - // this.selectedIndex = defaultIndex; - // } + // based on language only + if (defaultIndex == null) + for (int i = 0; i < locales.size(); i++) + if (locales.get(i).getLanguage().equals(defaultLocale.getLanguage())) + defaultIndex = i; + + if (defaultIndex == null) + throw new ArgeoException("Default locale " + defaultLocale + " is not in available locales " + locales); + this.defaultIndex = defaultIndex; + + this.selectedIndex = defaultIndex; + } /** * Convenience constructor based on a comma separated list of iso codes (en, * en_US, fr_CA, etc.). Default selection is default locale. */ public LocaleChoice(String locales, Locale defaultLocale) { - if (locales == null || locales.trim().equals("")) - return; - String[] codes = locales.split(","); - for (int i = 0; i < codes.length; i++) { - String code = codes[i]; - // variant not supported - int indexUnd = code.indexOf("_"); - Locale locale; - if (indexUnd > 0) { - String language = code.substring(0, indexUnd); - String country = code.substring(indexUnd + 1); - locale = new Locale(language, country); - } else { - locale = new Locale(code); - } - availableLocales.add(locale); - if (locale.equals(defaultLocale)) - defaultIndex = i; - } - - if (defaultIndex == null) - defaultIndex = 0; - - this.selectedIndex = defaultIndex; + this(asLocaleList(locales), defaultLocale); } public String[] getSupportedLocalesLabels() { - String[] labels = new String[availableLocales.size()]; - for (int i = 0; i < availableLocales.size(); i++) { - Locale locale = availableLocales.get(i); + String[] labels = new String[locales.size()]; + for (int i = 0; i < locales.size(); i++) { + Locale locale = locales.get(i); if (locale.getCountry().equals("")) - labels[i] = locale.getDisplayLanguage(locale) + " [" - + locale.getLanguage() + "]"; + labels[i] = locale.getDisplayLanguage(locale) + " [" + locale.getLanguage() + "]"; else - labels[i] = locale.getDisplayLanguage(locale) + " (" - + locale.getDisplayCountry(locale) + ") [" - + locale.getLanguage() + "_" + locale.getCountry() - + "]"; + labels[i] = locale.getDisplayLanguage(locale) + " (" + locale.getDisplayCountry(locale) + ") [" + + locale.getLanguage() + "_" + locale.getCountry() + "]"; } return labels; @@ -87,30 +76,56 @@ public class LocaleChoice { public Locale getSelectedLocale() { if (selectedIndex == null) return null; - return availableLocales.get(selectedIndex); + return locales.get(selectedIndex); } public void setSelectedIndex(Integer selectedIndex) { this.selectedIndex = selectedIndex; } + public Integer getSelectedIndex() { + return selectedIndex; + } + public Integer getDefaultIndex() { return defaultIndex; } - public List getAvailableLocales() { - return availableLocales; + public List getLocales() { + return locales; } public Locale getDefaultLocale() { - return availableLocales.get(getDefaultIndex()); + return locales.get(getDefaultIndex()); + } + + /** Returns null if argument is null. */ + public static List asLocaleList(Object locales) { + if (locales == null) + return null; + ArrayList availableLocales = new ArrayList(); + String[] codes = locales.toString().split(","); + for (int i = 0; i < codes.length; i++) { + String code = codes[i]; + // variant not supported + int indexUnd = code.indexOf("_"); + Locale locale; + if (indexUnd > 0) { + String language = code.substring(0, indexUnd); + String country = code.substring(indexUnd + 1); + locale = new Locale(language, country); + } else { + locale = new Locale(code); + } + availableLocales.add(locale); + } + return availableLocales; } public static void main(String[] args) { for (String isoL : Locale.getISOLanguages()) { Locale locale = new Locale(isoL); - System.out.println(isoL + "\t" + locale.getDisplayLanguage() + "\t" - + locale.getDisplayLanguage(locale)); + System.out.println(isoL + "\t" + locale.getDisplayLanguage() + "\t" + locale.getDisplayLanguage(locale)); } }