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