Update to version 1.5.0 (breaking Bundle/Meta)
[nikiroo-utils.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;
db31c358 18import be.nikiroo.utils.resources.TransBundle;
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;
34 private List<ConfigItem<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
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
NR
57
58 items = ConfigItem.getItems(type, bundle);
59 for (ConfigItem<E> item : items) {
49f79f31 60 main.add(item);
d350b96b
NR
61 }
62
49f79f31 63 main.add(createButton("Reset", new ActionListener() {
d350b96b
NR
64 public void actionPerformed(ActionEvent e) {
65 for (ConfigItem<E> item : items) {
66 item.reload();
67 }
68 }
49f79f31 69 }));
d350b96b 70
49f79f31 71 main.add(createButton("Default", new ActionListener() {
d350b96b
NR
72 public void actionPerformed(ActionEvent e) {
73 Object snap = bundle.takeSnapshot();
74 bundle.reload(true);
75 for (ConfigItem<E> item : items) {
76 item.reload();
77 }
78 bundle.reload(false);
79 bundle.restoreSnapshot(snap);
80 }
49f79f31 81 }));
d350b96b 82
49f79f31 83 main.add(createButton("Save", new ActionListener() {
d350b96b
NR
84 public void actionPerformed(ActionEvent e) {
85 for (ConfigItem<E> item : items) {
86 item.save();
87 }
88
89 try {
90 bundle.updateFile();
91 } catch (IOException e1) {
92 e1.printStackTrace();
93 }
94 }
49f79f31 95 }));
d350b96b
NR
96 }
97
98 /**
99 * Add an action button for this action.
100 *
101 * @param title
102 * the action title
103 * @param listener
104 * the action
105 */
49f79f31 106 private JComponent createButton(String title, ActionListener listener) {
d350b96b
NR
107 JButton button = new JButton(title);
108 button.addActionListener(listener);
109
110 JPanel panel = new JPanel();
111 panel.setLayout(new BorderLayout());
112 panel.setBorder(new EmptyBorder(2, 10, 2, 10));
113 panel.add(button, BorderLayout.CENTER);
114
49f79f31 115 return panel;
d350b96b
NR
116 }
117}