1 package SnowMailClient.gnupg.Views; 2 3 import SnowMailClient.gnupg.*; 4 import SnowMailClient.gnupg.Main.GnuPGCommands; 5 import SnowMailClient.Language.Language; 6 import SnowMailClient.SnowMailClientApp; 7 import snow.sortabletable.*; 8 import snow.utils.gui.*; 9 10 import SnowMailClient.model.accounts.*; 11 12 import SnowMailClient.gnupg.model.*; 13 14 import java.awt.*; 15 import java.awt.event.*; 16 import javax.swing.*; 17 import javax.swing.event.*; 18 import javax.swing.border.*; 19 import java.util.*; 20 21 22 public final class GetKeyFromServerDialog extends JDialog 23 { 24 private final KeysTableModel tableModel; 25 26 private final JTextField searchTF = new JTextField(12); 27 private final JButton searchButton = new JButton(Language.translate("Search")); 28 29 31 private final KeySearchResultTableModel previewTableModel = new KeySearchResultTableModel(); 32 private final SortableTableModel stm; 33 private final JTable previewTable = new JTable(); 34 35 public GetKeyFromServerDialog(JDialog parent, KeysTableModel tableModel) 36 { 37 super(parent, Language.translate("Search/retrieve public keys from keyserver"), true); 38 this.tableModel = tableModel; 39 getContentPane().setLayout(new BorderLayout()); 40 41 JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); 43 searchPanel.setBorder(new EmptyBorder(3,4,3,4)); 44 getContentPane().add(searchPanel, BorderLayout.NORTH); 45 searchPanel.add(new JLabel(Language.translate("Search")+":")); 46 searchPanel.add(searchTF); 47 searchPanel.add(searchButton); 48 49 searchTF.addActionListener(new ActionListener() 50 { 51 public void actionPerformed(ActionEvent ae) 52 { 53 searchKeys(searchTF.getText()); 54 } 55 }); 56 57 searchButton.addActionListener(new ActionListener() 58 { 59 public void actionPerformed(ActionEvent ae) 60 { 61 searchKeys(searchTF.getText()); 62 } 63 }); 64 65 stm = new SortableTableModel(previewTableModel); 67 previewTable.setModel(stm); 68 stm.installGUI(previewTable); 69 getContentPane().add(new JScrollPane(previewTable), BorderLayout.CENTER); 70 71 72 final CloseControlPanel ccp = new CloseControlPanel(this, true, true, Language.translate("Import selected keys")); 74 getContentPane().add(ccp, BorderLayout.SOUTH); 75 76 previewTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() 77 { 78 public void valueChanged(ListSelectionEvent lse) 79 { 80 ccp.getOkButton().setEnabled(previewTable.getSelectedRows().length>0); 81 } 82 }); 83 84 ccp.getOkButton().setEnabled(false); 85 86 pack(); 88 SnowMailClientApp.centerComponentOnMainFrame(this); 89 setVisible(true); 90 91 if(!ccp.getWasAccepted()) return; 92 93 importSelectedKeys(); 95 } 96 97 98 private void searchKeys(final String text) 99 { 100 final ProgressModalDialog progress = new ProgressModalDialog(this, Language.translate("Sending request to the keyserver"), false); 101 SnowMailClientApp.centerComponentOnMainFrame(progress); 102 103 Thread t = new Thread () 104 { 105 public void run() 106 { 107 try 108 { 109 final Vector<KeyIDFromSearchResult> keys = GnuPGCommands.searchKeysOnServer( 110 SnowMailClientApp.getInstance().getGnuPGLink().getPathToGPG(), 111 text, 112 progress); 113 114 EventQueue.invokeLater(new Runnable () { public void run() { 115 previewTableModel.setKeys(keys); 116 117 }}); 118 } 119 catch(Exception ex) 120 { 121 JOptionPane.showMessageDialog(progress, 122 Language.translate("Error:")+"\n"+ex.getMessage(), 123 Language.translate("Cannot search keys"), JOptionPane.ERROR_MESSAGE); 124 ex.printStackTrace(); 125 } 126 finally 127 { 128 progress.closeDialog(); 129 } 130 } 131 }; 132 133 t.start(); 134 t.setPriority(Thread.NORM_PRIORITY-1); 135 progress.start(); 137 138 } 139 140 private void importSelectedKeys() 141 { 142 for(int sel : previewTable.getSelectedRows()) 143 { 144 int pos = stm.getIndexInUnsortedFromTablePos(sel); 145 KeyIDFromSearchResult key = this.previewTableModel.getKeyAt(pos); 146 try 147 { 148 GnuPGCommands.importKeyFromServer( 149 SnowMailClientApp.getInstance().getGnuPGLink().getPathToGPG(), 150 key, 151 null); 152 153 tableModel.refreshModel(SnowMailClientApp.getInstance().getGnuPGLink()); 154 155 } 156 catch(Exception e) 157 { 158 e.printStackTrace(); 159 } 160 } 161 } 162 163 164 } | Popular Tags |