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