View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2011  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 java.awt.*;
26  
27  /**
28   * Let user select a client certificate
29   */
30  public class SelectCertificateDialog extends JDialog {
31      protected final JList<String> aliasListBox;
32      protected final String[] aliases;
33      protected String selectedAlias;
34  
35      /**
36       * Gets user selected alias.
37       *
38       * @return user selected alias
39       */
40      public String getSelectedAlias() {
41          return this.selectedAlias;
42      }
43  
44      /**
45       * Select a client certificate
46       *
47       * @param aliases An array of certificate aliases for the user to pick from
48       */
49      public SelectCertificateDialog(String[] aliases, String[] descriptions) {
50          setAlwaysOnTop(true);
51          this.aliases = aliases;
52  
53          setTitle(BundleMessage.format("UI_CERTIFICATE_ALIAS_PROMPT"));
54          try {
55              setIconImages(DavGatewayTray.getFrameIcons());
56          } catch (NoSuchMethodError error) {
57              DavGatewayTray.debug(new BundleMessage("LOG_UNABLE_TO_SET_ICON_IMAGE"));
58          }
59  
60          JPanel questionPanel = new JPanel();
61          questionPanel.setLayout(new BoxLayout(questionPanel, BoxLayout.Y_AXIS));
62          JLabel imageLabel = new JLabel();
63          imageLabel.setIcon(UIManager.getIcon("OptionPane.questionIcon"));
64          imageLabel.setText(BundleMessage.format("UI_CERTIFICATE_ALIAS_PROMPT"));
65          questionPanel.add(imageLabel);
66  
67          aliasListBox = new JList<>(descriptions);
68          aliasListBox.setMaximumSize(aliasListBox.getPreferredSize());
69  
70          JPanel aliasPanel = new JPanel();
71          aliasPanel.setLayout(new BoxLayout(aliasPanel, BoxLayout.Y_AXIS));
72          aliasPanel.add(aliasListBox);
73  
74          add(questionPanel, BorderLayout.NORTH);
75          add(aliasPanel, BorderLayout.CENTER);
76          add(getButtonPanel(), BorderLayout.SOUTH);
77          setModal(true);
78  
79          pack();
80          // center frame
81          setLocation(getToolkit().getScreenSize().width / 2 -
82                  getSize().width / 2,
83                  getToolkit().getScreenSize().height / 2 -
84                          getSize().height / 2);
85  	setAlwaysOnTop(true);
86          setVisible(true);
87      }
88  
89      protected JPanel getButtonPanel() {
90          JPanel buttonPanel = new JPanel();
91          JButton okButton = new JButton(BundleMessage.format("UI_BUTTON_OK"));
92          JButton cancelButton = new JButton(BundleMessage.format("UI_BUTTON_CANCEL"));
93          okButton.addActionListener(evt -> {
94              selectedAlias = aliases[aliasListBox.getSelectedIndex()];
95              setVisible(false);
96          });
97          cancelButton.addActionListener(evt -> {
98              selectedAlias = null;
99              setVisible(false);
100         });
101 
102         buttonPanel.add(okButton);
103         buttonPanel.add(cancelButton);
104         return buttonPanel;
105     }
106 
107 }