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.addActionListener(e -> {
82 password = passwordField.getPassword();
83 setVisible(false);
84 });
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 questionPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
100 passwordPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
101 setModal(true);
102
103 pack();
104
105 setLocation(getToolkit().getScreenSize().width / 2 -
106 getSize().width / 2,
107 getToolkit().getScreenSize().height / 2 -
108 getSize().height / 2);
109 setAlwaysOnTop(true);
110 setVisible(true);
111 }
112
113 protected JPanel getButtonPanel() {
114 JPanel buttonPanel = new JPanel();
115 JButton okButton = new JButton(BundleMessage.format("UI_BUTTON_OK"));
116 JButton cancelButton = new JButton(BundleMessage.format("UI_BUTTON_CANCEL"));
117 okButton.addActionListener(evt -> {
118 password = passwordField.getPassword();
119 setVisible(false);
120 });
121 cancelButton.addActionListener(evt -> {
122 password = null;
123 setVisible(false);
124 });
125
126 buttonPanel.add(okButton);
127 buttonPanel.add(cancelButton);
128 return buttonPanel;
129 }
130
131 }