2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.security
.core
;
19 import java
.awt
.Container
;
20 import java
.awt
.GridLayout
;
21 import java
.awt
.Panel
;
22 import java
.awt
.event
.ActionEvent
;
23 import java
.awt
.event
.ActionListener
;
24 import java
.util
.Arrays
;
26 import javax
.swing
.JButton
;
27 import javax
.swing
.JDialog
;
28 import javax
.swing
.JFrame
;
29 import javax
.swing
.JLabel
;
30 import javax
.swing
.JPanel
;
31 import javax
.swing
.JPasswordField
;
32 import javax
.swing
.JTextField
;
34 public class UserPasswordDialog
extends JDialog
implements ActionListener
{
35 private static final long serialVersionUID
= -9052993072210981198L;
36 private static String OK
= "ok";
38 private JTextField username
= new JTextField("", 10);
39 private JPasswordField password
= new JPasswordField("", 10);
41 private JButton okButton
;
42 private JButton cancelButton
;
44 public UserPasswordDialog() {
45 setTitle("Credentials");
47 setLocationRelativeTo(null);
48 setDefaultCloseOperation(JFrame
.DISPOSE_ON_CLOSE
);
50 JPanel p1
= new JPanel(new GridLayout(2, 2, 3, 3));
51 p1
.add(new JLabel("User"));
53 p1
.add(new JLabel("Password"));
54 password
.setActionCommand(OK
);
55 password
.addActionListener(this);
59 Panel p2
= new Panel();
60 okButton
= addButton(p2
, "OK");
61 okButton
.setActionCommand(OK
);
62 cancelButton
= addButton(p2
, "Cancel");
69 /** To be overridden */
70 protected void useCredentials(String username
, char[] password
) {
74 private JButton
addButton(Container c
, String name
) {
75 JButton button
= new JButton(name
);
76 button
.addActionListener(this);
81 public final void actionPerformed(ActionEvent evt
) {
82 Object source
= evt
.getSource();
83 if (source
== okButton
|| evt
.getActionCommand().equals(OK
)) {
84 char[] p
= password
.getPassword();
85 useCredentials(username
.getText(), p
);
88 } else if (source
== cancelButton
)
92 private void cleanUp() {
97 public static void main(String
[] args
) {
98 UserPasswordDialog dialog
= new UserPasswordDialog() {
99 private static final long serialVersionUID
= -891646559691412088L;
101 protected void useCredentials(String username
, char[] password
) {
102 System
.out
.println(username
+ "/" + new String(password
));
105 dialog
.setVisible(true);
106 System
.out
.println("After show");