]> git.argeo.org Git - gpl/argeo-slc.git/blob - SwingUserInfo.java
14eb16b6db8231aa8e0f46d8db1358df0c2051a0
[gpl/argeo-slc.git] / SwingUserInfo.java
1 package org.argeo.slc.jsch;
2
3 import java.awt.Container;
4 import java.awt.GridLayout;
5 import java.awt.Panel;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.util.Arrays;
9
10 import javax.swing.JButton;
11 import javax.swing.JDialog;
12 import javax.swing.JFrame;
13 import javax.swing.JLabel;
14 import javax.swing.JPanel;
15 import javax.swing.JPasswordField;
16
17 /** Retrieves a password or a passphrase using standard Swing */
18 public class SwingUserInfo extends SimpleUserInfo {
19
20 private Boolean alwaysPrompt = false;
21
22 public boolean promptPassphrase(String message) {
23 if (passphrase != null)
24 return true;
25
26 if (!alwaysPrompt && passphraseSafe != null)
27 return true;
28
29 PasswordDialog dialog = new PasswordDialog(message) {
30 private static final long serialVersionUID = 3266299327166418364L;
31
32 @Override
33 protected void useCredentials(char[] password) {
34 passphraseSafe = new char[password.length];
35 System.arraycopy(password, 0, passphraseSafe, 0,
36 password.length);
37 // passphraseSafe = Arrays.copyOf(password, password.length);
38 }
39 };
40 dialog.setVisible(true);
41 return dialog.getWasProvided();
42 }
43
44 public boolean promptPassword(String message) {
45 if (password != null)
46 return true;
47
48 if (!alwaysPrompt && passwordSafe != null)
49 return true;
50
51 PasswordDialog dialog = new PasswordDialog(message) {
52 private static final long serialVersionUID = 3266299327166418364L;
53
54 @Override
55 protected void useCredentials(char[] password) {
56 // passwordSafe = Arrays.copyOf(password, password.length);
57 passwordSafe = new char[password.length];
58 System.arraycopy(password, 0, passwordSafe, 0, password.length);
59 }
60 };
61 dialog.setVisible(true);
62 return dialog.getWasProvided();
63 }
64
65 public void setAlwaysPrompt(Boolean alwaysPrompt) {
66 this.alwaysPrompt = alwaysPrompt;
67 }
68
69 protected static class PasswordDialog extends JDialog implements
70 ActionListener {
71 private static final long serialVersionUID = 3399155607980846207L;
72
73 private static final String OK = "ok";
74
75 private JPasswordField password = new JPasswordField("", 10);
76
77 private JButton okButton;
78 private JButton cancelButton;
79
80 private Boolean wasProvided = false;
81
82 public PasswordDialog(String title) {
83 setTitle(title);
84 setModal(true);
85 setLocationRelativeTo(null);
86 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
87
88 JPanel p1 = new JPanel(new GridLayout(1, 2, 3, 3));
89 p1.add(new JLabel("Password"));
90 password.setActionCommand(OK);
91 password.addActionListener(this);
92 p1.add(password);
93 add("Center", p1);
94
95 Panel p2 = new Panel();
96 okButton = addButton(p2, "OK");
97 okButton.setActionCommand(OK);
98 cancelButton = addButton(p2, "Cancel");
99 add("South", p2);
100 setSize(240, 120);
101
102 pack();
103 }
104
105 /** To be overridden */
106 protected void useCredentials(char[] password) {
107 // does nothing
108 }
109
110 private JButton addButton(Container c, String name) {
111 JButton button = new JButton(name);
112 button.addActionListener(this);
113 c.add(button);
114 return button;
115 }
116
117 public final void actionPerformed(ActionEvent evt) {
118 Object source = evt.getSource();
119 if (source == okButton || evt.getActionCommand().equals(OK)) {
120 char[] p = password.getPassword();
121 useCredentials(p);
122 wasProvided = true;
123 Arrays.fill(p, '0');
124 cleanUp();
125 } else if (source == cancelButton)
126 cleanUp();
127 }
128
129 private void cleanUp() {
130 password.setText("");
131 dispose();
132 }
133
134 public Boolean getWasProvided() {
135 return wasProvided;
136 }
137
138 }
139
140 }