001: /*
002: * FolderTreeModel.java
003: *
004: * Copyright (C) 2002-2003 Peter Graves
005: * $Id: FolderTreeModel.java,v 1.3 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.util.Enumeration;
025: import java.util.List;
026: import javax.swing.tree.DefaultMutableTreeNode;
027: import javax.swing.tree.DefaultTreeModel;
028: import javax.swing.tree.TreeNode;
029: import org.armedbear.j.Directories;
030: import org.armedbear.j.Editor;
031: import org.armedbear.j.File;
032: import org.armedbear.j.Property;
033:
034: public final class FolderTreeModel extends DefaultTreeModel {
035: private static FolderTreeModel model;
036:
037: private FolderTreeModel(TreeNode root) {
038: super (root);
039: }
040:
041: public static synchronized FolderTreeModel getDefaultModel() {
042: if (model == null) {
043: // Root will not be visible.
044: DefaultMutableTreeNode root = new DefaultMutableTreeNode();
045: File local = File.getInstance(Directories
046: .getMailDirectory(), "local");
047: if (local.isDirectory()) {
048: DefaultMutableTreeNode localFolders = new DefaultMutableTreeNode(
049: "Local Folders");
050: root.add(localFolders);
051: // Add the drafts folder.
052: Folder drafts = new Folder("drafts",
053: new LocalMailboxURL(Directories
054: .getDraftsFolder()));
055: localFolders.add(new DefaultMutableTreeNode(drafts));
056: // Enumerate directories under ~/.j/mail/local.
057: String[] list = local.list();
058: for (int i = 0; i < list.length; i++) {
059: final String name = list[i];
060: if (name.equals("drafts"))
061: continue;
062: File file = File.getInstance(local, name);
063: if (file.isDirectory()) {
064: File mailboxFile = File.getInstance(file,
065: "mbox");
066: final LocalMailboxURL url = new LocalMailboxURL(
067: mailboxFile);
068: Folder folder = new Folder(file.getName(), url);
069: localFolders.add(new DefaultMutableTreeNode(
070: folder));
071: }
072: }
073: }
074: model = new FolderTreeModel(root);
075: String inbox = Editor.preferences().getStringProperty(
076: Property.INBOX);
077: if (inbox != null) {
078: MailboxURL url = MailboxURL.parse(inbox);
079: if (url != null)
080: model.addNodeForFolder(url);
081: }
082: }
083: return model;
084: }
085:
086: public void maybeAddNodeForFolder(MailboxURL url) {
087: if (findNodeForFolder(url) == null)
088: addNodeForFolder(url);
089: }
090:
091: private DefaultMutableTreeNode findNodeForFolder(MailboxURL url) {
092: Enumeration nodes = ((DefaultMutableTreeNode) root)
093: .depthFirstEnumeration();
094: while (nodes.hasMoreElements()) {
095: DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes
096: .nextElement();
097: Object obj = node.getUserObject();
098: if (obj instanceof Folder
099: && ((Folder) obj).getUrl().equals(url))
100: return node;
101: }
102: return null;
103: }
104:
105: private void addNodeForFolder(MailboxURL url) {
106: if (url instanceof ImapURL) {
107: Enumeration nodes = root.children();
108: DefaultMutableTreeNode parent = null;
109: while (nodes.hasMoreElements()) {
110: DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes
111: .nextElement();
112: Object obj = node.getUserObject();
113: if (obj instanceof String) {
114: String s = (String) obj;
115: if (s.equals(url.getHost())) {
116: parent = node;
117: break;
118: }
119: }
120: }
121: if (parent == null) {
122: parent = new DefaultMutableTreeNode(url.getHost());
123: ((DefaultMutableTreeNode) root).add(parent);
124: }
125: List list = ((ImapURL) url).getFolderPathComponents();
126: for (int i = 0; i < list.size() - 1; i++) {
127: boolean add = true;
128: nodes = parent.children();
129: while (nodes.hasMoreElements()) {
130: DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes
131: .nextElement();
132: Object obj = node.getUserObject();
133: if (obj instanceof String) {
134: String s = (String) obj;
135: if (s.equals(list.get(i))) {
136: parent = node;
137: add = false;
138: break;
139: }
140: }
141: }
142: if (add) {
143: DefaultMutableTreeNode node = new DefaultMutableTreeNode(
144: list.get(i));
145: parent.add(node);
146: parent = node;
147: }
148: }
149: // Last component.
150: parent.add(new DefaultMutableTreeNode(new Folder(
151: (String) list.get(list.size() - 1), url)));
152: } else
153: ((DefaultMutableTreeNode) root)
154: .add(new DefaultMutableTreeNode(new Folder(url
155: .toString(), url)));
156: reload();
157: }
158: }
|