1 package org.columba.addressbook.folder; 19 20 import java.lang.reflect.Method ; 21 import java.util.Hashtable ; 22 23 import javax.swing.ImageIcon ; 24 import javax.swing.tree.DefaultMutableTreeNode ; 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 35 implements IFolder, IExtensionInterface { 36 protected final static ImageIcon expandedIcon = ImageLoader 37 .getSmallIcon(IconKeys.FOLDER_OPEN); 38 39 private static int nextFolderUid = 0; 40 41 protected ImageIcon collapsedIcon = ImageLoader 42 .getSmallIcon(IconKeys.FOLDER); 43 44 protected FolderItem node; 45 46 protected Lock myLock; 47 48 private final Class [] FOLDER_ITEM_ARG = new Class [] { FolderItem.class }; 49 50 public AddressbookTreeNode(String 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 generateNextFolderUid() { 63 return new Integer (++nextFolderUid); 64 } 65 66 70 public static void setNextFolderUid(int uid) { 71 nextFolderUid = uid; 72 } 73 74 77 public static int getNextFolderUid() { 78 return nextFolderUid; 79 } 80 81 84 public FolderItem getFolderItem() { 85 return node; 86 } 87 88 public ImageIcon getIcon() { 89 return collapsedIcon; 90 } 91 92 public final static FolderItem getDefaultItem(String 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 104 108 return new FolderItem(defaultElement); 109 } 110 111 121 122 public static XmlElement getDefaultProperties() { 123 return null; 124 } 125 126 127 public ImageIcon getExpandedIcon() { 128 return expandedIcon; 129 } 130 131 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 152 public XmlElement getNode() { 153 return node.getRoot(); 154 } 155 156 162 public void setNode(FolderItem node) { 163 this.node = node; 164 } 165 166 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) throws Exception { 178 Method 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 AbstractFolder subFolder = (AbstractFolder) childClass.getConstructor( 189 FOLDER_ITEM_ARG).newInstance(new Object [] { childNode }); 190 addWithXml(subFolder); 191 } 192 193 public void addFolder(String name) throws Exception { 194 addFolder(name, getDefaultChild()); 195 } 196 197 public void addWithXml(AddressbookTreeNode folder) { 198 add(folder); 199 this.getNode().addElement(folder.getNode()); 200 } 201 202 205 public void removeFromParent() { 206 207 getNode().removeFromParent(); 209 210 super.removeFromParent(); 212 } 213 214 public String getName() { 215 String name = null; 216 217 FolderItem item = getFolderItem(); 218 name = item.getString("property", "name"); 219 220 return name; 221 } 222 223 226 public void setName(String newName) { 227 FolderItem item = getFolderItem(); 228 item.setString("property", "name", newName); 229 } 230 } | Popular Tags |