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
.JComponent
;
12 import javax
.swing
.JLabel
;
13 import javax
.swing
.JPanel
;
14 import javax
.swing
.JScrollPane
;
15 import javax
.swing
.border
.EmptyBorder
;
17 import be
.nikiroo
.utils
.resources
.Bundle
;
18 import be
.nikiroo
.utils
.resources
.MetaInfo
;
21 * A configuration panel for a {@link Bundle}.
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
30 * the type of {@link Bundle} to edit
32 public class ConfigEditor
<E
extends Enum
<E
>> extends JPanel
{
33 private static final long serialVersionUID
= 1L;
34 private List
<MetaInfo
<E
>> items
;
37 * Create a new {@link ConfigEditor} for this {@link Bundle}.
40 * a class instance of the item type to work on
42 * the {@link Bundle} to sort through
44 * the title to display before the options
46 public ConfigEditor(Class
<E
> type
, final Bundle
<E
> bundle
, String title
) {
47 this.setLayout(new BorderLayout());
48 JPanel main
= new JPanel();
50 JScrollPane scroll
= new JScrollPane(main
);
51 scroll
.getVerticalScrollBar().setUnitIncrement(16);
52 this.add(scroll
, BorderLayout
.CENTER
);
54 main
.setLayout(new BoxLayout(main
, BoxLayout
.PAGE_AXIS
));
56 main
.add(new JLabel(title
));
58 items
= MetaInfo
.getItems(type
, bundle
);
59 for (MetaInfo
<E
> item
: items
) {
63 main
.add(createButton("Reset", new ActionListener() {
65 public void actionPerformed(ActionEvent e
) {
66 for (MetaInfo
<E
> item
: items
) {
72 main
.add(createButton("Default", new ActionListener() {
74 public void actionPerformed(ActionEvent e
) {
75 Object snap
= bundle
.takeSnapshot();
77 for (MetaInfo
<E
> item
: items
) {
81 bundle
.restoreSnapshot(snap
);
85 main
.add(createButton("Save", new ActionListener() {
87 public void actionPerformed(ActionEvent e
) {
88 for (MetaInfo
<E
> item
: items
) {
94 } catch (IOException e1
) {
101 private void addItem(JPanel main
, MetaInfo
<E
> item
) {
102 if (item
.isGroup()) {
104 for (MetaInfo
<E
> subitem
: item
) {
105 addItem(main
, subitem
);
108 main
.add(new ConfigItem
<E
>(item
));
113 * Add an action button for this action.
120 private JComponent
createButton(String title
, ActionListener listener
) {
121 JButton button
= new JButton(title
);
122 button
.addActionListener(listener
);
124 JPanel panel
= new JPanel();
125 panel
.setLayout(new BorderLayout());
126 panel
.setBorder(new EmptyBorder(2, 10, 2, 10));
127 panel
.add(button
, BorderLayout
.CENTER
);