]>
git.argeo.org Git - gpl/argeo-slc.git/blob - SwingUserInfo.java
14eb16b6db8231aa8e0f46d8db1358df0c2051a0
1 package org
.argeo
.slc
.jsch
;
3 import java
.awt
.Container
;
4 import java
.awt
.GridLayout
;
6 import java
.awt
.event
.ActionEvent
;
7 import java
.awt
.event
.ActionListener
;
8 import java
.util
.Arrays
;
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
;
17 /** Retrieves a password or a passphrase using standard Swing */
18 public class SwingUserInfo
extends SimpleUserInfo
{
20 private Boolean alwaysPrompt
= false;
22 public boolean promptPassphrase(String message
) {
23 if (passphrase
!= null)
26 if (!alwaysPrompt
&& passphraseSafe
!= null)
29 PasswordDialog dialog
= new PasswordDialog(message
) {
30 private static final long serialVersionUID
= 3266299327166418364L;
33 protected void useCredentials(char[] password
) {
34 passphraseSafe
= new char[password
.length
];
35 System
.arraycopy(password
, 0, passphraseSafe
, 0,
37 // passphraseSafe = Arrays.copyOf(password, password.length);
40 dialog
.setVisible(true);
41 return dialog
.getWasProvided();
44 public boolean promptPassword(String message
) {
48 if (!alwaysPrompt
&& passwordSafe
!= null)
51 PasswordDialog dialog
= new PasswordDialog(message
) {
52 private static final long serialVersionUID
= 3266299327166418364L;
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
);
61 dialog
.setVisible(true);
62 return dialog
.getWasProvided();
65 public void setAlwaysPrompt(Boolean alwaysPrompt
) {
66 this.alwaysPrompt
= alwaysPrompt
;
69 protected static class PasswordDialog
extends JDialog
implements
71 private static final long serialVersionUID
= 3399155607980846207L;
73 private static final String OK
= "ok";
75 private JPasswordField password
= new JPasswordField("", 10);
77 private JButton okButton
;
78 private JButton cancelButton
;
80 private Boolean wasProvided
= false;
82 public PasswordDialog(String title
) {
85 setLocationRelativeTo(null);
86 setDefaultCloseOperation(JFrame
.DISPOSE_ON_CLOSE
);
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);
95 Panel p2
= new Panel();
96 okButton
= addButton(p2
, "OK");
97 okButton
.setActionCommand(OK
);
98 cancelButton
= addButton(p2
, "Cancel");
105 /** To be overridden */
106 protected void useCredentials(char[] password
) {
110 private JButton
addButton(Container c
, String name
) {
111 JButton button
= new JButton(name
);
112 button
.addActionListener(this);
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();
125 } else if (source
== cancelButton
)
129 private void cleanUp() {
130 password
.setText("");
134 public Boolean
getWasProvided() {