001: // The contents of this file are subject to the Mozilla Public License Version
002: // 1.1
003: //(the "License"); you may not use this file except in compliance with the
004: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
005: //
006: //Software distributed under the License is distributed on an "AS IS" basis,
007: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: //for the specific language governing rights and
009: //limitations under the License.
010: //
011: //The Original Code is "The Columba Project"
012: //
013: //The Initial Developers of the Original Code are Frederik Dietz and Timo
014: // Stich.
015: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
016: //
017: //All Rights Reserved.
018: package org.columba.addressbook.gui.tree;
019:
020: import java.util.Enumeration;
021:
022: import javax.swing.tree.DefaultTreeModel;
023: import javax.swing.tree.TreeModel;
024:
025: import org.columba.addressbook.config.AddressbookConfig;
026: import org.columba.addressbook.config.FolderItem;
027: import org.columba.addressbook.folder.AddressbookTreeNode;
028: import org.columba.addressbook.folder.IFolder;
029: import org.columba.addressbook.folder.Root;
030: import org.columba.addressbook.gui.tree.util.ISelectFolderDialog;
031: import org.columba.addressbook.gui.tree.util.SelectAddressbookFolderDialog;
032: import org.columba.addressbook.plugin.IExtensionHandlerKeys;
033: import org.columba.api.plugin.IExtension;
034: import org.columba.api.plugin.IExtensionHandler;
035: import org.columba.api.plugin.PluginHandlerNotFoundException;
036: import org.columba.core.config.DefaultXmlConfig;
037: import org.columba.core.gui.dialog.ErrorDialog;
038: import org.columba.core.plugin.PluginManager;
039: import org.columba.core.xml.XmlElement;
040:
041: public class AddressbookTreeModel extends DefaultTreeModel implements
042: TreeModel {
043:
044: protected DefaultXmlConfig folderXmlConfig;
045:
046: private static AddressbookTreeModel instance = new AddressbookTreeModel(
047: AddressbookConfig.getInstance().get("tree").getElement(
048: "/tree"));
049:
050: public AddressbookTreeModel(XmlElement root) {
051: super (new Root(root));
052:
053: createDirectories(((AddressbookTreeNode) getRoot()).getNode(),
054: (AddressbookTreeNode) getRoot());
055: }
056:
057: public static AddressbookTreeModel getInstance() {
058: return instance;
059: }
060:
061: public ISelectFolderDialog getSelectAddressbookFolderDialog() {
062: ISelectFolderDialog dialog = new SelectAddressbookFolderDialog(
063: this );
064:
065: return dialog;
066: }
067:
068: public void createDirectories(XmlElement parentTreeNode,
069: AddressbookTreeNode parentFolder) {
070: int count = parentTreeNode.count();
071:
072: XmlElement child;
073:
074: for (int i = 0; i < count; i++) {
075: child = parentTreeNode.getElement(i);
076:
077: String name = child.getName();
078:
079: if (name.equals("folder")) {
080: AddressbookTreeNode folder = add(child, parentFolder);
081:
082: if (folder != null) {
083: createDirectories(child, folder);
084: }
085: }
086: }
087: }
088:
089: public AddressbookTreeNode add(XmlElement childNode,
090: AddressbookTreeNode parentFolder) {
091: FolderItem item = new FolderItem(childNode);
092:
093: if (item == null) {
094: return null;
095: }
096:
097: // XmlElement.printNode(item.getRoot(), "");
098: int uid = item.getInteger("uid");
099:
100: if (AddressbookTreeNode.getNextFolderUid() <= uid)
101: AddressbookTreeNode.setNextFolderUid(uid + 1);
102:
103: // now instanciate the folder classes
104: String type = item.get("type");
105:
106: Object[] args = { item };
107:
108: IExtensionHandler handler = null;
109:
110: try {
111: handler = PluginManager.getInstance().getExtensionHandler(
112: IExtensionHandlerKeys.ORG_COLUMBA_CONTACT_FOLDER);
113: } catch (PluginHandlerNotFoundException ex) {
114: ErrorDialog.createDialog(ex.getMessage(), ex);
115: }
116:
117: AddressbookTreeNode folder = null;
118:
119: try {
120: IExtension extension = handler.getExtension(type);
121: folder = (AddressbookTreeNode) extension
122: .instanciateExtension(args);
123: parentFolder.add(folder);
124: } catch (Exception ex) {
125: ex.printStackTrace();
126: }
127:
128: return folder;
129:
130: }
131:
132: public AddressbookTreeNode getFolder(String uid) {
133: AddressbookTreeNode root = (AddressbookTreeNode) getRoot();
134:
135: for (Enumeration e = root.breadthFirstEnumeration(); e
136: .hasMoreElements();) {
137: AddressbookTreeNode node = (AddressbookTreeNode) e
138: .nextElement();
139:
140: if (node instanceof Root) {
141: continue;
142: }
143:
144: String id = node.getId();
145:
146: if (uid.equals(id)) {
147: return node;
148: }
149: }
150:
151: return null;
152: }
153:
154: public IFolder getFolderByName(String name) {
155: AddressbookTreeNode root = (AddressbookTreeNode) getRoot();
156:
157: for (Enumeration e = root.breadthFirstEnumeration(); e
158: .hasMoreElements();) {
159: AddressbookTreeNode node = (AddressbookTreeNode) e
160: .nextElement();
161:
162: if (node.getName().equals(name))
163: return node;
164:
165: }
166:
167: return null;
168: }
169:
170: }
|