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 Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.folderoptions;
017:
018: import java.util.Enumeration;
019:
020: import org.columba.api.plugin.IExtension;
021: import org.columba.api.plugin.IExtensionHandler;
022: import org.columba.api.plugin.PluginException;
023: import org.columba.api.plugin.PluginHandlerNotFoundException;
024: import org.columba.core.plugin.PluginManager;
025: import org.columba.core.xml.XmlElement;
026: import org.columba.mail.config.FolderItem;
027: import org.columba.mail.config.IFolderItem;
028: import org.columba.mail.folder.IMailbox;
029: import org.columba.mail.gui.frame.MailFrameMediator;
030: import org.columba.mail.plugin.IExtensionHandlerKeys;
031:
032: /**
033: * Controller used by <code>TableController</code> to handle all folder-related
034: * option plugins.
035: * <p>
036: * Note, that every <code>MailFrameMediator</code> keeps its own
037: * <code>FolderOptionsController<code>, which makes sure that
038: * all plugins are singletons.
039: *
040: * @author fdietz
041: */
042: public class FolderOptionsController implements
043: IFolderOptionsController {
044: /**
045: * Plugins executed before updating the table model
046: * <p>
047: * example:<b> Sorting/Filtering state
048: */
049: public final static int STATE_BEFORE = 0;
050:
051: /**
052: * Plugins executed after updating the table model
053: * <p>
054: * example:<br>
055: * Selection of messages
056: */
057: public final static int STATE_AFTER = 1;
058:
059: /**
060: * mail frame mediator
061: */
062: private MailFrameMediator mediator;
063:
064: /**
065: * plugin handler for instanciating folder options plugins
066: */
067: private IExtensionHandler handler;
068:
069: /**
070: * Constructor
071: *
072: * @param mediator
073: * mail frame mediator
074: */
075: public FolderOptionsController(MailFrameMediator mediator) {
076: this .mediator = mediator;
077:
078: // init plugin handler
079: try {
080: handler = PluginManager
081: .getInstance()
082: .getExtensionHandler(
083: IExtensionHandlerKeys.ORG_COLUMBA_MAIL_FOLDEROPTIONS);
084: } catch (PluginHandlerNotFoundException e) {
085: // TODO (@author fdietz): show error dialoghere
086: e.printStackTrace();
087: }
088: }
089:
090: /**
091: * Get plugin with specific name.
092: *
093: * @param name
094: * name of plugin
095: * @return instance of plugin
096: */
097: public AbstractFolderOptionsPlugin getPlugin(String name) {
098:
099: AbstractFolderOptionsPlugin plugin = null;
100:
101: try {
102: IExtension extension = handler.getExtension(name);
103:
104: plugin = (AbstractFolderOptionsPlugin) extension
105: .instanciateExtension(new Object[] { mediator });
106: } catch (Exception e) {
107: // TODO (@author fdietz): add error dialog
108: e.printStackTrace();
109: }
110:
111: return plugin;
112:
113: }
114:
115: /**
116: * Load all folder options for this folder.
117: *
118: * @param folder
119: * selected folder
120: */
121: public void load(IMailbox folder, int state) {
122: // get list of plugins
123: Enumeration e = handler.getExtensionEnumeration();
124: while (e.hasMoreElements()) {
125: IExtension extension = (IExtension) e.nextElement();
126: String stateString = extension.getMetadata().getAttribute(
127: "state");
128: try {
129: AbstractFolderOptionsPlugin plugin = (AbstractFolderOptionsPlugin) extension
130: .instanciateExtension(new Object[] { mediator });
131:
132: if ((state == STATE_BEFORE)
133: && (stateString.equals("before"))) {
134: plugin.loadOptionsFromXml(null);
135: } else if ((state == STATE_AFTER)
136: && (stateString.equals("after"))) {
137: plugin.loadOptionsFromXml(folder);
138: }
139: } catch (PluginException e1) {
140: e1.printStackTrace();
141: }
142:
143: }
144: }
145:
146: /**
147: * Save all folder options for this folder.
148: *
149: * @param folder
150: * selected folder
151: */
152: public void save(IMailbox folder) {
153: // get list of plugins
154: String[] ids = handler.getPluginIdList();
155:
156: for (int i = 0; i < ids.length; i++) {
157: AbstractFolderOptionsPlugin plugin = getPlugin(ids[i]);
158: plugin.saveOptionsToXml(folder);
159: }
160: }
161:
162: /**
163: * Load all folder options globally.
164: *
165: */
166: public void load(int state) {
167: // get list of plugins
168: Enumeration e = handler.getExtensionEnumeration();
169: while (e.hasMoreElements()) {
170: IExtension extension = (IExtension) e.nextElement();
171: String stateString = extension.getMetadata().getAttribute(
172: "state");
173: try {
174: AbstractFolderOptionsPlugin plugin = (AbstractFolderOptionsPlugin) extension
175: .instanciateExtension(new Object[] { mediator });
176:
177: if ((state == STATE_BEFORE)
178: && (stateString.equals("before"))) {
179: plugin.loadOptionsFromXml(null);
180: } else if ((state == STATE_AFTER)
181: && (stateString.equals("after"))) {
182: plugin.loadOptionsFromXml(null);
183: }
184: } catch (Exception e1) {
185: // TODO (@author fdietz): add error dialog
186: e1.printStackTrace();
187: }
188: }
189: }
190:
191: /**
192: * Get parent configuration node of plugin.
193: * <p>
194: * Example for the sorting plugin configuration node. This is how it can be
195: * found in options.xml and tree.xml:<br>
196: *
197: * <pre>
198: *
199: *
200: * <sorting column="Date" order="true" />
201: *
202: *
203: * </pre>
204: *
205: * <p>
206: *
207: * @param folder
208: * selected folder
209: * @param name
210: * name of plugin (example: ColumnOptions)
211: * @return parent configuration node
212: */
213: public XmlElement getConfigNode(IMailbox folder, String name) {
214: XmlElement parent = null;
215: boolean global = false;
216:
217: if ((folder == null) || (name == null)) {
218: // if no folder was passed as argument, use global options
219: parent = FolderItem.getGlobalOptions();
220: global = true;
221: } else {
222: // use folder specific options
223: parent = folder.getConfiguration().getFolderOptions();
224: global = false;
225: }
226:
227: // load plugin
228: AbstractFolderOptionsPlugin plugin = getPlugin(name);
229: XmlElement child = parent.getElement(plugin.getName());
230:
231: if (child == null) {
232: // create default configuration
233: child = plugin.createDefaultElement(global);
234: parent.addElement(child);
235: }
236:
237: if (global) {
238: return child;
239: }
240:
241: String overwrite = child.getAttribute("overwrite");
242:
243: // check if this folder is overwriting global options
244: if ((overwrite != null) && (overwrite.equals("true"))) {
245: // use folder-based options
246: return child;
247: } else {
248: // use global options
249: parent = FolderItem.getGlobalOptions();
250: child = parent.getElement(plugin.getName());
251:
252: return child;
253: }
254: }
255:
256: /**
257: * Create default settings for this folder.
258: *
259: * @param folder
260: * selected folder
261: */
262: public void createDefaultSettings(IMailbox folder) {
263: IFolderItem item = folder.getConfiguration();
264: XmlElement parent = item.getElement("property");
265:
266: // use global settings
267: String[] ids = handler.getPluginIdList();
268:
269: for (int i = 0; i < ids.length; i++) {
270: AbstractFolderOptionsPlugin plugin = getPlugin(ids[i]);
271: XmlElement child = plugin.createDefaultElement(false);
272: parent.addElement(child);
273: }
274: }
275:
276: /**
277: * Save global settings.
278: * <p>
279: * Method is called when shutting down Columba. Note, that when a folder is
280: * selected which overwrites options, only his options are saved.
281: *
282: * @param folder
283: * selected folder
284: */
285: /*
286: * public void saveGlobalSettings(AbstractFolder folder) { if (folder
287: * instanceof AbstractMessageFolder) { if
288: * (isOverwritingDefaults((AbstractMessageFolder) folder)) {
289: *
290: * save((AbstractMessageFolder)folder); } else { save(null); } } }
291: */
292: }
|