1 package be
.nikiroo
.utils
.ui
;
3 import java
.awt
.BorderLayout
;
4 import java
.awt
.event
.ActionEvent
;
5 import java
.awt
.event
.ActionListener
;
6 import java
.util
.HashMap
;
9 import javax
.swing
.JButton
;
10 import javax
.swing
.JComponent
;
11 import javax
.swing
.JPanel
;
12 import javax
.swing
.JPasswordField
;
14 import be
.nikiroo
.utils
.resources
.MetaInfo
;
16 class 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
= //
21 + "iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAQAAAC1+jfqAAAAAnNCSVQICFXsRgQAAAD5SURBVCjP"
22 + "ndG9LoNxGIbxHxJTG9U0IsJAdCSNqZEa9BR87BaHYfW5ESYkmjQh4giwIU00MWFwAPWRSmpgaf6G"
23 + "6ts36eZ+xuu+lvuhlTGjOFHAsXldWVDRa82WhE9pZFxrtmBeUY87+yqCH3UzMh4E1VYhp2ZVVfi7"
24 + "C0PuBc9G2v6KoOlIQUoyhovyLb+uZla/TbsRHnOgJkfSi4YpbDiXjuwJDS+SlASLYC9mw5KgxJlg"
25 + "CWJ4OyqckvKkIWswwmXrmPbl0QBkHcbsHRv6Fbz6MNnesWMnpMw51vRmphuXo7FujHf+cCt4NGza"
26 + "lbp3l5b1xR/1rWrYf/MLWpplWwswQpMAAAAASUVORK5CYII=";
28 /** A small 16x16 pass-unprotecet icon in PNG, base64 encoded. */
29 private static String img64passUnprotected
= //
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"
40 private Map
<JComponent
, JPasswordField
> fields
= new HashMap
<JComponent
, JPasswordField
>();
43 * Create a new {@link ConfigItemPassword} for the given {@link MetaInfo}.
46 * the {@link MetaInfo}
48 public ConfigItemPassword(MetaInfo
<E
> info
) {
53 protected Object
getFromField(int item
) {
54 JPasswordField field
= fields
.get(getField(item
));
56 return new String(field
.getPassword());
63 protected Object
getFromInfo(int item
) {
64 return getInfo().getString(item
, false);
68 protected void setToField(Object value
, int item
) {
69 JPasswordField field
= fields
.get(getField(item
));
71 field
.setText(value
== null ?
"" : value
.toString());
76 protected void setToInfo(Object value
, int item
) {
77 getInfo().setString((String
) value
, item
);
81 protected JComponent
createEmptyField(int item
) {
82 JPanel pane
= new JPanel(new BorderLayout());
83 final JPasswordField field
= new JPasswordField();
84 field
.setEchoChar('*');
86 final JButton show
= new JButton();
87 final Boolean
[] visible
= new Boolean
[] { false };
88 setImage(show
, img64passProtected
, "/");
89 show
.addActionListener(new ActionListener() {
91 public void actionPerformed(ActionEvent e
) {
92 visible
[0] = !visible
[0];
94 field
.setEchoChar((char) 0);
95 setImage(show
, img64passUnprotected
, "o");
97 field
.setEchoChar('*');
98 setImage(show
, img64passProtected
, "/");
103 pane
.add(field
, BorderLayout
.CENTER
);
104 pane
.add(show
, BorderLayout
.EAST
);
106 fields
.put(pane
, field
);