KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > FolderTreeModel


1 /*
2  * FolderTreeModel.java
3  *
4  * Copyright (C) 2002-2003 Peter Graves
5  * $Id: FolderTreeModel.java,v 1.3 2003/06/29 00:19:34 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.util.Enumeration JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
27 import javax.swing.tree.DefaultTreeModel JavaDoc;
28 import javax.swing.tree.TreeNode JavaDoc;
29 import org.armedbear.j.Directories;
30 import org.armedbear.j.Editor;
31 import org.armedbear.j.File;
32 import org.armedbear.j.Property;
33
34 public final class FolderTreeModel extends DefaultTreeModel JavaDoc
35 {
36     private static FolderTreeModel model;
37
38     private FolderTreeModel(TreeNode JavaDoc root)
39     {
40         super(root);
41     }
42
43     public static synchronized FolderTreeModel getDefaultModel()
44     {
45         if (model == null) {
46             // Root will not be visible.
47
DefaultMutableTreeNode JavaDoc root = new DefaultMutableTreeNode JavaDoc();
48             File local =
49                 File.getInstance(Directories.getMailDirectory(), "local");
50             if (local.isDirectory()) {
51                 DefaultMutableTreeNode JavaDoc localFolders =
52                     new DefaultMutableTreeNode JavaDoc("Local Folders");
53                 root.add(localFolders);
54                 // Add the drafts folder.
55
Folder drafts = new Folder("drafts",
56                     new LocalMailboxURL(Directories.getDraftsFolder()));
57                 localFolders.add(new DefaultMutableTreeNode JavaDoc(drafts));
58                 // Enumerate directories under ~/.j/mail/local.
59
String JavaDoc[] list = local.list();
60                 for (int i = 0; i < list.length; i++) {
61                     final String JavaDoc name = list[i];
62                     if (name.equals("drafts"))
63                         continue;
64                     File file = File.getInstance(local, name);
65                     if (file.isDirectory()) {
66                         File mailboxFile = File.getInstance(file, "mbox");
67                         final LocalMailboxURL url =
68                             new LocalMailboxURL(mailboxFile);
69                         Folder folder = new Folder(file.getName(), url);
70                         localFolders.add(new DefaultMutableTreeNode JavaDoc(folder));
71                     }
72                 }
73             }
74             model = new FolderTreeModel(root);
75             String JavaDoc inbox =
76                 Editor.preferences().getStringProperty(Property.INBOX);
77             if (inbox != null) {
78                 MailboxURL url = MailboxURL.parse(inbox);
79                 if (url != null)
80                     model.addNodeForFolder(url);
81             }
82         }
83         return model;
84     }
85
86     public void maybeAddNodeForFolder(MailboxURL url)
87     {
88         if (findNodeForFolder(url) == null)
89             addNodeForFolder(url);
90     }
91
92     private DefaultMutableTreeNode JavaDoc findNodeForFolder(MailboxURL url)
93     {
94         Enumeration JavaDoc nodes =
95             ((DefaultMutableTreeNode JavaDoc) root).depthFirstEnumeration();
96         while (nodes.hasMoreElements()) {
97             DefaultMutableTreeNode JavaDoc node =
98                 (DefaultMutableTreeNode JavaDoc) nodes.nextElement();
99             Object JavaDoc obj = node.getUserObject();
100             if (obj instanceof Folder && ((Folder) obj).getUrl().equals(url))
101                 return node;
102         }
103         return null;
104     }
105
106     private void addNodeForFolder(MailboxURL url)
107     {
108         if (url instanceof ImapURL) {
109             Enumeration JavaDoc nodes = root.children();
110             DefaultMutableTreeNode JavaDoc parent = null;
111             while (nodes.hasMoreElements()) {
112                 DefaultMutableTreeNode JavaDoc node =
113                     (DefaultMutableTreeNode JavaDoc) nodes.nextElement();
114                 Object JavaDoc obj = node.getUserObject();
115                 if (obj instanceof String JavaDoc) {
116                     String JavaDoc s = (String JavaDoc) obj;
117                     if (s.equals(url.getHost())) {
118                         parent = node;
119                         break;
120                     }
121                 }
122             }
123             if (parent == null) {
124                 parent = new DefaultMutableTreeNode JavaDoc(url.getHost());
125                 ((DefaultMutableTreeNode JavaDoc) root).add(parent);
126             }
127             List JavaDoc list = ((ImapURL)url).getFolderPathComponents();
128             for (int i = 0; i < list.size()-1; i++) {
129                 boolean add = true;
130                 nodes = parent.children();
131                 while (nodes.hasMoreElements()) {
132                     DefaultMutableTreeNode JavaDoc node =
133                         (DefaultMutableTreeNode JavaDoc) nodes.nextElement();
134                     Object JavaDoc obj = node.getUserObject();
135                     if (obj instanceof String JavaDoc) {
136                         String JavaDoc s = (String JavaDoc) obj;
137                         if (s.equals(list.get(i))) {
138                             parent = node;
139                             add = false;
140                             break;
141                         }
142                     }
143                 }
144                 if (add) {
145                     DefaultMutableTreeNode JavaDoc node =
146                         new DefaultMutableTreeNode JavaDoc(list.get(i));
147                     parent.add(node);
148                     parent = node;
149                 }
150             }
151             // Last component.
152
parent.add(new DefaultMutableTreeNode JavaDoc(new Folder((String JavaDoc)list.get(list.size()-1), url)));
153         } else
154             ((DefaultMutableTreeNode JavaDoc)root).add(new DefaultMutableTreeNode JavaDoc(new Folder(url.toString(), url)));
155         reload();
156     }
157 }
158
Popular Tags