Add 'src/be/nikiroo/utils/' from commit '46add0670fdee4bd936a13fe2448c5e20a7ffd0a'
[fanfix.git] / src / be / nikiroo / utils / ui / ConfigItemBoolean.java
diff --git a/src/be/nikiroo/utils/ui/ConfigItemBoolean.java b/src/be/nikiroo/utils/ui/ConfigItemBoolean.java
new file mode 100644 (file)
index 0000000..255ec13
--- /dev/null
@@ -0,0 +1,66 @@
+package be.nikiroo.utils.ui;
+
+import javax.swing.JCheckBox;
+import javax.swing.JComponent;
+
+import be.nikiroo.utils.resources.MetaInfo;
+
+class ConfigItemBoolean<E extends Enum<E>> extends ConfigItem<E> {
+       private static final long serialVersionUID = 1L;
+
+       /**
+        * Create a new {@link ConfigItemBoolean} for the given {@link MetaInfo}.
+        * 
+        * @param info
+        *            the {@link MetaInfo}
+        */
+       public ConfigItemBoolean(MetaInfo<E> info) {
+               super(info, true);
+       }
+
+       @Override
+       protected Object getFromField(int item) {
+               JCheckBox field = (JCheckBox) getField(item);
+               if (field != null) {
+                       return field.isSelected();
+               }
+
+               return null;
+       }
+
+       @Override
+       protected Object getFromInfo(int item) {
+               return info.getBoolean(item, true);
+       }
+
+       @Override
+       protected void setToField(Object value, int item) {
+               JCheckBox field = (JCheckBox) getField(item);
+               if (field != null) {
+                       // Should not happen if config enum is correct
+                       // (but this is not enforced)
+                       if (value == null) {
+                               value = false;
+                       }
+
+                       field.setSelected((Boolean) value);
+               }
+       }
+
+       @Override
+       protected void setToInfo(Object value, int item) {
+               info.setBoolean((Boolean) value, item);
+       }
+
+       @Override
+       protected JComponent createEmptyField(int item) {
+               // Should not happen!
+               if (getFromInfo(item) == null) {
+                       System.err
+                                       .println("No default value given for BOOLEAN parameter \""
+                                                       + info.getName() + "\", we consider it is FALSE");
+               }
+
+               return new JCheckBox();
+       }
+}