ConfigItem: add all types except LOCALE (array still not working)
[fanfix.git] / src / be / nikiroo / utils / ui / ConfigEditor.java
CommitLineData
d350b96b
NR
1package be.nikiroo.utils.ui;
2
3import java.awt.BorderLayout;
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.io.IOException;
7import java.util.List;
8
9import javax.swing.BoxLayout;
10import javax.swing.JButton;
49f79f31
NR
11import javax.swing.JComponent;
12import javax.swing.JLabel;
d350b96b 13import javax.swing.JPanel;
49f79f31 14import javax.swing.JScrollPane;
d350b96b
NR
15import javax.swing.border.EmptyBorder;
16
17import be.nikiroo.utils.resources.Bundle;
9e834013 18import be.nikiroo.utils.resources.MetaInfo;
d350b96b
NR
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
db31c358 28 *
d350b96b
NR
29 * @param <E>
30 * the type of {@link Bundle} to edit
31 */
32public class ConfigEditor<E extends Enum<E>> extends JPanel {
33 private static final long serialVersionUID = 1L;
9e834013 34 private List<MetaInfo<E>> items;
d350b96b
NR
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
49f79f31
NR
43 * @param title
44 * the title to display before the options
d350b96b 45 */
49f79f31
NR
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));
d350b96b 57
9e834013
NR
58 items = MetaInfo.getItems(type, bundle);
59 for (MetaInfo<E> item : items) {
60 main.add(new ConfigItem<E>(item));
d350b96b
NR
61 }
62
49f79f31 63 main.add(createButton("Reset", new ActionListener() {
cd0c27d2 64 @Override
d350b96b 65 public void actionPerformed(ActionEvent e) {
9e834013 66 for (MetaInfo<E> item : items) {
d350b96b
NR
67 item.reload();
68 }
69 }
49f79f31 70 }));
d350b96b 71
49f79f31 72 main.add(createButton("Default", new ActionListener() {
cd0c27d2 73 @Override
d350b96b
NR
74 public void actionPerformed(ActionEvent e) {
75 Object snap = bundle.takeSnapshot();
76 bundle.reload(true);
9e834013 77 for (MetaInfo<E> item : items) {
d350b96b
NR
78 item.reload();
79 }
80 bundle.reload(false);
81 bundle.restoreSnapshot(snap);
82 }
49f79f31 83 }));
d350b96b 84
49f79f31 85 main.add(createButton("Save", new ActionListener() {
cd0c27d2 86 @Override
d350b96b 87 public void actionPerformed(ActionEvent e) {
9e834013 88 for (MetaInfo<E> item : items) {
d350b96b
NR
89 item.save();
90 }
91
92 try {
93 bundle.updateFile();
94 } catch (IOException e1) {
95 e1.printStackTrace();
96 }
97 }
49f79f31 98 }));
d350b96b
NR
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 */
49f79f31 109 private JComponent createButton(String title, ActionListener listener) {
d350b96b
NR
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
49f79f31 118 return panel;
d350b96b
NR
119 }
120}