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.util.Hashtable;
021: import java.util.Map;
022:
023: import javax.swing.ImageIcon;
024:
025: import org.columba.addressbook.config.FolderItem;
026: import org.columba.addressbook.facade.IContactItem;
027: import org.columba.addressbook.model.ContactModelFactory;
028: import org.columba.addressbook.model.ContactModelPartial;
029: import org.columba.addressbook.model.GroupModel;
030: import org.columba.addressbook.model.IContactModel;
031: import org.columba.addressbook.model.IContactModelPartial;
032: import org.columba.addressbook.model.IGroupModel;
033: import org.columba.api.command.IWorkerStatusController;
034: import org.columba.api.exception.StoreException;
035: import org.columba.core.resourceloader.IconKeys;
036: import org.columba.core.resourceloader.ImageLoader;
037: import org.columba.core.xml.XmlElement;
038:
039: /**
040: * Group folder storing a list of contact indices.
041: * <p>
042: * This can be seen as a filtered view of a contact folder.
043: *
044: * @author fdietz
045: *
046: */
047: public class GroupFolder extends AbstractFolder implements
048: IContactStorage, IGroupFolder {
049:
050: private IGroupModel group;
051:
052: private ImageIcon groupImageIcon = ImageLoader
053: .getSmallIcon(IconKeys.USER);
054:
055: /**
056: * @param name
057: */
058: public GroupFolder(String name, String dir) {
059: super (name, dir);
060:
061: group = new GroupModel();
062: }
063:
064: /**
065: * @param item
066: */
067: public GroupFolder(FolderItem item) {
068: super (item);
069:
070: XmlElement property = item.getElement("property");
071: XmlElement e = property.getElement("group");
072: if (e == null) {
073: e = new XmlElement("group");
074: property.addElement(e);
075: }
076:
077: group = new GroupModel(e, getId());
078: }
079:
080: public void createChildren(IWorkerStatusController worker) {
081: }
082:
083: /**
084: * @see org.columba.addressbook.folder.IContactStorage#add(IContactModel)
085: */
086: public String add(IContactModel contact) throws StoreException {
087: String uid = contact.getId();
088:
089: group.addMember(uid);
090:
091: fireItemAdded(uid);
092:
093: return uid;
094: }
095:
096: /**
097: * @see org.columba.addressbook.folder.IContactStorage#count()
098: */
099: public int count() throws StoreException {
100:
101: return group.count();
102: }
103:
104: /**
105: * @see org.columba.addressbook.folder.IContactStorage#exists(java.lang.Object)
106: */
107: public boolean exists(String uid) throws StoreException {
108:
109: return group.exists(uid);
110: }
111:
112: /**
113: * @see org.columba.addressbook.folder.IContactStorage#get(java.lang.Object)
114: */
115: public IContactModel get(String uid) throws StoreException {
116:
117: AbstractFolder parent = (AbstractFolder) getParent();
118:
119: return parent.get(uid);
120: }
121:
122: /**
123: * @see org.columba.addressbook.folder.IContactStorage#modify(java.lang.Object,
124: * IContactModel)
125: */
126: public void modify(String uid, IContactModel contact)
127: throws StoreException {
128: AbstractFolder parent = (AbstractFolder) getParent();
129:
130: parent.modify(uid, contact);
131:
132: fireItemChanged(uid);
133:
134: }
135:
136: /**
137: * @see org.columba.addressbook.folder.IContactStorage#remove(java.lang.Object)
138: */
139: public void remove(String uid) throws StoreException {
140: group.remove(uid);
141:
142: fireItemRemoved(uid);
143: }
144:
145: /**
146: * @see org.columba.addressbook.folder.IContactStorage#getHeaderItemList()
147: */
148: public Map<String, IContactModelPartial> getContactItemMap()
149: throws StoreException {
150: AbstractFolder parent = (AbstractFolder) getParent();
151:
152: Map<String, IContactModelPartial> filter = new Hashtable<String, IContactModelPartial>();
153:
154: String[] members = group.getMembers();
155: for (int i = 0; i < members.length; i++) {
156: IContactModel c = parent.get(members[i]);
157: if (c == null) {
158: // contact doesn't exist in parent folder anymore
159: // -> remove it
160:
161: remove(members[i]);
162: } else {
163: IContactModelPartial item = ContactModelFactory
164: .createContactModelPartial(c, c.getId());
165:
166: filter.put(members[i], item);
167: }
168: }
169:
170: return filter;
171: }
172:
173: /**
174: * @return Returns the group.
175: */
176: public IGroupModel getGroup() {
177: return group;
178: }
179:
180: /**
181: * @see org.columba.addressbook.folder.AddressbookTreeNode#getIcon()
182: */
183: public ImageIcon getIcon() {
184: return groupImageIcon;
185: }
186: }
|