]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/auth/LocaleChoice.java
Re-add org.argeo.cms.util.useradmin
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / internal / auth / 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.cms.internal.auth;
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().equals(defaultLocale.getLanguage()))
45 defaultIndex = i;
46
47 if (defaultIndex == null)
48 throw new ArgeoException("Default locale " + defaultLocale + " is not in available locales " + locales);
49 this.defaultIndex = defaultIndex;
50
51 this.selectedIndex = defaultIndex;
52 }
53
54 /**
55 * Convenience constructor based on a comma separated list of iso codes (en,
56 * en_US, fr_CA, etc.). Default selection is default locale.
57 */
58 public LocaleChoice(String locales, Locale defaultLocale) {
59 this(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
102 /** Returns null if argument is null. */
103 public static List<Locale> asLocaleList(Object locales) {
104 if (locales == null)
105 return null;
106 ArrayList<Locale> availableLocales = new ArrayList<Locale>();
107 String[] codes = locales.toString().split(",");
108 for (int i = 0; i < codes.length; i++) {
109 String code = codes[i];
110 // variant not supported
111 int indexUnd = code.indexOf("_");
112 Locale locale;
113 if (indexUnd > 0) {
114 String language = code.substring(0, indexUnd);
115 String country = code.substring(indexUnd + 1);
116 locale = new Locale(language, country);
117 } else {
118 locale = new Locale(code);
119 }
120 availableLocales.add(locale);
121 }
122 return availableLocales;
123 }
124
125 public static void main(String[] args) {
126 for (String isoL : Locale.getISOLanguages()) {
127 Locale locale = new Locale(isoL);
128 System.out.println(isoL + "\t" + locale.getDisplayLanguage() + "\t" + locale.getDisplayLanguage(locale));
129 }
130 }
131
132 }