ConfigItem: improve logic, UI
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / ConfigItem.java
1 package be.nikiroo.utils.ui;
2
3 import java.awt.BorderLayout;
4
5 import javax.swing.JCheckBox;
6 import javax.swing.JLabel;
7 import javax.swing.JPanel;
8 import javax.swing.JTextField;
9
10 import be.nikiroo.utils.resources.Bundle;
11 import be.nikiroo.utils.resources.Meta.Format;
12 import be.nikiroo.utils.resources.MetaInfo;
13
14 /**
15 * A graphical item that reflect a configuration option from the given
16 * {@link Bundle}.
17 *
18 * @author niki
19 *
20 * @param <E>
21 * the type of {@link Bundle} to edit
22 */
23 public class ConfigItem<E extends Enum<E>> extends JPanel {
24 private static final long serialVersionUID = 1L;
25
26 public ConfigItem(final MetaInfo<E> info) {
27 this.setLayout(new BorderLayout());
28 // this.setBorder(new EmptyBorder(2, 10, 2, 10));
29
30 if (info.getFormat() == Format.BOOLEAN) {
31 final JCheckBox field = new JCheckBox();
32 field.setToolTipText(info.getDescription());
33 Boolean state = info.getBoolean();
34 if (state == null) {
35 info.getDefaultBoolean();
36 }
37
38 // Should not happen!
39 if (state == null) {
40 System.err
41 .println("No default value given for BOOLEAN parameter \""
42 + info.getName()
43 + "\", we consider it is FALSE");
44 state = false;
45 }
46
47 field.setSelected(state);
48
49 info.addReloadedListener(new Runnable() {
50 @Override
51 public void run() {
52 Boolean state = info.getBoolean();
53 if (state == null) {
54 info.getDefaultBoolean();
55 }
56 if (state == null) {
57 state = false;
58 }
59
60 field.setSelected(state);
61 }
62 });
63 info.addSaveListener(new Runnable() {
64 @Override
65 public void run() {
66 info.setBoolean(field.isSelected());
67 }
68 });
69
70 this.add(new JLabel(info.getName() + ": "), BorderLayout.WEST);
71 this.add(field, BorderLayout.CENTER);
72 } else {
73 final JTextField field = new JTextField();
74 field.setToolTipText(info.getDescription());
75 field.setText(info.getString());
76
77 info.addReloadedListener(new Runnable() {
78 @Override
79 public void run() {
80 field.setText(info.getString());
81 }
82 });
83 info.addSaveListener(new Runnable() {
84 @Override
85 public void run() {
86 info.setString(field.getText());
87 }
88 });
89
90 this.add(new JLabel(info.getName() + ": "), BorderLayout.WEST);
91 this.add(field, BorderLayout.CENTER);
92 }
93 }
94 }