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