2 * Copyright (C) 2007-2012 Argeo GmbH
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
.security
.activemq
;
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
;
31 import javax
.swing
.JTextField
;
34 * Small Swing-based UI to pass user/name and password. TODO better integrate
35 * with JAAS callbacks.
37 public class UserPasswordDialog
extends JDialog
implements ActionListener
{
38 private static final long serialVersionUID
= -9052993072210981198L;
39 private static String OK
= "ok";
41 private JTextField username
= new JTextField("", 10);
42 private JPasswordField password
= new JPasswordField("", 10);
44 private JButton okButton
;
45 private JButton cancelButton
;
47 public UserPasswordDialog() {
48 setTitle("Credentials");
50 setLocationRelativeTo(null);
51 setDefaultCloseOperation(JFrame
.DISPOSE_ON_CLOSE
);
53 JPanel p1
= new JPanel(new GridLayout(2, 2, 3, 3));
54 p1
.add(new JLabel("User"));
56 p1
.add(new JLabel("Password"));
57 password
.setActionCommand(OK
);
58 password
.addActionListener(this);
62 Panel p2
= new Panel();
63 okButton
= addButton(p2
, "OK");
64 okButton
.setActionCommand(OK
);
65 cancelButton
= addButton(p2
, "Cancel");
72 /** To be overridden */
73 protected void useCredentials(String username
, char[] password
) {
77 private JButton
addButton(Container c
, String name
) {
78 JButton button
= new JButton(name
);
79 button
.addActionListener(this);
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
);
91 } else if (source
== cancelButton
)
95 private void cleanUp() {
100 public static void main(String
[] args
) {
101 UserPasswordDialog dialog
= new UserPasswordDialog() {
102 private static final long serialVersionUID
= -891646559691412088L;
104 protected void useCredentials(String username
, char[] password
) {
105 System
.out
.println(username
+ "/" + new String(password
));
108 dialog
.setVisible(true);
109 System
.out
.println("After show");