View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2009  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
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   * Get smartcard password
30   */
31  public class PasswordPromptDialog extends JDialog {
32      final JPasswordField passwordField = new JPasswordField(20);
33      protected char[] password;
34  
35      /**
36       * Get user password.
37       *
38       * @return user password as char array
39       */
40      public char[] getPassword() {
41          if (password != null) {
42              return password.clone();
43          } else {
44              return "".toCharArray();
45          }
46      }
47  
48      /**
49       * Get smartcard password.
50       *
51       * @param prompt password prompt from PKCS11 module
52       */
53      public PasswordPromptDialog(String prompt) {
54          this(prompt, null);
55      }
56  
57      /**
58       * Get smartcard password.
59       *
60       * @param prompt       password prompt from PKCS11 module
61       * @param captchaImage ISA filter pinsafe image
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         // center frame
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 }