fix ConfigItems, especially regarding items
[nikiroo-utils.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.HashMap;
14 import java.util.List;
15 import java.util.Map;
16
17 import javax.swing.BoxLayout;
18 import javax.swing.ImageIcon;
19 import javax.swing.JButton;
20 import javax.swing.JComponent;
21 import javax.swing.JLabel;
22 import javax.swing.JOptionPane;
23 import javax.swing.JPanel;
24 import javax.swing.JTextField;
25
26 import be.nikiroo.utils.Image;
27 import be.nikiroo.utils.StringUtils;
28 import be.nikiroo.utils.StringUtils.Alignment;
29 import be.nikiroo.utils.resources.Bundle;
30 import be.nikiroo.utils.resources.MetaInfo;
31
32 /**
33 * A graphical item that reflect a configuration option from the given
34 * {@link Bundle}.
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.
39 *
40 * @author niki
41 *
42 * @param <E>
43 * the type of {@link Bundle} to edit
44 */
45 public abstract class ConfigItem<E extends Enum<E>> extends JPanel {
46 private static final long serialVersionUID = 1L;
47
48 private static int minimumHeight = -1;
49
50 /** A small 16x16 "?" blue in PNG, base64 encoded. */
51 private static String img64info = //
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
62 /** A small 16x16 "+" image with colours */
63 private static String img64add = //
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
79 /** A small 32x32 "-" image with colours */
80 private static String img64remove = //
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
96 /** The original value before current changes. */
97 private Object orig;
98 private List<Object> origs = new ArrayList<Object>();
99 private List<Integer> dirtyBits;
100
101 /** The fields (one for non-array, a list for arrays). */
102 private JComponent field;
103 private List<JComponent> fields = new ArrayList<JComponent>();
104
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
111 /** The {@link MetaInfo} linked to the field. */
112 protected MetaInfo<E> info;
113
114 /**
115 * Create a new {@link ConfigItem} for the given {@link MetaInfo}.
116 *
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)
122 */
123 public void init(int nhgap) {
124 if (info.isArray()) {
125 this.setLayout(new BorderLayout());
126 add(label(nhgap), BorderLayout.WEST);
127
128 main = new JPanel();
129
130 main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
131 int size = info.getListSize(false);
132 for (int i = 0; i < size; i++) {
133 addItem(i);
134 }
135 main.revalidate();
136 main.repaint();
137
138 final JButton add = new JButton();
139 setImage(add, img64add, "+");
140
141 add.addActionListener(new ActionListener() {
142 @Override
143 public void actionPerformed(ActionEvent e) {
144 addItem(fields.size());
145 main.revalidate();
146 main.repaint();
147 }
148 });
149
150 JPanel tmp = new JPanel(new BorderLayout());
151 tmp.add(add, BorderLayout.WEST);
152
153 JPanel mainPlus = new JPanel(new BorderLayout());
154 mainPlus.add(main, BorderLayout.CENTER);
155 mainPlus.add(tmp, BorderLayout.SOUTH);
156
157 add(mainPlus, BorderLayout.CENTER);
158 } else {
159 this.setLayout(new BorderLayout());
160 add(label(nhgap), BorderLayout.WEST);
161
162 JComponent field = createField(-1);
163 add(field, BorderLayout.CENTER);
164 }
165 }
166
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
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>();
224 }
225 }
226
227 /**
228 * Create an empty graphical component to be used later by
229 * {@link ConfigItem#createField(int)}.
230 * <p>
231 * Note that {@link ConfigItem#reload(int)} will be called after it was
232 * created by {@link ConfigItem#createField(int)}.
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 */
241 abstract protected JComponent createEmptyField(int item);
242
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 */
254 abstract protected Object getFromInfo(int item);
255
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 */
266 abstract protected void setToInfo(Object value, int item);
267
268 /**
269 * The value present in the given item's related field in the subclass
270 * preferred format.
271 *
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 *
277 * @return the value present in the given item's related field in the
278 * subclass preferred format
279 */
280 abstract protected Object getFromField(int item);
281
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 */
292 abstract protected void setToField(Object value, int item);
293
294 /**
295 * Create a new field for the given graphical component at the given index
296 * (note that the component is usually created by
297 * {@link ConfigItem#createEmptyField(int)}).
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 }
311
312 for (int i = fields.size(); i <= item; i++) {
313 fields.add(null);
314 }
315
316 fields.set(item, field);
317 }
318
319 /**
320 * Retrieve the associated graphical component that was created with
321 * {@link ConfigItem#createEmptyField(int)}.
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 }
334
335 if (item < fields.size()) {
336 return fields.get(item);
337 }
338
339 return null;
340 }
341
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
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 }
405
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
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)
418 *
419 * @return TRUE if it has
420 */
421 protected boolean hasValueChanged(Object value, int item) {
422 // We consider "" and NULL to be equals
423 Object orig = getOrig(item);
424 if (orig == null) {
425 orig = "";
426 }
427 return !orig.equals(value == null ? "" : value);
428 }
429
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
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 */
456 private void reload(int item) {
457 if (item >= 0 && !itemFields.containsKey(item)) {
458 addItem(item);
459 }
460
461 // if (item >= 0) {
462 // Object value = getFromField(item);
463 // if (value == null) {
464 // value = "";
465 // }
466 //
467 // boolean empty = value.equals("");
468 //
469 // if (!empty && item >= info.getListSize(false)) {
470 // // item was deleted, remove it
471 // removeItem(item);
472 // return;
473 // }
474 //
475 // // in case of reload after remove
476 // if (!itemFields.containsKey(item)) {
477 // addItem(item);
478 // }
479 // }
480
481 Object value = getFromInfo(item);
482 setToField(value, item);
483 setOrig(value == null ? "" : value, item);
484 }
485
486 /**
487 * If the item has been modified, set the {@link MetaInfo} to dirty then
488 * modify it to, reflect the changes so it can be saved later.
489 * <p>
490 * This method does <b>not</b> call {@link MetaInfo#save(boolean)}.
491 */
492 private void save() {
493 if (info.isArray()) {
494 boolean dirty = fields.size() != info.getListSize(false);
495 for (int item = 0; item < fields.size(); item++) {
496 if (getDirtyBit(item)) {
497 dirty = true;
498 }
499 }
500
501 if (dirty) {
502 info.setString(null, -1);
503 for (int item = 0; item < fields.size(); item++) {
504 Object value = null;
505 if (getField(item) != null) {
506 value = getFromField(item);
507 if ("".equals(value)) {
508 value = null;
509 }
510 }
511
512 info.setDirty();
513 setToInfo(value, item);
514 setOrig(value, item);
515 }
516 }
517 } else {
518 if (getDirtyBit(-1)) {
519 Object value = getFromField(-1);
520
521 info.setDirty();
522 setToInfo(value, -1);
523 setOrig(value, -1);
524 }
525 }
526 }
527
528 /**
529 * Check if the item is dirty, and clear the dirty bit if set.
530 *
531 * @param item
532 * the item number to get for an array of values, or -1 to get
533 * the whole value (has no effect if {@link MetaInfo#isArray()}
534 * is FALSE)
535 *
536 * @return TRUE if it was dirty, FALSE if not
537 */
538 private boolean getDirtyBit(int item) {
539 if (dirtyBits != null) {
540 return dirtyBits.remove((Integer) item);
541 }
542
543 Object value = null;
544 if (getField(item) != null) {
545 value = getFromField(item);
546 }
547
548 return hasValueChanged(value, item);
549 }
550
551 /**
552 * Create a new field for the given item.
553 *
554 * @param item
555 * the item number to get for an array of values, or -1 to get
556 * the whole value (has no effect if {@link MetaInfo#isArray()}
557 * is FALSE)
558 *
559 * @return the newly created field
560 */
561 protected JComponent createField(final int item) {
562 JComponent field = createEmptyField(item);
563 setField(item, field);
564 reload(item);
565
566 info.addReloadedListener(new Runnable() {
567 @Override
568 public void run() {
569 reload();
570 }
571 });
572 info.addSaveListener(new Runnable() {
573 @Override
574 public void run() {
575 save();
576 }
577 });
578
579 int height = Math
580 .max(getMinimumHeight(), field.getMinimumSize().height);
581 field.setPreferredSize(new Dimension(200, height));
582
583 return field;
584 }
585
586 /**
587 * Create a label which width is constrained in lock steps.
588 *
589 * @param nhgap
590 * negative horisontal gap in pixel to use for the label, i.e.,
591 * the step lock sized labels will start smaller by that amount
592 * (the use case would be to align controls that start at a
593 * different horisontal position)
594 *
595 * @return the label
596 */
597 protected JComponent label(int nhgap) {
598 final JLabel label = new JLabel(info.getName());
599
600 Dimension ps = label.getPreferredSize();
601 if (ps == null) {
602 ps = label.getSize();
603 }
604
605 ps.height = Math.max(ps.height, getMinimumHeight());
606
607 int w = ps.width;
608 int step = 150;
609 for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
610 if (w < i) {
611 w = i;
612 break;
613 }
614 }
615
616 final Runnable showInfo = new Runnable() {
617 @Override
618 public void run() {
619 StringBuilder builder = new StringBuilder();
620 String text = (info.getDescription().replace("\\n", "\n"))
621 .trim();
622 for (String line : StringUtils.justifyText(text, 80,
623 Alignment.LEFT)) {
624 if (builder.length() > 0) {
625 builder.append("\n");
626 }
627 builder.append(line);
628 }
629 text = builder.toString();
630 JOptionPane.showMessageDialog(ConfigItem.this, text,
631 info.getName(), JOptionPane.INFORMATION_MESSAGE);
632 }
633 };
634
635 JLabel help = new JLabel("");
636 help.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
637 setImage(help, img64info, "?");
638
639 help.addMouseListener(new MouseAdapter() {
640 @Override
641 public void mouseClicked(MouseEvent e) {
642 showInfo.run();
643 }
644 });
645
646 JPanel pane2 = new JPanel(new BorderLayout());
647 pane2.add(help, BorderLayout.WEST);
648 pane2.add(new JLabel(" "), BorderLayout.CENTER);
649
650 JPanel contentPane = new JPanel(new BorderLayout());
651 contentPane.add(label, BorderLayout.WEST);
652 contentPane.add(pane2, BorderLayout.CENTER);
653
654 ps.width = w + 30; // 30 for the (?) sign
655 contentPane.setSize(ps);
656 contentPane.setPreferredSize(ps);
657
658 JPanel pane = new JPanel(new BorderLayout());
659 pane.add(contentPane, BorderLayout.NORTH);
660
661 return pane;
662 }
663
664 /**
665 * Create a new {@link ConfigItem} for the given {@link MetaInfo}.
666 *
667 * @param <E>
668 * the type of {@link Bundle} to edit
669 *
670 * @param info
671 * the {@link MetaInfo}
672 * @param nhgap
673 * negative horisontal gap in pixel to use for the label, i.e.,
674 * the step lock sized labels will start smaller by that amount
675 * (the use case would be to align controls that start at a
676 * different horisontal position)
677 *
678 * @return the new {@link ConfigItem}
679 */
680 static public <E extends Enum<E>> ConfigItem<E> createItem(
681 MetaInfo<E> info, int nhgap) {
682
683 ConfigItem<E> configItem;
684 switch (info.getFormat()) {
685 case BOOLEAN:
686 configItem = new ConfigItemBoolean<E>(info);
687 break;
688 case COLOR:
689 configItem = new ConfigItemColor<E>(info);
690 break;
691 case FILE:
692 configItem = new ConfigItemBrowse<E>(info, false);
693 break;
694 case DIRECTORY:
695 configItem = new ConfigItemBrowse<E>(info, true);
696 break;
697 case COMBO_LIST:
698 configItem = new ConfigItemCombobox<E>(info, true);
699 break;
700 case FIXED_LIST:
701 configItem = new ConfigItemCombobox<E>(info, false);
702 break;
703 case INT:
704 configItem = new ConfigItemInteger<E>(info);
705 break;
706 case PASSWORD:
707 configItem = new ConfigItemPassword<E>(info);
708 break;
709 case LOCALE:
710 configItem = new ConfigItemLocale<E>(info);
711 break;
712 case STRING:
713 default:
714 configItem = new ConfigItemString<E>(info);
715 break;
716 }
717
718 configItem.init(nhgap);
719 return configItem;
720 }
721
722 /**
723 * Set an image to the given {@link JButton}, with a fallback text if it
724 * fails.
725 *
726 * @param button
727 * the button to set
728 * @param image64
729 * the image in BASE64 (should be PNG or similar)
730 * @param fallbackText
731 * text to use in case the image cannot be created
732 */
733 static protected void setImage(JLabel button, String image64,
734 String fallbackText) {
735 try {
736 Image img = new Image(image64);
737 try {
738 BufferedImage bImg = ImageUtilsAwt.fromImage(img);
739 button.setIcon(new ImageIcon(bImg));
740 } finally {
741 img.close();
742 }
743 } catch (IOException e) {
744 // This is an hard-coded image, should not happen
745 button.setText(fallbackText);
746 }
747 }
748
749 /**
750 * Set an image to the given {@link JButton}, with a fallback text if it
751 * fails.
752 *
753 * @param button
754 * the button to set
755 * @param image64
756 * the image in BASE64 (should be PNG or similar)
757 * @param fallbackText
758 * text to use in case the image cannot be created
759 */
760 static protected void setImage(JButton button, String image64,
761 String fallbackText) {
762 try {
763 Image img = new Image(image64);
764 try {
765 BufferedImage bImg = ImageUtilsAwt.fromImage(img);
766 button.setIcon(new ImageIcon(bImg));
767 } finally {
768 img.close();
769 }
770 } catch (IOException e) {
771 // This is an hard-coded image, should not happen
772 button.setText(fallbackText);
773 }
774 }
775
776 static private int getMinimumHeight() {
777 if (minimumHeight < 0) {
778 minimumHeight = new JTextField("Test").getMinimumSize().height;
779 }
780
781 return minimumHeight;
782 }
783 }