]> git.argeo.org Git - lgpl/argeo-commons.git/blob - UserPasswordDialog.java
67250766242eee2a2ae54a1ed103ff76546c086b
[lgpl/argeo-commons.git] / UserPasswordDialog.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.security.activemq;
18
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;
25
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;
33
34 public class UserPasswordDialog extends JDialog implements ActionListener {
35 private static final long serialVersionUID = -9052993072210981198L;
36 private static String OK = "ok";
37
38 private JTextField username = new JTextField("", 10);
39 private JPasswordField password = new JPasswordField("", 10);
40
41 private JButton okButton;
42 private JButton cancelButton;
43
44 public UserPasswordDialog() {
45 setTitle("Credentials");
46 setModal(true);
47 setLocationRelativeTo(null);
48 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
49
50 JPanel p1 = new JPanel(new GridLayout(2, 2, 3, 3));
51 p1.add(new JLabel("User"));
52 p1.add(username);
53 p1.add(new JLabel("Password"));
54 password.setActionCommand(OK);
55 password.addActionListener(this);
56 p1.add(password);
57 add("Center", p1);
58
59 Panel p2 = new Panel();
60 okButton = addButton(p2, "OK");
61 okButton.setActionCommand(OK);
62 cancelButton = addButton(p2, "Cancel");
63 add("South", p2);
64 setSize(240, 120);
65
66 pack();
67 }
68
69 /** To be overridden */
70 protected void useCredentials(String username, char[] password) {
71 // does nothing
72 }
73
74 private JButton addButton(Container c, String name) {
75 JButton button = new JButton(name);
76 button.addActionListener(this);
77 c.add(button);
78 return button;
79 }
80
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);
86 Arrays.fill(p, '0');
87 cleanUp();
88 } else if (source == cancelButton)
89 cleanUp();
90 }
91
92 private void cleanUp() {
93 password.setText("");
94 dispose();
95 }
96
97 public static void main(String[] args) {
98 UserPasswordDialog dialog = new UserPasswordDialog() {
99 private static final long serialVersionUID = -891646559691412088L;
100
101 protected void useCredentials(String username, char[] password) {
102 System.out.println(username + "/" + new String(password));
103 }
104 };
105 dialog.setVisible(true);
106 System.out.println("After show");
107 }
108 }