Merge commit 'a286f64f40c6c923fcb7d66b6a2272a31a510831'
[fanfix.git] / ui / ConfigItemPassword.java
CommitLineData
d5026c09
NR
1package be.nikiroo.utils.ui;
2
6a7c8ef2
NR
3import java.awt.BorderLayout;
4import java.awt.event.ActionEvent;
5import java.awt.event.ActionListener;
6import java.util.HashMap;
7import java.util.Map;
8
9import javax.swing.JButton;
d5026c09 10import javax.swing.JComponent;
6a7c8ef2 11import javax.swing.JPanel;
d5026c09
NR
12import javax.swing.JPasswordField;
13
14import be.nikiroo.utils.resources.MetaInfo;
15
8cea9dc5 16class ConfigItemPassword<E extends Enum<E>> extends ConfigItem<E> {
d5026c09 17 private static final long serialVersionUID = 1L;
6a7c8ef2 18 /** A small 16x16 pass-protecet icon in PNG, base64 encoded. */
a0376372 19 private static String img64passProtected = //
6a7c8ef2
NR
20 ""
21 + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAnNCSVQICFXsRgQAAAD5SURBVCjP"
22 + "ndG9LoNxGIbxHxJTG9U0IsJAdCSNqZEa9BR87BaHYfW5ESYkmjQh4giwIU00MWFwAPWRSmpgaf6G"
23 + "6ts36eZ+xuu+lvuhlTGjOFHAsXldWVDRa82WhE9pZFxrtmBeUY87+yqCH3UzMh4E1VYhp2ZVVfi7"
24 + "C0PuBc9G2v6KoOlIQUoyhovyLb+uZla/TbsRHnOgJkfSi4YpbDiXjuwJDS+SlASLYC9mw5KgxJlg"
25 + "CWJ4OyqckvKkIWswwmXrmPbl0QBkHcbsHRv6Fbz6MNnesWMnpMw51vRmphuXo7FujHf+cCt4NGza"
26 + "lbp3l5b1xR/1rWrYf/MLWpplWwswQpMAAAAASUVORK5CYII=";
27
28 /** A small 16x16 pass-unprotecet icon in PNG, base64 encoded. */
a0376372 29 private static String img64passUnprotected = //
6a7c8ef2
NR
30 ""
31 + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAmJLR0QAAKqNIzIAAAAJcEhZcwAA"
32 + "CxMAAAsTAQCanBgAAAAHdElNRQfjBR8MIilwhCwdAAABK0lEQVQoz5XQv0uUAQCH8c/7qod4nect"
33 + "gop3BIKDFBIiRyiKtATmcEiBDW7+Ae5ODt5gW0SLigouKTg6SJvkjw4Co8mcNeWgc+o839dBBXPz"
34 + "+Y7PM33r3NCpWcWKM1lfHapJq0B4G/TbEDoyZlyHQxuGtdw6eSMC33yyJxa79MW+wIj8TdDrxJSS"
35 + "+N5KppQNEchrkrMosmzRT0/0eGdSaFrob6DXloSqgu9mNWlUNqPPpmYNJkg5UvEMResystYVpbwW"
36 + "qWpjVWwcfNQqLS1rAXwQOw4N4SWoqZeUVFMGuzgg65/IqIw5a3LarZnDcxd+ScMrkcikhB8+m1eU"
37 + "MODUua67q967EttR0KHFoCVX/nhxp1N4o/rfUTueekC332KRM9veqnuoAwQyHs81DiddylUvrecA"
38 + "AAAASUVORK5CYII=";
39
40 private Map<JComponent, JPasswordField> fields = new HashMap<JComponent, JPasswordField>();
d5026c09
NR
41
42 /**
43 * Create a new {@link ConfigItemPassword} for the given {@link MetaInfo}.
44 *
45 * @param info
46 * the {@link MetaInfo}
47 */
48 public ConfigItemPassword(MetaInfo<E> info) {
49 super(info, true);
50 }
51
52 @Override
53 protected Object getFromField(int item) {
6a7c8ef2 54 JPasswordField field = fields.get(getField(item));
d5026c09
NR
55 if (field != null) {
56 return new String(field.getPassword());
57 }
58
59 return null;
60 }
61
62 @Override
63 protected Object getFromInfo(int item) {
aa9c3626 64 return getInfo().getString(item, false);
d5026c09
NR
65 }
66
67 @Override
68 protected void setToField(Object value, int item) {
6a7c8ef2 69 JPasswordField field = fields.get(getField(item));
d5026c09
NR
70 if (field != null) {
71 field.setText(value == null ? "" : value.toString());
72 }
73 }
74
75 @Override
76 protected void setToInfo(Object value, int item) {
aa9c3626 77 getInfo().setString((String) value, item);
d5026c09
NR
78 }
79
80 @Override
a0376372 81 protected JComponent createEmptyField(int item) {
6a7c8ef2
NR
82 JPanel pane = new JPanel(new BorderLayout());
83 final JPasswordField field = new JPasswordField();
84 field.setEchoChar('*');
85
86 final JButton show = new JButton();
87 final Boolean[] visible = new Boolean[] { false };
a0376372 88 setImage(show, img64passProtected, "/");
6a7c8ef2
NR
89 show.addActionListener(new ActionListener() {
90 @Override
91 public void actionPerformed(ActionEvent e) {
92 visible[0] = !visible[0];
93 if (visible[0]) {
94 field.setEchoChar((char) 0);
a0376372 95 setImage(show, img64passUnprotected, "o");
6a7c8ef2
NR
96 } else {
97 field.setEchoChar('*');
a0376372 98 setImage(show, img64passProtected, "/");
6a7c8ef2
NR
99 }
100 }
101 });
102
103 pane.add(field, BorderLayout.CENTER);
104 pane.add(show, BorderLayout.EAST);
105
106 fields.put(pane, field);
107 return pane;
d5026c09
NR
108 }
109}