| 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 | class ConfigItemCombobox<E extends Enum<E>> extends ConfigItem<E> { |
| 9 | private static final long serialVersionUID = 1L; |
| 10 | |
| 11 | private boolean editable; |
| 12 | private String[] allowedValues; |
| 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; |
| 25 | this.allowedValues = info.getAllowedValues(); |
| 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) { |
| 42 | return getInfo().getString(item, false); |
| 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) { |
| 57 | getInfo().setString((String) value, item); |
| 58 | } |
| 59 | |
| 60 | // rawtypes for Java 1.6 (and 1.7 ?) support |
| 61 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 62 | @Override |
| 63 | protected JComponent createEmptyField(int item) { |
| 64 | JComboBox field = new JComboBox(allowedValues); |
| 65 | field.setEditable(editable); |
| 66 | return field; |
| 67 | } |
| 68 | } |