KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > actions > TestAccountSettings


1 package SnowMailClient.view.actions;
2
3 import SnowMailClient.MailEngine.*;
4 import SnowMailClient.SnowMailClientApp;
5 import SnowMailClient.model.*;
6 import SnowMailClient.model.accounts.*;
7 import SnowMailClient.view.*;
8 import SnowMailClient.view.accounts.AccountsEditor;
9 import snow.utils.gui.*;
10
11 import SnowMailClient.utils.*;
12 import SnowMailClient.view.dialogs.*;
13 import SnowMailClient.Language.Language;
14 import snow.lookandfeel.*;
15
16 import java.awt.*;
17 import java.awt.event.*;
18 import javax.swing.*;
19 import java.util.Vector JavaDoc;
20 import java.io.*;
21 import java.text.*;
22
23 /**
24 */

25 public class TestAccountSettings extends AbstractAction
26 {
27    MailAccount account;
28    AccountsEditor ref;
29
30    public TestAccountSettings(AccountsEditor ref, MailAccount account)
31    {
32        super(Language.translate("Test account settings"));
33        this.account = account;
34        this.ref= ref;
35
36        putValue(AbstractAction.SMALL_ICON, Icons.OkIcon.shared10);
37        putValue(AbstractAction.ACCELERATOR_KEY,
38                  KeyStroke.getKeyStroke( KeyEvent.VK_T,
39                                          KeyEvent.CTRL_MASK ) );
40    }
41
42    public synchronized void actionPerformed(ActionEvent actionEvent)
43    {
44       ref.saveSelectedMailAccount();
45
46 // TODO: leave edt later, after comp creation !
47
Thread JavaDoc t = new Thread JavaDoc()
48       {
49         public void run()
50         {
51           performTest();
52         }
53       };
54       t.start();
55    }
56
57    private void performTest()
58    {
59       final JDialog dialog = new JDialog( ref,
60          Language.translate("Test settings for %",account.getAddress()), false);
61       dialog.setContentPane(new SnowBackgroundPanel(new BorderLayout()));
62       StyledTextDocument doc = new StyledTextDocument();
63       JTextPane textPane = new JTextPane(doc.doc);
64       JScrollPane jsp = new JScrollPane(textPane);
65       jsp.setOpaque(false);
66       jsp.getViewport().setOpaque(false);
67       textPane.setOpaque(false);
68
69       dialog.getContentPane().add(jsp, BorderLayout.CENTER);
70       final CloseControlPanel ccp = new CloseControlPanel(dialog, true, false, Language.translate("Close"));
71       ccp.setOkEnabled(false); // later
72
ccp.getOkButton().setIcon(new Icons.OkIcon(10,10,true));
73       dialog.getContentPane().add(ccp, BorderLayout.SOUTH);
74
75       dialog.setSize(ThemesManager.getLabelFontSize()*50, ThemesManager.getLabelFontSize()*55);
76       SnowMailClientApp.centerComponentOnMainFrame(dialog);
77       EventQueue.invokeLater(new Runnable JavaDoc() { public void run()
78       {
79         // modal !!
80
dialog.setVisible(true);
81       }});
82
83       SecurePopConnection spop = null;
84       try
85       {
86         doc.appendStyled("1) Connecting to pop server at "+account.getPop()+"\n", "bold");
87         Thread.sleep(500);
88         spop = account.getCheckedPopConnection();
89         doc.append(" OK.");
90         doc.appendComment(spop.getConnectionStatusExplanation()+"\n");
91
92         if(ccp.getWasCancelled())
93         {
94           doc.append("\n Test cancelled.");
95           EventQueue.invokeLater(new Runnable JavaDoc() { public void run()
96           {
97             ccp.setOkEnabled(true);
98           }});
99           return;
100         }
101
102         doc.appendStyled("\n2) Retrieving the number of new messages\n", "bold");
103         Thread.sleep(500);
104         int[] nms = spop.getNumberOfMessages();
105         doc.append("\n OK, you have "+nms[0]+" new messages ("+nms[1]+" bytes)");
106
107         String JavaDoc[] messUIDLS = spop.getMessagesUIDLs();
108         for(int i=0; i<messUIDLS.length; i++)
109         {
110            doc.append("\nMess "+(i+1)+": \t");
111
112            String JavaDoc messHeaderText = spop.getMessageTop(messUIDLS[i], 0);
113            try
114            {
115              Header header = new Header();
116              Header.parseHeader(new NumberedLineReader(messHeaderText), header);
117              HeaderEntry he = header.getEntry("From");
118              if(he!=null)
119              {
120                Address ad = new Address(he.getValue());
121                doc.append(" "+ad.getMailAddress());
122              }
123              else
124              {
125                doc.append(" NO FROM ");
126              }
127
128              he = header.getEntry("Subject");
129              if(he!=null)
130              {
131                doc.append("\t"+he.getValue());
132              }
133              else
134              {
135                doc.append("\t NO Subject ");
136              }
137
138            }
139            catch(Exception JavaDoc e)
140            {
141              doc.append("\tHEADER ERROR "+e.getMessage());
142              e.printStackTrace();
143            }
144         }
145       }
146       catch(Exception JavaDoc e)
147       {
148         //e.printStackTrace();
149
doc.appendError(" Failed: "+e.getMessage());
150       }
151       finally
152       {
153         if(spop!=null)
154         {
155           try {spop.terminateSession();}
156           catch(Exception JavaDoc ee){}
157         }
158       }
159
160       if(ccp.getWasCancelled())
161       {
162         doc.append("\n Test cancelled.");
163           EventQueue.invokeLater(new Runnable JavaDoc() { public void run()
164           {
165             ccp.setOkEnabled(true);
166           }});
167
168         return;
169       }
170
171       if(account.getUseToSendMails())
172       {
173         SecureSmtpConnection ssmtp = null;
174         try
175         {
176           Thread.sleep(500);
177           doc.appendStyled("\n3) Connecting to smtp server at "+account.getSMTP()+"\n", "bold");
178           ssmtp = account.getSMTPConnection();
179           doc.append("\n OK.");
180           doc.appendComment(ssmtp.getConnectionStatusExplanation()+"\n");
181
182           if(ccp.getWasCancelled())
183           {
184             doc.append("\n Test cancelled.");
185             EventQueue.invokeLater(new Runnable JavaDoc() { public void run()
186             {
187               ccp.setOkEnabled(true);
188             }});
189
190             return;
191           }
192
193           doc.appendStyled("\n4) Sending a test mail\n", "bold");
194
195           Address from = new Address(""+account.getName()+" <"+account.getAddress()+">") ;
196
197           MailMessage mess = new MailMessage();
198           Vector JavaDoc<Address> tos =new Vector JavaDoc<Address>();
199           tos.addElement(from);
200           mess.setMessage(
201             from,
202             tos,
203             Language.translate("Test message from SnowMailClient."),
204             Language.translate("This is a test message sent from SnowMailClient to test the account settings.")+"\r\n");
205
206   /* mess.parse("from:"+from+"\nto:"+from
207              +"\nsubject: Test message from SnowMailClient."
208              +"\n\nThis is a test message sent from SnowMailClient to test the account settings.");*/

209
210           mess.setEditable(false);
211           ssmtp.sendMessage(mess, null, null);
212
213           doc.append(" OK.");
214         }
215         catch(Exception JavaDoc e)
216         {
217           //e.printStackTrace();
218
doc.appendError("\n Failed: "+e.getMessage());
219         }
220         finally
221         {
222           if(ssmtp!=null)
223           {
224             try {ssmtp.terminateSession();}
225             catch(Exception JavaDoc ee){}
226           }
227         }
228       }
229
230       EventQueue.invokeLater(new Runnable JavaDoc() { public void run()
231       {
232         ccp.setOkEnabled(true);
233       }});
234     }
235 }
Popular Tags