2651db10e481abef512290a2859dcde672be31b2
[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.TransBundle;
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<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
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 = ConfigItem.getItems(type, bundle);
59 for (ConfigItem<E> item : items) {
60 main.add(item);
61 }
62
63 main.add(createButton("Reset", new ActionListener() {
64 public void actionPerformed(ActionEvent e) {
65 for (ConfigItem<E> item : items) {
66 item.reload();
67 }
68 }
69 }));
70
71 main.add(createButton("Default", new ActionListener() {
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 }
81 }));
82
83 main.add(createButton("Save", new ActionListener() {
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 }
95 }));
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 */
106 private JComponent createButton(String title, ActionListener listener) {
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
115 return panel;
116 }
117 }