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 ; 13 import java.util.*; 14 15 24 public final class StorageTreeModel extends DefaultTreeModel 25 { 26 private final FolderTreeNode rootNode; 27 28 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 39 public StorageTreeModel(File rootFolder) throws Exception 40 { 41 super(new FolderTreeNode(rootFolder.getAbsolutePath(), null)); 42 rootNode = (FolderTreeNode) getRoot(); 43 45 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 (){ public void run() 58 { 59 try 60 { 61 inboxNode.getMailFolder().addMessage(greetingMessage); 62 inboxNode.saveMailFolder(false); } 64 catch(Exception 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 FolderTreeNode node = rootNode.getChild("Personal Folders"); 128 rootNode.remove(node); 129 rootNode.insert(node, rootNode.getChildCount()); 130 } 131 132 } 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 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 156 { 157 rootNode.analyseAllMailsToTrainSPAMFilter(SnowMailClientApp.getInstance().getWordStatistic(), p, true); 158 } 159 160 162 public void filterAllMailsIntoSPAMCategory(ProgressModalDialog p) throws Exception 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 177 private void storeAllFoldersRecurse(FolderTreeNode folder, boolean close) 178 { 179 try 181 { 182 folder.saveMailFolder(close); 183 } 184 catch(Exception e) 185 { 186 e.printStackTrace(); 187 } 188 189 for(int i=0;i<folder.getChildCount(); i++) 191 { 192 FolderTreeNode child = folder.getFolderChildAt(i); 193 storeAllFoldersRecurse(child, close); 194 } 195 } 196 197 200 public String [] getPathForNode( FolderTreeNode node) 201 { 202 Vector<String > v = new Vector<String >(); 203 getPathForNode(v,node); 204 String [] path = new String [v.size()]; 205 return v.toArray(path); 206 } 207 208 private void getPathForNode(Vector<String > 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 217 public FolderTreeNode getNodeForPath(String [] path) 218 { 219 if(path==null || path.length==0) 220 { 221 return this.rootNode; 222 } 223 224 Vector<String > pathV = new Vector<String >(Arrays.asList(path)); 225 pathV.remove(0); 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 > namesPath) 232 { 233 234 if(namesPath.size()==0) return base; 235 236 String name = namesPath.elementAt(0); 237 namesPath.remove(0); 238 239 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 return null; 251 } 252 253 254 } | Popular Tags |