1 package org.columba.addressbook.folder.virtual; 2 3 import java.util.Enumeration ; 4 import java.util.Hashtable ; 5 import java.util.Iterator ; 6 import java.util.Map ; 7 import java.util.UUID ; 8 import java.util.Vector ; 9 10 import javax.swing.ImageIcon ; 11 import javax.swing.tree.TreeNode ; 12 13 import org.columba.addressbook.folder.AbstractFolder; 14 import org.columba.addressbook.folder.IContactStorage; 15 import org.columba.addressbook.model.ContactModelFactory; 16 import org.columba.addressbook.model.IContactModel; 17 import org.columba.addressbook.model.IContactModelPartial; 18 import org.columba.api.exception.StoreException; 19 20 public class VirtualFolder extends AbstractFolder implements IContactStorage { 21 22 private Map <String , VirtualItem> map = new Hashtable <String , VirtualItem>(); 23 24 public VirtualFolder() { 25 super("name", "directory"); 26 } 27 28 public String add(IContactStorage parentStore, String parentId) { 29 if ( parentStore == null ) throw new IllegalArgumentException ("parentStore == null"); 30 if ( parentId == null ) throw new IllegalArgumentException ("parentId == null"); 31 32 VirtualItem item = new VirtualItem(parentStore, parentId); 33 String uuid = UUID.randomUUID().toString(); 34 map.put(uuid, item); 35 36 return uuid; 37 } 38 39 public String add(IContactModel contact) throws StoreException { 40 throw new IllegalArgumentException ("add() not supported"); 41 } 42 43 public int count() throws StoreException { 44 return map.size(); 45 } 46 47 public boolean exists(String id) throws StoreException { 48 if ( id == null ) throw new IllegalArgumentException ("id == null"); 49 return map.containsKey(id); 50 } 51 52 56 @Override 57 public Map <String , IContactModelPartial> getContactItemMap() 58 throws StoreException { 59 Map <String , IContactModelPartial> result = new Hashtable <String , IContactModelPartial>(); 60 61 Iterator <Map.Entry <String , VirtualItem>> it = map.entrySet().iterator(); 62 while (it.hasNext()) { 63 Map.Entry <String , VirtualItem> entry = it.next(); 64 VirtualItem item = entry.getValue(); 65 IContactStorage parentStore = item.getParentStore(); 66 String parentId = item.getParentId(); 67 68 IContactModel model = parentStore.get(parentId); 69 IContactModelPartial partial = ContactModelFactory 70 .createContactModelPartial(model, entry.getKey()); 71 result.put(entry.getKey(), partial); 72 } 73 74 return result; 75 } 76 77 public IContactModel get(String id) throws StoreException { 78 if ( id == null ) throw new IllegalArgumentException ("id == null"); 79 80 VirtualItem item = map.get(id); 81 IContactStorage parentStore = item.getParentStore(); 82 String parentId = item.getParentId(); 83 84 IContactModel model = parentStore.get(parentId); 85 return model; 86 } 87 88 public void modify(String id, IContactModel contact) throws StoreException { 89 if ( id == null ) throw new IllegalArgumentException ("id == null"); 90 if ( contact == null ) throw new IllegalArgumentException ("contact == null"); 91 92 VirtualItem item = map.get(id); 93 IContactStorage parentStore = item.getParentStore(); 94 String parentId = item.getParentId(); 95 96 parentStore.modify(parentId, contact); 97 } 98 99 public void remove(String id) throws StoreException { 100 if ( id == null ) throw new IllegalArgumentException ("id == null"); 101 102 VirtualItem item = map.get(id); 103 IContactStorage parentStore = item.getParentStore(); 104 String parentId = item.getParentId(); 105 106 parentStore.remove(parentId); 107 } 108 109 public ImageIcon getIcon() { 110 return null; 111 } 112 113 public String getId() { 114 return "id"; 115 } 116 117 public String getName() { 118 return "vfolder"; 119 } 120 121 public Enumeration children() { 122 return new Vector ().elements(); 123 } 124 125 public boolean getAllowsChildren() { 126 return false; 127 } 128 129 public TreeNode getChildAt(int childIndex) { 130 return null; 131 } 132 133 public int getChildCount() { 134 return 0; 135 } 136 137 public int getIndex(TreeNode node) { 138 return 0; 139 } 140 141 public TreeNode getParent() { 142 return null; 143 } 144 145 public boolean isLeaf() { 146 return false; 147 } 148 149 } 150 | Popular Tags |