Improve ConfigItems and fix some related bugs
[fanfix.git] / src / be / nikiroo / utils / ui / ConfigItemCombobox.java
diff --git a/src/be/nikiroo/utils/ui/ConfigItemCombobox.java b/src/be/nikiroo/utils/ui/ConfigItemCombobox.java
new file mode 100644 (file)
index 0000000..db24589
--- /dev/null
@@ -0,0 +1,66 @@
+package be.nikiroo.utils.ui;
+
+import javax.swing.JComboBox;
+import javax.swing.JComponent;
+
+import be.nikiroo.utils.resources.MetaInfo;
+
+public class ConfigItemCombobox<E extends Enum<E>> extends ConfigItem<E> {
+       private static final long serialVersionUID = 1L;
+
+       private boolean editable;
+
+       /**
+        * Create a new {@link ConfigItemCombobox} for the given {@link MetaInfo}.
+        * 
+        * @param info
+        *            the {@link MetaInfo}
+        * @param editable
+        *            allows the user to type in another value not in the list
+        */
+       public ConfigItemCombobox(MetaInfo<E> info, boolean editable) {
+               super(info, true);
+               this.editable = editable;
+       }
+
+       @Override
+       protected Object getFromField(int item) {
+               // rawtypes for Java 1.6 (and 1.7 ?) support
+               @SuppressWarnings("rawtypes")
+               JComboBox field = (JComboBox) getField(item);
+               if (field != null) {
+                       return field.getSelectedItem();
+               }
+
+               return null;
+       }
+
+       @Override
+       protected Object getFromInfo(int item) {
+               return info.getString(item, false);
+       }
+
+       @Override
+       protected void setToField(Object value, int item) {
+               // rawtypes for Java 1.6 (and 1.7 ?) support
+               @SuppressWarnings("rawtypes")
+               JComboBox field = (JComboBox) getField(item);
+               if (field != null) {
+                       field.setSelectedItem(value);
+               }
+       }
+
+       @Override
+       protected void setToInfo(Object value, int item) {
+               info.setString((String) value, item);
+       }
+
+       // rawtypes for Java 1.6 (and 1.7 ?) support
+       @SuppressWarnings({ "unchecked", "rawtypes" })
+       @Override
+       protected JComponent createField(int item) {
+               JComboBox field = new JComboBox(info.getAllowedValues());
+               field.setEditable(editable);
+               return field;
+       }
+}