1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.argeo.cms.ui.widgets.auth;
17
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Locale;
21
22 import javax.security.auth.callback.LanguageCallback;
23
24 import org.argeo.cms.CmsException;
25 import org.argeo.cms.LocaleUtils;
26
27
28 public class LocaleChoice {
29 private final List<Locale> locales;
30
31 private Integer selectedIndex = null;
32 private final Integer defaultIndex;
33
34 public LocaleChoice(List<Locale> locales, Locale defaultLocale) {
35 Integer defaultIndex = null;
36 this.locales = Collections.unmodifiableList(locales);
37 for (int i = 0; i < locales.size(); i++)
38 if (locales.get(i).equals(defaultLocale))
39 defaultIndex = i;
40
41
42 if (defaultIndex == null)
43 for (int i = 0; i < locales.size(); i++)
44 if (locales.get(i).getLanguage().equals(defaultLocale.getLanguage()))
45 defaultIndex = i;
46
47 if (defaultIndex == null)
48 throw new CmsException("Default locale " + defaultLocale + " is not in available locales " + locales);
49 this.defaultIndex = defaultIndex;
50
51 this.selectedIndex = defaultIndex;
52 }
53
54
55
56
57
58 public LocaleChoice(String locales, Locale defaultLocale) {
59 this(LocaleUtils.asLocaleList(locales), defaultLocale);
60 }
61
62 public String[] getSupportedLocalesLabels() {
63 String[] labels = new String[locales.size()];
64 for (int i = 0; i < locales.size(); i++) {
65 Locale locale = locales.get(i);
66 if (locale.getCountry().equals(""))
67 labels[i] = locale.getDisplayLanguage(locale) + " [" + locale.getLanguage() + "]";
68 else
69 labels[i] = locale.getDisplayLanguage(locale) + " (" + locale.getDisplayCountry(locale) + ") ["
70 + locale.getLanguage() + "_" + locale.getCountry() + "]";
71
72 }
73 return labels;
74 }
75
76 public Locale getSelectedLocale() {
77 if (selectedIndex == null)
78 return null;
79 return locales.get(selectedIndex);
80 }
81
82 public void setSelectedIndex(Integer selectedIndex) {
83 this.selectedIndex = selectedIndex;
84 }
85
86 public Integer getSelectedIndex() {
87 return selectedIndex;
88 }
89
90 public Integer getDefaultIndex() {
91 return defaultIndex;
92 }
93
94 public List<Locale> getLocales() {
95 return locales;
96 }
97
98 public Locale getDefaultLocale() {
99 return locales.get(getDefaultIndex());
100 }
101 }