]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.util/src/org/argeo/util/LocaleCallback.java
Merge adaptations related to the new third parties.
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / util / LocaleCallback.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.Callback;
24
25 /** Choose in a list of locales */
26 public class LocaleCallback implements Callback {
27 private List<Locale> availableLocales = new ArrayList<Locale>();
28
29 private Integer selectedIndex = null;
30 private Integer defaultIndex = null;
31 private String prompt = "Language";
32
33 public LocaleCallback(Integer defaultIndex, List<Locale> availableLocales) {
34 this.availableLocales = Collections
35 .unmodifiableList(new ArrayList<Locale>(availableLocales));
36 this.defaultIndex = defaultIndex;
37 this.selectedIndex = defaultIndex;
38 }
39
40 /**
41 * Convenience constructor based on a comma separated list of iso codes (en,
42 * en_US, fr_CA, etc.). Default selection is default locale.
43 */
44 public LocaleCallback(String locales) {
45 if (locales == null || locales.trim().equals(""))
46 return;
47 String[] codes = locales.split(",");
48 for (int i = 0; i < codes.length; i++) {
49 String code = codes[i];
50 // variant not supported
51 int indexUnd = code.indexOf("_");
52 Locale locale;
53 if (indexUnd > 0) {
54 String language = code.substring(0, indexUnd);
55 String country = code.substring(indexUnd + 1);
56 locale = new Locale(language, country);
57 } else {
58 locale = new Locale(code);
59 }
60 availableLocales.add(locale);
61 if (locale.equals(Locale.getDefault()))
62 defaultIndex = i;
63 }
64
65 if (defaultIndex == null)
66 defaultIndex = 0;
67
68 this.selectedIndex = defaultIndex;
69 }
70
71 public String[] getSupportedLocalesLabels() {
72 String[] labels = new String[availableLocales.size()];
73 for (int i = 0; i < availableLocales.size(); i++) {
74 Locale locale = availableLocales.get(i);
75 if (locale.getCountry().equals(""))
76 labels[i] = locale.getDisplayLanguage(locale) + " ["
77 + locale.getLanguage() + "]";
78 else
79 labels[i] = locale.getDisplayLanguage(locale) + " ("
80 + locale.getDisplayCountry(locale) + ") ["
81 + locale.getLanguage() + "_" + locale.getCountry()
82 + "]";
83
84 }
85 return labels;
86 }
87
88 public Locale getSelectedLocale() {
89 if (selectedIndex == null)
90 return null;
91 return availableLocales.get(selectedIndex);
92 }
93
94 public void setSelectedIndex(Integer selectedIndex) {
95 this.selectedIndex = selectedIndex;
96 }
97
98 public Integer getDefaultIndex() {
99 return defaultIndex;
100 }
101
102 public String getPrompt() {
103 // TODO localize it?
104 return prompt;
105 }
106
107 public void setPrompt(String prompt) {
108 this.prompt = prompt;
109 }
110
111 public List<Locale> getAvailableLocales() {
112 return availableLocales;
113 }
114
115 public static void main(String[] args) {
116 for (String isoL : Locale.getISOLanguages()) {
117 Locale locale = new Locale(isoL);
118 System.out.println(isoL + "\t" + locale.getDisplayLanguage() + "\t"
119 + locale.getDisplayLanguage(locale));
120 }
121 }
122
123 }