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; | |
db31c358 | 14 | import be.nikiroo.utils.resources.Meta; |
d350b96b NR |
15 | |
16 | /** | |
17 | * A graphical item that reflect a configuration option from the given | |
18 | * {@link Bundle}. | |
19 | * | |
20 | * @author niki | |
db31c358 | 21 | * |
d350b96b NR |
22 | * @param <E> |
23 | * the type of {@link Bundle} to edit | |
24 | */ | |
25 | public class ConfigItem<E extends Enum<E>> extends JPanel { | |
26 | private static final long serialVersionUID = 1L; | |
db31c358 | 27 | private Class<E> type; |
d350b96b NR |
28 | private final Bundle<E> bundle; |
29 | private final E id; | |
db31c358 NR |
30 | |
31 | private Meta meta; | |
d350b96b NR |
32 | private String value; |
33 | ||
34 | private JTextField valueField; | |
35 | ||
36 | public ConfigItem(Class<E> type, Bundle<E> bundle, E id) { | |
db31c358 | 37 | this.type = type; |
d350b96b NR |
38 | this.bundle = bundle; |
39 | this.id = id; | |
40 | ||
db31c358 NR |
41 | try { |
42 | this.meta = type.getDeclaredField(id.name()).getAnnotation( | |
43 | Meta.class); | |
44 | } catch (NoSuchFieldException e) { | |
45 | } catch (SecurityException e) { | |
46 | } | |
47 | ||
d350b96b NR |
48 | this.setLayout(new BorderLayout()); |
49 | this.setBorder(new EmptyBorder(2, 10, 2, 10)); | |
50 | ||
db31c358 NR |
51 | String tooltip = null; |
52 | if (bundle.getDescriptionBundle() != null) { | |
53 | tooltip = bundle.getDescriptionBundle().getString(id); | |
82430d85 | 54 | if (tooltip != null && tooltip.trim().isEmpty()) { |
db31c358 NR |
55 | tooltip = null; |
56 | } | |
57 | } | |
58 | ||
49f79f31 NR |
59 | String name = id.toString(); |
60 | if (name.length() > 1) { | |
61 | name = name.substring(0, 1) + name.substring(1).toLowerCase(); | |
62 | name = name.replace("_", " "); | |
63 | } | |
64 | ||
65 | JLabel nameLabel = new JLabel(name); | |
db31c358 | 66 | nameLabel.setToolTipText(tooltip); |
d350b96b NR |
67 | nameLabel.setPreferredSize(new Dimension(400, 0)); |
68 | this.add(nameLabel, BorderLayout.WEST); | |
69 | ||
70 | valueField = new JTextField(); | |
71 | valueField.setText(value); | |
72 | ||
73 | reload(); | |
74 | this.add(valueField, BorderLayout.CENTER); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Reload the value from the {@link Bundle}. | |
79 | */ | |
80 | public void reload() { | |
81 | value = bundle.getString(id); | |
82 | valueField.setText(value); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Save the current value to the {@link Bundle}. | |
87 | */ | |
88 | public void save() { | |
89 | value = valueField.getText(); | |
90 | bundle.setString(id, value); | |
91 | } | |
92 | ||
93 | /** | |
94 | * Create a list of {@link ConfigItem}, one for each of the item in the | |
95 | * given {@link Bundle}. | |
96 | * | |
db31c358 NR |
97 | * @param <E> |
98 | * the type of {@link Bundle} to edit | |
d350b96b NR |
99 | * @param type |
100 | * a class instance of the item type to work on | |
101 | * @param bundle | |
102 | * the {@link Bundle} to sort through | |
103 | * | |
104 | * @return the list | |
105 | */ | |
106 | static public <E extends Enum<E>> List<ConfigItem<E>> getItems( | |
107 | Class<E> type, Bundle<E> bundle) { | |
108 | List<ConfigItem<E>> list = new ArrayList<ConfigItem<E>>(); | |
109 | for (E id : type.getEnumConstants()) { | |
110 | list.add(new ConfigItem<E>(type, bundle, id)); | |
111 | } | |
112 | ||
113 | return list; | |
114 | } | |
115 | } |