]>
git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/SwingUserInfo.java
2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.jsch
;
18 import java
.awt
.Container
;
19 import java
.awt
.GridLayout
;
20 import java
.awt
.Panel
;
21 import java
.awt
.event
.ActionEvent
;
22 import java
.awt
.event
.ActionListener
;
23 import java
.util
.Arrays
;
25 import javax
.swing
.JButton
;
26 import javax
.swing
.JDialog
;
27 import javax
.swing
.JFrame
;
28 import javax
.swing
.JLabel
;
29 import javax
.swing
.JPanel
;
30 import javax
.swing
.JPasswordField
;
33 public class SwingUserInfo
extends SimpleUserInfo
{
35 private Boolean alwaysPrompt
= false;
37 public boolean promptPassphrase(String message
) {
38 if (passphrase
!= null)
41 if (!alwaysPrompt
&& passphraseSafe
!= null)
44 PasswordDialog dialog
= new PasswordDialog(message
) {
45 private static final long serialVersionUID
= 3266299327166418364L;
48 protected void useCredentials(char[] password
) {
49 passphraseSafe
= new char[password
.length
];
50 System
.arraycopy(password
, 0, passphraseSafe
, 0,
52 // passphraseSafe = Arrays.copyOf(password, password.length);
55 dialog
.setVisible(true);
56 return dialog
.getWasProvided();
59 public boolean promptPassword(String message
) {
63 if (!alwaysPrompt
&& passwordSafe
!= null)
66 PasswordDialog dialog
= new PasswordDialog(message
) {
67 private static final long serialVersionUID
= 3266299327166418364L;
70 protected void useCredentials(char[] password
) {
71 // passwordSafe = Arrays.copyOf(password, password.length);
72 passwordSafe
= new char[password
.length
];
73 System
.arraycopy(password
, 0, passwordSafe
, 0, password
.length
);
76 dialog
.setVisible(true);
77 return dialog
.getWasProvided();
80 public void setAlwaysPrompt(Boolean alwaysPrompt
) {
81 this.alwaysPrompt
= alwaysPrompt
;
84 protected static class PasswordDialog
extends JDialog
implements
86 private static final long serialVersionUID
= 3399155607980846207L;
88 private static final String OK
= "ok";
90 private JPasswordField password
= new JPasswordField("", 10);
92 private JButton okButton
;
93 private JButton cancelButton
;
95 private Boolean wasProvided
= false;
97 public PasswordDialog(String title
) {
100 setLocationRelativeTo(null);
101 setDefaultCloseOperation(JFrame
.DISPOSE_ON_CLOSE
);
103 JPanel p1
= new JPanel(new GridLayout(1, 2, 3, 3));
104 p1
.add(new JLabel("Password"));
105 password
.setActionCommand(OK
);
106 password
.addActionListener(this);
110 Panel p2
= new Panel();
111 okButton
= addButton(p2
, "OK");
112 okButton
.setActionCommand(OK
);
113 cancelButton
= addButton(p2
, "Cancel");
120 /** To be overridden */
121 protected void useCredentials(char[] password
) {
125 private JButton
addButton(Container c
, String name
) {
126 JButton button
= new JButton(name
);
127 button
.addActionListener(this);
132 public final void actionPerformed(ActionEvent evt
) {
133 Object source
= evt
.getSource();
134 if (source
== okButton
|| evt
.getActionCommand().equals(OK
)) {
135 char[] p
= password
.getPassword();
140 } else if (source
== cancelButton
)
144 private void cleanUp() {
145 password
.setText("");
149 public Boolean
getWasProvided() {