]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/LocaleChoice.java
[maven-release-plugin] prepare release argeo-commons-2.1.39
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / LocaleChoice.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.util;
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.List;
21 import java.util.Locale;
22
23 import javax.security.auth.callback.LanguageCallback;
24
25 import org.argeo.ArgeoException;
26
27 /** Choose in a list of locales. TODO: replace with {@link LanguageCallback} */
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 // based on language only
42 if (defaultIndex == null)
43 for (int i = 0; i < locales.size(); i++)
44 if (locales.get(i).getLanguage()
45 .equals(defaultLocale.getLanguage()))
46 defaultIndex = i;
47
48 if (defaultIndex == null)
49 throw new ArgeoException("Default locale " + defaultLocale
50 + " is not in available locales " + locales);
51 this.defaultIndex = defaultIndex;
52
53 this.selectedIndex = defaultIndex;
54 }
55
56 /**
57 * Convenience constructor based on a comma separated list of iso codes (en,
58 * en_US, fr_CA, etc.). Default selection is default locale.
59 */
60 public LocaleChoice(String locales, Locale defaultLocale) {
61 this(asLocaleList(locales), defaultLocale);
62 }
63
64 public String[] getSupportedLocalesLabels() {
65 String[] labels = new String[locales.size()];
66 for (int i = 0; i < locales.size(); i++) {
67 Locale locale = locales.get(i);
68 if (locale.getCountry().equals(""))
69 labels[i] = locale.getDisplayLanguage(locale) + " ["
70 + locale.getLanguage() + "]";
71 else
72 labels[i] = locale.getDisplayLanguage(locale) + " ("
73 + locale.getDisplayCountry(locale) + ") ["
74 + locale.getLanguage() + "_" + locale.getCountry()
75 + "]";
76
77 }
78 return labels;
79 }
80
81 public Locale getSelectedLocale() {
82 if (selectedIndex == null)
83 return null;
84 return locales.get(selectedIndex);
85 }
86
87 public void setSelectedIndex(Integer selectedIndex) {
88 this.selectedIndex = selectedIndex;
89 }
90
91 public Integer getSelectedIndex() {
92 return selectedIndex;
93 }
94
95 public Integer getDefaultIndex() {
96 return defaultIndex;
97 }
98
99 public List<Locale> getLocales() {
100 return locales;
101 }
102
103 public Locale getDefaultLocale() {
104 return locales.get(getDefaultIndex());
105 }
106
107 /** Returns null if argument is null. */
108 public static List<Locale> asLocaleList(String locales) {
109 if (locales == null)
110 return null;
111 ArrayList<Locale> availableLocales = new ArrayList<Locale>();
112 String[] codes = locales.split(",");
113 for (int i = 0; i < codes.length; i++) {
114 String code = codes[i];
115 // variant not supported
116 int indexUnd = code.indexOf("_");
117 Locale locale;
118 if (indexUnd > 0) {
119 String language = code.substring(0, indexUnd);
120 String country = code.substring(indexUnd + 1);
121 locale = new Locale(language, country);
122 } else {
123 locale = new Locale(code);
124 }
125 availableLocales.add(locale);
126 }
127 return availableLocales;
128 }
129
130 public static void main(String[] args) {
131 for (String isoL : Locale.getISOLanguages()) {
132 Locale locale = new Locale(isoL);
133 System.out.println(isoL + "\t" + locale.getDisplayLanguage() + "\t"
134 + locale.getDisplayLanguage(locale));
135 }
136 }
137
138 }