KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > gnupg > Views > GetKeyFromServerDialog


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  // private final JTextArea resultTA = new JTextArea(10,50);
30

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     // center
42
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     // center
66
stm = new SortableTableModel(previewTableModel);
67     previewTable.setModel(stm);
68     stm.installGUI(previewTable);
69     getContentPane().add(new JScrollPane(previewTable), BorderLayout.CENTER);
70
71
72     // south
73
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     // modal dialog show
87
pack();
88     SnowMailClientApp.centerComponentOnMainFrame(this);
89     setVisible(true);
90
91     if(!ccp.getWasAccepted()) return;
92
93     // retrieve selected
94
importSelectedKeys();
95   }
96
97
98   private void searchKeys(final String JavaDoc text)
99   {
100      final ProgressModalDialog progress = new ProgressModalDialog(this, Language.translate("Sending request to the keyserver"), false);
101      SnowMailClientApp.centerComponentOnMainFrame(progress);
102
103      Thread JavaDoc t = new Thread JavaDoc()
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 JavaDoc() { public void run() {
115              previewTableModel.setKeys(keys);
116
117            }});
118          }
119          catch(Exception JavaDoc 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      // Wait
136
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 JavaDoc e)
157        {
158           e.printStackTrace();
159        }
160      }
161   }
162
163
164 } // GetKeyFromServerDialog
Popular Tags