Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / be / nikiroo / utils / ui / ConfigEditor.java
index fb98eba01f72458ca21f5c97302350b058206ba5..c687c98fee5637dfb8e10b7bad32f6be102ef9d5 100644 (file)
@@ -1,9 +1,11 @@
 package be.nikiroo.utils.ui;
 
 import java.awt.BorderLayout;
+import java.awt.Color;
 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.List;
 
 import javax.swing.BoxLayout;
@@ -12,9 +14,13 @@ import javax.swing.JComponent;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
 import javax.swing.border.EmptyBorder;
+import javax.swing.border.TitledBorder;
 
+import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.resources.Bundle;
+import be.nikiroo.utils.resources.MetaInfo;
 
 /**
  * A configuration panel for a {@link Bundle}.
@@ -24,13 +30,13 @@ import be.nikiroo.utils.resources.Bundle;
  * values.
  * 
  * @author niki
- *
+ * 
  * @param <E>
  *            the type of {@link Bundle} to edit
  */
 public class ConfigEditor<E extends Enum<E>> extends JPanel {
        private static final long serialVersionUID = 1L;
-       private List<ConfigItem<E>> items;
+       private List<MetaInfo<E>> items;
 
        /**
         * Create a new {@link ConfigEditor} for this {@link Bundle}.
@@ -44,34 +50,39 @@ public class ConfigEditor<E extends Enum<E>> extends JPanel {
         */
        public ConfigEditor(Class<E> type, final Bundle<E> bundle, String title) {
                this.setLayout(new BorderLayout());
-               JPanel main = new JPanel();
-
-               JScrollPane scroll = new JScrollPane(main);
-               scroll.getVerticalScrollBar().setUnitIncrement(16);
-               this.add(scroll, BorderLayout.CENTER);
 
+               JPanel main = new JPanel();
                main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
+               main.setBorder(new EmptyBorder(5, 5, 5, 5));
 
                main.add(new JLabel(title));
 
-               items = ConfigItem.getItems(type, bundle);
-               for (ConfigItem<E> item : items) {
-                       main.add(item);
+               items = new ArrayList<MetaInfo<E>>();
+               List<MetaInfo<E>> groupedItems = MetaInfo.getItems(type, bundle);
+               for (MetaInfo<E> item : groupedItems) {
+                       // will init this.items
+                       addItem(main, item, 0);
                }
 
-               main.add(createButton("Reset", new ActionListener() {
+               JPanel buttons = new JPanel();
+               buttons.setLayout(new BoxLayout(buttons, BoxLayout.PAGE_AXIS));
+               buttons.setBorder(new EmptyBorder(5, 5, 5, 5));
+
+               buttons.add(createButton("Reset", new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
-                               for (ConfigItem<E> item : items) {
+                               for (MetaInfo<E> item : items) {
                                        item.reload();
                                }
                        }
                }));
 
-               main.add(createButton("Default", new ActionListener() {
+               buttons.add(createButton("Default", new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
                                Object snap = bundle.takeSnapshot();
                                bundle.reload(true);
-                               for (ConfigItem<E> item : items) {
+                               for (MetaInfo<E> item : items) {
                                        item.reload();
                                }
                                bundle.reload(false);
@@ -79,10 +90,11 @@ public class ConfigEditor<E extends Enum<E>> extends JPanel {
                        }
                }));
 
-               main.add(createButton("Save", new ActionListener() {
+               buttons.add(createButton("Save", new ActionListener() {
+                       @Override
                        public void actionPerformed(ActionEvent e) {
-                               for (ConfigItem<E> item : items) {
-                                       item.save();
+                               for (MetaInfo<E> item : items) {
+                                       item.save(true);
                                }
 
                                try {
@@ -92,6 +104,43 @@ public class ConfigEditor<E extends Enum<E>> extends JPanel {
                                }
                        }
                }));
+
+               JScrollPane scroll = new JScrollPane(main);
+               scroll.getVerticalScrollBar().setUnitIncrement(16);
+
+               this.add(scroll, BorderLayout.CENTER);
+               this.add(buttons, BorderLayout.SOUTH);
+       }
+
+       private void addItem(JPanel main, MetaInfo<E> item, int nhgap) {
+               if (item.isGroup()) {
+                       JPanel bpane = new JPanel(new BorderLayout());
+                       bpane.setBorder(new TitledBorder(item.getName()));
+                       JPanel pane = new JPanel();
+                       pane.setBorder(new EmptyBorder(5, 5, 5, 5));
+                       pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
+
+                       String info = item.getDescription();
+                       info = StringUtils.justifyTexts(info, 100);
+                       if (!info.isEmpty()) {
+                               info = info + "\n";
+                               JTextArea text = new JTextArea(info);
+                               text.setWrapStyleWord(true);
+                               text.setOpaque(false);
+                               text.setForeground(new Color(100, 100, 180));
+                               text.setEditable(false);
+                               pane.add(text);
+                       }
+
+                       for (MetaInfo<E> subitem : item) {
+                               addItem(pane, subitem, nhgap + 11);
+                       }
+                       bpane.add(pane, BorderLayout.CENTER);
+                       main.add(bpane);
+               } else {
+                       items.add(item);
+                       main.add(ConfigItem.createItem(item, nhgap));
+               }
        }
 
        /**