Merge commit '9bd801bfc6a10e5298cdec6e48ca0506ecdfde53'
[nikiroo-utils.git] / ui / ConfigItemLocale.java
1 package be.nikiroo.utils.ui;
2
3 import java.awt.Component;
4 import java.util.Locale;
5
6 import javax.swing.DefaultListCellRenderer;
7 import javax.swing.JComboBox;
8 import javax.swing.JComponent;
9 import javax.swing.JList;
10
11 import be.nikiroo.utils.resources.MetaInfo;
12
13 class ConfigItemLocale<E extends Enum<E>> extends ConfigItemCombobox<E> {
14 private static final long serialVersionUID = 1L;
15
16 /**
17 * Create a new {@link ConfigItemLocale} for the given {@link MetaInfo}.
18 *
19 * @param info
20 * the {@link MetaInfo}
21 */
22 public ConfigItemLocale(MetaInfo<E> info) {
23 super(info, true);
24 }
25
26 // rawtypes for Java 1.6 (and 1.7 ?) support
27 @SuppressWarnings({ "unchecked", "rawtypes" })
28 @Override
29 protected JComponent createEmptyField(int item) {
30 JComboBox field = (JComboBox) super.createEmptyField(item);
31 field.setRenderer(new DefaultListCellRenderer() {
32 private static final long serialVersionUID = 1L;
33
34 @Override
35 public Component getListCellRendererComponent(JList list,
36 Object value, int index, boolean isSelected,
37 boolean cellHasFocus) {
38
39 String svalue = value == null ? "" : value.toString();
40 String[] tab = svalue.split("-");
41 Locale locale = null;
42 if (tab.length == 1) {
43 locale = new Locale(tab[0]);
44 } else if (tab.length == 2) {
45 locale = new Locale(tab[0], tab[1]);
46 } else if (tab.length == 3) {
47 locale = new Locale(tab[0], tab[1], tab[2]);
48 }
49
50 String displayValue = svalue;
51 if (locale != null) {
52 displayValue = locale.getDisplayName();
53 }
54
55 return super.getListCellRendererComponent(list, displayValue,
56 index, isSelected, cellHasFocus);
57 }
58 });
59
60 return field;
61 }
62 }