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; | |
49f79f31 NR |
11 | import javax.swing.JComponent; |
12 | import javax.swing.JLabel; | |
d350b96b | 13 | import javax.swing.JPanel; |
49f79f31 | 14 | import javax.swing.JScrollPane; |
d350b96b NR |
15 | import javax.swing.border.EmptyBorder; |
16 | ||
17 | import 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 | |
db31c358 | 27 | * |
d350b96b NR |
28 | * @param <E> |
29 | * the type of {@link Bundle} to edit | |
30 | */ | |
31 | public 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() { |
cd0c27d2 | 63 | @Override |
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() { |
cd0c27d2 | 72 | @Override |
d350b96b NR |
73 | public void actionPerformed(ActionEvent e) { |
74 | Object snap = bundle.takeSnapshot(); | |
75 | bundle.reload(true); | |
76 | for (ConfigItem<E> item : items) { | |
77 | item.reload(); | |
78 | } | |
79 | bundle.reload(false); | |
80 | bundle.restoreSnapshot(snap); | |
81 | } | |
49f79f31 | 82 | })); |
d350b96b | 83 | |
49f79f31 | 84 | main.add(createButton("Save", new ActionListener() { |
cd0c27d2 | 85 | @Override |
d350b96b NR |
86 | public void actionPerformed(ActionEvent e) { |
87 | for (ConfigItem<E> item : items) { | |
88 | item.save(); | |
89 | } | |
90 | ||
91 | try { | |
92 | bundle.updateFile(); | |
93 | } catch (IOException e1) { | |
94 | e1.printStackTrace(); | |
95 | } | |
96 | } | |
49f79f31 | 97 | })); |
d350b96b NR |
98 | } |
99 | ||
100 | /** | |
101 | * Add an action button for this action. | |
102 | * | |
103 | * @param title | |
104 | * the action title | |
105 | * @param listener | |
106 | * the action | |
107 | */ | |
49f79f31 | 108 | private JComponent createButton(String title, ActionListener listener) { |
d350b96b NR |
109 | JButton button = new JButton(title); |
110 | button.addActionListener(listener); | |
111 | ||
112 | JPanel panel = new JPanel(); | |
113 | panel.setLayout(new BorderLayout()); | |
114 | panel.setBorder(new EmptyBorder(2, 10, 2, 10)); | |
115 | panel.add(button, BorderLayout.CENTER); | |
116 | ||
49f79f31 | 117 | return panel; |
d350b96b NR |
118 | } |
119 | } |