KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.suberic.pooka;
2
3 import javax.mail.*;
4 import javax.mail.event.*;
5 import javax.swing.event.EventListenerList JavaDoc;
6 import java.util.*;
7 import javax.swing.AbstractAction JavaDoc;
8 import javax.swing.Action JavaDoc;
9 import java.awt.event.ActionEvent JavaDoc;
10 import net.suberic.pooka.gui.*;
11 import net.suberic.pooka.thread.*;
12 import net.suberic.pooka.event.*;
13 import net.suberic.util.ValueChangeListener;
14 import net.suberic.util.thread.ActionThread;
15
16 /**
17  * This class emulates a FolderInfo for things such as Search Results.
18  */

19
20 public class VirtualFolderInfo extends FolderInfo {
21   FolderInfo[] parents;
22   MessageInfo[] originalMessages;
23
24   /**
25    * Creates a new VirtualFolderInfo from the list of messageInfos,
26    * which in turn should belong to the FolderInfos in parents.
27    */

28   public VirtualFolderInfo(MessageInfo[] newMessages, FolderInfo[] newParents) {
29     parents = newParents;
30     originalMessages = newMessages;
31     setStatus(CONNECTED);
32     initializeFolderInfo();
33
34     createFilters();
35
36     resetDefaultActions();
37
38     setNotifyNewMessagesMain(false);
39     setNotifyNewMessagesNode(false);
40   }
41
42   /**
43    * Loads the FolderInfo.
44    */

45   public void loadFolder() {
46     return;
47   }
48
49   /**
50    * Initialized the FolderInfo. Basically adds itself as a listener
51    * to all the parent FolderInfos.
52    */

53   protected void initializeFolderInfo() {
54     for (int i = 0; i < parents.length; i++) {
55       parents[i].addMessageCountListener(this);
56       parents[i].addMessageChangedListener(this);
57     }
58
59     loadAllMessages();
60   }
61
62   /**
63    * Disposes of this VirtualFolderInfo.
64    */

65   public void dispose() {
66     for (int i = 0; i < parents.length; i++) {
67       parents[i].removeMessageCountListener(this);
68       parents[i].removeMessageChangedListener(this);
69     }
70   }
71
72   /**
73    * Loads all the Messages into a new FolderTableModel.
74    */

75   public synchronized void loadAllMessages() {
76     if (folderTableModel == null) {
77       Vector messageProxies = new Vector();
78       createColumnInformation();
79
80       for (int i = 0 ; i < originalMessages.length; i++) {
81         if (Pooka.isDebug())
82           System.out.println("originalMessages[" + i + "] = " + originalMessages[i]);
83         messageProxies.add(originalMessages[i].getMessageProxy());
84         messageToInfoTable.put(originalMessages[i].getMessage(), originalMessages[i]);
85       }
86
87       if (Pooka.isDebug()) {
88         System.out.println("originalMessages.length = " + originalMessages.length + "; messageProxies.size() = " + messageProxies.size() + "; getColumnNames() = " + getColumnNames() + "; getColumnSizes() = " + getColumnSizes());
89         for (int i = 0 ; i < getColumnNames().size() ; i++) {
90           System.out.println("column name " + i + " = " + getColumnNames().get(i));
91         }
92       }
93       FolderTableModel ftm = new FolderTableModel(messageProxies, getColumnNames(), getColumnSizes(), getColumnValues(), getColumnIds());
94       setFolderTableModel(ftm);
95
96       resetMessageCounts();
97     }
98   }
99
100   /**
101    * Gets the row number of the first unread message. Returns 0.
102    */

103   public int getFirstUnreadMessage() {
104     return -1;
105   }
106
107   /**
108    * This sets the given Flag for all the MessageInfos given.
109    */

110   public void setFlags(MessageInfo[] msgs, Flags flag, boolean value) throws MessagingException {
111     // these should only belong to FolderInfos in the parents array.
112
HashMap map = new HashMap();
113     for (int i = 0; i < parents.length; i++) {
114       map.put(parents[i], new Vector());
115     }
116
117     for (int i = 0; i < msgs.length; i++) {
118       Vector v = (Vector) map.get(msgs[i].getFolderInfo());
119       if (v != null)
120         v.add(msgs[i]);
121     }
122
123     for (int i = 0; i < parents.length; i++) {
124       Vector v = (Vector) map.get(parents[i]);
125       if (v.size() > 0) {
126         MessageInfo[] folderMsgs = new MessageInfo[v.size()];
127         for (int j = 0; j < v.size(); j++) {
128           folderMsgs[j] = (MessageInfo) v.elementAt(j);
129         }
130
131         synchronized(parents[i].getFolderThread().getRunLock()) {
132           parents[i].setFlags(folderMsgs, flag, value);
133         }
134       }
135
136     }
137   }
138
139   /**
140    * This copies the given messages to the given FolderInfo.
141    */

142   public void copyMessages(MessageInfo[] msgs, FolderInfo targetFolder) throws MessagingException {
143     // these should only belong to FolderInfos in the parents array.
144
HashMap map = new HashMap();
145     for (int i = 0; i < parents.length; i++) {
146       map.put(parents[i], new Vector());
147     }
148
149     for (int i = 0; i < msgs.length; i++) {
150       Vector v = (Vector) map.get(msgs[i].getFolderInfo());
151       if (v != null)
152         v.add(msgs[i]);
153     }
154
155     for (int i = 0; i < parents.length; i++) {
156       Vector v = (Vector) map.get(parents[i]);
157       if (v.size() > 0) {
158         MessageInfo[] folderMsgs = new MessageInfo[v.size()];
159         for (int j = 0; j < v.size(); j++) {
160           folderMsgs[j] = (MessageInfo) v.elementAt(j);
161         }
162
163         synchronized(parents[i].getFolderThread().getRunLock()) {
164           parents[i].copyMessages(folderMsgs, targetFolder);
165         }
166       }
167
168     }
169   }
170
171   /**
172    * This appends the given message to the given FolderInfo.
173    */

174   public void appendMessages(MessageInfo[] msgs) throws MessagingException {
175     throw new MessagingException (Pooka.getProperty("error.search.appendToSearchFolder", "Cannot append to a Virtual Folder."));
176   }
177
178   /**
179    * This expunges the deleted messages from the Folder.
180    */

181   public void expunge() throws MessagingException {
182     for (int i = 0; i < parents.length; i++)
183       synchronized(parents[i].getFolderThread().getRunLock()) {
184         parents[i].expunge();
185       }
186   }
187
188
189   /**
190    * The parent FolderInfos should be updating the MessageProxies, so
191    * we can just pass this on.
192    */

193   protected void runMessageChanged(MessageChangedEvent mce) {
194     fireMessageChangedEvent(mce);
195     resetMessageCounts();
196   }
197
198   /**
199    * Searches for messages in this folder which match the given
200    * SearchTerm.
201    *
202    */

203   public MessageInfo[] search(javax.mail.search.SearchTerm JavaDoc term)
204     throws MessagingException {
205     Vector matches = new Vector();
206     for (int i = 0; i < getFolderTableModel().getRowCount(); i++) {
207       MessageInfo currentInfo = getFolderTableModel().getMessageProxy(i).getMessageInfo();
208       if (term.match(currentInfo.getMessage())) {
209         matches.add(currentInfo);
210       }
211     }
212     MessageInfo returnValue[] = new MessageInfo[matches.size()];
213     for (int i = 0; i < matches.size(); i++) {
214       returnValue[i] = (MessageInfo) matches.elementAt(i);
215     }
216     return returnValue;
217   }
218
219   /**
220    * This puts up the gui for the Search.
221    */

222   public void showSearchFolder() {
223     Vector allowedValues = new Vector();
224     allowedValues.add(this);
225     Pooka.getUIFactory().showSearchForm(new FolderInfo[] { this }, allowedValues);
226   }
227
228   protected void removeFromListeners(FolderDisplayUI display) {
229     if (display != null) {
230       removeMessageChangedListener(display);
231       removeMessageCountListener(display);
232     }
233   }
234
235   protected void addToListeners(FolderDisplayUI display) {
236     if (display != null) {
237       addMessageChangedListener(display);
238       addMessageCountListener(display);
239     }
240   }
241
242   /**
243    * don't add new messages to this FolderInfo.
244    */

245   protected void runMessagesAdded(MessageCountEvent mce) {
246     return;
247   }
248
249   /**
250    * This forces an update of both the total and unread message counts.
251    */

252   public void resetMessageCounts() {
253     if (Pooka.isDebug()) {
254       System.out.println("running resetMessageCounts.");
255     }
256
257     int tmpUnreadCount = 0;
258     int tmpMessageCount = 0;
259     Enumeration msgs = messageToInfoTable.keys();
260     while (msgs.hasMoreElements()) {
261       MessageInfo mi = (MessageInfo) messageToInfoTable.get(msgs.nextElement());
262       tmpMessageCount++;
263       if (! mi.isSeen())
264         tmpUnreadCount++;
265     }
266     unreadCount = tmpUnreadCount;
267     messageCount = tmpMessageCount;
268   }
269
270   public ActionThread getFolderThread() {
271     return Pooka.getSearchThread();
272   }
273
274   public StoreInfo getParentStore() {
275     return null;
276   }
277
278   public FolderInfo getTrashFolder() {
279     return null;
280   }
281
282   public String JavaDoc getFolderDisplayName() {
283     return Pooka.getProperty("title.searchResults", "Seach Results");
284   }
285
286   public String JavaDoc getFolderID() {
287     return Pooka.getProperty("title.searchResults", "Search results");
288   }
289
290   /**
291    * This sets the given FolderDisplayUI to be the UI for this
292    * FolderInfo.
293    *
294    * It automatically registers that FolderDisplayUI to be a listener
295    * to MessageCount, MessageChanged, and Connection events.
296    *
297    * For a VirtualFolderInfo, if the FDUI is set to null, the FolderInfo
298    * is disposed of.
299    */

300   public void setFolderDisplayUI(FolderDisplayUI newValue) {
301     super.setFolderDisplayUI(newValue);
302     if (newValue == null)
303       this.dispose();
304   }
305 }
306
Popular Tags