Add 'src/jexer/' from commit 'cf01c92f5809a0732409e280fb0f32f27393618d'
[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 Object value = getFromInfo(item);
462 setToField(value, item);
463 setOrig(value == null ? "" : value, item);
464 }
465
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)}.
471 */
472 private void save() {
473 if (info.isArray()) {
474 boolean dirty = itemFields.size() != info.getListSize(false);
475 for (int item = 0; item < itemFields.size(); item++) {
476 if (getDirtyBit(item)) {
477 dirty = true;
478 }
479 }
480
481 if (dirty) {
482 info.setDirty();
483 info.setString(null, -1);
484
485 for (int item = 0; item < itemFields.size(); item++) {
486 Object value = null;
487 if (getField(item) != null) {
488 value = getFromField(item);
489 if ("".equals(value)) {
490 value = null;
491 }
492 }
493
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.
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)
516 *
517 * @return TRUE if it was dirty, FALSE if not
518 */
519 private boolean getDirtyBit(int item) {
520 if (dirtyBits != null) {
521 return dirtyBits.remove((Integer) item);
522 }
523
524 Object value = null;
525 if (getField(item) != null) {
526 value = getFromField(item);
527 }
528
529 return hasValueChanged(value, item);
530 }
531
532 /**
533 * Create a new field for the given item.
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)
539 *
540 * @return the newly created field
541 */
542 protected JComponent createField(final int item) {
543 JComponent field = createEmptyField(item);
544 setField(item, field);
545 reload(item);
546
547 info.addReloadedListener(new Runnable() {
548 @Override
549 public void run() {
550 reload();
551 }
552 });
553 info.addSaveListener(new Runnable() {
554 @Override
555 public void run() {
556 save();
557 }
558 });
559
560 int height = Math
561 .max(getMinimumHeight(), field.getMinimumSize().height);
562 field.setPreferredSize(new Dimension(200, height));
563
564 return field;
565 }
566
567 /**
568 * Create a label which width is constrained in lock steps.
569 *
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)
575 *
576 * @return the label
577 */
578 protected JComponent label(int nhgap) {
579 final JLabel label = new JLabel(info.getName());
580
581 Dimension ps = label.getPreferredSize();
582 if (ps == null) {
583 ps = label.getSize();
584 }
585
586 ps.height = Math.max(ps.height, getMinimumHeight());
587
588 int w = ps.width;
589 int step = 150;
590 for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
591 if (w < i) {
592 w = i;
593 break;
594 }
595 }
596
597 final Runnable showInfo = new Runnable() {
598 @Override
599 public void run() {
600 StringBuilder builder = new StringBuilder();
601 String text = (info.getDescription().replace("\\n", "\n"))
602 .trim();
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();
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));
618 setImage(help, img64info, "?");
619
620 help.addMouseListener(new MouseAdapter() {
621 @Override
622 public void mouseClicked(MouseEvent e) {
623 showInfo.run();
624 }
625 });
626
627 JPanel pane2 = new JPanel(new BorderLayout());
628 pane2.add(help, BorderLayout.WEST);
629 pane2.add(new JLabel(" "), BorderLayout.CENTER);
630
631 JPanel contentPane = new JPanel(new BorderLayout());
632 contentPane.add(label, BorderLayout.WEST);
633 contentPane.add(pane2, BorderLayout.CENTER);
634
635 ps.width = w + 30; // 30 for the (?) sign
636 contentPane.setSize(ps);
637 contentPane.setPreferredSize(ps);
638
639 JPanel pane = new JPanel(new BorderLayout());
640 pane.add(contentPane, BorderLayout.NORTH);
641
642 return pane;
643 }
644
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) {
663
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;
697 }
698
699 configItem.init(nhgap);
700 return configItem;
701 }
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 */
714 static protected void setImage(JLabel button, String image64,
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 */
741 static protected void setImage(JButton button, String image64,
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 }
756
757 static private int getMinimumHeight() {
758 if (minimumHeight < 0) {
759 minimumHeight = new JTextField("Test").getMinimumSize().height;
760 }
761
762 return minimumHeight;
763 }
764 }