KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > folder > AddressbookTreeNode


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.addressbook.folder;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Hashtable JavaDoc;
22
23 import javax.swing.ImageIcon JavaDoc;
24 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
25
26 import org.columba.addressbook.config.FolderItem;
27 import org.columba.api.command.IWorkerStatusController;
28 import org.columba.api.plugin.IExtensionInterface;
29 import org.columba.core.base.Lock;
30 import org.columba.core.resourceloader.IconKeys;
31 import org.columba.core.resourceloader.ImageLoader;
32 import org.columba.core.xml.XmlElement;
33
34 public abstract class AddressbookTreeNode extends DefaultMutableTreeNode JavaDoc
35         implements IFolder, IExtensionInterface {
36     protected final static ImageIcon JavaDoc expandedIcon = ImageLoader
37             .getSmallIcon(IconKeys.FOLDER_OPEN);
38
39     private static int nextFolderUid = 0;
40
41     protected ImageIcon JavaDoc collapsedIcon = ImageLoader
42             .getSmallIcon(IconKeys.FOLDER);
43
44     protected FolderItem node;
45
46     protected Lock myLock;
47
48     private final Class JavaDoc[] FOLDER_ITEM_ARG = new Class JavaDoc[] { FolderItem.class };
49
50     public AddressbookTreeNode(String JavaDoc name) {
51         super(name);
52
53     }
54
55     public AddressbookTreeNode(FolderItem item) {
56         super();
57
58         setNode(item);
59
60     }
61
62     public static Object JavaDoc generateNextFolderUid() {
63         return new Integer JavaDoc(++nextFolderUid);
64     }
65
66     /**
67      * @param nextUid
68      * The nextUid to set.
69      */

70     public static void setNextFolderUid(int uid) {
71         nextFolderUid = uid;
72     }
73
74     /**
75      * @return Returns the nextUid.
76      */

77     public static int getNextFolderUid() {
78         return nextFolderUid;
79     }
80
81     /*
82      * public AddressbookTreeNode(String name) { super(name); this.name = name; }
83      */

84     public FolderItem getFolderItem() {
85         return node;
86     }
87
88     public ImageIcon JavaDoc getIcon() {
89         return collapsedIcon;
90     }
91
92     public final static FolderItem getDefaultItem(String JavaDoc className,
93             XmlElement props) {
94         XmlElement defaultElement = new XmlElement("folder");
95         defaultElement.addAttribute("class", className);
96         defaultElement.addAttribute("uid", Integer.toString(nextFolderUid++));
97
98         if (props != null) {
99             defaultElement.addElement(props);
100         }
101
102         // FAILURE!!!
103

104         /*
105          * XmlElement filter = new XmlElement("filter");
106          * defaultElement.addElement(filter);
107          */

108         return new FolderItem(defaultElement);
109     }
110
111     /*
112      * public TreePath getSelectionTreePath() { //TreeNodeList list = new
113      * TreeNodeList( getTreePath() ); TreeNode[] treeNodes = getPathToRoot(this,
114      * 0); TreePath path = new TreePath(treeNodes[0]);
115      *
116      * for (int i = 1; i < treeNodes.length; i++) { Folder folder = (Folder)
117      * treeNodes[i]; path.pathByAddingChild(folder); }
118      *
119      * return path; }
120      */

121
122     public static XmlElement getDefaultProperties() {
123         return null;
124     }
125
126
127     public ImageIcon JavaDoc getExpandedIcon() {
128         return expandedIcon;
129     }
130
131     /*
132      * public void setName(String s) { name = s; } public String getName() {
133      * return name; }
134      */

135     public String JavaDoc getId() {
136         return node.get("uid");
137     }
138
139     public boolean tryToGetLock() {
140         return myLock.tryToGetLock(null);
141     }
142
143     public void releaseLock() {
144         myLock.release(null);
145     }
146
147     /**
148      * Returns the node.
149      *
150      * @return FolderItem
151      */

152     public XmlElement getNode() {
153         return node.getRoot();
154     }
155
156     /**
157      * Sets the node.
158      *
159      * @param node
160      * The node to set
161      */

162     public void setNode(FolderItem node) {
163         this.node = node;
164     }
165
166     // public abstract Class getDefaultChild();
167
public Class JavaDoc getDefaultChild() {
168         return null;
169     }
170
171     final public Hashtable JavaDoc getAttributes() {
172         return node.getElement("property").getAttributes();
173     }
174
175     public abstract void createChildren(IWorkerStatusController worker);
176
177     public void addFolder(String JavaDoc name, Class JavaDoc childClass) throws Exception JavaDoc {
178         Method JavaDoc m_getDefaultProperties = childClass.getMethod(
179                 "getDefaultProperties", null);
180
181         XmlElement props = (XmlElement) m_getDefaultProperties.invoke(null,
182                 null);
183         FolderItem childNode = getDefaultItem(childClass.getName(), props);
184
185         childNode.setString("property", "name", name);
186
187         // Put properties that should be copied from parent here
188
AbstractFolder subFolder = (AbstractFolder) childClass.getConstructor(
189                 FOLDER_ITEM_ARG).newInstance(new Object JavaDoc[] { childNode });
190         addWithXml(subFolder);
191     }
192
193     public void addFolder(String JavaDoc name) throws Exception JavaDoc {
194         addFolder(name, getDefaultChild());
195     }
196
197     public void addWithXml(AddressbookTreeNode folder) {
198         add(folder);
199         this.getNode().addElement(folder.getNode());
200     }
201
202     /**
203      * @see javax.swing.tree.MutableTreeNode#removeFromParent()
204      */

205     public void removeFromParent() {
206
207         // remove xml config
208
getNode().removeFromParent();
209
210         // remove node
211
super.removeFromParent();
212     }
213
214     public String JavaDoc getName() {
215         String JavaDoc name = null;
216
217         FolderItem item = getFolderItem();
218         name = item.getString("property", "name");
219
220         return name;
221     }
222
223     /**
224      * @see org.columba.modules.mail.folder.FolderTreeNode#setName(String)
225      */

226     public void setName(String JavaDoc newName) {
227         FolderItem item = getFolderItem();
228         item.setString("property", "name", newName);
229     }
230 }
Popular Tags