1 |
alfonx |
1150 |
package skrueger.i8n; |
2 |
|
|
|
3 |
|
|
import java.util.ArrayList; |
4 |
|
|
import java.util.Collection; |
5 |
|
|
import java.util.Collections; |
6 |
|
|
import java.util.Vector; |
7 |
|
|
|
8 |
|
|
import javax.swing.DefaultComboBoxModel; |
9 |
|
|
import javax.swing.JComboBox; |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* A JComboBox that shows the user a list of languages |
13 |
|
|
*/ |
14 |
|
|
public class LanguagesComboBox extends JComboBox { |
15 |
|
|
|
16 |
alfonx |
1373 |
private final Vector<String> availLangs = new Vector<String>(); |
17 |
alfonx |
1150 |
|
18 |
|
|
public LanguagesComboBox() { |
19 |
|
|
super(); |
20 |
|
|
} |
21 |
|
|
|
22 |
|
|
/** |
23 |
|
|
* Create a ComboBox with the given langauges as options |
24 |
|
|
*/ |
25 |
|
|
public LanguagesComboBox(Collection<String> langCodes) { |
26 |
|
|
this(); |
27 |
|
|
updateModel(langCodes, null); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* Create a ComboBox with the given languages as options, excluding the |
32 |
|
|
* second parameter. |
33 |
|
|
* |
34 |
|
|
* @param langCodes |
35 |
|
|
* If <code>null</code>, {@link I8NUtil#getLanguageCodes()} will |
36 |
|
|
* be used |
37 |
|
|
*/ |
38 |
|
|
public LanguagesComboBox(Collection<String> langCodes, |
39 |
|
|
Collection<String> langCodesNotOffer) { |
40 |
|
|
this(); |
41 |
|
|
updateModel(langCodes, langCodesNotOffer); |
42 |
|
|
} |
43 |
|
|
|
44 |
|
|
public void updateModel(Collection<String> langCodes, |
45 |
|
|
Collection<String> langCodesNotOffer) { |
46 |
|
|
|
47 |
|
|
if (langCodes == null) { |
48 |
|
|
langCodes = I8NUtil.getLanguageCodes(); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
if (langCodesNotOffer != null) { |
52 |
|
|
langCodes = new ArrayList<String>(langCodes); |
53 |
|
|
langCodes.removeAll(langCodesNotOffer); |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
availLangs.clear(); |
57 |
|
|
|
58 |
|
|
for (String lc : langCodes) { |
59 |
alfonx |
1373 |
// |
60 |
|
|
// if (I8NUtil.isValidISOLangCode(lc)) { |
61 |
|
|
// Locale locale = I8NUtil.getFirstLocaleForLang(lc); |
62 |
|
|
// availLangs.add(locale.getDisplayLanguage() + " / " |
63 |
|
|
// + locale.getDisplayLanguage(locale) + " " + lc); |
64 |
|
|
// } else if (I8NUtil.isPropertiesLanguage(lc)) { |
65 |
|
|
// |
66 |
|
|
// PropertiesLocale pl = I8NUtil.propLocales.get(lc); |
67 |
|
|
// availLangs.add(pl.getDisplayLanguage() + " / " |
68 |
|
|
// + pl.getDisplayLanguage(lc) + " " + lc); |
69 |
|
|
// } |
70 |
alfonx |
1150 |
|
71 |
alfonx |
1373 |
availLangs.add(I8NUtil.getMultilanguageString(lc)); |
72 |
alfonx |
1150 |
} |
73 |
|
|
|
74 |
|
|
Collections.sort(availLangs); |
75 |
|
|
|
76 |
|
|
setModel(new DefaultComboBoxModel(availLangs)); |
77 |
|
|
setSelectedIndex(-1); |
78 |
|
|
repaint(); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
public String getSelectedLanguage() { |
82 |
|
|
if (getSelectedIndex() < 0) |
83 |
|
|
return null; |
84 |
|
|
String langDescription = availLangs.get(getSelectedIndex()); |
85 |
|
|
|
86 |
|
|
return langDescription.substring(langDescription.lastIndexOf(" ")) |
87 |
|
|
.trim(); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
} |