2 * Copyright (C) 2007-2012 Mathieu Baudier
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
;
33 public class UserPasswordDialog
extends JDialog
implements ActionListener
{
34 private static final long serialVersionUID
= -9052993072210981198L;
35 private static String OK
= "ok";
37 private JTextField username
= new JTextField("", 10);
38 private JPasswordField password
= new JPasswordField("", 10);
40 private JButton okButton
;
41 private JButton cancelButton
;
43 public UserPasswordDialog() {
44 setTitle("Credentials");
46 setLocationRelativeTo(null);
47 setDefaultCloseOperation(JFrame
.DISPOSE_ON_CLOSE
);
49 JPanel p1
= new JPanel(new GridLayout(2, 2, 3, 3));
50 p1
.add(new JLabel("User"));
52 p1
.add(new JLabel("Password"));
53 password
.setActionCommand(OK
);
54 password
.addActionListener(this);
58 Panel p2
= new Panel();
59 okButton
= addButton(p2
, "OK");
60 okButton
.setActionCommand(OK
);
61 cancelButton
= addButton(p2
, "Cancel");
68 /** To be overridden */
69 protected void useCredentials(String username
, char[] password
) {
73 private JButton
addButton(Container c
, String name
) {
74 JButton button
= new JButton(name
);
75 button
.addActionListener(this);
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
);
87 } else if (source
== cancelButton
)
91 private void cleanUp() {
96 public static void main(String
[] args
) {
97 UserPasswordDialog dialog
= new UserPasswordDialog() {
98 private static final long serialVersionUID
= -891646559691412088L;
100 protected void useCredentials(String username
, char[] password
) {
101 System
.out
.println(username
+ "/" + new String(password
));
104 dialog
.setVisible(true);
105 System
.out
.println("After show");