6b820bb1a45c183a9a72f695a9394506c032ef99
1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.event
.ActionEvent
;
5 import java
.awt
.event
.ActionListener
;
6 import java
.io
.IOException
;
9 import javax
.swing
.BoxLayout
;
10 import javax
.swing
.JButton
;
11 import javax
.swing
.JPanel
;
12 import javax
.swing
.border
.EmptyBorder
;
14 import be
.nikiroo
.utils
.resources
.Bundle
;
17 * A configuration panel for a {@link Bundle}.
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
26 * the type of {@link Bundle} to edit
28 public class ConfigEditor
<E
extends Enum
<E
>> extends JPanel
{
29 private static final long serialVersionUID
= 1L;
30 private List
<ConfigItem
<E
>> items
;
33 * Create a new {@link ConfigEditor} for this {@link Bundle}.
36 * a class instance of the item type to work on
38 * the {@link Bundle} to sort through
40 public ConfigEditor(Class
<E
> type
, final Bundle
<E
> bundle
) {
41 this.setLayout(new BoxLayout(this, BoxLayout
.PAGE_AXIS
));
43 items
= ConfigItem
.getItems(type
, bundle
);
44 for (ConfigItem
<E
> item
: items
) {
48 addButton("Reset", new ActionListener() {
49 public void actionPerformed(ActionEvent e
) {
50 for (ConfigItem
<E
> item
: items
) {
56 addButton("Default", new ActionListener() {
57 public void actionPerformed(ActionEvent e
) {
58 Object snap
= bundle
.takeSnapshot();
60 for (ConfigItem
<E
> item
: items
) {
64 bundle
.restoreSnapshot(snap
);
68 addButton("Save", new ActionListener() {
69 public void actionPerformed(ActionEvent e
) {
70 for (ConfigItem
<E
> item
: items
) {
76 } catch (IOException e1
) {
84 * Add an action button for this action.
91 private void addButton(String title
, ActionListener listener
) {
92 JButton button
= new JButton(title
);
93 button
.addActionListener(listener
);
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
);