KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folder > FolderFactory


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16

17 package org.columba.mail.folder;
18
19 import java.util.Enumeration JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25 import org.columba.api.plugin.ExtensionMetadata;
26 import org.columba.api.plugin.IExtension;
27 import org.columba.api.plugin.IExtensionHandler;
28 import org.columba.api.plugin.PluginHandlerNotFoundException;
29 import org.columba.core.plugin.PluginManager;
30 import org.columba.mail.config.AccountItem;
31 import org.columba.mail.config.IFolderItem;
32 import org.columba.mail.config.MailConfig;
33 import org.columba.mail.folder.imap.IMAPFolder;
34 import org.columba.mail.folder.imap.IMAPRootFolder;
35 import org.columba.mail.gui.tree.FolderTreeModel;
36 import org.columba.mail.plugin.IExtensionHandlerKeys;
37
38 /**
39  * Factory for creating subfolders. Implemented as a singelton. Use
40  * {@link #getInstance()}.
41  *
42  * @author Timo Stich <tstich@users.sourceforge.net>
43  */

44 public class FolderFactory {
45     // Groups are separated by at least one WS character
46
private static final Pattern JavaDoc groupPattern = Pattern
47             .compile("([^\\s]+)\\s*");
48
49     private static FolderFactory instance;
50
51     private IExtensionHandler handler;
52
53     // parent directory for mail folders
54
// for example: ".columba/mail/"
55
private String JavaDoc path = MailConfig.getInstance().getConfigDirectory()
56             .getPath();
57
58     protected FolderFactory() throws PluginHandlerNotFoundException {
59         // Get the handler
60
handler = PluginManager.getInstance().getExtensionHandler(
61                 IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FOLDER);
62
63     }
64
65     /**
66      * Singleton - pattern
67      *
68      * @return the instance of the factory
69      */

70     public static FolderFactory getInstance() {
71         if (instance == null) {
72             try {
73                 instance = new FolderFactory();
74             } catch (PluginHandlerNotFoundException phnfe) {
75                 throw new RuntimeException JavaDoc(phnfe);
76             }
77         }
78         return instance;
79     }
80
81     /**
82      * Gets a list of all possible child foldertypes.
83      *
84      * @param parent
85      * @return a list that contains Strings of foldertypes
86      */

87     public List JavaDoc getPossibleChilds(IMailFolder parent) {
88         List JavaDoc list = new LinkedList JavaDoc();
89
90         // which parents are possible ?
91
IFolderItem item = parent.getConfiguration();
92         String JavaDoc parentType = item.get("type");
93
94         // the group of the given parent
95
String JavaDoc parentGroup = getGroup(parentType);
96
97         // iterate through all foldertypes to find suitable ones
98
Enumeration JavaDoc e = handler.getExtensionEnumeration();
99         while (e.hasMoreElements()) {
100             IExtension extension = (IExtension) e.nextElement();
101             ExtensionMetadata metadata = (ExtensionMetadata) extension.getMetadata();
102             String JavaDoc possibleParents = metadata.getAttribute("possible_parents");
103             String JavaDoc id = metadata.getId();
104
105             if (possibleParents != null) {
106                 Matcher JavaDoc matcher = groupPattern.matcher(possibleParents);
107
108                 while (matcher.find()) {
109                     if (matcher.group(1).equals(parentGroup)
110                             || matcher.group(1).equals("all")) {
111                         list.add(id);
112                     }
113                 }
114             }
115         }
116
117         return list;
118     }
119
120     /**
121      * Creates the default child for the given parent.
122      *
123      * @param parent
124      * the parent folder
125      * @return the childfolder
126      * @throws Exception
127      */

128     public IMailFolder createDefaultChild(IMailFolder parent, String JavaDoc name)
129             throws FolderCreationException {
130         List JavaDoc possibleChilds = getPossibleChilds(parent);
131
132         if (possibleChilds.size() > 0) {
133             String JavaDoc childType = (String JavaDoc) possibleChilds.get(0);
134             return createChild(parent, name, childType);
135         } else {
136             return null;
137         }
138     }
139
140     /**
141      * Creates a subfolder for the given folder with the given type.
142      *
143      * @param parent
144      * the parentfolder
145      * @param childType
146      * the type of the child (e.g. CachedMHFolder )
147      * @return the childfolder
148      * @throws Exception
149      */

150     public IMailFolder createChild(IMailFolder parent, String JavaDoc name,
151             String JavaDoc childType) throws FolderCreationException {
152         IMailFolder child;
153         try {
154             IExtension extension = handler.getExtension(childType);
155
156             child = (IMailFolder) extension.instanciateExtension(new Object JavaDoc[] {
157                     name, childType, path });
158
159             // Add child to parent
160
parent.addSubfolder(child);
161         } catch (Exception JavaDoc e) {
162             throw new FolderCreationException(e);
163         }
164         return child;
165     }
166
167     public IMailFolder createVirtualFolder(IMailFolder parent, String JavaDoc name) throws FolderCreationException {
168         IMailFolder child;
169         try {
170             IExtension extension = handler.getExtension("VirtualFolder");
171
172             child = (IMailFolder) extension.instanciateExtension(new Object JavaDoc[] {name});
173
174             // Add child to parent
175
parent.addSubfolder(child);
176         } catch (Exception JavaDoc e) {
177             throw new FolderCreationException(e);
178         }
179         return child;
180     }
181
182     public IMAPFolder createIMAPRootFolder(AccountItem account) throws FolderCreationException {
183
184         try {
185             IExtension extension = handler.getExtension("IMAPRootFolder");
186
187             IMAPRootFolder child = (IMAPRootFolder) extension.instanciateExtension(new Object JavaDoc[] {account, path});
188
189             // get root folder
190
IMailFolder parent = (IMailFolder) FolderTreeModel.getInstance().getRoot();
191
192             // Add child to parent
193
parent.addSubfolder(child);
194
195             // create mandatory IMAP Inbox folder
196
IExtension extension2 = handler.getExtension("IMAPFolder");
197             IMAPFolder inbox = (IMAPFolder) extension2.instanciateExtension(new Object JavaDoc[] {
198                     "INBOX", "IMAPFolder", path });
199
200             // associate inbox with root folder
201
child.setInbox(inbox);
202
203             // notify folder tree model
204
FolderTreeModel.getInstance().nodeStructureChanged(parent);
205
206             return inbox;
207         } catch (Exception JavaDoc e) {
208             throw new FolderCreationException(e);
209         }
210     }
211
212     public String JavaDoc getGroup(String JavaDoc parentType) {
213         // iterate through all foldertypes to find suitable ones
214
Enumeration JavaDoc e = handler.getExtensionEnumeration();
215         while (e.hasMoreElements()) {
216             IExtension extension = (IExtension) e.nextElement();
217             ExtensionMetadata metadata = (ExtensionMetadata) extension.getMetadata();
218             String JavaDoc id = metadata.getId();
219             String JavaDoc group = metadata.getAttribute("group");
220
221             if (id.equals(parentType))
222                 return group;
223         }
224
225         return null;
226
227     }
228 }
229
Popular Tags