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
;
10 import javax
.swing
.BoxLayout
;
11 import javax
.swing
.JButton
;
12 import javax
.swing
.JComponent
;
13 import javax
.swing
.JLabel
;
14 import javax
.swing
.JPanel
;
15 import javax
.swing
.JScrollPane
;
16 import javax
.swing
.JTextArea
;
17 import javax
.swing
.border
.EmptyBorder
;
18 import javax
.swing
.border
.TitledBorder
;
20 import be
.nikiroo
.utils
.StringUtils
;
21 import be
.nikiroo
.utils
.resources
.Bundle
;
22 import be
.nikiroo
.utils
.resources
.MetaInfo
;
25 * A configuration panel for a {@link Bundle}.
27 * All the items in the given {@link Bundle} will be displayed in editable
28 * controls, with options to Save, Reset and/or Reset to the application default
34 * the type of {@link Bundle} to edit
36 public class ConfigEditor
<E
extends Enum
<E
>> extends JPanel
{
37 private static final long serialVersionUID
= 1L;
38 private List
<MetaInfo
<E
>> items
;
41 * Create a new {@link ConfigEditor} for this {@link Bundle}.
44 * a class instance of the item type to work on
46 * the {@link Bundle} to sort through
48 * the title to display before the options
50 public ConfigEditor(Class
<E
> type
, final Bundle
<E
> bundle
, String title
) {
51 this.setLayout(new BorderLayout());
52 JPanel main
= new JPanel();
54 JScrollPane scroll
= new JScrollPane(main
);
55 scroll
.getVerticalScrollBar().setUnitIncrement(16);
56 this.add(scroll
, BorderLayout
.CENTER
);
58 main
.setLayout(new BoxLayout(main
, BoxLayout
.PAGE_AXIS
));
59 main
.setBorder(new EmptyBorder(5, 5, 5, 5));
61 main
.add(new JLabel(title
));
63 items
= MetaInfo
.getItems(type
, bundle
);
64 for (MetaInfo
<E
> item
: items
) {
65 addItem(main
, item
, 0);
68 main
.add(createButton("Reset", new ActionListener() {
70 public void actionPerformed(ActionEvent e
) {
71 for (MetaInfo
<E
> item
: items
) {
77 main
.add(createButton("Default", new ActionListener() {
79 public void actionPerformed(ActionEvent e
) {
80 Object snap
= bundle
.takeSnapshot();
82 for (MetaInfo
<E
> item
: items
) {
86 bundle
.restoreSnapshot(snap
);
90 main
.add(createButton("Save", new ActionListener() {
92 public void actionPerformed(ActionEvent e
) {
93 for (MetaInfo
<E
> item
: items
) {
99 } catch (IOException e1
) {
100 e1
.printStackTrace();
106 private void addItem(JPanel main
, MetaInfo
<E
> item
, int nhgap
) {
107 if (item
.isGroup()) {
108 JPanel bpane
= new JPanel(new BorderLayout());
109 bpane
.setBorder(new TitledBorder(item
.getName()));
110 JPanel pane
= new JPanel();
111 pane
.setBorder(new EmptyBorder(5, 5, 5, 5));
112 pane
.setLayout(new BoxLayout(pane
, BoxLayout
.Y_AXIS
));
114 String info
= item
.getDescription();
115 info
= StringUtils
.justifyTexts(info
, 100);
116 if (!info
.isEmpty()) {
118 JTextArea text
= new JTextArea(info
);
119 text
.setWrapStyleWord(true);
120 text
.setOpaque(false);
121 text
.setForeground(new Color(100, 100, 180));
122 text
.setEditable(false);
126 for (MetaInfo
<E
> subitem
: item
) {
127 addItem(pane
, subitem
, nhgap
+ 11);
129 bpane
.add(pane
, BorderLayout
.CENTER
);
132 main
.add(new ConfigItem
<E
>(item
, nhgap
));
137 * Add an action button for this action.
144 private JComponent
createButton(String title
, ActionListener listener
) {
145 JButton button
= new JButton(title
);
146 button
.addActionListener(listener
);
148 JPanel panel
= new JPanel();
149 panel
.setLayout(new BorderLayout());
150 panel
.setBorder(new EmptyBorder(2, 10, 2, 10));
151 panel
.add(button
, BorderLayout
.CENTER
);