ConfigItem: improve logic, UI
[fanfix.git] / src / be / nikiroo / utils / ui / ConfigItem.java
index 3593d7ce3c8ded39e42289f08285f32b131accdf..3780a7e21058a1c9a80475ce8932112aa350e039 100644 (file)
@@ -1,86 +1,94 @@
 package be.nikiroo.utils.ui;
 
 import java.awt.BorderLayout;
-import java.awt.Dimension;
-import java.util.ArrayList;
-import java.util.List;
 
+import javax.swing.JCheckBox;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextField;
-import javax.swing.border.EmptyBorder;
 
 import be.nikiroo.utils.resources.Bundle;
+import be.nikiroo.utils.resources.Meta.Format;
+import be.nikiroo.utils.resources.MetaInfo;
 
 /**
  * A graphical item that reflect a configuration option from the given
  * {@link Bundle}.
  * 
  * @author niki
- *
+ * 
  * @param <E>
  *            the type of {@link Bundle} to edit
  */
 public class ConfigItem<E extends Enum<E>> extends JPanel {
        private static final long serialVersionUID = 1L;
-       private final Bundle<E> bundle;
-       private final E id;
-       private String value;
 
-       private JTextField valueField;
+       public ConfigItem(final MetaInfo<E> info) {
+               this.setLayout(new BorderLayout());
+               // this.setBorder(new EmptyBorder(2, 10, 2, 10));
 
-       public ConfigItem(Class<E> type, Bundle<E> bundle, E id) {
-               this.bundle = bundle;
-               this.id = id;
+               if (info.getFormat() == Format.BOOLEAN) {
+                       final JCheckBox field = new JCheckBox();
+                       field.setToolTipText(info.getDescription());
+                       Boolean state = info.getBoolean();
+                       if (state == null) {
+                               info.getDefaultBoolean();
+                       }
 
-               this.setLayout(new BorderLayout());
-               this.setBorder(new EmptyBorder(2, 10, 2, 10));
+                       // Should not happen!
+                       if (state == null) {
+                               System.err
+                                               .println("No default value given for BOOLEAN parameter \""
+                                                               + info.getName()
+                                                               + "\", we consider it is FALSE");
+                               state = false;
+                       }
 
-               JLabel nameLabel = new JLabel(id.toString());
-               nameLabel.setPreferredSize(new Dimension(400, 0));
-               this.add(nameLabel, BorderLayout.WEST);
+                       field.setSelected(state);
 
-               valueField = new JTextField();
-               valueField.setText(value);
+                       info.addReloadedListener(new Runnable() {
+                               @Override
+                               public void run() {
+                                       Boolean state = info.getBoolean();
+                                       if (state == null) {
+                                               info.getDefaultBoolean();
+                                       }
+                                       if (state == null) {
+                                               state = false;
+                                       }
 
-               reload();
-               this.add(valueField, BorderLayout.CENTER);
-       }
+                                       field.setSelected(state);
+                               }
+                       });
+                       info.addSaveListener(new Runnable() {
+                               @Override
+                               public void run() {
+                                       info.setBoolean(field.isSelected());
+                               }
+                       });
 
-       /**
-        * Reload the value from the {@link Bundle}.
-        */
-       public void reload() {
-               value = bundle.getString(id);
-               valueField.setText(value);
-       }
+                       this.add(new JLabel(info.getName() + ": "), BorderLayout.WEST);
+                       this.add(field, BorderLayout.CENTER);
+               } else {
+                       final JTextField field = new JTextField();
+                       field.setToolTipText(info.getDescription());
+                       field.setText(info.getString());
 
-       /**
-        * Save the current value to the {@link Bundle}.
-        */
-       public void save() {
-               value = valueField.getText();
-               bundle.setString(id, value);
-       }
+                       info.addReloadedListener(new Runnable() {
+                               @Override
+                               public void run() {
+                                       field.setText(info.getString());
+                               }
+                       });
+                       info.addSaveListener(new Runnable() {
+                               @Override
+                               public void run() {
+                                       info.setString(field.getText());
+                               }
+                       });
 
-       /**
-        * Create a list of {@link ConfigItem}, one for each of the item in the
-        * given {@link Bundle}.
-        * 
-        * @param type
-        *            a class instance of the item type to work on
-        * @param bundle
-        *            the {@link Bundle} to sort through
-        * 
-        * @return the list
-        */
-       static public <E extends Enum<E>> List<ConfigItem<E>> getItems(
-                       Class<E> type, Bundle<E> bundle) {
-               List<ConfigItem<E>> list = new ArrayList<ConfigItem<E>>();
-               for (E id : type.getEnumConstants()) {
-                       list.add(new ConfigItem<E>(type, bundle, id));
+                       this.add(new JLabel(info.getName() + ": "), BorderLayout.WEST);
+                       this.add(field, BorderLayout.CENTER);
                }
-
-               return list;
        }
 }