]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ui/src/org/argeo/cms/widgets/auth/LocaleChoice.java
Move RCP support to Argeo SLC
[lgpl/argeo-commons.git] / org.argeo.cms.ui / src / org / argeo / cms / widgets / 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.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 /** 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 CmsException("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(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 }