weblib: use version in name
[nikiroo-utils.git] / src / be / nikiroo / utils / ui / ConfigItemPassword.java
... / ...
CommitLineData
1package be.nikiroo.utils.ui;
2
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;
10import javax.swing.JComponent;
11import javax.swing.JPanel;
12import javax.swing.JPasswordField;
13
14import be.nikiroo.utils.resources.MetaInfo;
15
16class ConfigItemPassword<E extends Enum<E>> extends ConfigItem<E> {
17 private static final long serialVersionUID = 1L;
18 /** A small 16x16 pass-protecet icon in PNG, base64 encoded. */
19 private static String img64passProtected = //
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. */
29 private static String img64passUnprotected = //
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>();
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) {
54 JPasswordField field = fields.get(getField(item));
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) {
64 return getInfo().getString(item, false);
65 }
66
67 @Override
68 protected void setToField(Object value, int item) {
69 JPasswordField field = fields.get(getField(item));
70 if (field != null) {
71 field.setText(value == null ? "" : value.toString());
72 }
73 }
74
75 @Override
76 protected void setToInfo(Object value, int item) {
77 getInfo().setString((String) value, item);
78 }
79
80 @Override
81 protected JComponent createEmptyField(int item) {
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 };
88 setImage(show, img64passProtected, "/");
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);
95 setImage(show, img64passUnprotected, "o");
96 } else {
97 field.setEchoChar('*');
98 setImage(show, img64passProtected, "/");
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;
108 }
109}