1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.Dimension
;
5 import java
.util
.ArrayList
;
8 import javax
.swing
.JLabel
;
9 import javax
.swing
.JPanel
;
10 import javax
.swing
.JTextField
;
11 import javax
.swing
.border
.EmptyBorder
;
13 import be
.nikiroo
.utils
.resources
.Bundle
;
16 * A graphical item that reflect a configuration option from the given
22 * the type of {@link Bundle} to edit
24 public class ConfigItem
<E
extends Enum
<E
>> extends JPanel
{
25 private static final long serialVersionUID
= 1L;
26 private final Bundle
<E
> bundle
;
30 private JTextField valueField
;
32 public ConfigItem(Class
<E
> type
, Bundle
<E
> bundle
, E id
) {
36 this.setLayout(new BorderLayout());
37 this.setBorder(new EmptyBorder(2, 10, 2, 10));
39 String name
= id
.toString();
40 if (name
.length() > 1) {
41 name
= name
.substring(0, 1) + name
.substring(1).toLowerCase();
42 name
= name
.replace("_", " ");
45 JLabel nameLabel
= new JLabel(name
);
46 nameLabel
.setPreferredSize(new Dimension(400, 0));
47 this.add(nameLabel
, BorderLayout
.WEST
);
49 valueField
= new JTextField();
50 valueField
.setText(value
);
53 this.add(valueField
, BorderLayout
.CENTER
);
57 * Reload the value from the {@link Bundle}.
59 public void reload() {
60 value
= bundle
.getString(id
);
61 valueField
.setText(value
);
65 * Save the current value to the {@link Bundle}.
68 value
= valueField
.getText();
69 bundle
.setString(id
, value
);
73 * Create a list of {@link ConfigItem}, one for each of the item in the
74 * given {@link Bundle}.
77 * a class instance of the item type to work on
79 * the {@link Bundle} to sort through
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
));