fix items in Bundle
[fanfix.git] / src / be / nikiroo / utils / ui / ConfigItem.java
CommitLineData
d350b96b
NR
1package be.nikiroo.utils.ui;
2
3import java.awt.BorderLayout;
daf0fd5a 4import java.awt.Cursor;
c637d2e0 5import java.awt.Dimension;
424dcb0d
NR
6import java.awt.event.ActionEvent;
7import java.awt.event.ActionListener;
daf0fd5a
NR
8import java.awt.event.MouseAdapter;
9import java.awt.event.MouseEvent;
424dcb0d 10import java.awt.image.BufferedImage;
daf0fd5a 11import java.io.IOException;
d5026c09
NR
12import java.util.ArrayList;
13import java.util.List;
424dcb0d 14
d5026c09 15import javax.swing.BoxLayout;
424dcb0d
NR
16import javax.swing.ImageIcon;
17import javax.swing.JButton;
0877d6f5 18import javax.swing.JComponent;
8517b60c 19import javax.swing.JLabel;
0877d6f5 20import javax.swing.JOptionPane;
d350b96b
NR
21import javax.swing.JPanel;
22import javax.swing.JTextField;
d350b96b 23
daf0fd5a 24import be.nikiroo.utils.Image;
0877d6f5
NR
25import be.nikiroo.utils.StringUtils;
26import be.nikiroo.utils.StringUtils.Alignment;
d350b96b 27import be.nikiroo.utils.resources.Bundle;
9e834013 28import be.nikiroo.utils.resources.MetaInfo;
d350b96b
NR
29
30/**
31 * A graphical item that reflect a configuration option from the given
32 * {@link Bundle}.
76b51de9
NR
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.
d350b96b
NR
37 *
38 * @author niki
db31c358 39 *
d350b96b
NR
40 * @param <E>
41 * the type of {@link Bundle} to edit
42 */
43public class ConfigItem<E extends Enum<E>> extends JPanel {
44 private static final long serialVersionUID = 1L;
db31c358 45
e95f4fb6
NR
46 private static int minimumHeight = -1;
47
01a5d26c 48 /** A small 16x16 "?" blue in PNG, base64 encoded. */
249e4ef4 49 private static String img64Info = //
daf0fd5a
NR
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
249e4ef4
NR
60 /** A small 16x16 "+" image with colours */
61 private static String img54add = //
01a5d26c
NR
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
249e4ef4
NR
77 /** A small 32x32 "-" image with colours */
78 private static String img64Remove = //
01a5d26c
NR
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
fde375c1
NR
94 /** The original value before current changes. */
95 private Object orig;
d5026c09
NR
96 private List<Integer> dirtyBits;
97
d5026c09
NR
98 private JComponent field;
99 private List<JComponent> fields = new ArrayList<JComponent>();
fde375c1 100
01a5d26c
NR
101 /** The {@link MetaInfo} linked to the field. */
102 protected MetaInfo<E> info;
103
76b51de9
NR
104 /**
105 * Create a new {@link ConfigItem} for the given {@link MetaInfo}.
106 *
107 * @param info
108 * the {@link MetaInfo}
d18e136e
NR
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)
76b51de9 114 */
d18e136e 115 public ConfigItem(MetaInfo<E> info, int nhgap) {
d5026c09 116 this(info, true);
76b51de9 117
d5026c09
NR
118 ConfigItem<E> configItem;
119 switch (info.getFormat()) {
0877d6f5 120 case BOOLEAN:
d5026c09 121 configItem = new ConfigItemBoolean<E>(info);
0877d6f5
NR
122 break;
123 case COLOR:
d5026c09 124 configItem = new ConfigItemColor<E>(info);
0877d6f5
NR
125 break;
126 case FILE:
d5026c09 127 configItem = new ConfigItemBrowse<E>(info, false);
0877d6f5
NR
128 break;
129 case DIRECTORY:
d5026c09 130 configItem = new ConfigItemBrowse<E>(info, true);
0877d6f5
NR
131 break;
132 case COMBO_LIST:
d5026c09 133 configItem = new ConfigItemCombobox<E>(info, true);
0877d6f5
NR
134 break;
135 case FIXED_LIST:
d5026c09 136 configItem = new ConfigItemCombobox<E>(info, false);
0877d6f5
NR
137 break;
138 case INT:
d5026c09 139 configItem = new ConfigItemInteger<E>(info);
0877d6f5
NR
140 break;
141 case PASSWORD:
d5026c09 142 configItem = new ConfigItemPassword<E>(info);
0877d6f5 143 break;
0f7de31e
NR
144 case LOCALE:
145 configItem = new ConfigItemLocale<E>(info);
146 break;
0877d6f5 147 case STRING:
0877d6f5 148 default:
d5026c09 149 configItem = new ConfigItemString<E>(info);
0877d6f5
NR
150 break;
151 }
d350b96b 152
d5026c09
NR
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 }
fde375c1 164
01a5d26c 165 final JButton add = new JButton();
249e4ef4 166 setImage(add, img54add, "+");
01a5d26c 167
d5026c09
NR
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
ee7a6ccb
NR
177 main.revalidate();
178 main.repaint();
d5026c09
NR
179 }
180 });
fde375c1 181
d5026c09
NR
182 JPanel tmp = new JPanel(new BorderLayout());
183 tmp.add(add, BorderLayout.WEST);
fde375c1 184
d5026c09
NR
185 JPanel mainPlus = new JPanel(new BorderLayout());
186 mainPlus.add(main, BorderLayout.CENTER);
187 mainPlus.add(tmp, BorderLayout.SOUTH);
fde375c1 188
d5026c09
NR
189 add(mainPlus, BorderLayout.CENTER);
190 } else {
191 this.setLayout(new BorderLayout());
192 add(label(nhgap), BorderLayout.WEST);
0877d6f5 193
d5026c09
NR
194 JComponent field = configItem.createComponent(-1);
195 add(field, BorderLayout.CENTER);
196 }
0877d6f5 197 }
9e834013 198
d5026c09
NR
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>();
0877d6f5 214 }
d5026c09 215 }
0877d6f5 216
d5026c09
NR
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 }
0877d6f5 235
d5026c09
NR
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 }
d18e136e 251
d5026c09
NR
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
0877d6f5
NR
265 }
266
d5026c09
NR
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 }
0877d6f5 279
d5026c09
NR
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 }
0877d6f5 294
d5026c09
NR
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 }
0877d6f5 312
d5026c09
NR
313 for (int i = fields.size(); i <= item; i++) {
314 fields.add(null);
315 }
d18e136e 316
d5026c09 317 fields.set(item, field);
0877d6f5
NR
318 }
319
d5026c09
NR
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 }
0877d6f5 335
d5026c09
NR
336 if (item < fields.size()) {
337 return fields.get(item);
338 }
d18e136e 339
d5026c09 340 return null;
0877d6f5
NR
341 }
342
d5026c09
NR
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 }
d18e136e 360
d5026c09
NR
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);
0877d6f5
NR
375 }
376
d5026c09
NR
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);
0877d6f5 388
d5026c09
NR
389 // We consider "" and NULL to be equals
390 orig = (value == null ? "" : value);
391 }
0877d6f5 392
d5026c09
NR
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 }
d18e136e 414
d5026c09
NR
415 if (dirty) {
416 info.setDirty();
417 setToInfo(value, item);
418 orig = (value == null ? "" : value);
419 }
0877d6f5
NR
420 }
421
d5026c09
NR
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
01a5d26c
NR
430 *
431 * @return
d5026c09
NR
432 */
433 protected JComponent createComponent(final int item) {
434 setField(item, createField(item));
435 reload(item);
0877d6f5
NR
436
437 info.addReloadedListener(new Runnable() {
438 @Override
439 public void run() {
d5026c09 440 reload(item);
0877d6f5
NR
441 }
442 });
443 info.addSaveListener(new Runnable() {
444 @Override
445 public void run() {
d5026c09 446 save(item);
0877d6f5
NR
447 }
448 });
9e834013 449
d5026c09 450 JComponent field = getField(item);
d18e136e 451 setPreferredSize(field);
d5026c09
NR
452
453 return field;
d350b96b 454 }
c637d2e0
NR
455
456 /**
457 * Create a label which width is constrained in lock steps.
458 *
d18e136e
NR
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)
c637d2e0
NR
464 *
465 * @return the label
466 */
d5026c09 467 protected JComponent label(int nhgap) {
0877d6f5 468 final JLabel label = new JLabel(info.getName());
c637d2e0
NR
469
470 Dimension ps = label.getPreferredSize();
471 if (ps == null) {
472 ps = label.getSize();
473 }
474
e95f4fb6
NR
475 ps.height = Math.max(ps.height, getMinimumHeight());
476
c637d2e0 477 int w = ps.width;
daf0fd5a 478 int step = 150;
d18e136e 479 for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
c637d2e0
NR
480 if (w < i) {
481 w = i;
482 break;
483 }
484 }
485
daf0fd5a 486 final Runnable showInfo = new Runnable() {
0877d6f5 487 @Override
daf0fd5a 488 public void run() {
0877d6f5 489 StringBuilder builder = new StringBuilder();
d18e136e
NR
490 String text = (info.getDescription().replace("\\n", "\n"))
491 .trim();
0877d6f5
NR
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();
daf0fd5a
NR
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));
249e4ef4 507 setImage(help, img64Info, "?");
daf0fd5a
NR
508
509 help.addMouseListener(new MouseAdapter() {
510 @Override
511 public void mouseClicked(MouseEvent e) {
512 showInfo.run();
0877d6f5
NR
513 }
514 });
515
516 JPanel pane2 = new JPanel(new BorderLayout());
517 pane2.add(help, BorderLayout.WEST);
518 pane2.add(new JLabel(" "), BorderLayout.CENTER);
519
e95f4fb6
NR
520 JPanel contentPane = new JPanel(new BorderLayout());
521 contentPane.add(label, BorderLayout.WEST);
522 contentPane.add(pane2, BorderLayout.CENTER);
0877d6f5
NR
523
524 ps.width = w + 30; // 30 for the (?) sign
e95f4fb6
NR
525 contentPane.setSize(ps);
526 contentPane.setPreferredSize(ps);
527
528 JPanel pane = new JPanel(new BorderLayout());
529 pane.add(contentPane, BorderLayout.NORTH);
c637d2e0 530
0877d6f5 531 return pane;
c637d2e0 532 }
424dcb0d 533
d5026c09 534 protected void setPreferredSize(JComponent field) {
e95f4fb6
NR
535 int height = Math
536 .max(getMinimumHeight(), field.getMinimumSize().height);
d18e136e
NR
537 setPreferredSize(new Dimension(200, height));
538 }
e95f4fb6
NR
539
540 static private int getMinimumHeight() {
541 if (minimumHeight < 0) {
542 minimumHeight = new JTextField("Test").getMinimumSize().height;
543 }
544
545 return minimumHeight;
546 }
01a5d26c
NR
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 */
249e4ef4 559 static protected void setImage(JLabel button, String image64,
01a5d26c
NR
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 */
249e4ef4 586 static protected void setImage(JButton button, String image64,
01a5d26c
NR
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 }
d350b96b 601}