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