KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > model > folders > StorageTreeModel


1 package SnowMailClient.model.folders;
2
3 import SnowMailClient.model.*;
4 import SnowMailClient.SnowMailClientApp;
5 import snow.utils.gui.*;
6 import SnowMailClient.SpamFilter.*;
7
8 import java.awt.*;
9 import java.awt.event.*;
10 import javax.swing.*;
11 import javax.swing.tree.*;
12 import java.io.File JavaDoc;
13 import java.util.*;
14
15 /** this is the main folder tree model, containing the folders structure
16     it is used as the JTree model.
17     Features:
18       + maintain the 5 basic folders (always open)
19
20     Remarks:
21       + always keep the references on the nodes and DO not cache the mailFolders, because
22         their references may be cleaerd to free memory (weak ref)
23 */

24 public final class StorageTreeModel extends DefaultTreeModel
25 {
26   private final FolderTreeNode rootNode;
27
28   // these folders are always existing and are open, these are default folders
29
//
30
private final FolderTreeNode inboxNode;
31   private final FolderTreeNode sentNode;
32   private final FolderTreeNode outboxNode;
33   private final FolderTreeNode composeNode;
34   private final FolderTreeNode deletedNode;
35
36
37   /** create the model, with the 5 basic folders
38   */

39   public StorageTreeModel(File JavaDoc rootFolder) throws Exception JavaDoc
40   {
41      super(new FolderTreeNode(rootFolder.getAbsolutePath(), null));
42      rootNode = (FolderTreeNode) getRoot();
43      //rootNode.setIsSystemFolder(true);
44

45      // default nodes
46
if(!rootNode.hasChild("Inbox"))
47      {
48         inboxNode = new FolderTreeNode("Inbox", rootNode);
49         final MailMessage greetingMessage = new MailMessage();
50         greetingMessage.parse(
51            "from: SnowMail\r\n"
52           +"to: you, dear user\r\n"
53           +"subject: Hello\r\n\r\nwelcome in Snowmail"
54           +"\n\n");
55         greetingMessage.setEditable(false);
56
57         EventQueue.invokeLater(new Runnable JavaDoc(){ public void run()
58         {
59           try
60           {
61             inboxNode.getMailFolder().addMessage(greetingMessage);
62             inboxNode.saveMailFolder(false); // don't close
63
}
64           catch(Exception JavaDoc e) { e.printStackTrace(); }
65         }});
66      }
67      else
68      {
69         inboxNode = rootNode.getChild("Inbox");
70      }
71      inboxNode.setIsSystemFolder(true);
72
73
74
75      if(!rootNode.hasChild("Sent"))
76      {
77         sentNode = new FolderTreeNode("Sent", rootNode);
78      }
79      else
80      {
81         sentNode = rootNode.getChild("Sent");
82      }
83      sentNode.setIsSystemFolder(true);
84
85
86
87      if(!rootNode.hasChild("Outbox"))
88      {
89         outboxNode = new FolderTreeNode("Outbox", rootNode);
90      }
91      else
92      {
93         outboxNode = rootNode.getChild("Outbox");
94      }
95      outboxNode.setIsSystemFolder(true);
96
97
98
99      if(!rootNode.hasChild("Compose"))
100      {
101         composeNode = new FolderTreeNode("Compose", rootNode);
102      }
103      else
104      {
105         composeNode = rootNode.getChild("Compose");
106      }
107      composeNode.setIsSystemFolder(true);
108
109      if(!rootNode.hasChild("Deleted"))
110      {
111         deletedNode = new FolderTreeNode("Deleted", rootNode);
112      }
113      else
114      {
115         deletedNode = rootNode.getChild("Deleted");
116      }
117      deletedNode.setIsSystemFolder(true);
118
119
120      if(!rootNode.hasChild("Personal Folders"))
121      {
122         new FolderTreeNode("Personal Folders", rootNode);
123      }
124      else
125      {
126         // put it as last child
127
FolderTreeNode node = rootNode.getChild("Personal Folders");
128         rootNode.remove(node);
129         rootNode.insert(node, rootNode.getChildCount());
130      }
131
132   } // Constructor
133

134
135   public FolderTreeNode getInboxFolder() { return inboxNode; }
136   public FolderTreeNode getSentFolder() { return sentNode; }
137   public FolderTreeNode getOutboxFolder() { return outboxNode; }
138   public FolderTreeNode getComposeFolder() { return composeNode; }
139   public FolderTreeNode getDeletedFolder() { return deletedNode; }
140
141
142   /** this stores all opened folders,
143       should be called prior to a backup or when closing
144   */

145   public void storeAllOpenedFolders(boolean close)
146   {
147      storeAllFoldersRecurse(rootNode, close);
148   }
149
150   public int getTotalNumberOfFolders()
151   {
152      return rootNode.getTotalNumberOfFoldersIncludingThis();
153   }
154
155   public void analyseAllMailsToTrainSPAMFilter(ProgressModalDialog p) throws Exception JavaDoc
156   {
157      rootNode.analyseAllMailsToTrainSPAMFilter(SnowMailClientApp.getInstance().getWordStatistic(), p, true);
158   }
159
160   /** Analyse all mails and place words either in SPAM or HAM category.
161   */

162   public void filterAllMailsIntoSPAMCategory(ProgressModalDialog p) throws Exception JavaDoc
163   {
164      WordStatistic stat = SnowMailClientApp.getInstance().getWordStatistic();
165      stat.result_detected_spams = 0;
166      stat.result_false_positives = 0;
167
168      rootNode.calculateSPAMNoteForEachMailRecurse(
169        stat,
170        p,
171        true);
172   }
173
174   /** Stores all the opened folders
175      @param close if true, also close the folder
176   */

177   private void storeAllFoldersRecurse(FolderTreeNode folder, boolean close)
178   {
179     // store this
180
try
181     {
182       folder.saveMailFolder(close);
183     }
184     catch(Exception JavaDoc e)
185     {
186       e.printStackTrace();
187     }
188
189     // and all childs
190
for(int i=0;i<folder.getChildCount(); i++)
191     {
192        FolderTreeNode child = folder.getFolderChildAt(i);
193        storeAllFoldersRecurse(child, close);
194     }
195   }
196
197   // used to store/restore the selection
198
//
199

200   public String JavaDoc[] getPathForNode( FolderTreeNode node)
201   {
202     Vector<String JavaDoc> v = new Vector<String JavaDoc>();
203     getPathForNode(v,node);
204     String JavaDoc[] path = new String JavaDoc[v.size()];
205     return v.toArray(path);
206   }
207
208   private void getPathForNode(Vector<String JavaDoc> path, FolderTreeNode node)
209   {
210     path.insertElementAt(node.getFolderName(),0);
211     FolderTreeNode parent = (FolderTreeNode) node.getParent();
212     if(parent!=null) getPathForNode(path, parent);
213   }
214
215   /** used in FolderView to restore old selected folder at startup
216   */

217   public FolderTreeNode getNodeForPath(String JavaDoc[] path)
218   {
219      if(path==null || path.length==0)
220      {
221        return this.rootNode;
222      }
223
224      Vector<String JavaDoc> pathV = new Vector<String JavaDoc>(Arrays.asList(path));
225      pathV.remove(0); // root
226
FolderTreeNode found = getNodeForPath(rootNode, pathV);
227      if(found==null) return this.rootNode;
228      return found;
229   }
230
231   private FolderTreeNode getNodeForPath(FolderTreeNode base, Vector<String JavaDoc> namesPath)
232   {
233
234      if(namesPath.size()==0) return base;
235
236      String JavaDoc name = namesPath.elementAt(0);
237      namesPath.remove(0);
238
239      //System.out.println("Looking for "+name+" in "+base.getFolderName());
240

241      for(int i=0; i<base.getChildCount(); i++)
242      {
243         FolderTreeNode child = base.getFolderChildAt(i);
244         if(child.getFolderName().equals(name))
245         {
246           return getNodeForPath(child, namesPath);
247         }
248      }
249      // not found
250
return null;
251   }
252
253
254 } // StorageTreeModel
Popular Tags