1 package SnowMailClient.gnupg.Views; 2 3 import javax.swing.*; 4 import javax.swing.border.*; 5 import java.awt.*; 6 import java.awt.event.*; 7 import java.util.*; 8 import snow.utils.gui.*; 9 import SnowMailClient.Language.Language; 10 import SnowMailClient.utils.storage.*; 11 import SnowMailClient.gnupg.Main.GnuPGCommands; 12 import SnowMailClient.gnupg.model.GnuPGKeyID; 13 import SnowMailClient.gnupg.*; 14 import SnowMailClient.SnowMailClientApp; 15 import snow.lookandfeel.*; 16 17 public final class GPGPasswordDialog extends JDialog 18 { 19 final private transient GnuPGLink link; 20 final private transient GnuPGKeyID keyID; 21 22 private final transient JPasswordField passField = new JPasswordField(); 23 private boolean wasCancelled = false; 24 25 public GPGPasswordDialog(JFrame owner, GnuPGLink link, GnuPGKeyID keyID, String description) 26 { 27 super(owner, Language.translate("GPG Password dialog"), true); 28 this.link = link; 29 this.keyID = keyID; 30 31 JTextArea descrTA = new JTextArea(description); 32 descrTA.setOpaque(false); 33 descrTA.setBorder(BorderFactory.createEmptyBorder(3,3,3,3)); 34 descrTA.setEditable(false); 35 36 getContentPane().setLayout( new BorderLayout()); 37 38 JPanel centerPanel = new JPanel(new BorderLayout()); 39 centerPanel.setBorder(BorderFactory.createEmptyBorder(5,3,5,3)); 40 JPanel passPanel = new JPanel(new GridLayout(1, 2)); 41 centerPanel.add(passPanel,BorderLayout.CENTER); 42 getContentPane().add(centerPanel, BorderLayout.CENTER); 43 getContentPane().add(descrTA, BorderLayout.NORTH); 44 45 JPanel contrlpanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); 46 47 getContentPane().add(contrlpanel, BorderLayout.SOUTH); 48 49 ActionListener disposeActionListener = new ActionListener() 50 { 51 public void actionPerformed(ActionEvent e) 52 { 53 wasCancelled = true; 54 dispose(); 55 } 56 }; 57 ActionListener okActionListener = new ActionListener() 58 { 59 public void actionPerformed(ActionEvent e) 60 { 61 okAction(); 62 } 63 }; 64 65 66 JButton cancel = new JButton(Language.translate("Cancel"), Icons.CrossIcon.shared10); 67 contrlpanel.add(cancel); 68 cancel.addActionListener(disposeActionListener); 69 70 JButton close = new JButton(Language.translate(" Ok "), Icons.OkIcon.shared10); 71 contrlpanel.add(close); 72 close.addActionListener(okActionListener); 74 75 passPanel.add(new JLabel("Password ")); 76 passPanel.add(passField); 77 78 passField.addActionListener(okActionListener); 79 80 81 this.getRootPane().registerKeyboardAction( disposeActionListener, 82 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, false), 83 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); 84 85 this.getRootPane().registerKeyboardAction( okActionListener, 86 KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, false), 87 JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT ); 88 89 90 pack(); 93 SnowMailClientApp.centerComponentOnMainFrame(this); 94 passField.requestFocus(); 95 setVisible(true); 96 } 97 98 private void okAction() 99 { 100 if(isPasswordValid()) dispose(); 102 passField.setBackground(ThemesManager.getInstance().getRed()); 103 } 104 105 public boolean isPasswordValid() 106 { 107 try 108 { 109 return GnuPGCommands.verifySecretKeyPassword(link.getPathToGPG(), keyID, getPassword()); 110 } 111 catch(Exception e) 112 { 113 e.printStackTrace(); 114 return false; 115 } 116 } 117 118 public byte[] getPassword() 119 { 120 return new String (passField.getPassword()).getBytes(); 121 } 122 123 public boolean hasBeenCancelled() { return wasCancelled; } 124 125 126 } | Popular Tags |