Commit | Line | Data |
---|---|---|
d350b96b NR |
1 | package be.nikiroo.utils.ui; |
2 | ||
3 | import java.awt.BorderLayout; | |
4 | import java.awt.Dimension; | |
5 | import java.util.ArrayList; | |
6 | import java.util.List; | |
7 | ||
8 | import javax.swing.JLabel; | |
9 | import javax.swing.JPanel; | |
10 | import javax.swing.JTextField; | |
11 | import javax.swing.border.EmptyBorder; | |
12 | ||
13 | import be.nikiroo.utils.resources.Bundle; | |
14 | ||
15 | /** | |
16 | * A graphical item that reflect a configuration option from the given | |
17 | * {@link Bundle}. | |
18 | * | |
19 | * @author niki | |
20 | * | |
21 | * @param <E> | |
22 | * the type of {@link Bundle} to edit | |
23 | */ | |
24 | public class ConfigItem<E extends Enum<E>> extends JPanel { | |
25 | private static final long serialVersionUID = 1L; | |
26 | private final Bundle<E> bundle; | |
27 | private final E id; | |
28 | private String value; | |
29 | ||
30 | private JTextField valueField; | |
31 | ||
32 | public ConfigItem(Class<E> type, Bundle<E> bundle, E id) { | |
33 | this.bundle = bundle; | |
34 | this.id = id; | |
35 | ||
36 | this.setLayout(new BorderLayout()); | |
37 | this.setBorder(new EmptyBorder(2, 10, 2, 10)); | |
38 | ||
49f79f31 NR |
39 | String name = id.toString(); |
40 | if (name.length() > 1) { | |
41 | name = name.substring(0, 1) + name.substring(1).toLowerCase(); | |
42 | name = name.replace("_", " "); | |
43 | } | |
44 | ||
45 | JLabel nameLabel = new JLabel(name); | |
d350b96b NR |
46 | nameLabel.setPreferredSize(new Dimension(400, 0)); |
47 | this.add(nameLabel, BorderLayout.WEST); | |
48 | ||
49 | valueField = new JTextField(); | |
50 | valueField.setText(value); | |
51 | ||
52 | reload(); | |
53 | this.add(valueField, BorderLayout.CENTER); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Reload the value from the {@link Bundle}. | |
58 | */ | |
59 | public void reload() { | |
60 | value = bundle.getString(id); | |
61 | valueField.setText(value); | |
62 | } | |
63 | ||
64 | /** | |
65 | * Save the current value to the {@link Bundle}. | |
66 | */ | |
67 | public void save() { | |
68 | value = valueField.getText(); | |
69 | bundle.setString(id, value); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Create a list of {@link ConfigItem}, one for each of the item in the | |
74 | * given {@link Bundle}. | |
75 | * | |
76 | * @param type | |
77 | * a class instance of the item type to work on | |
78 | * @param bundle | |
79 | * the {@link Bundle} to sort through | |
80 | * | |
81 | * @return the list | |
82 | */ | |
83 | static public <E extends Enum<E>> List<ConfigItem<E>> getItems( | |
84 | Class<E> type, Bundle<E> bundle) { | |
85 | List<ConfigItem<E>> list = new ArrayList<ConfigItem<E>>(); | |
86 | for (E id : type.getEnumConstants()) { | |
87 | list.add(new ConfigItem<E>(type, bundle, id)); | |
88 | } | |
89 | ||
90 | return list; | |
91 | } | |
92 | } |