ConfigItem upgrade
[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.Color;
5 import java.awt.Cursor;
6 import java.awt.Dimension;
7 import java.awt.Graphics2D;
8 import java.awt.event.ActionEvent;
9 import java.awt.event.ActionListener;
10 import java.awt.event.MouseAdapter;
11 import java.awt.event.MouseEvent;
12 import java.awt.image.BufferedImage;
13 import java.io.File;
14 import java.io.IOException;
15
16 import javax.swing.Icon;
17 import javax.swing.ImageIcon;
18 import javax.swing.JButton;
19 import javax.swing.JCheckBox;
20 import javax.swing.JColorChooser;
21 import javax.swing.JComboBox;
22 import javax.swing.JComponent;
23 import javax.swing.JFileChooser;
24 import javax.swing.JLabel;
25 import javax.swing.JOptionPane;
26 import javax.swing.JPanel;
27 import javax.swing.JPasswordField;
28 import javax.swing.JSpinner;
29 import javax.swing.JTextField;
30
31 import be.nikiroo.utils.Image;
32 import be.nikiroo.utils.StringUtils;
33 import be.nikiroo.utils.StringUtils.Alignment;
34 import be.nikiroo.utils.resources.Bundle;
35 import be.nikiroo.utils.resources.Meta.Format;
36 import be.nikiroo.utils.resources.MetaInfo;
37
38 /**
39 * A graphical item that reflect a configuration option from the given
40 * {@link Bundle}.
41 * <p>
42 * This graphical item can be edited, and the result will be saved back into the
43 * linked {@link MetaInfo}; you still have to save the {@link MetaInfo} should
44 * you wish to, of course.
45 *
46 * @author niki
47 *
48 * @param <E>
49 * the type of {@link Bundle} to edit
50 */
51 public class ConfigItem<E extends Enum<E>> extends JPanel {
52 private static final long serialVersionUID = 1L;
53
54 /** A small (?) blue in PNG, base64 encoded. */
55 private static String infoImage64 = //
56 ""
57 + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABmJLR0QA/wD/AP+gvaeTAAAACXBI"
58 + "WXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH4wURFRg6IrtcdgAAATdJREFUOMvtkj8sQ1EUxr9z/71G"
59 + "m1RDogYxq7WDDYMYTSajSG4n6YRYzSaSLibWbiaDIGwdiLIYDFKDNJEgKu969xi8UNHy7H7LPcN3"
60 + "v/Odcy+hG9oOIeIcBCJS9MAvlZtOMtHxsrFrJHGqe0RVGnHAHpcIbPlng8BS3HmKBJYzabGUzcrJ"
61 + "XK+ckIrqANYR2JEv2nYDEVck0WKGfHzyq82Go+btxoX3XAcAIqTj8wPqOH6mtMeM4bGCLhyfhTMA"
62 + "qlLhKHqujCfaweCAmV0p50dPzsNpEKpK01V/n55HIvTnfDC2odKlfeYadZN/T+AqDACUsnkhqaU1"
63 + "LRIVuX1x7ciuSWQxVIrunONrfq3dI6oh+T94Z8453vEem/HTqT8ZpFJ0qDXtGkPbAGAMeSRngQCA"
64 + "eUvgn195AwlZWyvjtQdhAAAAAElFTkSuQmCC";
65
66 /**
67 * Create a new {@link ConfigItem} for the given {@link MetaInfo}.
68 *
69 * @param info
70 * the {@link MetaInfo}
71 * @param nhgap
72 * negative horisontal gap in pixel to use for the label, i.e.,
73 * the step lock sized labels will start smaller by that amount
74 * (the use case would be to align controls that start at a
75 * different horisontal position)
76 */
77 public ConfigItem(MetaInfo<E> info, int nhgap) {
78 this.setLayout(new BorderLayout());
79
80 // TODO: support arrays
81 Format fmt = info.getFormat();
82 if (info.isArray()) {
83 fmt = Format.STRING;
84 }
85
86 switch (fmt) {
87 case BOOLEAN:
88 addBooleanField(info, nhgap);
89 break;
90 case COLOR:
91 addColorField(info, nhgap);
92 break;
93 case FILE:
94 addBrowseField(info, nhgap, false);
95 break;
96 case DIRECTORY:
97 addBrowseField(info, nhgap, true);
98 break;
99 case COMBO_LIST:
100 addComboboxField(info, nhgap, true);
101 break;
102 case FIXED_LIST:
103 addComboboxField(info, nhgap, false);
104 break;
105 case INT:
106 addIntField(info, nhgap);
107 break;
108 case PASSWORD:
109 addPasswordField(info, nhgap);
110 break;
111 case STRING:
112 case LOCALE: // TODO?
113 default:
114 addStringField(info, nhgap);
115 break;
116 }
117 }
118
119 private void addStringField(final MetaInfo<E> info, int nhgap) {
120 final JTextField field = new JTextField();
121 field.setToolTipText(info.getDescription());
122 field.setText(info.getString(false));
123
124 info.addReloadedListener(new Runnable() {
125 @Override
126 public void run() {
127 field.setText(info.getString(false));
128 }
129 });
130 info.addSaveListener(new Runnable() {
131 @Override
132 public void run() {
133 info.setString(field.getText());
134 }
135 });
136
137 this.add(label(info, nhgap), BorderLayout.WEST);
138 this.add(field, BorderLayout.CENTER);
139
140 setPreferredSize(field);
141 }
142
143 private void addBooleanField(final MetaInfo<E> info, int nhgap) {
144 final JCheckBox field = new JCheckBox();
145 field.setToolTipText(info.getDescription());
146 Boolean state = info.getBoolean(true);
147
148 // Should not happen!
149 if (state == null) {
150 System.err
151 .println("No default value given for BOOLEAN parameter \""
152 + info.getName() + "\", we consider it is FALSE");
153 state = false;
154 }
155
156 field.setSelected(state);
157
158 info.addReloadedListener(new Runnable() {
159 @Override
160 public void run() {
161 Boolean state = info.getBoolean(true);
162 if (state == null) {
163 state = false;
164 }
165
166 field.setSelected(state);
167 }
168 });
169 info.addSaveListener(new Runnable() {
170 @Override
171 public void run() {
172 info.setBoolean(field.isSelected());
173 }
174 });
175
176 this.add(label(info, nhgap), BorderLayout.WEST);
177 this.add(field, BorderLayout.CENTER);
178
179 setPreferredSize(field);
180 }
181
182 private void addColorField(final MetaInfo<E> info, int nhgap) {
183 final JTextField field = new JTextField();
184 field.setToolTipText(info.getDescription());
185 field.setText(info.getString(false));
186
187 info.addReloadedListener(new Runnable() {
188 @Override
189 public void run() {
190 field.setText(info.getString(false));
191 }
192 });
193 info.addSaveListener(new Runnable() {
194 @Override
195 public void run() {
196 info.setString(field.getText());
197 }
198 });
199
200 this.add(label(info, nhgap), BorderLayout.WEST);
201 JPanel pane = new JPanel(new BorderLayout());
202
203 final JButton colorWheel = new JButton();
204 colorWheel.setIcon(getIcon(17, info.getColor(true)));
205 colorWheel.addActionListener(new ActionListener() {
206 @Override
207 public void actionPerformed(ActionEvent e) {
208 Color initialColor = new Color(info.getColor(true), true);
209 Color newColor = JColorChooser.showDialog(ConfigItem.this,
210 info.getName(), initialColor);
211 if (newColor != null) {
212 info.setColor(newColor.getRGB());
213 field.setText(info.getString(false));
214 colorWheel.setIcon(getIcon(17, info.getColor(true)));
215 }
216 }
217 });
218 pane.add(colorWheel, BorderLayout.WEST);
219 pane.add(field, BorderLayout.CENTER);
220 this.add(pane, BorderLayout.CENTER);
221
222 setPreferredSize(pane);
223 }
224
225 private void addBrowseField(final MetaInfo<E> info, int nhgap,
226 final boolean dir) {
227 final JTextField field = new JTextField();
228 field.setToolTipText(info.getDescription());
229 field.setText(info.getString(false));
230
231 info.addReloadedListener(new Runnable() {
232 @Override
233 public void run() {
234 field.setText(info.getString(false));
235 }
236 });
237 info.addSaveListener(new Runnable() {
238 @Override
239 public void run() {
240 info.setString(field.getText());
241 }
242 });
243
244 JButton browseButton = new JButton("...");
245 browseButton.addActionListener(new ActionListener() {
246 @Override
247 public void actionPerformed(ActionEvent e) {
248 JFileChooser chooser = new JFileChooser();
249 chooser.setCurrentDirectory(null);
250 chooser.setFileSelectionMode(dir ? JFileChooser.DIRECTORIES_ONLY
251 : JFileChooser.FILES_ONLY);
252 if (chooser.showOpenDialog(ConfigItem.this) == JFileChooser.APPROVE_OPTION) {
253 File file = chooser.getSelectedFile();
254 if (file != null) {
255 info.setString(file.getAbsolutePath());
256 field.setText(info.getString(false));
257 }
258 }
259 }
260 });
261
262 JPanel pane = new JPanel(new BorderLayout());
263 this.add(label(info, nhgap), BorderLayout.WEST);
264 pane.add(browseButton, BorderLayout.WEST);
265 pane.add(field, BorderLayout.CENTER);
266 this.add(pane, BorderLayout.CENTER);
267
268 setPreferredSize(pane);
269 }
270
271 private void addComboboxField(final MetaInfo<E> info, int nhgap,
272 boolean editable) {
273 // rawtypes for Java 1.6 (and 1.7 ?) support
274 @SuppressWarnings({ "rawtypes", "unchecked" })
275 final JComboBox field = new JComboBox(info.getAllowedValues());
276 field.setEditable(editable);
277 field.setSelectedItem(info.getString(false));
278
279 info.addReloadedListener(new Runnable() {
280 @Override
281 public void run() {
282 field.setSelectedItem(info.getString(false));
283 }
284 });
285 info.addSaveListener(new Runnable() {
286 @Override
287 public void run() {
288 info.setString(field.getSelectedItem().toString());
289 }
290 });
291
292 this.add(label(info, nhgap), BorderLayout.WEST);
293 this.add(field, BorderLayout.CENTER);
294
295 setPreferredSize(field);
296 }
297
298 private void addPasswordField(final MetaInfo<E> info, int nhgap) {
299 final JPasswordField field = new JPasswordField();
300 field.setToolTipText(info.getDescription());
301 field.setText(info.getString(true));
302
303 info.addReloadedListener(new Runnable() {
304 @Override
305 public void run() {
306 field.setText(info.getString(false));
307 }
308 });
309 info.addSaveListener(new Runnable() {
310 @Override
311 public void run() {
312 info.setString(new String(field.getPassword()));
313 }
314 });
315
316 this.add(label(info, nhgap), BorderLayout.WEST);
317 this.add(field, BorderLayout.CENTER);
318
319 setPreferredSize(field);
320 }
321
322 private void addIntField(final MetaInfo<E> info, int nhgap) {
323 final JSpinner field = new JSpinner();
324 field.setToolTipText(info.getDescription());
325 field.setValue(info.getInteger(true) == null ? 0 : info
326 .getInteger(true));
327
328 info.addReloadedListener(new Runnable() {
329 @Override
330 public void run() {
331 field.setValue(info.getInteger(true) == null ? 0 : info
332 .getInteger(true));
333 }
334 });
335 info.addSaveListener(new Runnable() {
336 @Override
337 public void run() {
338 info.setInteger((Integer) field.getValue());
339 Integer value = info.getInteger(false);
340 if (value == null) {
341 field.setValue(0);
342 }
343 }
344 });
345
346 this.add(label(info, nhgap), BorderLayout.WEST);
347 this.add(field, BorderLayout.CENTER);
348
349 setPreferredSize(field);
350 }
351
352 /**
353 * Create a label which width is constrained in lock steps.
354 *
355 * @param info
356 * the {@link MetaInfo} for which we want to add a label
357 * @param nhgap
358 * negative horisontal gap in pixel to use for the label, i.e.,
359 * the step lock sized labels will start smaller by that amount
360 * (the use case would be to align controls that start at a
361 * different horisontal position)
362 *
363 * @return the label
364 */
365 private JComponent label(final MetaInfo<E> info, int nhgap) {
366 final JLabel label = new JLabel(info.getName());
367
368 Dimension ps = label.getPreferredSize();
369 if (ps == null) {
370 ps = label.getSize();
371 }
372
373 int w = ps.width;
374 int step = 150;
375 for (int i = 2 * step - nhgap; i < 10 * step; i += step) {
376 if (w < i) {
377 w = i;
378 break;
379 }
380 }
381
382 final Runnable showInfo = new Runnable() {
383 @Override
384 public void run() {
385 StringBuilder builder = new StringBuilder();
386 String text = (info.getDescription().replace("\\n", "\n"))
387 .trim();
388 for (String line : StringUtils.justifyText(text, 80,
389 Alignment.LEFT)) {
390 if (builder.length() > 0) {
391 builder.append("\n");
392 }
393 builder.append(line);
394 }
395 text = builder.toString();
396 JOptionPane.showMessageDialog(ConfigItem.this, text,
397 info.getName(), JOptionPane.INFORMATION_MESSAGE);
398 }
399 };
400
401 JLabel help = new JLabel("");
402 help.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
403 try {
404 Image img = new Image(infoImage64);
405 try {
406 BufferedImage bImg = ImageUtilsAwt.fromImage(img);
407 help.setIcon(new ImageIcon(bImg));
408 } finally {
409 img.close();
410 }
411 } catch (IOException e) {
412 // This is an hard-coded image, should not happen
413 help.setText("?");
414 }
415
416 help.addMouseListener(new MouseAdapter() {
417 @Override
418 public void mouseClicked(MouseEvent e) {
419 showInfo.run();
420 }
421 });
422
423 JPanel pane2 = new JPanel(new BorderLayout());
424 pane2.add(help, BorderLayout.WEST);
425 pane2.add(new JLabel(" "), BorderLayout.CENTER);
426
427 JPanel pane = new JPanel(new BorderLayout());
428 pane.add(label, BorderLayout.WEST);
429 pane.add(pane2, BorderLayout.CENTER);
430
431 ps.width = w + 30; // 30 for the (?) sign
432 pane.setSize(ps);
433 pane.setPreferredSize(ps);
434
435 return pane;
436 }
437
438 /**
439 * Return an {@link Icon} to use as a colour badge for the colour field
440 * controls.
441 *
442 * @param size
443 * the size of the badge
444 * @param color
445 * the colour of the badge
446 *
447 * @return the badge
448 */
449 private Icon getIcon(int size, int color) {
450 Color c = new Color(color, true);
451 int avg = (c.getRed() + c.getGreen() + c.getBlue()) / 3;
452 Color border = (avg >= 128 ? Color.BLACK : Color.WHITE);
453
454 BufferedImage img = new BufferedImage(size, size,
455 BufferedImage.TYPE_4BYTE_ABGR);
456
457 Graphics2D g = img.createGraphics();
458 try {
459 g.setColor(c);
460 g.fillRect(0, 0, img.getWidth(), img.getHeight());
461 g.setColor(border);
462 g.drawRect(0, 0, img.getWidth() - 1, img.getHeight() - 1);
463 } finally {
464 g.dispose();
465 }
466
467 return new ImageIcon(img);
468 }
469
470 private void setPreferredSize(JComponent field) {
471 JTextField a = new JTextField("Test");
472 int height = Math.max(a.getMinimumSize().height,
473 field.getMinimumSize().height);
474 setPreferredSize(new Dimension(200, height));
475 }
476 }