1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.BorderLayout
;
5 import java
.awt
.event
.ActionEvent
;
6 import java
.awt
.event
.ActionListener
;
7 import java
.io
.IOException
;
8 import java
.util
.ArrayList
;
11 import javax
.swing
.BoxLayout
;
12 import javax
.swing
.JButton
;
13 import javax
.swing
.JComponent
;
14 import javax
.swing
.JLabel
;
15 import javax
.swing
.JPanel
;
16 import javax
.swing
.JScrollPane
;
17 import javax
.swing
.JTextArea
;
18 import javax
.swing
.border
.EmptyBorder
;
19 import javax
.swing
.border
.TitledBorder
;
21 import be
.nikiroo
.utils
.StringUtils
;
22 import be
.nikiroo
.utils
.resources
.Bundle
;
23 import be
.nikiroo
.utils
.resources
.MetaInfo
;
26 * A configuration panel for a {@link Bundle}.
28 * All the items in the given {@link Bundle} will be displayed in editable
29 * controls, with options to Save, Reset and/or Reset to the application default
35 * the type of {@link Bundle} to edit
37 public class ConfigEditor
<E
extends Enum
<E
>> extends JPanel
{
38 private static final long serialVersionUID
= 1L;
39 private List
<MetaInfo
<E
>> items
;
42 * Create a new {@link ConfigEditor} for this {@link Bundle}.
45 * a class instance of the item type to work on
47 * the {@link Bundle} to sort through
49 * the title to display before the options
51 public ConfigEditor(Class
<E
> type
, final Bundle
<E
> bundle
, String title
) {
52 this.setLayout(new BorderLayout());
54 JPanel main
= new JPanel();
55 main
.setLayout(new BoxLayout(main
, BoxLayout
.PAGE_AXIS
));
56 main
.setBorder(new EmptyBorder(5, 5, 5, 5));
58 main
.add(new JLabel(title
));
60 items
= new ArrayList
<MetaInfo
<E
>>();
61 List
<MetaInfo
<E
>> groupedItems
= MetaInfo
.getItems(type
, bundle
);
62 for (MetaInfo
<E
> item
: groupedItems
) {
63 // will init this.items
64 addItem(main
, item
, 0);
67 JPanel buttons
= new JPanel();
68 buttons
.setLayout(new BoxLayout(buttons
, BoxLayout
.PAGE_AXIS
));
69 buttons
.setBorder(new EmptyBorder(5, 5, 5, 5));
71 buttons
.add(createButton("Reset", new ActionListener() {
73 public void actionPerformed(ActionEvent e
) {
74 for (MetaInfo
<E
> item
: items
) {
80 buttons
.add(createButton("Default", new ActionListener() {
82 public void actionPerformed(ActionEvent e
) {
83 Object snap
= bundle
.takeSnapshot();
85 for (MetaInfo
<E
> item
: items
) {
89 bundle
.restoreSnapshot(snap
);
93 buttons
.add(createButton("Save", new ActionListener() {
95 public void actionPerformed(ActionEvent e
) {
96 for (MetaInfo
<E
> item
: items
) {
102 } catch (IOException e1
) {
103 e1
.printStackTrace();
108 JScrollPane scroll
= new JScrollPane(main
);
109 scroll
.getVerticalScrollBar().setUnitIncrement(16);
111 this.add(scroll
, BorderLayout
.CENTER
);
112 this.add(buttons
, BorderLayout
.SOUTH
);
115 private void addItem(JPanel main
, MetaInfo
<E
> item
, int nhgap
) {
116 if (item
.isGroup()) {
117 JPanel bpane
= new JPanel(new BorderLayout());
118 bpane
.setBorder(new TitledBorder(item
.getName()));
119 JPanel pane
= new JPanel();
120 pane
.setBorder(new EmptyBorder(5, 5, 5, 5));
121 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.Y_AXIS
));
123 String info
= item
.getDescription();
124 info
= StringUtils
.justifyTexts(info
, 100);
125 if (!info
.isEmpty()) {
127 JTextArea text
= new JTextArea(info
);
128 text
.setWrapStyleWord(true);
129 text
.setOpaque(false);
130 text
.setForeground(new Color(100, 100, 180));
131 text
.setEditable(false);
135 for (MetaInfo
<E
> subitem
: item
) {
136 addItem(pane
, subitem
, nhgap
+ 11);
138 bpane
.add(pane
, BorderLayout
.CENTER
);
142 main
.add(ConfigItem
.createItem(item
, nhgap
));
147 * Add an action button for this action.
154 private JComponent
createButton(String title
, ActionListener listener
) {
155 JButton button
= new JButton(title
);
156 button
.addActionListener(listener
);
158 JPanel panel
= new JPanel();
159 panel
.setLayout(new BorderLayout());
160 panel
.setBorder(new EmptyBorder(2, 10, 2, 10));
161 panel
.add(button
, BorderLayout
.CENTER
);