Merge commit 'edcd53bbbba9f94e21f43fd03d3a2febcc2b1564'
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / ConfigItemBoolean.java
1 package be.nikiroo.utils.ui;
2
3 import javax.swing.JCheckBox;
4 import javax.swing.JComponent;
5
6 import be.nikiroo.utils.resources.MetaInfo;
7
8 class ConfigItemBoolean<E extends Enum<E>> extends ConfigItem<E> {
9 private static final long serialVersionUID = 1L;
10
11 /**
12 * Create a new {@link ConfigItemBoolean} for the given {@link MetaInfo}.
13 *
14 * @param info
15 * the {@link MetaInfo}
16 */
17 public ConfigItemBoolean(MetaInfo<E> info) {
18 super(info, true);
19 }
20
21 @Override
22 protected Object getFromField(int item) {
23 JCheckBox field = (JCheckBox) getField(item);
24 if (field != null) {
25 return field.isSelected();
26 }
27
28 return null;
29 }
30
31 @Override
32 protected Object getFromInfo(int item) {
33 return getInfo().getBoolean(item, true);
34 }
35
36 @Override
37 protected void setToField(Object value, int item) {
38 JCheckBox field = (JCheckBox) getField(item);
39 if (field != null) {
40 // Should not happen if config enum is correct
41 // (but this is not enforced)
42 if (value == null) {
43 value = false;
44 }
45
46 field.setSelected((Boolean) value);
47 }
48 }
49
50 @Override
51 protected void setToInfo(Object value, int item) {
52 getInfo().setBoolean((Boolean) value, item);
53 }
54
55 @Override
56 protected JComponent createEmptyField(int item) {
57 // Should not happen!
58 if (getFromInfo(item) == null) {
59 System.err
60 .println("No default value given for BOOLEAN parameter \""
61 + getInfo().getName()
62 + "\", we consider it is FALSE");
63 }
64
65 return new JCheckBox();
66 }
67 }