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