Commit | Line | Data |
---|---|---|
d5026c09 NR |
1 | package be.nikiroo.utils.ui; |
2 | ||
3 | import javax.swing.JComboBox; | |
4 | import javax.swing.JComponent; | |
5 | ||
6 | import be.nikiroo.utils.resources.MetaInfo; | |
7 | ||
8cea9dc5 | 8 | class ConfigItemCombobox<E extends Enum<E>> extends ConfigItem<E> { |
d5026c09 NR |
9 | private static final long serialVersionUID = 1L; |
10 | ||
11 | private boolean editable; | |
0f7de31e | 12 | private String[] allowedValues; |
d5026c09 NR |
13 | |
14 | /** | |
15 | * Create a new {@link ConfigItemCombobox} for the given {@link MetaInfo}. | |
16 | * | |
17 | * @param info | |
18 | * the {@link MetaInfo} | |
19 | * @param editable | |
20 | * allows the user to type in another value not in the list | |
21 | */ | |
22 | public ConfigItemCombobox(MetaInfo<E> info, boolean editable) { | |
23 | super(info, true); | |
24 | this.editable = editable; | |
0f7de31e | 25 | this.allowedValues = info.getAllowedValues(); |
d5026c09 NR |
26 | } |
27 | ||
28 | @Override | |
29 | protected Object getFromField(int item) { | |
30 | // rawtypes for Java 1.6 (and 1.7 ?) support | |
31 | @SuppressWarnings("rawtypes") | |
32 | JComboBox field = (JComboBox) getField(item); | |
33 | if (field != null) { | |
34 | return field.getSelectedItem(); | |
35 | } | |
36 | ||
37 | return null; | |
38 | } | |
39 | ||
40 | @Override | |
41 | protected Object getFromInfo(int item) { | |
aa9c3626 | 42 | return getInfo().getString(item, false); |
d5026c09 NR |
43 | } |
44 | ||
45 | @Override | |
46 | protected void setToField(Object value, int item) { | |
47 | // rawtypes for Java 1.6 (and 1.7 ?) support | |
48 | @SuppressWarnings("rawtypes") | |
49 | JComboBox field = (JComboBox) getField(item); | |
50 | if (field != null) { | |
51 | field.setSelectedItem(value); | |
52 | } | |
53 | } | |
54 | ||
55 | @Override | |
56 | protected void setToInfo(Object value, int item) { | |
aa9c3626 | 57 | getInfo().setString((String) value, item); |
d5026c09 NR |
58 | } |
59 | ||
60 | // rawtypes for Java 1.6 (and 1.7 ?) support | |
61 | @SuppressWarnings({ "unchecked", "rawtypes" }) | |
62 | @Override | |
a0376372 | 63 | protected JComponent createEmptyField(int item) { |
0f7de31e | 64 | JComboBox field = new JComboBox(allowedValues); |
d5026c09 NR |
65 | field.setEditable(editable); |
66 | return field; | |
67 | } | |
68 | } |