fix ConfigItems, especially regarding items
[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 12import java.util.ArrayList;
a0376372 13import java.util.HashMap;
d5026c09 14import java.util.List;
a0376372 15import java.util.Map;
424dcb0d 16
d5026c09 17import javax.swing.BoxLayout;
424dcb0d
NR
18import javax.swing.ImageIcon;
19import javax.swing.JButton;
0877d6f5 20import javax.swing.JComponent;
8517b60c 21import javax.swing.JLabel;
0877d6f5 22import javax.swing.JOptionPane;
d350b96b
NR
23import javax.swing.JPanel;
24import javax.swing.JTextField;
d350b96b 25
daf0fd5a 26import be.nikiroo.utils.Image;
0877d6f5
NR
27import be.nikiroo.utils.StringUtils;
28import be.nikiroo.utils.StringUtils.Alignment;
d350b96b 29import be.nikiroo.utils.resources.Bundle;
9e834013 30import 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 45public 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
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
d5026c09
NR
481 Object value = getFromInfo(item);
482 setToField(value, item);
a0376372 483 setOrig(value == null ? "" : value, item);
d5026c09 484 }
0877d6f5 485
d5026c09
NR
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)}.
a0376372
NR
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.
d5026c09
NR
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)
a0376372
NR
535 *
536 * @return TRUE if it was dirty, FALSE if not
d5026c09 537 */
a0376372 538 private boolean getDirtyBit(int item) {
d5026c09 539 if (dirtyBits != null) {
a0376372 540 return dirtyBits.remove((Integer) item);
d5026c09 541 }
d18e136e 542
a0376372
NR
543 Object value = null;
544 if (getField(item) != null) {
545 value = getFromField(item);
d5026c09 546 }
a0376372
NR
547
548 return hasValueChanged(value, item);
0877d6f5
NR
549 }
550
d5026c09 551 /**
a0376372 552 * Create a new field for the given item.
d5026c09
NR
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)
01a5d26c 558 *
a0376372 559 * @return the newly created field
d5026c09 560 */
a0376372
NR
561 protected JComponent createField(final int item) {
562 JComponent field = createEmptyField(item);
563 setField(item, field);
d5026c09 564 reload(item);
0877d6f5
NR
565
566 info.addReloadedListener(new Runnable() {
567 @Override
568 public void run() {
a0376372 569 reload();
0877d6f5
NR
570 }
571 });
572 info.addSaveListener(new Runnable() {
573 @Override
574 public void run() {
a0376372 575 save();
0877d6f5
NR
576 }
577 });
9e834013 578
a0376372
NR
579 int height = Math
580 .max(getMinimumHeight(), field.getMinimumSize().height);
581 field.setPreferredSize(new Dimension(200, height));
d5026c09
NR
582
583 return field;
d350b96b 584 }
c637d2e0
NR
585
586 /**
587 * Create a label which width is constrained in lock steps.
588 *
d18e136e
NR
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)
c637d2e0
NR
594 *
595 * @return the label
596 */
d5026c09 597 protected JComponent label(int nhgap) {
0877d6f5 598 final JLabel label = new JLabel(info.getName());
c637d2e0
NR
599
600 Dimension ps = label.getPreferredSize();
601 if (ps == null) {
602 ps = label.getSize();
603 }
604
e95f4fb6
NR
605 ps.height = Math.max(ps.height, getMinimumHeight());
606
c637d2e0 607 int w = ps.width;
daf0fd5a 608 int step = 150;
d18e136e 609 for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
c637d2e0
NR
610 if (w < i) {
611 w = i;
612 break;
613 }
614 }
615
daf0fd5a 616 final Runnable showInfo = new Runnable() {
0877d6f5 617 @Override
daf0fd5a 618 public void run() {
0877d6f5 619 StringBuilder builder = new StringBuilder();
d18e136e
NR
620 String text = (info.getDescription().replace("\\n", "\n"))
621 .trim();
0877d6f5
NR
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();
daf0fd5a
NR
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));
a0376372 637 setImage(help, img64info, "?");
daf0fd5a
NR
638
639 help.addMouseListener(new MouseAdapter() {
640 @Override
641 public void mouseClicked(MouseEvent e) {
642 showInfo.run();
0877d6f5
NR
643 }
644 });
645
646 JPanel pane2 = new JPanel(new BorderLayout());
647 pane2.add(help, BorderLayout.WEST);
648 pane2.add(new JLabel(" "), BorderLayout.CENTER);
649
e95f4fb6
NR
650 JPanel contentPane = new JPanel(new BorderLayout());
651 contentPane.add(label, BorderLayout.WEST);
652 contentPane.add(pane2, BorderLayout.CENTER);
0877d6f5
NR
653
654 ps.width = w + 30; // 30 for the (?) sign
e95f4fb6
NR
655 contentPane.setSize(ps);
656 contentPane.setPreferredSize(ps);
657
658 JPanel pane = new JPanel(new BorderLayout());
659 pane.add(contentPane, BorderLayout.NORTH);
c637d2e0 660
0877d6f5 661 return pane;
c637d2e0 662 }
424dcb0d 663
a0376372
NR
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) {
e95f4fb6 682
a0376372
NR
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;
e95f4fb6
NR
716 }
717
a0376372
NR
718 configItem.init(nhgap);
719 return configItem;
e95f4fb6 720 }
01a5d26c
NR
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 */
249e4ef4 733 static protected void setImage(JLabel button, String image64,
01a5d26c
NR
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 */
249e4ef4 760 static protected void setImage(JButton button, String image64,
01a5d26c
NR
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 }
a0376372
NR
775
776 static private int getMinimumHeight() {
777 if (minimumHeight < 0) {
778 minimumHeight = new JTextField("Test").getMinimumSize().height;
779 }
780
781 return minimumHeight;
782 }
d350b96b 783}