Commit | Line | Data |
---|---|---|
d350b96b NR |
1 | package be.nikiroo.utils.ui; |
2 | ||
3 | import java.awt.BorderLayout; | |
daf0fd5a | 4 | import java.awt.Cursor; |
c637d2e0 | 5 | import java.awt.Dimension; |
424dcb0d NR |
6 | import java.awt.event.ActionEvent; |
7 | import java.awt.event.ActionListener; | |
daf0fd5a NR |
8 | import java.awt.event.MouseAdapter; |
9 | import java.awt.event.MouseEvent; | |
424dcb0d | 10 | import java.awt.image.BufferedImage; |
daf0fd5a | 11 | import java.io.IOException; |
d5026c09 | 12 | import java.util.ArrayList; |
a0376372 | 13 | import java.util.HashMap; |
d5026c09 | 14 | import java.util.List; |
a0376372 | 15 | import java.util.Map; |
424dcb0d | 16 | |
d5026c09 | 17 | import javax.swing.BoxLayout; |
424dcb0d NR |
18 | import javax.swing.ImageIcon; |
19 | import javax.swing.JButton; | |
0877d6f5 | 20 | import javax.swing.JComponent; |
8517b60c | 21 | import javax.swing.JLabel; |
0877d6f5 | 22 | import javax.swing.JOptionPane; |
d350b96b NR |
23 | import javax.swing.JPanel; |
24 | import javax.swing.JTextField; | |
d350b96b | 25 | |
daf0fd5a | 26 | import be.nikiroo.utils.Image; |
0877d6f5 NR |
27 | import be.nikiroo.utils.StringUtils; |
28 | import be.nikiroo.utils.StringUtils.Alignment; | |
d350b96b | 29 | import be.nikiroo.utils.resources.Bundle; |
9e834013 | 30 | import be.nikiroo.utils.resources.MetaInfo; |
d350b96b NR |
31 | |
32 | /** | |
33 | * A graphical item that reflect a configuration option from the given | |
34 | * {@link Bundle}. | |
76b51de9 NR |
35 | * <p> |
36 | * This graphical item can be edited, and the result will be saved back into the | |
37 | * linked {@link MetaInfo}; you still have to save the {@link MetaInfo} should | |
38 | * you wish to, of course. | |
d350b96b NR |
39 | * |
40 | * @author niki | |
db31c358 | 41 | * |
d350b96b NR |
42 | * @param <E> |
43 | * the type of {@link Bundle} to edit | |
44 | */ | |
a0376372 | 45 | public abstract class ConfigItem<E extends Enum<E>> extends JPanel { |
d350b96b | 46 | private static final long serialVersionUID = 1L; |
db31c358 | 47 | |
e95f4fb6 NR |
48 | private static int minimumHeight = -1; |
49 | ||
01a5d26c | 50 | /** A small 16x16 "?" blue in PNG, base64 encoded. */ |
a0376372 | 51 | private static String img64info = // |
daf0fd5a NR |
52 | "" |
53 | + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI" | |
54 | + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wURFRg6IrtcdgAAATdJREFUOMvtkj8sQ1EUxr9z/71G" | |
55 | + "m1RDogYxq7WDDYMYTSajSG4n6YRYzSaSLibWbiaDIGwdiLIYDFKDNJEgKu969xi8UNHy7H7LPcN3" | |
56 | + "v/Odcy+hG9oOIeIcBCJS9MAvlZtOMtHxsrFrJHGqe0RVGnHAHpcIbPlng8BS3HmKBJYzabGUzcrJ" | |
57 | + "XK+ckIrqANYR2JEv2nYDEVck0WKGfHzyq82Go+btxoX3XAcAIqTj8wPqOH6mtMeM4bGCLhyfhTMA" | |
58 | + "qlLhKHqujCfaweCAmV0p50dPzsNpEKpK01V/n55HIvTnfDC2odKlfeYadZN/T+AqDACUsnkhqaU1" | |
59 | + "LRIVuX1x7ciuSWQxVIrunONrfq3dI6oh+T94Z8453vEem/HTqT8ZpFJ0qDXtGkPbAGAMeSRngQCA" | |
60 | + "eUvgn195AwlZWyvjtQdhAAAAAElFTkSuQmCC"; | |
61 | ||
249e4ef4 | 62 | /** A small 16x16 "+" image with colours */ |
a0376372 | 63 | private static String img64add = // |
01a5d26c NR |
64 | "" |
65 | + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI" | |
66 | + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wUeES0QBFvvnAAAAB1pVFh0Q29tbWVudAAAAAAAQ3Jl" | |
67 | + "YXRlZCB3aXRoIEdJTVBkLmUHAAACH0lEQVQ4y42Tz0sVURTHP+fMmC7CQMpH1EjgIimCsEVBEIg/" | |
68 | + "qIbcBAW2Uai1m/oH2rlJXLQpeRJt2gQhTO0iTTKC1I2JBf5gKCJCRPvhPOed22LmvV70Fn7hwr3c" | |
69 | + "+z3ne+73HCFHEClxaASRHgduA91AW369BkwDI3Foy0GkEofmACQnSxyaCyItAkMClMzYdeCAJgVP" | |
70 | + "tJJrPA7tVoUjNZlngXMAiRmXClfoK/Tjq09x7T6LW+8RxOVJ5+LQzgSRojm5WCEDlMrQVbjIQNtN" | |
71 | + "rh0d5FTzaTLBmWKgM4h0Ig4NzWseohYCJUuqx123Sx0MBpF2+MAdyWUnlqX4lf4bIDHjR+rwJJPR" | |
72 | + "qNCgCjDsA10lM/oKIRcO9lByCYklnG/pqQa4euQ6J5tPoKI0yD6ef33Ku40Z80R7CSJNWyZxT+Ki" | |
73 | + "2ytGP911hyZxQaRp1RtPPPYKD4+sGJwPrDUp7Q9Xxnj9fYrUUnaszEAwQHfrZQAerT/g7cYMiuCp" | |
74 | + "z8LmLI0qBqz6wLQn2v5he57FrXkAtlPH2ZZOuskCzG2+4dnnx3iSuSgCKqLAlAIjmXPiVIRsgYjU" | |
75 | + "usrfO0Gq7cA9jUNbBsZrmiQnac1e6n3FeBzakpf39OSBG9IPHAZwzlFoagVg5edHXn57wZed9dpA" | |
76 | + "C3FoYRDpf8M0AQwKwu9yubxjeA7Y72ENqlp3mOqMcwcwDPQCx8gGchV4BYzGoS1V3gL8AVA5C5/0" | |
77 | + "oRFoAAAAAElFTkSuQmCC"; | |
78 | ||
249e4ef4 | 79 | /** A small 32x32 "-" image with colours */ |
a0376372 | 80 | private static String img64remove = // |
01a5d26c NR |
81 | "" |
82 | + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI" | |
83 | + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wUeESw5X/JGsQAAAB1pVFh0Q29tbWVudAAAAAAAQ3Jl" | |
84 | + "YXRlZCB3aXRoIEdJTVBkLmUHAAACKUlEQVQ4y5WTO2iTYRSG3+//v/+SJrG5SSABh1JQBHFJNUNR" | |
85 | + "YodCLoMoTkK0YKhQtBmsl01wKVZRBwcrgosg3SwFW9Cippe0VmlpB6uYqYIaNSZtbv/lOKRx0iR9" | |
86 | + "4YOzvOc8vOd8wLbG4nYGAKP9tshKr3Pq0zFXORt0UzbopvUeZ2ml1/niUcIWAYBzwwqr+xgAjCSt" | |
87 | + "wpXjWzx105Ha+1XsMgT8U6IJfPAacyfO50OXJi3VwbtbxMbidtZ3tiClbzi/eAuCmxgai4AfNvNn" | |
88 | + "KJn3X5xWKgwA0lHHYud3MdDUXMcmIOMx0oGJXJCN9tuiJ98p4//DbtTk2cFKhB/OSBcMgQHVMkir" | |
89 | + "AqwJBhGYrIIkCQc2eJK3aewI9Crko2FIh0K1Jo0mcwmV6XFUlmfRXhK7eXuRKaRVIYdiUGKnW8Kn" | |
90 | + "0ia0t6/hKHJVqCcLzncQgLhtIvBfbWbZZahq+cl96AuvQLre2Mw59NUlkCwjZ6USL0uYgSj26B/X" | |
91 | + "oK+vtkYgMAhMRF4x5oWlPdod0UQtfUFo7YEBBKz59BEGAAtRx1xHVgzu5AYyHmMmMJHrZolhhU3t" | |
92 | + "05XJe7s2PJuCq9k1MgKyNjOXiBf8kWW5JDy4XKHBl2ql6+pvX8ZjzDOqrcWsFQAAE/T3H3z2GG/6" | |
93 | + "zhT8sfdKeehWkUQAeJ7WcH23xTz1uPBwf1hclA3mBZjPojFOIOSsVPpmN1OznfpA+Gn+2kCHqg/d" | |
94 | + "LhIA/AFU5d0V6gTjtQAAAABJRU5ErkJggg=="; | |
95 | ||
fde375c1 NR |
96 | /** The original value before current changes. */ |
97 | private Object orig; | |
a0376372 | 98 | private List<Object> origs = new ArrayList<Object>(); |
d5026c09 NR |
99 | private List<Integer> dirtyBits; |
100 | ||
a0376372 | 101 | /** The fields (one for non-array, a list for arrays). */ |
d5026c09 NR |
102 | private JComponent field; |
103 | private List<JComponent> fields = new ArrayList<JComponent>(); | |
fde375c1 | 104 | |
a0376372 NR |
105 | /** The fields to panel map to get the actual item added to 'main'. */ |
106 | private Map<Integer, JComponent> itemFields = new HashMap<Integer, JComponent>(); | |
107 | ||
108 | /** The main panel with all the fields in it. */ | |
109 | private JPanel main; | |
110 | ||
01a5d26c NR |
111 | /** The {@link MetaInfo} linked to the field. */ |
112 | protected MetaInfo<E> info; | |
113 | ||
76b51de9 NR |
114 | /** |
115 | * Create a new {@link ConfigItem} for the given {@link MetaInfo}. | |
116 | * | |
d18e136e NR |
117 | * @param nhgap |
118 | * negative horisontal gap in pixel to use for the label, i.e., | |
119 | * the step lock sized labels will start smaller by that amount | |
120 | * (the use case would be to align controls that start at a | |
121 | * different horisontal position) | |
76b51de9 | 122 | */ |
a0376372 | 123 | public void init(int nhgap) { |
d5026c09 NR |
124 | if (info.isArray()) { |
125 | this.setLayout(new BorderLayout()); | |
126 | add(label(nhgap), BorderLayout.WEST); | |
127 | ||
a0376372 NR |
128 | main = new JPanel(); |
129 | ||
d5026c09 NR |
130 | main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS)); |
131 | int size = info.getListSize(false); | |
132 | for (int i = 0; i < size; i++) { | |
a0376372 | 133 | addItem(i); |
d5026c09 | 134 | } |
a0376372 NR |
135 | main.revalidate(); |
136 | main.repaint(); | |
fde375c1 | 137 | |
01a5d26c | 138 | final JButton add = new JButton(); |
a0376372 | 139 | setImage(add, img64add, "+"); |
01a5d26c | 140 | |
d5026c09 NR |
141 | add.addActionListener(new ActionListener() { |
142 | @Override | |
143 | public void actionPerformed(ActionEvent e) { | |
a0376372 | 144 | addItem(fields.size()); |
ee7a6ccb NR |
145 | main.revalidate(); |
146 | main.repaint(); | |
d5026c09 NR |
147 | } |
148 | }); | |
fde375c1 | 149 | |
d5026c09 NR |
150 | JPanel tmp = new JPanel(new BorderLayout()); |
151 | tmp.add(add, BorderLayout.WEST); | |
fde375c1 | 152 | |
d5026c09 NR |
153 | JPanel mainPlus = new JPanel(new BorderLayout()); |
154 | mainPlus.add(main, BorderLayout.CENTER); | |
155 | mainPlus.add(tmp, BorderLayout.SOUTH); | |
fde375c1 | 156 | |
d5026c09 NR |
157 | add(mainPlus, BorderLayout.CENTER); |
158 | } else { | |
159 | this.setLayout(new BorderLayout()); | |
160 | add(label(nhgap), BorderLayout.WEST); | |
0877d6f5 | 161 | |
a0376372 | 162 | JComponent field = createField(-1); |
d5026c09 NR |
163 | add(field, BorderLayout.CENTER); |
164 | } | |
0877d6f5 | 165 | } |
9e834013 | 166 | |
a0376372 NR |
167 | private void addItem(final int item) { |
168 | JPanel minusPanel = new JPanel(new BorderLayout()); | |
169 | itemFields.put(item, minusPanel); | |
170 | ||
171 | JComponent field = createField(item); | |
172 | ||
173 | final JButton remove = new JButton(); | |
174 | setImage(remove, img64remove, "-"); | |
175 | ||
176 | remove.addActionListener(new ActionListener() { | |
177 | @Override | |
178 | public void actionPerformed(ActionEvent e) { | |
179 | removeItem(item); | |
180 | } | |
181 | }); | |
182 | ||
183 | minusPanel.add(field, BorderLayout.CENTER); | |
184 | minusPanel.add(remove, BorderLayout.EAST); | |
185 | ||
186 | main.add(minusPanel); | |
187 | main.revalidate(); | |
188 | main.repaint(); | |
189 | } | |
190 | ||
191 | private void removeItem(int item) { | |
192 | int last = itemFields.size() - 1; | |
193 | ||
194 | for (int i = item; i <= last; i++) { | |
195 | Object value = null; | |
196 | if (i < last) { | |
197 | value = getFromField(i + 1); | |
198 | } | |
199 | setToField(value, i); | |
200 | setToInfo(value, i); | |
201 | setDirtyItem(i); | |
202 | } | |
203 | ||
204 | main.remove(itemFields.remove(last)); | |
205 | main.revalidate(); | |
206 | main.repaint(); | |
207 | } | |
208 | ||
d5026c09 NR |
209 | /** |
210 | * Prepare a new {@link ConfigItem} instance, linked to the given | |
211 | * {@link MetaInfo}. | |
212 | * | |
213 | * @param info | |
214 | * the info | |
215 | * @param autoDirtyHandling | |
216 | * TRUE to automatically manage the setDirty/Save operations, | |
217 | * FALSE if you want to do it yourself via | |
218 | * {@link ConfigItem#setDirtyItem(int)} | |
219 | */ | |
220 | protected ConfigItem(MetaInfo<E> info, boolean autoDirtyHandling) { | |
221 | this.info = info; | |
222 | if (!autoDirtyHandling) { | |
223 | dirtyBits = new ArrayList<Integer>(); | |
0877d6f5 | 224 | } |
d5026c09 | 225 | } |
0877d6f5 | 226 | |
d5026c09 NR |
227 | /** |
228 | * Create an empty graphical component to be used later by | |
a0376372 | 229 | * {@link ConfigItem#createField(int)}. |
d5026c09 NR |
230 | * <p> |
231 | * Note that {@link ConfigItem#reload(int)} will be called after it was | |
a0376372 | 232 | * created by {@link ConfigItem#createField(int)}. |
d5026c09 NR |
233 | * |
234 | * @param item | |
235 | * the item number to get for an array of values, or -1 to get | |
236 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
237 | * is FALSE) | |
238 | * | |
239 | * @return the graphical component | |
240 | */ | |
a0376372 | 241 | abstract protected JComponent createEmptyField(int item); |
0877d6f5 | 242 | |
d5026c09 NR |
243 | /** |
244 | * Get the information from the {@link MetaInfo} in the subclass preferred | |
245 | * format. | |
246 | * | |
247 | * @param item | |
248 | * the item number to get for an array of values, or -1 to get | |
249 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
250 | * is FALSE) | |
251 | * | |
252 | * @return the information in the subclass preferred format | |
253 | */ | |
a0376372 | 254 | abstract protected Object getFromInfo(int item); |
d18e136e | 255 | |
d5026c09 NR |
256 | /** |
257 | * Set the value to the {@link MetaInfo}. | |
258 | * | |
259 | * @param value | |
260 | * the value in the subclass preferred format | |
261 | * @param item | |
262 | * the item number to get for an array of values, or -1 to get | |
263 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
264 | * is FALSE) | |
265 | */ | |
a0376372 | 266 | abstract protected void setToInfo(Object value, int item); |
0877d6f5 | 267 | |
d5026c09 | 268 | /** |
a0376372 NR |
269 | * The value present in the given item's related field in the subclass |
270 | * preferred format. | |
271 | * | |
d5026c09 NR |
272 | * @param item |
273 | * the item number to get for an array of values, or -1 to get | |
274 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
275 | * is FALSE) | |
276 | * | |
a0376372 NR |
277 | * @return the value present in the given item's related field in the |
278 | * subclass preferred format | |
d5026c09 | 279 | */ |
a0376372 | 280 | abstract protected Object getFromField(int item); |
0877d6f5 | 281 | |
d5026c09 NR |
282 | /** |
283 | * Set the value (in the subclass preferred format) into the field. | |
284 | * | |
285 | * @param value | |
286 | * the value in the subclass preferred format | |
287 | * @param item | |
288 | * the item number to get for an array of values, or -1 to get | |
289 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
290 | * is FALSE) | |
291 | */ | |
a0376372 | 292 | abstract protected void setToField(Object value, int item); |
0877d6f5 | 293 | |
d5026c09 NR |
294 | /** |
295 | * Create a new field for the given graphical component at the given index | |
296 | * (note that the component is usually created by | |
a0376372 | 297 | * {@link ConfigItem#createEmptyField(int)}). |
d5026c09 NR |
298 | * |
299 | * @param item | |
300 | * the item number to get for an array of values, or -1 to get | |
301 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
302 | * is FALSE) | |
303 | * @param field | |
304 | * the graphical component | |
305 | */ | |
306 | private void setField(int item, JComponent field) { | |
307 | if (item < 0) { | |
308 | this.field = field; | |
309 | return; | |
310 | } | |
0877d6f5 | 311 | |
d5026c09 NR |
312 | for (int i = fields.size(); i <= item; i++) { |
313 | fields.add(null); | |
314 | } | |
d18e136e | 315 | |
d5026c09 | 316 | fields.set(item, field); |
0877d6f5 NR |
317 | } |
318 | ||
d5026c09 NR |
319 | /** |
320 | * Retrieve the associated graphical component that was created with | |
a0376372 | 321 | * {@link ConfigItem#createEmptyField(int)}. |
d5026c09 NR |
322 | * |
323 | * @param item | |
324 | * the item number to get for an array of values, or -1 to get | |
325 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
326 | * is FALSE) | |
327 | * | |
328 | * @return the graphical component | |
329 | */ | |
330 | protected JComponent getField(int item) { | |
331 | if (item < 0) { | |
332 | return field; | |
333 | } | |
0877d6f5 | 334 | |
d5026c09 NR |
335 | if (item < fields.size()) { |
336 | return fields.get(item); | |
337 | } | |
d18e136e | 338 | |
d5026c09 | 339 | return null; |
0877d6f5 NR |
340 | } |
341 | ||
a0376372 NR |
342 | /** |
343 | * The original value (before any changes to the {@link MetaInfo}) for this | |
344 | * item. | |
345 | * | |
346 | * @param item | |
347 | * the item number to get for an array of values, or -1 to get | |
348 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
349 | * is FALSE) | |
350 | * | |
351 | * @return the original value | |
352 | */ | |
353 | private Object getOrig(int item) { | |
354 | if (item < 0) { | |
355 | return orig; | |
356 | } | |
357 | ||
358 | if (item < origs.size()) { | |
359 | return origs.get(item); | |
360 | } | |
361 | ||
362 | return null; | |
363 | } | |
364 | ||
365 | /** | |
366 | * The original value (before any changes to the {@link MetaInfo}) for this | |
367 | * item. | |
368 | * | |
369 | * @param item | |
370 | * the item number to get for an array of values, or -1 to get | |
371 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
372 | * is FALSE) | |
373 | * @param value | |
374 | * the new original value | |
375 | */ | |
376 | private void setOrig(Object value, int item) { | |
377 | if (item < 0) { | |
378 | orig = value; | |
379 | } else { | |
380 | while (item >= origs.size()) { | |
381 | origs.add(null); | |
382 | } | |
383 | ||
384 | origs.set(item, value); | |
385 | } | |
386 | } | |
387 | ||
d5026c09 NR |
388 | /** |
389 | * Manually specify that the given item is "dirty" and thus should be saved | |
390 | * when asked. | |
391 | * <p> | |
392 | * Has no effect if the class is using automatic dirty handling (see | |
393 | * {@link ConfigItem#ConfigItem(MetaInfo, boolean)}). | |
394 | * | |
395 | * @param item | |
396 | * the item number to get for an array of values, or -1 to get | |
397 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
398 | * is FALSE) | |
399 | */ | |
400 | protected void setDirtyItem(int item) { | |
401 | if (dirtyBits != null) { | |
402 | dirtyBits.add(item); | |
403 | } | |
404 | } | |
d18e136e | 405 | |
d5026c09 NR |
406 | /** |
407 | * Check if the value changed since the last load/save into the linked | |
408 | * {@link MetaInfo}. | |
409 | * <p> | |
410 | * Note that we consider NULL and an Empty {@link String} to be equals. | |
411 | * | |
412 | * @param value | |
413 | * the value to test | |
a0376372 NR |
414 | * @param item |
415 | * the item number to get for an array of values, or -1 to get | |
416 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
417 | * is FALSE) | |
d5026c09 NR |
418 | * |
419 | * @return TRUE if it has | |
420 | */ | |
a0376372 | 421 | protected boolean hasValueChanged(Object value, int item) { |
d5026c09 | 422 | // We consider "" and NULL to be equals |
a0376372 NR |
423 | Object orig = getOrig(item); |
424 | if (orig == null) { | |
425 | orig = ""; | |
426 | } | |
d5026c09 | 427 | return !orig.equals(value == null ? "" : value); |
0877d6f5 NR |
428 | } |
429 | ||
a0376372 NR |
430 | /** |
431 | * Reload the values to what they currently are in the {@link MetaInfo}. | |
432 | */ | |
433 | private void reload() { | |
434 | if (info.isArray()) { | |
435 | while (!itemFields.isEmpty()) { | |
436 | main.remove(itemFields.remove(itemFields.size() - 1)); | |
437 | } | |
438 | main.revalidate(); | |
439 | main.repaint(); | |
440 | for (int item = 0; item < info.getListSize(false); item++) { | |
441 | reload(item); | |
442 | } | |
443 | } else { | |
444 | reload(-1); | |
445 | } | |
446 | } | |
447 | ||
d5026c09 NR |
448 | /** |
449 | * Reload the values to what they currently are in the {@link MetaInfo}. | |
450 | * | |
451 | * @param item | |
452 | * the item number to get for an array of values, or -1 to get | |
453 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
454 | * is FALSE) | |
455 | */ | |
a0376372 NR |
456 | private void reload(int item) { |
457 | if (item >= 0 && !itemFields.containsKey(item)) { | |
458 | addItem(item); | |
459 | } | |
460 | ||
d5026c09 NR |
461 | Object value = getFromInfo(item); |
462 | setToField(value, item); | |
a0376372 | 463 | setOrig(value == null ? "" : value, item); |
d5026c09 | 464 | } |
0877d6f5 | 465 | |
d5026c09 NR |
466 | /** |
467 | * If the item has been modified, set the {@link MetaInfo} to dirty then | |
468 | * modify it to, reflect the changes so it can be saved later. | |
469 | * <p> | |
470 | * This method does <b>not</b> call {@link MetaInfo#save(boolean)}. | |
a0376372 NR |
471 | */ |
472 | private void save() { | |
473 | if (info.isArray()) { | |
a1848a90 NR |
474 | boolean dirty = itemFields.size() != info.getListSize(false); |
475 | for (int item = 0; item < itemFields.size(); item++) { | |
a0376372 NR |
476 | if (getDirtyBit(item)) { |
477 | dirty = true; | |
478 | } | |
479 | } | |
480 | ||
481 | if (dirty) { | |
a1848a90 | 482 | info.setDirty(); |
a0376372 | 483 | info.setString(null, -1); |
a1848a90 NR |
484 | |
485 | for (int item = 0; item < itemFields.size(); item++) { | |
a0376372 NR |
486 | Object value = null; |
487 | if (getField(item) != null) { | |
488 | value = getFromField(item); | |
489 | if ("".equals(value)) { | |
490 | value = null; | |
491 | } | |
492 | } | |
493 | ||
a0376372 NR |
494 | setToInfo(value, item); |
495 | setOrig(value, item); | |
496 | } | |
497 | } | |
498 | } else { | |
499 | if (getDirtyBit(-1)) { | |
500 | Object value = getFromField(-1); | |
501 | ||
502 | info.setDirty(); | |
503 | setToInfo(value, -1); | |
504 | setOrig(value, -1); | |
505 | } | |
506 | } | |
507 | } | |
508 | ||
509 | /** | |
510 | * Check if the item is dirty, and clear the dirty bit if set. | |
d5026c09 NR |
511 | * |
512 | * @param item | |
513 | * the item number to get for an array of values, or -1 to get | |
514 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
515 | * is FALSE) | |
a0376372 NR |
516 | * |
517 | * @return TRUE if it was dirty, FALSE if not | |
d5026c09 | 518 | */ |
a0376372 | 519 | private boolean getDirtyBit(int item) { |
d5026c09 | 520 | if (dirtyBits != null) { |
a0376372 | 521 | return dirtyBits.remove((Integer) item); |
d5026c09 | 522 | } |
d18e136e | 523 | |
a0376372 NR |
524 | Object value = null; |
525 | if (getField(item) != null) { | |
526 | value = getFromField(item); | |
d5026c09 | 527 | } |
a0376372 NR |
528 | |
529 | return hasValueChanged(value, item); | |
0877d6f5 NR |
530 | } |
531 | ||
d5026c09 | 532 | /** |
a0376372 | 533 | * Create a new field for the given item. |
d5026c09 NR |
534 | * |
535 | * @param item | |
536 | * the item number to get for an array of values, or -1 to get | |
537 | * the whole value (has no effect if {@link MetaInfo#isArray()} | |
538 | * is FALSE) | |
01a5d26c | 539 | * |
a0376372 | 540 | * @return the newly created field |
d5026c09 | 541 | */ |
a0376372 NR |
542 | protected JComponent createField(final int item) { |
543 | JComponent field = createEmptyField(item); | |
544 | setField(item, field); | |
d5026c09 | 545 | reload(item); |
0877d6f5 NR |
546 | |
547 | info.addReloadedListener(new Runnable() { | |
548 | @Override | |
549 | public void run() { | |
a0376372 | 550 | reload(); |
0877d6f5 NR |
551 | } |
552 | }); | |
553 | info.addSaveListener(new Runnable() { | |
554 | @Override | |
555 | public void run() { | |
a0376372 | 556 | save(); |
0877d6f5 NR |
557 | } |
558 | }); | |
9e834013 | 559 | |
a0376372 NR |
560 | int height = Math |
561 | .max(getMinimumHeight(), field.getMinimumSize().height); | |
562 | field.setPreferredSize(new Dimension(200, height)); | |
d5026c09 NR |
563 | |
564 | return field; | |
d350b96b | 565 | } |
c637d2e0 NR |
566 | |
567 | /** | |
568 | * Create a label which width is constrained in lock steps. | |
569 | * | |
d18e136e NR |
570 | * @param nhgap |
571 | * negative horisontal gap in pixel to use for the label, i.e., | |
572 | * the step lock sized labels will start smaller by that amount | |
573 | * (the use case would be to align controls that start at a | |
574 | * different horisontal position) | |
c637d2e0 NR |
575 | * |
576 | * @return the label | |
577 | */ | |
d5026c09 | 578 | protected JComponent label(int nhgap) { |
0877d6f5 | 579 | final JLabel label = new JLabel(info.getName()); |
c637d2e0 NR |
580 | |
581 | Dimension ps = label.getPreferredSize(); | |
582 | if (ps == null) { | |
583 | ps = label.getSize(); | |
584 | } | |
585 | ||
e95f4fb6 NR |
586 | ps.height = Math.max(ps.height, getMinimumHeight()); |
587 | ||
c637d2e0 | 588 | int w = ps.width; |
daf0fd5a | 589 | int step = 150; |
d18e136e | 590 | for (int i = 2 * step - nhgap; i < 10 * step; i += step) { |
c637d2e0 NR |
591 | if (w < i) { |
592 | w = i; | |
593 | break; | |
594 | } | |
595 | } | |
596 | ||
daf0fd5a | 597 | final Runnable showInfo = new Runnable() { |
0877d6f5 | 598 | @Override |
daf0fd5a | 599 | public void run() { |
0877d6f5 | 600 | StringBuilder builder = new StringBuilder(); |
d18e136e NR |
601 | String text = (info.getDescription().replace("\\n", "\n")) |
602 | .trim(); | |
0877d6f5 NR |
603 | for (String line : StringUtils.justifyText(text, 80, |
604 | Alignment.LEFT)) { | |
605 | if (builder.length() > 0) { | |
606 | builder.append("\n"); | |
607 | } | |
608 | builder.append(line); | |
609 | } | |
610 | text = builder.toString(); | |
daf0fd5a NR |
611 | JOptionPane.showMessageDialog(ConfigItem.this, text, |
612 | info.getName(), JOptionPane.INFORMATION_MESSAGE); | |
613 | } | |
614 | }; | |
615 | ||
616 | JLabel help = new JLabel(""); | |
617 | help.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); | |
a0376372 | 618 | setImage(help, img64info, "?"); |
daf0fd5a NR |
619 | |
620 | help.addMouseListener(new MouseAdapter() { | |
621 | @Override | |
622 | public void mouseClicked(MouseEvent e) { | |
623 | showInfo.run(); | |
0877d6f5 NR |
624 | } |
625 | }); | |
626 | ||
627 | JPanel pane2 = new JPanel(new BorderLayout()); | |
628 | pane2.add(help, BorderLayout.WEST); | |
629 | pane2.add(new JLabel(" "), BorderLayout.CENTER); | |
630 | ||
e95f4fb6 NR |
631 | JPanel contentPane = new JPanel(new BorderLayout()); |
632 | contentPane.add(label, BorderLayout.WEST); | |
633 | contentPane.add(pane2, BorderLayout.CENTER); | |
0877d6f5 NR |
634 | |
635 | ps.width = w + 30; // 30 for the (?) sign | |
e95f4fb6 NR |
636 | contentPane.setSize(ps); |
637 | contentPane.setPreferredSize(ps); | |
638 | ||
639 | JPanel pane = new JPanel(new BorderLayout()); | |
640 | pane.add(contentPane, BorderLayout.NORTH); | |
c637d2e0 | 641 | |
0877d6f5 | 642 | return pane; |
c637d2e0 | 643 | } |
424dcb0d | 644 | |
a0376372 NR |
645 | /** |
646 | * Create a new {@link ConfigItem} for the given {@link MetaInfo}. | |
647 | * | |
648 | * @param <E> | |
649 | * the type of {@link Bundle} to edit | |
650 | * | |
651 | * @param info | |
652 | * the {@link MetaInfo} | |
653 | * @param nhgap | |
654 | * negative horisontal gap in pixel to use for the label, i.e., | |
655 | * the step lock sized labels will start smaller by that amount | |
656 | * (the use case would be to align controls that start at a | |
657 | * different horisontal position) | |
658 | * | |
659 | * @return the new {@link ConfigItem} | |
660 | */ | |
661 | static public <E extends Enum<E>> ConfigItem<E> createItem( | |
662 | MetaInfo<E> info, int nhgap) { | |
e95f4fb6 | 663 | |
a0376372 NR |
664 | ConfigItem<E> configItem; |
665 | switch (info.getFormat()) { | |
666 | case BOOLEAN: | |
667 | configItem = new ConfigItemBoolean<E>(info); | |
668 | break; | |
669 | case COLOR: | |
670 | configItem = new ConfigItemColor<E>(info); | |
671 | break; | |
672 | case FILE: | |
673 | configItem = new ConfigItemBrowse<E>(info, false); | |
674 | break; | |
675 | case DIRECTORY: | |
676 | configItem = new ConfigItemBrowse<E>(info, true); | |
677 | break; | |
678 | case COMBO_LIST: | |
679 | configItem = new ConfigItemCombobox<E>(info, true); | |
680 | break; | |
681 | case FIXED_LIST: | |
682 | configItem = new ConfigItemCombobox<E>(info, false); | |
683 | break; | |
684 | case INT: | |
685 | configItem = new ConfigItemInteger<E>(info); | |
686 | break; | |
687 | case PASSWORD: | |
688 | configItem = new ConfigItemPassword<E>(info); | |
689 | break; | |
690 | case LOCALE: | |
691 | configItem = new ConfigItemLocale<E>(info); | |
692 | break; | |
693 | case STRING: | |
694 | default: | |
695 | configItem = new ConfigItemString<E>(info); | |
696 | break; | |
e95f4fb6 NR |
697 | } |
698 | ||
a0376372 NR |
699 | configItem.init(nhgap); |
700 | return configItem; | |
e95f4fb6 | 701 | } |
01a5d26c NR |
702 | |
703 | /** | |
704 | * Set an image to the given {@link JButton}, with a fallback text if it | |
705 | * fails. | |
706 | * | |
707 | * @param button | |
708 | * the button to set | |
709 | * @param image64 | |
710 | * the image in BASE64 (should be PNG or similar) | |
711 | * @param fallbackText | |
712 | * text to use in case the image cannot be created | |
713 | */ | |
249e4ef4 | 714 | static protected void setImage(JLabel button, String image64, |
01a5d26c NR |
715 | String fallbackText) { |
716 | try { | |
717 | Image img = new Image(image64); | |
718 | try { | |
719 | BufferedImage bImg = ImageUtilsAwt.fromImage(img); | |
720 | button.setIcon(new ImageIcon(bImg)); | |
721 | } finally { | |
722 | img.close(); | |
723 | } | |
724 | } catch (IOException e) { | |
725 | // This is an hard-coded image, should not happen | |
726 | button.setText(fallbackText); | |
727 | } | |
728 | } | |
729 | ||
730 | /** | |
731 | * Set an image to the given {@link JButton}, with a fallback text if it | |
732 | * fails. | |
733 | * | |
734 | * @param button | |
735 | * the button to set | |
736 | * @param image64 | |
737 | * the image in BASE64 (should be PNG or similar) | |
738 | * @param fallbackText | |
739 | * text to use in case the image cannot be created | |
740 | */ | |
249e4ef4 | 741 | static protected void setImage(JButton button, String image64, |
01a5d26c NR |
742 | String fallbackText) { |
743 | try { | |
744 | Image img = new Image(image64); | |
745 | try { | |
746 | BufferedImage bImg = ImageUtilsAwt.fromImage(img); | |
747 | button.setIcon(new ImageIcon(bImg)); | |
748 | } finally { | |
749 | img.close(); | |
750 | } | |
751 | } catch (IOException e) { | |
752 | // This is an hard-coded image, should not happen | |
753 | button.setText(fallbackText); | |
754 | } | |
755 | } | |
a0376372 NR |
756 | |
757 | static private int getMinimumHeight() { | |
758 | if (minimumHeight < 0) { | |
759 | minimumHeight = new JTextField("Test").getMinimumSize().height; | |
760 | } | |
761 | ||
762 | return minimumHeight; | |
763 | } | |
d350b96b | 764 | } |