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.folder;
019:
020: import java.lang.reflect.Method;
021: import java.util.Hashtable;
022:
023: import javax.swing.ImageIcon;
024: import javax.swing.tree.DefaultMutableTreeNode;
025:
026: import org.columba.addressbook.config.FolderItem;
027: import org.columba.api.command.IWorkerStatusController;
028: import org.columba.api.plugin.IExtensionInterface;
029: import org.columba.core.base.Lock;
030: import org.columba.core.resourceloader.IconKeys;
031: import org.columba.core.resourceloader.ImageLoader;
032: import org.columba.core.xml.XmlElement;
033:
034: public abstract class AddressbookTreeNode extends
035: DefaultMutableTreeNode implements IFolder, IExtensionInterface {
036: protected final static ImageIcon expandedIcon = ImageLoader
037: .getSmallIcon(IconKeys.FOLDER_OPEN);
038:
039: private static int nextFolderUid = 0;
040:
041: protected ImageIcon collapsedIcon = ImageLoader
042: .getSmallIcon(IconKeys.FOLDER);
043:
044: protected FolderItem node;
045:
046: protected Lock myLock;
047:
048: private final Class[] FOLDER_ITEM_ARG = new Class[] { FolderItem.class };
049:
050: public AddressbookTreeNode(String name) {
051: super (name);
052:
053: }
054:
055: public AddressbookTreeNode(FolderItem item) {
056: super ();
057:
058: setNode(item);
059:
060: }
061:
062: public static Object generateNextFolderUid() {
063: return new Integer(++nextFolderUid);
064: }
065:
066: /**
067: * @param nextUid
068: * The nextUid to set.
069: */
070: public static void setNextFolderUid(int uid) {
071: nextFolderUid = uid;
072: }
073:
074: /**
075: * @return Returns the nextUid.
076: */
077: public static int getNextFolderUid() {
078: return nextFolderUid;
079: }
080:
081: /*
082: * public AddressbookTreeNode(String name) { super(name); this.name = name; }
083: */
084: public FolderItem getFolderItem() {
085: return node;
086: }
087:
088: public ImageIcon getIcon() {
089: return collapsedIcon;
090: }
091:
092: public final static FolderItem getDefaultItem(String className,
093: XmlElement props) {
094: XmlElement defaultElement = new XmlElement("folder");
095: defaultElement.addAttribute("class", className);
096: defaultElement.addAttribute("uid", Integer
097: .toString(nextFolderUid++));
098:
099: if (props != null) {
100: defaultElement.addElement(props);
101: }
102:
103: // FAILURE!!!
104:
105: /*
106: * XmlElement filter = new XmlElement("filter");
107: * defaultElement.addElement(filter);
108: */
109: return new FolderItem(defaultElement);
110: }
111:
112: /*
113: * public TreePath getSelectionTreePath() { //TreeNodeList list = new
114: * TreeNodeList( getTreePath() ); TreeNode[] treeNodes = getPathToRoot(this,
115: * 0); TreePath path = new TreePath(treeNodes[0]);
116: *
117: * for (int i = 1; i < treeNodes.length; i++) { Folder folder = (Folder)
118: * treeNodes[i]; path.pathByAddingChild(folder); }
119: *
120: * return path; }
121: */
122:
123: public static XmlElement getDefaultProperties() {
124: return null;
125: }
126:
127: public ImageIcon 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 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 getDefaultChild() {
168: return null;
169: }
170:
171: final public Hashtable getAttributes() {
172: return node.getElement("property").getAttributes();
173: }
174:
175: public abstract void createChildren(IWorkerStatusController worker);
176:
177: public void addFolder(String name, Class childClass)
178: throws Exception {
179: Method m_getDefaultProperties = childClass.getMethod(
180: "getDefaultProperties", null);
181:
182: XmlElement props = (XmlElement) m_getDefaultProperties.invoke(
183: null, null);
184: FolderItem childNode = getDefaultItem(childClass.getName(),
185: props);
186:
187: childNode.setString("property", "name", name);
188:
189: // Put properties that should be copied from parent here
190: AbstractFolder subFolder = (AbstractFolder) childClass
191: .getConstructor(FOLDER_ITEM_ARG).newInstance(
192: new Object[] { childNode });
193: addWithXml(subFolder);
194: }
195:
196: public void addFolder(String name) throws Exception {
197: addFolder(name, getDefaultChild());
198: }
199:
200: public void addWithXml(AddressbookTreeNode folder) {
201: add(folder);
202: this .getNode().addElement(folder.getNode());
203: }
204:
205: /**
206: * @see javax.swing.tree.MutableTreeNode#removeFromParent()
207: */
208: public void removeFromParent() {
209:
210: // remove xml config
211: getNode().removeFromParent();
212:
213: // remove node
214: super .removeFromParent();
215: }
216:
217: public String getName() {
218: String name = null;
219:
220: FolderItem item = getFolderItem();
221: name = item.getString("property", "name");
222:
223: return name;
224: }
225:
226: /**
227: * @see org.columba.modules.mail.folder.FolderTreeNode#setName(String)
228: */
229: public void setName(String newName) {
230: FolderItem item = getFolderItem();
231: item.setString("property", "name", newName);
232: }
233: }
|