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