KickJava   Java API By Example, From Geeks To Geeks.

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


1 package SnowMailClient.view.actions;
2
3 import SnowMailClient.model.folders.*;
4 import SnowMailClient.model.accounts.*;
5 import SnowMailClient.model.*;
6 import snow.utils.gui.*;
7
8 import SnowMailClient.MailEngine.*;
9 import SnowMailClient.MailEngine.transfer.TransferFunctions;
10 import SnowMailClient.SnowMailClientApp;
11 import SnowMailClient.view.folders.*;
12 import SnowMailClient.view.FolderView;
13 import SnowMailClient.Language.Language;
14 import SnowMailClient.gnupg.*;
15 import SnowMailClient.gnupg.model.GnuPGKeyID;
16
17 import java.awt.EventQueue JavaDoc;
18 import java.util.*;
19 import java.awt.event.*;
20 import javax.swing.*;
21 import javax.swing.event.*;
22 import javax.swing.tree.*;
23
24 /** send mail action
25 */

26 public class SendAction extends AbstractAction
27 {
28   FolderTreeNode sent, outbox;
29
30   MailFolder sentFolder, outboxFolder;
31   FolderView folderView;
32
33   private SendAction( )
34   {
35      super(Language.translate("Send"));
36
37      this.sent = SnowMailClientApp.getInstance().getFoldersModel().getSentFolder();
38      this.outbox = SnowMailClientApp.getInstance().getFoldersModel().getOutboxFolder();
39      this.folderView = SnowMailClientApp.getInstance().getFolderView();
40
41      putValue(AbstractAction.SMALL_ICON,
42               SnowMailClientApp.loadImageIcon("pics/sendmail2.PNG") );
43
44 /*Dangerous, BETTER: user MUST press the send button
45      putValue(AbstractAction.ACCELERATOR_KEY,
46               KeyStroke.getKeyStroke( KeyEvent.VK_R, KeyEvent.CTRL_MASK ) );*/

47
48      putValue(AbstractAction.SHORT_DESCRIPTION,
49               Language.translate("Send selected mails and outbox content"));
50
51      folderView.getTableSelectionModel().addListSelectionListener(
52        new ListSelectionListener()
53        {
54           public void valueChanged(ListSelectionEvent e)
55           {
56             updateButtonVisibility();
57           }
58        }
59      );
60
61      try
62      {
63        sentFolder = sent.getMailFolder();
64        outboxFolder = outbox.getMailFolder();
65        outboxFolder.addTableModelListener(new TableModelListener()
66        {
67           public void tableChanged(TableModelEvent e)
68           {
69              updateButtonVisibility();
70           }
71        });
72      }
73      catch(Exception JavaDoc ex)
74      {
75        ex.printStackTrace();
76      }
77
78      updateButtonVisibility();
79   } // Constructor
80

81
82   /** action enable if either the outbox has items or messages are selected
83       in another folder
84   */

85   private void updateButtonVisibility()
86   {
87       int n = outboxFolder.getRowCount() + folderView.getTableSelectionCount();
88       this.setEnabled( n>0 );
89   }
90
91   public void actionPerformed(ActionEvent e)
92   {
93      // EDT
94
// 1) put the selected message in the outbox
95
// (but only if we are not already in the outbox)
96
//
97
if(folderView.getMailFolder()!=this.outboxFolder)
98      {
99         MailMessage[] messs = folderView.getSelectedMessages();
100         for(MailMessage m: messs)
101         {
102           try
103           {
104             outboxFolder.addMessage(m); // ok, we're in the EDT !!
105
folderView.removeMail(m);
106             m.selectedInView = false;
107           }
108           catch(Exception JavaDoc ee)
109           {
110             ee.printStackTrace();
111           }
112         }
113      }
114
115      // 2) send all messages present in the outbox
116
//
117
// here we're in the EDT, do send (long task) in a thread
118
final ProgressModalDialog progressDialog = new ProgressModalDialog(
119             SnowMailClientApp.getInstance(), "Sending mail", true, false);
120      progressDialog.start();
121
122      Thread JavaDoc t = new Thread JavaDoc()
123      {
124
125        public void run()
126        {
127          try
128          {
129            sendAction(progressDialog);
130          }
131          finally
132          {
133            progressDialog.closeDialog();
134          }
135
136        }
137      };
138      t.start();
139   }
140
141   /** this long task occurs in a separate thread...
142       method is public because it is called from the send/receive action
143   */

144   public void sendAction(final ProgressModalDialog progressDialog)
145   {
146      if(outboxFolder.getRowCount()==0)
147      {
148         JOptionPane.showMessageDialog(progressDialog,
149           Language.translate("There are no messages to send in the outbox"),
150           "No messages to send", JOptionPane.INFORMATION_MESSAGE);
151         return;
152      }
153
154      progressDialog.setProgressBounds(outboxFolder.getRowCount());
155      EventQueue.invokeLater(new Runnable JavaDoc() { public void run() {
156         progressDialog.setTitle(Language.translate("Sending mail"));
157      }});
158
159      AddressBook addressBook = SnowMailClientApp.getInstance().getAddressBook();
160
161      // be careful, the folder size will decrease as the mails are sent...
162
int count = outboxFolder.getRowCount();
163
164      // collect mails to send
165
List<MailMessage> mailsToSend = new Vector<MailMessage>();
166      mailsToSend.addAll( outboxFolder.getAllMessages() );
167
168      // send
169
int counter = 0;
170      for(MailMessage mess: mailsToSend)
171      {
172         counter++;
173         String JavaDoc toAddress = mess.getToAddressesText();
174
175         progressDialog.setProgressValue(counter, Language.translate("Message")+" "+counter+" / "+count);
176         progressDialog.setCommentLabel(Language.translate("Sending mail to %",toAddress));
177
178         if(progressDialog.wasCancelled()) break;
179
180         try
181         {
182           // add the address to the book
183
List<Address> addresses = Address.parseAddressList(toAddress);
184           for(Address a : addresses)
185           {
186             addressBook.incrementMailsSendedTo( a );
187           }
188         }
189         catch(Exception JavaDoc e)
190         {
191           JOptionPane.showMessageDialog(SnowMailClientApp.getInstance().getContentPane(),
192             ""+e.getMessage(),
193             Language.translate("Cannot send mail"), JOptionPane.ERROR_MESSAGE);
194
195           e.printStackTrace();
196
197         }
198      }
199   }
200
201
202
203
204
205
206 }// SendAction
Popular Tags