Commit | Line | Data |
---|---|---|
9e834013 NR |
1 | package be.nikiroo.utils.resources; |
2 | ||
3 | import java.util.ArrayList; | |
4 | import java.util.List; | |
5 | ||
6 | import be.nikiroo.utils.resources.Meta.Format; | |
7 | ||
8 | /** | |
9 | * A graphical item that reflect a configuration option from the given | |
10 | * {@link Bundle}. | |
11 | * | |
12 | * @author niki | |
13 | * | |
14 | * @param <E> | |
15 | * the type of {@link Bundle} to edit | |
16 | */ | |
17 | public class MetaInfo<E extends Enum<E>> { | |
18 | private final Bundle<E> bundle; | |
19 | private final E id; | |
20 | ||
21 | private Meta meta; | |
22 | ||
23 | private String value; | |
24 | private List<Runnable> reloadListeners = new ArrayList<Runnable>(); | |
25 | ||
26 | private String name; | |
27 | private String description; | |
28 | ||
29 | public MetaInfo(Class<E> type, Bundle<E> bundle, E id) { | |
30 | this.bundle = bundle; | |
31 | this.id = id; | |
32 | ||
33 | try { | |
34 | this.meta = type.getDeclaredField(id.name()).getAnnotation( | |
35 | Meta.class); | |
36 | } catch (NoSuchFieldException e) { | |
37 | } catch (SecurityException e) { | |
38 | } | |
39 | ||
40 | // We consider that if a description bundle is used, everything is in it | |
41 | ||
42 | String description = null; | |
43 | if (bundle.getDescriptionBundle() != null) { | |
44 | description = bundle.getDescriptionBundle().getString(id); | |
45 | if (description != null && description.trim().isEmpty()) { | |
46 | description = null; | |
47 | } | |
48 | } | |
49 | ||
50 | if (description == null) { | |
51 | description = meta.description(); | |
52 | if (meta.info() != null && !meta.info().isEmpty()) { | |
53 | description += "\n" + meta.info(); | |
54 | } | |
55 | } | |
56 | ||
57 | String name = id.toString(); | |
58 | if (name.length() > 1) { | |
59 | name = name.substring(0, 1) + name.substring(1).toLowerCase(); | |
60 | name = name.replace("_", " "); | |
61 | } | |
62 | ||
63 | this.name = name; | |
64 | this.description = description; | |
65 | ||
66 | reload(); | |
67 | } | |
68 | ||
69 | /** | |
70 | * THe name of this item, deduced from its ID. | |
71 | * <p> | |
72 | * In other words, it is the ID but presented in a displayable form. | |
73 | * | |
74 | * @return the name | |
75 | */ | |
76 | public String getName() { | |
77 | return name; | |
78 | } | |
79 | ||
80 | /** | |
81 | * The description of this item (information to present to the user). | |
82 | * | |
83 | * @return the description | |
84 | */ | |
85 | public String getDescription() { | |
86 | return description; | |
87 | } | |
88 | ||
89 | public Format getFormat() { | |
90 | return meta.format(); | |
91 | } | |
92 | ||
93 | /** | |
94 | * The value stored by this item, as a {@link String}. | |
95 | * | |
96 | * @return the value | |
97 | */ | |
98 | public String getString() { | |
99 | return value; | |
100 | } | |
101 | ||
102 | public String getDefaultString() { | |
103 | return meta.def(); | |
104 | } | |
105 | ||
106 | public Boolean getBoolean() { | |
107 | return BundleHelper.parseBoolean(getString()); | |
108 | } | |
109 | ||
110 | public Boolean getDefaultBoolean() { | |
111 | return BundleHelper.parseBoolean(getDefaultString()); | |
112 | } | |
113 | ||
114 | public Character getCharacter() { | |
115 | return BundleHelper.parseCharacter(getString()); | |
116 | } | |
117 | ||
118 | public Character getDefaultCharacter() { | |
119 | return BundleHelper.parseCharacter(getDefaultString()); | |
120 | } | |
121 | ||
122 | public Integer getInteger() { | |
123 | return BundleHelper.parseInteger(getString()); | |
124 | } | |
125 | ||
126 | public Integer getDefaultInteger() { | |
127 | return BundleHelper.parseInteger(getDefaultString()); | |
128 | } | |
129 | ||
130 | public Integer getColor() { | |
131 | return BundleHelper.parseColor(getString()); | |
132 | } | |
133 | ||
134 | public Integer getDefaultColor() { | |
135 | return BundleHelper.parseColor(getDefaultString()); | |
136 | } | |
137 | ||
138 | public List<String> getList() { | |
139 | return BundleHelper.parseList(getString()); | |
140 | } | |
141 | ||
142 | public List<String> getDefaultList() { | |
143 | return BundleHelper.parseList(getDefaultString()); | |
144 | } | |
145 | ||
146 | /** | |
147 | * The value stored by this item, as a {@link String}. | |
148 | * | |
149 | * @param value | |
150 | * the new value | |
151 | */ | |
152 | public void setString(String value) { | |
153 | this.value = value; | |
154 | } | |
155 | ||
156 | public void setBoolean(boolean value) { | |
157 | setString(BundleHelper.fromBoolean(value)); | |
158 | } | |
159 | ||
160 | public void setCharacter(char value) { | |
161 | setString(BundleHelper.fromCharacter(value)); | |
162 | } | |
163 | ||
164 | public void setInteger(int value) { | |
165 | setString(BundleHelper.fromInteger(value)); | |
166 | } | |
167 | ||
168 | public void setColor(int value) { | |
169 | setString(BundleHelper.fromColor(value)); | |
170 | } | |
171 | ||
172 | public void setList(List<String> value) { | |
173 | setString(BundleHelper.fromList(value)); | |
174 | } | |
175 | ||
176 | /** | |
177 | * Reload the value from the {@link Bundle}. | |
178 | */ | |
179 | public void reload() { | |
180 | value = bundle.getString(id); | |
181 | for (Runnable listener : reloadListeners) { | |
182 | try { | |
183 | listener.run(); | |
184 | } catch (Exception e) { | |
185 | // TODO: error management? | |
186 | e.printStackTrace(); | |
187 | } | |
188 | } | |
189 | } | |
190 | ||
191 | public void addReloadListener(Runnable listener) { | |
192 | reloadListeners.add(listener); | |
193 | } | |
194 | ||
195 | /** | |
196 | * Save the current value to the {@link Bundle}. | |
197 | */ | |
198 | public void save() { | |
199 | bundle.setString(id, value); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Create a list of {@link MetaInfo}, one for each of the item in the given | |
204 | * {@link Bundle}. | |
205 | * | |
206 | * @param <E> | |
207 | * the type of {@link Bundle} to edit | |
208 | * @param type | |
209 | * a class instance of the item type to work on | |
210 | * @param bundle | |
211 | * the {@link Bundle} to sort through | |
212 | * | |
213 | * @return the list | |
214 | */ | |
215 | static public <E extends Enum<E>> List<MetaInfo<E>> getItems(Class<E> type, | |
216 | Bundle<E> bundle) { | |
217 | List<MetaInfo<E>> list = new ArrayList<MetaInfo<E>>(); | |
218 | for (E id : type.getEnumConstants()) { | |
219 | list.add(new MetaInfo<E>(type, bundle, id)); | |
220 | } | |
221 | ||
222 | return list; | |
223 | } | |
224 | ||
225 | // TODO: by groups, a-là Authors/Sources | |
226 | } |