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
;
14 import be
.nikiroo
.utils
.resources
.Meta
;
17 * A graphical item that reflect a configuration option from the given
23 * the type of {@link Bundle} to edit
25 public class ConfigItem
<E
extends Enum
<E
>> extends JPanel
{
26 private static final long serialVersionUID
= 1L;
27 private Class
<E
> type
;
28 private final Bundle
<E
> bundle
;
34 private JTextField valueField
;
36 public ConfigItem(Class
<E
> type
, Bundle
<E
> bundle
, E id
) {
42 this.meta
= type
.getDeclaredField(id
.name()).getAnnotation(
44 } catch (NoSuchFieldException e
) {
45 } catch (SecurityException e
) {
48 this.setLayout(new BorderLayout());
49 this.setBorder(new EmptyBorder(2, 10, 2, 10));
51 String tooltip
= null;
52 if (bundle
.getDescriptionBundle() != null) {
53 tooltip
= bundle
.getDescriptionBundle().getString(id
);
54 if (tooltip
!= null && tooltip
.trim().isEmpty()) {
59 String name
= id
.toString();
60 if (name
.length() > 1) {
61 name
= name
.substring(0, 1) + name
.substring(1).toLowerCase();
62 name
= name
.replace("_", " ");
65 JLabel nameLabel
= new JLabel(name
);
66 nameLabel
.setToolTipText(tooltip
);
67 nameLabel
.setPreferredSize(new Dimension(400, 0));
68 this.add(nameLabel
, BorderLayout
.WEST
);
70 valueField
= new JTextField();
71 valueField
.setText(value
);
74 this.add(valueField
, BorderLayout
.CENTER
);
78 * Reload the value from the {@link Bundle}.
80 public void reload() {
81 value
= bundle
.getString(id
);
82 valueField
.setText(value
);
86 * Save the current value to the {@link Bundle}.
89 value
= valueField
.getText();
90 bundle
.setString(id
, value
);
94 * Create a list of {@link ConfigItem}, one for each of the item in the
95 * given {@link Bundle}.
98 * the type of {@link Bundle} to edit
100 * a class instance of the item type to work on
102 * the {@link Bundle} to sort through
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
));