1 package org.columba.mail.gui.util; 17 18 import java.awt.BorderLayout ; 19 import java.awt.GridBagConstraints ; 20 import java.awt.GridBagLayout ; 21 import java.awt.GridLayout ; 22 import java.awt.Insets ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.KeyEvent ; 26 import java.text.MessageFormat ; 27 28 import javax.swing.BorderFactory ; 29 import javax.swing.JButton ; 30 import javax.swing.JCheckBox ; 31 import javax.swing.JComponent ; 32 import javax.swing.JDialog ; 33 import javax.swing.JLabel ; 34 import javax.swing.JPanel ; 35 import javax.swing.JPasswordField ; 36 import javax.swing.KeyStroke ; 37 38 import org.columba.core.gui.base.ButtonWithMnemonic; 39 import org.columba.core.gui.frame.FrameManager; 40 import org.columba.core.resourceloader.ImageLoader; 41 import org.columba.mail.util.MailResourceLoader; 42 43 public class PGPPassphraseDialog implements ActionListener { 44 private char[] password; 45 46 private JDialog dialog; 48 49 private boolean bool = false; 50 51 private JPasswordField passwordField; 52 53 private JCheckBox checkbox; 55 56 private boolean save; 57 58 private JButton okButton; 59 60 private JButton cancelButton; 61 62 private JButton helpButton; 63 64 public PGPPassphraseDialog() { 67 } 68 69 protected JPanel createButtonPanel() { 70 JPanel bottom = new JPanel (); 71 bottom.setLayout(new BorderLayout ()); 72 73 bottom.setBorder(BorderFactory.createEmptyBorder(17, 12, 11, 11)); 75 76 cancelButton = new ButtonWithMnemonic(MailResourceLoader.getString( 78 "global", "cancel")); 79 80 cancelButton.addActionListener(this); 82 cancelButton.setActionCommand("CANCEL"); 84 okButton = new ButtonWithMnemonic(MailResourceLoader.getString( 85 "global", "ok")); 86 87 okButton.addActionListener(this); 89 okButton.setActionCommand("OK"); okButton.setDefaultCapable(true); 91 dialog.getRootPane().setDefaultButton(okButton); 92 93 helpButton = new ButtonWithMnemonic(MailResourceLoader.getString( 94 "global", "help")); 95 96 JPanel buttonPanel = new JPanel (); 98 buttonPanel.setLayout(new GridLayout (1, 3, 5, 0)); 99 buttonPanel.add(okButton); 100 buttonPanel.add(cancelButton); 101 buttonPanel.add(helpButton); 102 103 bottom.add(buttonPanel, BorderLayout.EAST); 105 106 return bottom; 107 } 108 109 public void showDialog(String userID, String password, boolean save) { 110 111 JLabel hostLabel = new JLabel (MessageFormat.format(MailResourceLoader 113 .getString("dialog", "password", "enter_passphrase"), 114 new Object [] { userID })); 115 116 passwordField = new JPasswordField (password, 40); 117 118 checkbox = new JCheckBox (MailResourceLoader.getString("dialog", 119 "password", "save_passphrase")); 120 checkbox.setSelected(save); 121 122 dialog = new JDialog (FrameManager.getInstance().getActiveFrame(), true); 123 dialog.setTitle(MailResourceLoader.getString("dialog", "password", 124 "dialog_title_passphrase")); 125 126 JPanel centerPanel = new JPanel (); 127 centerPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 128 129 dialog.getContentPane().add(centerPanel, BorderLayout.CENTER); 130 131 GridBagLayout mainLayout = new GridBagLayout (); 132 centerPanel.setLayout(mainLayout); 133 134 GridBagConstraints mainConstraints = new GridBagConstraints (); 135 136 JLabel iconLabel = new JLabel (ImageLoader 137 .getMiscIcon("signature-nokey.png")); 138 mainConstraints.anchor = GridBagConstraints.NORTHWEST; 139 mainConstraints.weightx = 1.0; 140 mainConstraints.gridwidth = GridBagConstraints.RELATIVE; 141 mainConstraints.fill = GridBagConstraints.HORIZONTAL; 142 mainLayout.setConstraints(iconLabel, mainConstraints); 143 centerPanel.add(iconLabel); 144 145 mainConstraints.gridwidth = GridBagConstraints.REMAINDER; 146 mainConstraints.anchor = GridBagConstraints.WEST; 147 mainConstraints.insets = new Insets (0, 5, 0, 0); 148 mainLayout.setConstraints(hostLabel, mainConstraints); 149 centerPanel.add(hostLabel); 150 151 mainConstraints.insets = new Insets (5, 5, 0, 0); 152 mainLayout.setConstraints(passwordField, mainConstraints); 153 centerPanel.add(passwordField); 154 155 mainConstraints.insets = new Insets (5, 5, 0, 0); 156 mainLayout.setConstraints(checkbox, mainConstraints); 157 centerPanel.add(checkbox); 158 159 JPanel bottomPanel = new JPanel (); 160 bottomPanel.setLayout(new BorderLayout ()); 161 162 JPanel buttonPanel = createButtonPanel(); 163 bottomPanel.add(buttonPanel, BorderLayout.CENTER); 164 165 dialog.getContentPane().add(bottomPanel, BorderLayout.SOUTH); 166 dialog.getRootPane().setDefaultButton(okButton); 167 dialog.getRootPane().registerKeyboardAction(this, "CANCEL", 168 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 169 JComponent.WHEN_IN_FOCUSED_WINDOW); 170 dialog.pack(); 171 dialog.setLocationRelativeTo(null); 172 dialog.setVisible(true); 173 dialog.requestFocus(); 174 passwordField.requestFocus(); 175 } 176 177 public char[] getPassword() { 178 return password; 179 } 180 181 public boolean success() { 182 return bool; 183 } 184 185 public boolean getSave() { 186 return save; 187 } 188 189 public void actionPerformed(ActionEvent e) { 190 String action = e.getActionCommand(); 191 192 if (action.equals("OK")) { 193 password = passwordField.getPassword(); 194 195 save = checkbox.isSelected(); 197 198 bool = true; 200 dialog.dispose(); 201 } else if (action.equals("CANCEL")) { 202 bool = false; 203 dialog.dispose(); 204 } 205 } 206 } 207 | Popular Tags |