ConfigItem: remove logic from UI, improve UI
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / ConfigEditor.java
1 package be.nikiroo.utils.ui;
2
3 import java.awt.BorderLayout;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.io.IOException;
7 import java.util.List;
8
9 import javax.swing.BoxLayout;
10 import javax.swing.JButton;
11 import javax.swing.JComponent;
12 import javax.swing.JLabel;
13 import javax.swing.JPanel;
14 import javax.swing.JScrollPane;
15 import javax.swing.border.EmptyBorder;
16
17 import be.nikiroo.utils.resources.Bundle;
18 import be.nikiroo.utils.resources.MetaInfo;
19
20 /**
21 * A configuration panel for a {@link Bundle}.
22 * <p>
23 * All the items in the given {@link Bundle} will be displayed in editable
24 * controls, with options to Save, Reset and/or Reset to the application default
25 * values.
26 *
27 * @author niki
28 *
29 * @param <E>
30 * the type of {@link Bundle} to edit
31 */
32 public class ConfigEditor<E extends Enum<E>> extends JPanel {
33 private static final long serialVersionUID = 1L;
34 private List<MetaInfo<E>> items;
35
36 /**
37 * Create a new {@link ConfigEditor} for this {@link Bundle}.
38 *
39 * @param type
40 * a class instance of the item type to work on
41 * @param bundle
42 * the {@link Bundle} to sort through
43 * @param title
44 * the title to display before the options
45 */
46 public ConfigEditor(Class<E> type, final Bundle<E> bundle, String title) {
47 this.setLayout(new BorderLayout());
48 JPanel main = new JPanel();
49
50 JScrollPane scroll = new JScrollPane(main);
51 scroll.getVerticalScrollBar().setUnitIncrement(16);
52 this.add(scroll, BorderLayout.CENTER);
53
54 main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
55
56 main.add(new JLabel(title));
57
58 items = MetaInfo.getItems(type, bundle);
59 for (MetaInfo<E> item : items) {
60 main.add(new ConfigItem<E>(item));
61 }
62
63 main.add(createButton("Reset", new ActionListener() {
64 @Override
65 public void actionPerformed(ActionEvent e) {
66 for (MetaInfo<E> item : items) {
67 item.reload();
68 }
69 }
70 }));
71
72 main.add(createButton("Default", new ActionListener() {
73 @Override
74 public void actionPerformed(ActionEvent e) {
75 Object snap = bundle.takeSnapshot();
76 bundle.reload(true);
77 for (MetaInfo<E> item : items) {
78 item.reload();
79 }
80 bundle.reload(false);
81 bundle.restoreSnapshot(snap);
82 }
83 }));
84
85 main.add(createButton("Save", new ActionListener() {
86 @Override
87 public void actionPerformed(ActionEvent e) {
88 for (MetaInfo<E> item : items) {
89 item.save();
90 }
91
92 try {
93 bundle.updateFile();
94 } catch (IOException e1) {
95 e1.printStackTrace();
96 }
97 }
98 }));
99 }
100
101 /**
102 * Add an action button for this action.
103 *
104 * @param title
105 * the action title
106 * @param listener
107 * the action
108 */
109 private JComponent createButton(String title, ActionListener listener) {
110 JButton button = new JButton(title);
111 button.addActionListener(listener);
112
113 JPanel panel = new JPanel();
114 panel.setLayout(new BorderLayout());
115 panel.setBorder(new EmptyBorder(2, 10, 2, 10));
116 panel.add(button, BorderLayout.CENTER);
117
118 return panel;
119 }
120 }