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.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         // center frame
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 }