X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fui%2FConfigEditor.java;h=c687c98fee5637dfb8e10b7bad32f6be102ef9d5;hb=d46b7b96f94e88a776bcd2dfd756549ffb300cc9;hp=6b820bb1a45c183a9a72f695a9394506c032ef99;hpb=d350b96b06c55189e5d0ceed9db6f237e7d8d871;p=fanfix.git diff --git a/src/be/nikiroo/utils/ui/ConfigEditor.java b/src/be/nikiroo/utils/ui/ConfigEditor.java index 6b820bb..c687c98 100644 --- a/src/be/nikiroo/utils/ui/ConfigEditor.java +++ b/src/be/nikiroo/utils/ui/ConfigEditor.java @@ -1,17 +1,26 @@ 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; import javax.swing.JButton; +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}. @@ -21,13 +30,13 @@ import be.nikiroo.utils.resources.Bundle; * values. * * @author niki - * + * * @param * the type of {@link Bundle} to edit */ public class ConfigEditor> extends JPanel { private static final long serialVersionUID = 1L; - private List> items; + private List> items; /** * Create a new {@link ConfigEditor} for this {@link Bundle}. @@ -36,39 +45,56 @@ public class ConfigEditor> extends JPanel { * a class instance of the item type to work on * @param bundle * the {@link Bundle} to sort through + * @param title + * the title to display before the options */ - public ConfigEditor(Class type, final Bundle bundle) { - this.setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS)); + public ConfigEditor(Class type, final Bundle bundle, String title) { + this.setLayout(new BorderLayout()); + + 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 item : items) { - this.add(item); + items = new ArrayList>(); + List> groupedItems = MetaInfo.getItems(type, bundle); + for (MetaInfo item : groupedItems) { + // will init this.items + addItem(main, item, 0); } - addButton("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 item : items) { + for (MetaInfo item : items) { item.reload(); } } - }); + })); - addButton("Default", new ActionListener() { + buttons.add(createButton("Default", new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { Object snap = bundle.takeSnapshot(); bundle.reload(true); - for (ConfigItem item : items) { + for (MetaInfo item : items) { item.reload(); } bundle.reload(false); bundle.restoreSnapshot(snap); } - }); + })); - addButton("Save", new ActionListener() { + buttons.add(createButton("Save", new ActionListener() { + @Override public void actionPerformed(ActionEvent e) { - for (ConfigItem item : items) { - item.save(); + for (MetaInfo item : items) { + item.save(true); } try { @@ -77,7 +103,44 @@ public class ConfigEditor> extends JPanel { e1.printStackTrace(); } } - }); + })); + + 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 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 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)); + } } /** @@ -88,7 +151,7 @@ public class ConfigEditor> extends JPanel { * @param listener * the action */ - private void addButton(String title, ActionListener listener) { + private JComponent createButton(String title, ActionListener listener) { JButton button = new JButton(title); button.addActionListener(listener); @@ -97,6 +160,6 @@ public class ConfigEditor> extends JPanel { panel.setBorder(new EmptyBorder(2, 10, 2, 10)); panel.add(button, BorderLayout.CENTER); - this.add(panel); + return panel; } }