001: package org.columba.addressbook.folder.virtual;
002:
003: import java.util.Enumeration;
004: import java.util.Hashtable;
005: import java.util.Iterator;
006: import java.util.Map;
007: import java.util.UUID;
008: import java.util.Vector;
009:
010: import javax.swing.ImageIcon;
011: import javax.swing.tree.TreeNode;
012:
013: import org.columba.addressbook.folder.AbstractFolder;
014: import org.columba.addressbook.folder.IContactStorage;
015: import org.columba.addressbook.model.ContactModelFactory;
016: import org.columba.addressbook.model.IContactModel;
017: import org.columba.addressbook.model.IContactModelPartial;
018: import org.columba.api.exception.StoreException;
019:
020: public class VirtualFolder extends AbstractFolder implements
021: IContactStorage {
022:
023: private Map<String, VirtualItem> map = new Hashtable<String, VirtualItem>();
024:
025: public VirtualFolder() {
026: super ("name", "directory");
027: }
028:
029: public String add(IContactStorage parentStore, String parentId) {
030: if (parentStore == null)
031: throw new IllegalArgumentException("parentStore == null");
032: if (parentId == null)
033: throw new IllegalArgumentException("parentId == null");
034:
035: VirtualItem item = new VirtualItem(parentStore, parentId);
036: String uuid = UUID.randomUUID().toString();
037: map.put(uuid, item);
038:
039: return uuid;
040: }
041:
042: public String add(IContactModel contact) throws StoreException {
043: throw new IllegalArgumentException("add() not supported");
044: }
045:
046: public int count() throws StoreException {
047: return map.size();
048: }
049:
050: public boolean exists(String id) throws StoreException {
051: if (id == null)
052: throw new IllegalArgumentException("id == null");
053: return map.containsKey(id);
054: }
055:
056: /**
057: * @see org.columba.addressbook.folder.IContactStorage#getHeaderItemList()
058: *
059: */
060: @Override
061: public Map<String, IContactModelPartial> getContactItemMap()
062: throws StoreException {
063: Map<String, IContactModelPartial> result = new Hashtable<String, IContactModelPartial>();
064:
065: Iterator<Map.Entry<String, VirtualItem>> it = map.entrySet()
066: .iterator();
067: while (it.hasNext()) {
068: Map.Entry<String, VirtualItem> entry = it.next();
069: VirtualItem item = entry.getValue();
070: IContactStorage parentStore = item.getParentStore();
071: String parentId = item.getParentId();
072:
073: IContactModel model = parentStore.get(parentId);
074: IContactModelPartial partial = ContactModelFactory
075: .createContactModelPartial(model, entry.getKey());
076: result.put(entry.getKey(), partial);
077: }
078:
079: return result;
080: }
081:
082: public IContactModel get(String id) throws StoreException {
083: if (id == null)
084: throw new IllegalArgumentException("id == null");
085:
086: VirtualItem item = map.get(id);
087: IContactStorage parentStore = item.getParentStore();
088: String parentId = item.getParentId();
089:
090: IContactModel model = parentStore.get(parentId);
091: return model;
092: }
093:
094: public void modify(String id, IContactModel contact)
095: throws StoreException {
096: if (id == null)
097: throw new IllegalArgumentException("id == null");
098: if (contact == null)
099: throw new IllegalArgumentException("contact == null");
100:
101: VirtualItem item = map.get(id);
102: IContactStorage parentStore = item.getParentStore();
103: String parentId = item.getParentId();
104:
105: parentStore.modify(parentId, contact);
106: }
107:
108: public void remove(String id) throws StoreException {
109: if (id == null)
110: throw new IllegalArgumentException("id == null");
111:
112: VirtualItem item = map.get(id);
113: IContactStorage parentStore = item.getParentStore();
114: String parentId = item.getParentId();
115:
116: parentStore.remove(parentId);
117: }
118:
119: public ImageIcon getIcon() {
120: return null;
121: }
122:
123: public String getId() {
124: return "id";
125: }
126:
127: public String getName() {
128: return "vfolder";
129: }
130:
131: public Enumeration children() {
132: return new Vector().elements();
133: }
134:
135: public boolean getAllowsChildren() {
136: return false;
137: }
138:
139: public TreeNode getChildAt(int childIndex) {
140: return null;
141: }
142:
143: public int getChildCount() {
144: return 0;
145: }
146:
147: public int getIndex(TreeNode node) {
148: return 0;
149: }
150:
151: public TreeNode getParent() {
152: return null;
153: }
154:
155: public boolean isLeaf() {
156: return false;
157: }
158:
159: }
|