fix Bundles
[fanfix.git] / src / be / nikiroo / utils / ui / ConfigEditor.java
CommitLineData
d350b96b
NR
1package be.nikiroo.utils.ui;
2
3import java.awt.BorderLayout;
d18e136e 4import java.awt.Color;
d350b96b
NR
5import java.awt.event.ActionEvent;
6import java.awt.event.ActionListener;
7import java.io.IOException;
8import java.util.List;
9
10import javax.swing.BoxLayout;
11import javax.swing.JButton;
49f79f31
NR
12import javax.swing.JComponent;
13import javax.swing.JLabel;
d350b96b 14import javax.swing.JPanel;
49f79f31 15import javax.swing.JScrollPane;
d18e136e 16import javax.swing.JTextArea;
d350b96b 17import javax.swing.border.EmptyBorder;
d18e136e 18import javax.swing.border.TitledBorder;
d350b96b 19
d18e136e 20import be.nikiroo.utils.StringUtils;
d350b96b 21import be.nikiroo.utils.resources.Bundle;
9e834013 22import be.nikiroo.utils.resources.MetaInfo;
d350b96b
NR
23
24/**
25 * A configuration panel for a {@link Bundle}.
26 * <p>
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
29 * values.
30 *
31 * @author niki
db31c358 32 *
d350b96b
NR
33 * @param <E>
34 * the type of {@link Bundle} to edit
35 */
36public class ConfigEditor<E extends Enum<E>> extends JPanel {
37 private static final long serialVersionUID = 1L;
9e834013 38 private List<MetaInfo<E>> items;
d350b96b
NR
39
40 /**
41 * Create a new {@link ConfigEditor} for this {@link Bundle}.
42 *
43 * @param type
44 * a class instance of the item type to work on
45 * @param bundle
46 * the {@link Bundle} to sort through
49f79f31
NR
47 * @param title
48 * the title to display before the options
d350b96b 49 */
49f79f31
NR
50 public ConfigEditor(Class<E> type, final Bundle<E> bundle, String title) {
51 this.setLayout(new BorderLayout());
52 JPanel main = new JPanel();
53
54 JScrollPane scroll = new JScrollPane(main);
55 scroll.getVerticalScrollBar().setUnitIncrement(16);
56 this.add(scroll, BorderLayout.CENTER);
57
58 main.setLayout(new BoxLayout(main, BoxLayout.PAGE_AXIS));
d18e136e
NR
59 main.setBorder(new EmptyBorder(5, 5, 5, 5));
60
49f79f31 61 main.add(new JLabel(title));
d350b96b 62
9e834013
NR
63 items = MetaInfo.getItems(type, bundle);
64 for (MetaInfo<E> item : items) {
d18e136e 65 addItem(main, item, 0);
d350b96b
NR
66 }
67
49f79f31 68 main.add(createButton("Reset", new ActionListener() {
cd0c27d2 69 @Override
d350b96b 70 public void actionPerformed(ActionEvent e) {
9e834013 71 for (MetaInfo<E> item : items) {
d350b96b
NR
72 item.reload();
73 }
74 }
49f79f31 75 }));
d350b96b 76
49f79f31 77 main.add(createButton("Default", new ActionListener() {
cd0c27d2 78 @Override
d350b96b
NR
79 public void actionPerformed(ActionEvent e) {
80 Object snap = bundle.takeSnapshot();
81 bundle.reload(true);
9e834013 82 for (MetaInfo<E> item : items) {
d350b96b
NR
83 item.reload();
84 }
85 bundle.reload(false);
86 bundle.restoreSnapshot(snap);
87 }
49f79f31 88 }));
d350b96b 89
49f79f31 90 main.add(createButton("Save", new ActionListener() {
cd0c27d2 91 @Override
d350b96b 92 public void actionPerformed(ActionEvent e) {
9e834013 93 for (MetaInfo<E> item : items) {
d350b96b
NR
94 item.save();
95 }
96
97 try {
98 bundle.updateFile();
99 } catch (IOException e1) {
100 e1.printStackTrace();
101 }
102 }
49f79f31 103 }));
d350b96b
NR
104 }
105
d18e136e 106 private void addItem(JPanel main, MetaInfo<E> item, int nhgap) {
76b51de9 107 if (item.isGroup()) {
d18e136e
NR
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));
113
114 String info = item.getDescription();
115 info = StringUtils.justifyTexts(info, 100);
116 if (!info.isEmpty()) {
117 JTextArea text = new JTextArea(info);
118 text.setWrapStyleWord(true);
119 text.setOpaque(false);
120 text.setForeground(new Color(100, 100, 180));
121 text.setEditable(false);
122 pane.add(text);
123 }
124
76b51de9 125 for (MetaInfo<E> subitem : item) {
d18e136e 126 addItem(pane, subitem, nhgap + 11);
76b51de9 127 }
d18e136e
NR
128 bpane.add(pane, BorderLayout.CENTER);
129 main.add(bpane);
76b51de9 130 } else {
d18e136e 131 main.add(new ConfigItem<E>(item, nhgap));
76b51de9
NR
132 }
133 }
134
d350b96b
NR
135 /**
136 * Add an action button for this action.
137 *
138 * @param title
139 * the action title
140 * @param listener
141 * the action
142 */
49f79f31 143 private JComponent createButton(String title, ActionListener listener) {
d350b96b
NR
144 JButton button = new JButton(title);
145 button.addActionListener(listener);
146
147 JPanel panel = new JPanel();
148 panel.setLayout(new BorderLayout());
149 panel.setBorder(new EmptyBorder(2, 10, 2, 10));
150 panel.add(button, BorderLayout.CENTER);
151
49f79f31 152 return panel;
d350b96b
NR
153 }
154}