KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > folderoptions > FolderOptionsController


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
package org.columba.mail.folderoptions;
17
18 import java.util.Enumeration JavaDoc;
19
20 import org.columba.api.plugin.IExtension;
21 import org.columba.api.plugin.IExtensionHandler;
22 import org.columba.api.plugin.PluginException;
23 import org.columba.api.plugin.PluginHandlerNotFoundException;
24 import org.columba.core.plugin.PluginManager;
25 import org.columba.core.xml.XmlElement;
26 import org.columba.mail.config.FolderItem;
27 import org.columba.mail.config.IFolderItem;
28 import org.columba.mail.folder.IMailbox;
29 import org.columba.mail.gui.frame.MailFrameMediator;
30 import org.columba.mail.plugin.IExtensionHandlerKeys;
31
32 /**
33  * Controller used by <code>TableController</code> to handle all folder-related
34  * option plugins.
35  * <p>
36  * Note, that every <code>MailFrameMediator</code> keeps its own
37  * <code>FolderOptionsController<code>, which makes sure that
38  * all plugins are singletons.
39  *
40  * @author fdietz
41  */

42 public class FolderOptionsController implements IFolderOptionsController {
43     /**
44      * Plugins executed before updating the table model
45      * <p>
46      * example:<b> Sorting/Filtering state
47      */

48     public final static int STATE_BEFORE = 0;
49
50     /**
51      * Plugins executed after updating the table model
52      * <p>
53      * example:<br>
54      * Selection of messages
55      */

56     public final static int STATE_AFTER = 1;
57
58     /**
59      * mail frame mediator
60      */

61     private MailFrameMediator mediator;
62
63     /**
64      * plugin handler for instanciating folder options plugins
65      */

66     private IExtensionHandler handler;
67
68     /**
69      * Constructor
70      *
71      * @param mediator
72      * mail frame mediator
73      */

74     public FolderOptionsController(MailFrameMediator mediator) {
75         this.mediator = mediator;
76
77         // init plugin handler
78
try {
79             handler = PluginManager.getInstance().getExtensionHandler(
80                     IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FOLDEROPTIONS);
81         } catch (PluginHandlerNotFoundException e) {
82             // TODO (@author fdietz): show error dialoghere
83
e.printStackTrace();
84         }
85     }
86
87     /**
88      * Get plugin with specific name.
89      *
90      * @param name
91      * name of plugin
92      * @return instance of plugin
93      */

94     public AbstractFolderOptionsPlugin getPlugin(String JavaDoc name) {
95
96         AbstractFolderOptionsPlugin plugin = null;
97
98         try {
99             IExtension extension = handler.getExtension(name);
100
101             plugin = (AbstractFolderOptionsPlugin) extension
102                     .instanciateExtension(new Object JavaDoc[] { mediator });
103         } catch (Exception JavaDoc e) {
104             // TODO (@author fdietz): add error dialog
105
e.printStackTrace();
106         }
107
108         return plugin;
109
110     }
111
112     /**
113      * Load all folder options for this folder.
114      *
115      * @param folder
116      * selected folder
117      */

118     public void load(IMailbox folder, int state) {
119         // get list of plugins
120
Enumeration JavaDoc e = handler.getExtensionEnumeration();
121         while (e.hasMoreElements()) {
122             IExtension extension = (IExtension) e.nextElement();
123             String JavaDoc stateString = extension.getMetadata().getAttribute("state");
124             try {
125                 AbstractFolderOptionsPlugin plugin = (AbstractFolderOptionsPlugin) extension
126                         .instanciateExtension(new Object JavaDoc[] { mediator });
127
128                 if ((state == STATE_BEFORE) && (stateString.equals("before"))) {
129                     plugin.loadOptionsFromXml(null);
130                 } else if ((state == STATE_AFTER)
131                         && (stateString.equals("after"))) {
132                     plugin.loadOptionsFromXml(folder);
133                 }
134             } catch (PluginException e1) {
135                 e1.printStackTrace();
136             }
137
138         }
139     }
140
141     /**
142      * Save all folder options for this folder.
143      *
144      * @param folder
145      * selected folder
146      */

147     public void save(IMailbox folder) {
148         // get list of plugins
149
String JavaDoc[] ids = handler.getPluginIdList();
150
151         for (int i = 0; i < ids.length; i++) {
152             AbstractFolderOptionsPlugin plugin = getPlugin(ids[i]);
153             plugin.saveOptionsToXml(folder);
154         }
155     }
156
157     /**
158      * Load all folder options globally.
159      *
160      */

161     public void load(int state) {
162         // get list of plugins
163
Enumeration JavaDoc e = handler.getExtensionEnumeration();
164         while (e.hasMoreElements()) {
165             IExtension extension = (IExtension) e.nextElement();
166             String JavaDoc stateString = extension.getMetadata().getAttribute("state");
167             try {
168                 AbstractFolderOptionsPlugin plugin = (AbstractFolderOptionsPlugin) extension
169                         .instanciateExtension(new Object JavaDoc[] { mediator });
170
171                 if ((state == STATE_BEFORE) && (stateString.equals("before"))) {
172                     plugin.loadOptionsFromXml(null);
173                 } else if ((state == STATE_AFTER)
174                         && (stateString.equals("after"))) {
175                     plugin.loadOptionsFromXml(null);
176                 }
177             } catch (Exception JavaDoc e1) {
178                 // TODO (@author fdietz): add error dialog
179
e1.printStackTrace();
180             }
181         }
182     }
183
184     /**
185      * Get parent configuration node of plugin.
186      * <p>
187      * Example for the sorting plugin configuration node. This is how it can be
188      * found in options.xml and tree.xml:<br>
189      *
190      * <pre>
191      *
192      *
193      * &lt;sorting column=&quot;Date&quot; order=&quot;true&quot; /&gt;
194      *
195      *
196      * </pre>
197      *
198      * <p>
199      *
200      * @param folder
201      * selected folder
202      * @param name
203      * name of plugin (example: ColumnOptions)
204      * @return parent configuration node
205      */

206     public XmlElement getConfigNode(IMailbox folder, String JavaDoc name) {
207         XmlElement parent = null;
208         boolean global = false;
209
210         if ((folder == null) || (name == null)) {
211             // if no folder was passed as argument, use global options
212
parent = FolderItem.getGlobalOptions();
213             global = true;
214         } else {
215             // use folder specific options
216
parent = folder.getConfiguration().getFolderOptions();
217             global = false;
218         }
219
220         // load plugin
221
AbstractFolderOptionsPlugin plugin = getPlugin(name);
222         XmlElement child = parent.getElement(plugin.getName());
223
224         if (child == null) {
225             // create default configuration
226
child = plugin.createDefaultElement(global);
227             parent.addElement(child);
228         }
229
230         if (global) {
231             return child;
232         }
233
234         String JavaDoc overwrite = child.getAttribute("overwrite");
235
236         // check if this folder is overwriting global options
237
if ((overwrite != null) && (overwrite.equals("true"))) {
238             // use folder-based options
239
return child;
240         } else {
241             // use global options
242
parent = FolderItem.getGlobalOptions();
243             child = parent.getElement(plugin.getName());
244
245             return child;
246         }
247     }
248
249     /**
250      * Create default settings for this folder.
251      *
252      * @param folder
253      * selected folder
254      */

255     public void createDefaultSettings(IMailbox folder) {
256         IFolderItem item = folder.getConfiguration();
257         XmlElement parent = item.getElement("property");
258
259         // use global settings
260
String JavaDoc[] ids = handler.getPluginIdList();
261
262         for (int i = 0; i < ids.length; i++) {
263             AbstractFolderOptionsPlugin plugin = getPlugin(ids[i]);
264             XmlElement child = plugin.createDefaultElement(false);
265             parent.addElement(child);
266         }
267     }
268
269     /**
270      * Save global settings.
271      * <p>
272      * Method is called when shutting down Columba. Note, that when a folder is
273      * selected which overwrites options, only his options are saved.
274      *
275      * @param folder
276      * selected folder
277      */

278     /*
279      * public void saveGlobalSettings(AbstractFolder folder) { if (folder
280      * instanceof AbstractMessageFolder) { if
281      * (isOverwritingDefaults((AbstractMessageFolder) folder)) {
282      *
283      * save((AbstractMessageFolder)folder); } else { save(null); } } }
284      */

285 }
286
Popular Tags