001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo
013: // Stich.
014: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
015: //
016: //All Rights Reserved.
017: package org.columba.mail.gui.tree;
018:
019: import java.util.Enumeration;
020: import java.util.Map;
021: import java.util.MissingResourceException;
022: import java.util.WeakHashMap;
023:
024: import javax.swing.tree.DefaultTreeModel;
025:
026: import org.columba.api.plugin.IExtension;
027: import org.columba.api.plugin.IExtensionHandler;
028: import org.columba.api.plugin.PluginHandlerNotFoundException;
029: import org.columba.core.config.DefaultConfigDirectory;
030: import org.columba.core.gui.dialog.ErrorDialog;
031: import org.columba.core.plugin.PluginManager;
032: import org.columba.core.shutdown.ShutdownManager;
033: import org.columba.core.xml.XmlElement;
034: import org.columba.mail.config.FolderItem;
035: import org.columba.mail.config.FolderXmlConfig;
036: import org.columba.mail.config.IFolderItem;
037: import org.columba.mail.config.MailConfig;
038: import org.columba.mail.folder.AbstractMessageFolder;
039: import org.columba.mail.folder.IMailFolder;
040: import org.columba.mail.folder.Root;
041: import org.columba.mail.folder.imap.IMAPRootFolder;
042: import org.columba.mail.folder.temp.TempFolder;
043: import org.columba.mail.gui.tree.util.TreeNodeList;
044: import org.columba.mail.plugin.IExtensionHandlerKeys;
045: import org.columba.mail.util.MailResourceLoader;
046:
047: /**
048: * @author freddy
049: *
050: * To change this generated comment edit the template variable "typecomment":
051: * Window>Preferences>Java>Templates. To enable and disable the creation of type
052: * comments go to Window>Preferences>Java>Code Generation.
053: */
054:
055: public class FolderTreeModel extends DefaultTreeModel {
056: protected FolderXmlConfig folderXmlConfig;
057:
058: protected TempFolder tempFolder;
059:
060: private Map<String, IMailFolder> temporaryFolders = new WeakHashMap<String, IMailFolder>();
061:
062: private static FolderTreeModel instance = new FolderTreeModel(
063: MailConfig.getInstance().getFolderConfig());
064:
065: public FolderTreeModel(FolderXmlConfig folderConfig) {
066: super (new Root(folderConfig.getRoot().getElement("tree")));
067: this .folderXmlConfig = folderConfig;
068:
069: // create temporary folder in "<your-config-folder>/mail/"
070: tempFolder = new TempFolder(DefaultConfigDirectory
071: .getInstance().getCurrentPath()
072: + "/mail/");
073:
074: createDirectories(((IMailFolder) getRoot()).getConfiguration()
075: .getRoot(), (IMailFolder) getRoot());
076:
077: // register at shutdownmanager
078: // -> when closing Columba, this will automatically save all folder data
079: ShutdownManager.getInstance().register(new Runnable() {
080: public void run() {
081: saveFolder((IMailFolder) getRoot());
082: }
083:
084: protected void saveFolder(IMailFolder parentFolder) {
085: IMailFolder child;
086:
087: for (Enumeration e = parentFolder.children(); e
088: .hasMoreElements();) {
089: child = (IMailFolder) e.nextElement();
090:
091: if (child instanceof AbstractMessageFolder) {
092: try {
093: ((AbstractMessageFolder) child).save();
094: } catch (Exception ex) {
095: ex.printStackTrace();
096: }
097: }
098:
099: saveFolder(child);
100: }
101: }
102: });
103: }
104:
105: public static FolderTreeModel getInstance() {
106: return instance;
107: }
108:
109: public void addTemporaryFolder(IMailFolder folder) {
110: temporaryFolders.put(folder.getId(), folder);
111: }
112:
113: public void createDirectories(XmlElement parentTreeNode,
114: IMailFolder parentFolder) {
115: int count = parentTreeNode.count();
116: XmlElement child;
117:
118: if (count > 0) {
119: for (int i = 0; i < count; i++) {
120: child = parentTreeNode.getElement(i);
121:
122: String name = child.getName();
123:
124: if (name.equals("folder")) {
125: IMailFolder folder = add(child, parentFolder);
126:
127: if (folder != null) {
128: createDirectories(child, folder);
129: }
130: }
131: }
132: }
133: }
134:
135: public IMailFolder add(XmlElement childNode,
136: IMailFolder parentFolder) {
137: FolderItem item = new FolderItem(childNode);
138:
139: if (item == null) {
140: return null;
141: }
142:
143: // i18n stuff
144: String name = null;
145:
146: // XmlElement.printNode(item.getRoot(), "");
147: int uid = item.getInteger("uid");
148:
149: try {
150: if (uid == 100) {
151: name = MailResourceLoader.getString("tree",
152: "localfolders");
153: } else if (uid == 101) {
154: name = MailResourceLoader.getString("tree", "inbox");
155: } else if (uid == 102) {
156: name = MailResourceLoader.getString("tree", "drafts");
157: } else if (uid == 103) {
158: name = MailResourceLoader.getString("tree", "outbox");
159: } else if (uid == 104) {
160: name = MailResourceLoader.getString("tree", "sent");
161: } else if (uid == 105) {
162: name = MailResourceLoader.getString("tree", "trash");
163: } else if (uid == 106) {
164: name = MailResourceLoader.getString("tree", "search");
165: } else if (uid == 107) {
166: name = MailResourceLoader
167: .getString("tree", "templates");
168: } else {
169: name = item.getString("property", "name");
170: }
171:
172: item.setString("property", "name", name);
173: } catch (MissingResourceException ex) {
174: name = item.getString("property", "name");
175: }
176:
177: // now instanciate the folder classes
178: String type = item.get("type");
179: IExtensionHandler handler = null;
180:
181: try {
182: handler = PluginManager.getInstance().getExtensionHandler(
183: IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FOLDER);
184: } catch (PluginHandlerNotFoundException ex) {
185: ErrorDialog.createDialog(ex.getMessage(), ex);
186: }
187:
188: // parent directory for mail folders
189: // for example: ".columba/mail/"
190: String path = DefaultConfigDirectory.getInstance()
191: .getCurrentPath()
192: + "/mail/";
193: Object[] args = { item, path };
194: IMailFolder folder = null;
195:
196: try {
197: IExtension extension = handler.getExtension(type);
198:
199: folder = (IMailFolder) extension.instanciateExtension(args);
200: parentFolder.add(folder);
201: } catch (Exception ex) {
202: ex.printStackTrace();
203: }
204:
205: return folder;
206: }
207:
208: public IMailFolder getFolder(String folderId) {
209: IMailFolder root = (IMailFolder) getRoot();
210:
211: for (Enumeration e = root.breadthFirstEnumeration(); e
212: .hasMoreElements();) {
213: IMailFolder node = (IMailFolder) e.nextElement();
214: String id = node.getId();
215:
216: if (folderId.equals(id)) {
217: return node;
218: }
219: }
220:
221: if (temporaryFolders.containsKey(folderId))
222: return temporaryFolders.get(folderId);
223:
224: return null;
225: }
226:
227: public IMailFolder getTrashFolder() {
228: return getFolder("105");
229: }
230:
231: public IMailFolder getImapFolder(int accountUid) {
232: IMailFolder root = (IMailFolder) getRoot();
233:
234: for (Enumeration e = root.breadthFirstEnumeration(); e
235: .hasMoreElements();) {
236: IMailFolder node = (IMailFolder) e.nextElement();
237: IFolderItem item = node.getConfiguration();
238:
239: if (item == null) {
240: continue;
241: }
242:
243: // if (item.get("type").equals("IMAPRootFolder")) {
244: if (node instanceof IMAPRootFolder) {
245: int account = item.getInteger("account_uid");
246:
247: if (account == accountUid) {
248: String uid = item.get("uid");
249:
250: return getFolder(uid);
251: }
252: }
253: }
254:
255: return null;
256: }
257:
258: public IMailFolder getFolder(TreeNodeList list) {
259: IMailFolder parentFolder = (IMailFolder) getRoot();
260:
261: if ((list == null) || (list.count() == 0)) {
262: return parentFolder;
263: }
264:
265: IMailFolder child = parentFolder;
266:
267: for (int i = 0; i < list.count(); i++) {
268: String str = list.get(i);
269: child = findFolder(child, str);
270: }
271:
272: return child;
273: }
274:
275: public IMailFolder findFolder(IMailFolder parentFolder, String str) {
276: IMailFolder child;
277:
278: for (Enumeration e = parentFolder.children(); e
279: .hasMoreElements();) {
280: child = (IMailFolder) e.nextElement();
281:
282: if (child.getName().equals(str)) {
283: return child;
284: }
285: }
286:
287: return null;
288: }
289:
290: public TempFolder getTempFolder() {
291: return tempFolder;
292: }
293: }
|