KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > MultiMessageInfo


1 package net.suberic.pooka;
2 import javax.mail.*;
3 import net.suberic.pooka.filter.FilterAction;
4 import java.util.List JavaDoc;
5 import java.util.LinkedList JavaDoc;
6
7 /**
8  * This represents a bundle of MessageInfos.
9  */

10 public class MultiMessageInfo extends MessageInfo {
11
12   MessageInfo[] messages;
13
14   /**
15    * Creates a new MultiMessageInfo for the given newMessageInfos.
16    */

17   public MultiMessageInfo(MessageInfo[] newMessageInfos) {
18     messages = newMessageInfos;
19   }
20
21   /**
22    * Creates a new MultiMessageInfo for the given newMessageInfos,
23    * where all the given MessageInfos are from the FolderInfo
24    * newFolder.
25    */

26   public MultiMessageInfo(MessageInfo[] newMessageInfos, FolderInfo newFolder) {
27     messages = newMessageInfos;
28     folderInfo = newFolder;
29   }
30
31
32   /**
33    * This implementation just throws an exception, since this is not
34    * allowed on multiple messages.
35    *
36    * @overrides flagIsSet in MessageInfo
37    */

38   public boolean flagIsSet(String JavaDoc flagName) throws MessagingException {
39     throw new MessagingException(Pooka.getProperty("error.MultiMessage.operationNotAllowed", "This operation is not allowed on multiple messages."));
40   }
41
42   /**
43    * This implementation just throws an exception, since this is not
44    * allowed on multiple messages.
45    *
46    * @overrides getFlags() in MessageInfo
47    */

48   public Flags getFlags() throws MessagingException {
49     throw new MessagingException(Pooka.getProperty("error.MultiMessage.operationNotAllowed", "This operation is not allowed on multiple messages."));
50   }
51
52   /**
53    * This implementation just throws an exception, since this is not
54    * allowed on multiple messages.
55    *
56    * @overrides getMessageProperty() in MessageInfo
57    */

58   public Object JavaDoc getMessageProperty(String JavaDoc prop) throws MessagingException {
59     throw new MessagingException(Pooka.getProperty("error.MultiMessage.operationNotAllowed", "This operation is not allowed on multiple messages."));
60   }
61
62   /**
63    * Moves the Message into the target Folder.
64    */

65   public void moveMessage(FolderInfo targetFolder, boolean expunge) throws MessagingException {
66     if (folderInfo != null) {
67       folderInfo.copyMessages(messages, targetFolder);
68       folderInfo.setFlags(messages, new Flags(Flags.Flag.DELETED), true);
69       if (expunge)
70         folderInfo.expunge();
71     } else {
72       for (int i = 0; i < messages.length; i++)
73         messages[i].moveMessage(targetFolder, expunge);
74     }
75   }
76
77   /**
78    * Copies the Message into the target Folder.
79    */

80   public void copyMessage(FolderInfo targetFolder) throws MessagingException {
81     if (folderInfo != null) {
82       folderInfo.copyMessages(messages, targetFolder);
83     } else {
84       for (int i = 0; i < messages.length; i++)
85         messages[i].copyMessage(targetFolder);
86     }
87   }
88
89   /**
90    * deletes all the messages in the MultiMessageInfo.
91    */

92   public void deleteMessage(boolean expunge) throws MessagingException {
93     if (folderInfo != null) {
94       FolderInfo trashFolder = folderInfo.getTrashFolder();
95       if ((folderInfo.useTrashFolder()) && (trashFolder != null) && (trashFolder != folderInfo)) {
96         try {
97           moveMessage(trashFolder, expunge);
98         } catch (MessagingException me) {
99           throw new NoTrashFolderException(Pooka.getProperty("error.Messsage.DeleteNoTrashFolder", "No trash folder available."), me);
100         }
101       } else {
102         remove(expunge);
103       }
104     } else {
105       for (int i = 0; i < messages.length; i++)
106         messages[i].deleteMessage(expunge);
107
108     }
109   }
110
111   /**
112    * This actually marks the message as deleted, and, if autoexpunge is
113    * set to true, expunges the folder.
114    *
115    * This should not be called directly; rather, deleteMessage() should
116    * be used in order to ensure that the delete is done properly (using
117    * trash folders, for instance). If, however, the deleteMessage()
118    * throws an Exception, it may be necessary to follow up with a call
119    * to remove().
120    */

121   public void remove(boolean autoExpunge) throws MessagingException {
122     if (folderInfo != null) {
123       folderInfo.setFlags(messages, new Flags(Flags.Flag.DELETED), true);
124       if (autoExpunge)
125         folderInfo.expunge();
126     } else {
127       for (int i = 0; i < messages.length; i++)
128         messages[i].remove(autoExpunge);
129     }
130
131   }
132
133   /**
134    * Runs folder filters on this MessageInfo.
135    */

136   public void runBackendFilters() {
137     net.suberic.util.swing.ProgressDialog pd = null;
138
139     try {
140
141       if (folderInfo != null) {
142
143         int filterCount = folderInfo.getBackendFilters() == null ? 1 : folderInfo.getBackendFilters().length;
144
145         pd = Pooka.getUIFactory().createProgressDialog(0, messages.length * filterCount, 0, Pooka.getProperty("message.filteringMessages", "Filtering Messages..."), Pooka.getProperty("message.filteringMessages", "Filtering Messages..."));
146         pd.show();
147
148         java.util.List JavaDoc list = new LinkedList JavaDoc();
149         for (int i = 0; i < messages.length; i++) {
150           list.add(messages[i].getMessageProxy());
151         }
152         folderInfo.applyFilters(list, pd);
153       } else {
154         pd = Pooka.getUIFactory().createProgressDialog(0, messages.length, 0, Pooka.getProperty("message.filteringMessages", "Filtering Messages..."), Pooka.getProperty("message.filteringMessages", "Filtering Messages..."));
155         pd.show();
156
157         for (int i = 0; i < messages.length; i++) {
158           java.util.LinkedList JavaDoc list = new java.util.LinkedList JavaDoc();
159           list.add(messages[i]);
160           FolderInfo fi = messages[i].getFolderInfo();
161           fi.applyFilters(list);
162           pd.setValue(messages.length - i -1);
163         }
164       }
165     } finally {
166       pd.dispose();
167     }
168   }
169
170   /**
171    * Runs the configured spam action on this message.
172    */

173   public void runSpamAction() {
174     FilterAction spamFilter = null;
175     try {
176       spamFilter = MessageFilter.generateFilterAction("Pooka.spamAction");
177     } catch (Exception JavaDoc e) {
178       int configureNow = Pooka.getUIFactory().showConfirmDialog("Spam action currently not configured. Would you like to configure it now?", "Configure Spam action", javax.swing.JOptionPane.YES_NO_OPTION);
179       if (configureNow == javax.swing.JOptionPane.YES_OPTION) {
180         // show configure screen.
181
Pooka.getUIFactory().showEditorWindow(Pooka.getProperty("Preferences.Spam.label", "Spam"), "Pooka.spamAction");
182       }
183
184     }
185     if (spamFilter != null) {
186       List JavaDoc l = new LinkedList JavaDoc();
187       for (int i = 0; i < messages.length; i++) {
188         l.add(messages[i].getMessageProxy());
189       }
190       java.util.List JavaDoc removed = spamFilter.performFilter(l);
191       if (removed != null && removed.size() > 0) {
192         try {
193           getFolderInfo().expunge();
194         } catch (MessagingException me) {
195           // throw it away
196
}
197       }
198
199     }
200   }
201
202   /**
203    * Caches the current messages.
204    */

205   public void cacheMessage() throws MessagingException {
206
207     net.suberic.util.swing.ProgressDialog pd = Pooka.getUIFactory().createProgressDialog(0, messages.length, 0, Pooka.getProperty("message.cachingMessages", "Caching Messages..."), Pooka.getProperty("message.cachingMessages", "Caching Messages..."));
208
209     pd.show();
210     try {
211       for (int i = messages.length - 1; i >= 0 && ! pd.isCancelled(); i--) {
212         FolderInfo fi = messages[i].getFolderInfo();
213         if (fi != null && fi instanceof net.suberic.pooka.cache.CachingFolderInfo) {
214           ((net.suberic.pooka.cache.CachingFolderInfo) fi).cacheMessage(messages[i], net.suberic.pooka.cache.MessageCache.MESSAGE);
215
216         }
217         pd.setValue(messages.length - i -1);
218       }
219     } finally {
220       pd.dispose();
221     }
222
223   }
224
225   /**
226    * This returns the MessageInfo at the given index.
227    */

228   public MessageInfo getMessageInfo(int index) {
229     return messages[index];
230   }
231
232   /**
233    * This returns the number of Messages wrapped by the MultiMessageInfo.
234    */

235   public int getMessageCount() {
236     return messages.length;
237   }
238
239 }
240
241
242
243
Popular Tags