1 package be
.nikiroo
.utils
.resources
;
3 import java
.util
.ArrayList
;
6 import be
.nikiroo
.utils
.resources
.Meta
.Format
;
9 * A graphical item that reflect a configuration option from the given
15 * the type of {@link Bundle} to edit
17 public class MetaInfo
<E
extends Enum
<E
>> {
18 private final Bundle
<E
> bundle
;
24 private List
<Runnable
> reloadListeners
= new ArrayList
<Runnable
>();
27 private String description
;
29 public MetaInfo(Class
<E
> type
, Bundle
<E
> bundle
, E id
) {
34 this.meta
= type
.getDeclaredField(id
.name()).getAnnotation(
36 } catch (NoSuchFieldException e
) {
37 } catch (SecurityException e
) {
40 // We consider that if a description bundle is used, everything is in it
42 String description
= null;
43 if (bundle
.getDescriptionBundle() != null) {
44 description
= bundle
.getDescriptionBundle().getString(id
);
45 if (description
!= null && description
.trim().isEmpty()) {
50 if (description
== null) {
51 description
= meta
.description();
52 if (meta
.info() != null && !meta
.info().isEmpty()) {
53 description
+= "\n" + meta
.info();
57 String name
= id
.toString();
58 if (name
.length() > 1) {
59 name
= name
.substring(0, 1) + name
.substring(1).toLowerCase();
60 name
= name
.replace("_", " ");
64 this.description
= description
;
70 * THe name of this item, deduced from its ID.
72 * In other words, it is the ID but presented in a displayable form.
76 public String
getName() {
81 * The description of this item (information to present to the user).
83 * @return the description
85 public String
getDescription() {
89 public Format
getFormat() {
94 * The value stored by this item, as a {@link String}.
98 public String
getString() {
102 public String
getDefaultString() {
106 public Boolean
getBoolean() {
107 return BundleHelper
.parseBoolean(getString());
110 public Boolean
getDefaultBoolean() {
111 return BundleHelper
.parseBoolean(getDefaultString());
114 public Character
getCharacter() {
115 return BundleHelper
.parseCharacter(getString());
118 public Character
getDefaultCharacter() {
119 return BundleHelper
.parseCharacter(getDefaultString());
122 public Integer
getInteger() {
123 return BundleHelper
.parseInteger(getString());
126 public Integer
getDefaultInteger() {
127 return BundleHelper
.parseInteger(getDefaultString());
130 public Integer
getColor() {
131 return BundleHelper
.parseColor(getString());
134 public Integer
getDefaultColor() {
135 return BundleHelper
.parseColor(getDefaultString());
138 public List
<String
> getList() {
139 return BundleHelper
.parseList(getString());
142 public List
<String
> getDefaultList() {
143 return BundleHelper
.parseList(getDefaultString());
147 * The value stored by this item, as a {@link String}.
152 public void setString(String value
) {
156 public void setBoolean(boolean value
) {
157 setString(BundleHelper
.fromBoolean(value
));
160 public void setCharacter(char value
) {
161 setString(BundleHelper
.fromCharacter(value
));
164 public void setInteger(int value
) {
165 setString(BundleHelper
.fromInteger(value
));
168 public void setColor(int value
) {
169 setString(BundleHelper
.fromColor(value
));
172 public void setList(List
<String
> value
) {
173 setString(BundleHelper
.fromList(value
));
177 * Reload the value from the {@link Bundle}.
179 public void reload() {
180 value
= bundle
.getString(id
);
181 for (Runnable listener
: reloadListeners
) {
184 } catch (Exception e
) {
185 // TODO: error management?
191 public void addReloadListener(Runnable listener
) {
192 reloadListeners
.add(listener
);
196 * Save the current value to the {@link Bundle}.
199 bundle
.setString(id
, value
);
203 * Create a list of {@link MetaInfo}, one for each of the item in the given
207 * the type of {@link Bundle} to edit
209 * a class instance of the item type to work on
211 * the {@link Bundle} to sort through
215 static public <E
extends Enum
<E
>> List
<MetaInfo
<E
>> getItems(Class
<E
> type
,
217 List
<MetaInfo
<E
>> list
= new ArrayList
<MetaInfo
<E
>>();
218 for (E id
: type
.getEnumConstants()) {
219 list
.add(new MetaInfo
<E
>(type
, bundle
, id
));
225 // TODO: by groups, a-là Authors/Sources