]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/runtime/org.argeo.security.activemq/src/main/java/org/argeo/security/activemq/UserPasswordDialog.java
[maven-release-plugin] prepare for next development iteration
[lgpl/argeo-commons.git] / security / runtime / org.argeo.security.activemq / src / main / java / org / argeo / security / activemq / UserPasswordDialog.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
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 package org.argeo.security.activemq;
17
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;
24
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;
31 import javax.swing.JTextField;
32
33 public class UserPasswordDialog extends JDialog implements ActionListener {
34 private static final long serialVersionUID = -9052993072210981198L;
35 private static String OK = "ok";
36
37 private JTextField username = new JTextField("", 10);
38 private JPasswordField password = new JPasswordField("", 10);
39
40 private JButton okButton;
41 private JButton cancelButton;
42
43 public UserPasswordDialog() {
44 setTitle("Credentials");
45 setModal(true);
46 setLocationRelativeTo(null);
47 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
48
49 JPanel p1 = new JPanel(new GridLayout(2, 2, 3, 3));
50 p1.add(new JLabel("User"));
51 p1.add(username);
52 p1.add(new JLabel("Password"));
53 password.setActionCommand(OK);
54 password.addActionListener(this);
55 p1.add(password);
56 add("Center", p1);
57
58 Panel p2 = new Panel();
59 okButton = addButton(p2, "OK");
60 okButton.setActionCommand(OK);
61 cancelButton = addButton(p2, "Cancel");
62 add("South", p2);
63 setSize(240, 120);
64
65 pack();
66 }
67
68 /** To be overridden */
69 protected void useCredentials(String username, char[] password) {
70 // does nothing
71 }
72
73 private JButton addButton(Container c, String name) {
74 JButton button = new JButton(name);
75 button.addActionListener(this);
76 c.add(button);
77 return button;
78 }
79
80 public final void actionPerformed(ActionEvent evt) {
81 Object source = evt.getSource();
82 if (source == okButton || evt.getActionCommand().equals(OK)) {
83 char[] p = password.getPassword();
84 useCredentials(username.getText(), p);
85 Arrays.fill(p, '0');
86 cleanUp();
87 } else if (source == cancelButton)
88 cleanUp();
89 }
90
91 private void cleanUp() {
92 password.setText("");
93 dispose();
94 }
95
96 public static void main(String[] args) {
97 UserPasswordDialog dialog = new UserPasswordDialog() {
98 private static final long serialVersionUID = -891646559691412088L;
99
100 protected void useCredentials(String username, char[] password) {
101 System.out.println(username + "/" + new String(password));
102 }
103 };
104 dialog.setVisible(true);
105 System.out.println("After show");
106 }
107 }