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