1 package davmail.ui;
2
3 import davmail.BundleMessage;
4 import davmail.ui.tray.DavGatewayTray;
5
6 import javax.swing.*;
7 import java.awt.*;
8
9
10
11
12 public class CredentialPromptDialog extends JDialog {
13 final JTextField principalField = new JTextField(15);
14 final JPasswordField passwordField = new JPasswordField(15);
15 protected String principal;
16 protected char[] password;
17
18
19
20
21
22
23 public char[] getPassword() {
24 if (password != null) {
25 return password.clone();
26 } else {
27 return "".toCharArray();
28 }
29 }
30
31
32
33
34
35
36 public String getPrincipal() {
37 return principal;
38 }
39
40
41
42
43
44
45 public CredentialPromptDialog(String prompt) {
46 setAlwaysOnTop(true);
47
48 setTitle(BundleMessage.format("UI_KERBEROS_CREDENTIAL_PROMPT"));
49
50 try {
51 setIconImages(DavGatewayTray.getFrameIcons());
52 } catch (NoSuchMethodError error) {
53 DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_SET_ICON_IMAGE"));
54 }
55
56
57 JPanel questionPanel = new JPanel();
58 questionPanel.setLayout(new BoxLayout(questionPanel, BoxLayout.Y_AXIS));
59 JLabel imageLabel = new JLabel();
60 imageLabel.setIcon(UIManager.getIcon("OptionPane.questionIcon"));
61 questionPanel.add(imageLabel);
62
63 passwordField.setMaximumSize(passwordField.getPreferredSize());
64 passwordField.addActionListener(e -> {
65 principal = principalField.getText();
66 password = passwordField.getPassword();
67 setVisible(false);
68 });
69 JPanel credentialPanel = new JPanel(new GridLayout(2, 2));
70
71 JLabel promptLabel = new JLabel(' ' +prompt.trim());
72 promptLabel.setHorizontalAlignment(SwingConstants.RIGHT);
73 promptLabel.setVerticalAlignment(SwingConstants.CENTER);
74
75 credentialPanel.add(promptLabel);
76
77 principalField.setMaximumSize(principalField.getPreferredSize());
78 credentialPanel.add(principalField);
79
80 JLabel passwordLabel = new JLabel(BundleMessage.format("UI_KERBEROS_PASSWORD_PROMPT"));
81 passwordLabel.setHorizontalAlignment(SwingConstants.RIGHT);
82 passwordLabel.setVerticalAlignment(SwingConstants.CENTER);
83 credentialPanel.add(passwordLabel);
84
85 passwordField.setMaximumSize(passwordField.getPreferredSize());
86 credentialPanel.add(passwordField);
87
88 add(questionPanel, BorderLayout.WEST);
89 add(credentialPanel, BorderLayout.CENTER);
90 add(getButtonPanel(), BorderLayout.SOUTH);
91 setModal(true);
92
93 pack();
94
95 setLocation(getToolkit().getScreenSize().width / 2 -
96 getSize().width / 2,
97 getToolkit().getScreenSize().height / 2 -
98 getSize().height / 2);
99 setAlwaysOnTop(true);
100 setVisible(true);
101 }
102
103 protected JPanel getButtonPanel() {
104 JPanel buttonPanel = new JPanel();
105 JButton okButton = new JButton(BundleMessage.format("UI_BUTTON_OK"));
106 JButton cancelButton = new JButton(BundleMessage.format("UI_BUTTON_CANCEL"));
107 okButton.addActionListener(evt -> {
108 principal = principalField.getText();
109 password = passwordField.getPassword();
110 setVisible(false);
111 });
112 cancelButton.addActionListener(evt -> {
113 principal = null;
114 password = null;
115 setVisible(false);
116 });
117
118 buttonPanel.add(okButton);
119 buttonPanel.add(cancelButton);
120 return buttonPanel;
121 }
122
123 }