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