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 | ||
39 | JLabel nameLabel = new JLabel(id.toString()); | |
40 | nameLabel.setPreferredSize(new Dimension(400, 0)); | |
41 | this.add(nameLabel, BorderLayout.WEST); | |
42 | ||
43 | valueField = new JTextField(); | |
44 | valueField.setText(value); | |
45 | ||
46 | reload(); | |
47 | this.add(valueField, BorderLayout.CENTER); | |
48 | } | |
49 | ||
50 | /** | |
51 | * Reload the value from the {@link Bundle}. | |
52 | */ | |
53 | public void reload() { | |
54 | value = bundle.getString(id); | |
55 | valueField.setText(value); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Save the current value to the {@link Bundle}. | |
60 | */ | |
61 | public void save() { | |
62 | value = valueField.getText(); | |
63 | bundle.setString(id, value); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Create a list of {@link ConfigItem}, one for each of the item in the | |
68 | * given {@link Bundle}. | |
69 | * | |
70 | * @param type | |
71 | * a class instance of the item type to work on | |
72 | * @param bundle | |
73 | * the {@link Bundle} to sort through | |
74 | * | |
75 | * @return the list | |
76 | */ | |
77 | static public <E extends Enum<E>> List<ConfigItem<E>> getItems( | |
78 | Class<E> type, Bundle<E> bundle) { | |
79 | List<ConfigItem<E>> list = new ArrayList<ConfigItem<E>>(); | |
80 | for (E id : type.getEnumConstants()) { | |
81 | list.add(new ConfigItem<E>(type, bundle, id)); | |
82 | } | |
83 | ||
84 | return list; | |
85 | } | |
86 | } |