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.gui.autocomplete;
019:
020: import java.util.Hashtable;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Vector;
024:
025: import org.columba.addressbook.folder.AbstractFolder;
026: import org.columba.addressbook.folder.IGroupFolder;
027: import org.columba.addressbook.gui.tree.AddressbookTreeModel;
028: import org.columba.addressbook.model.ContactModelFactory;
029: import org.columba.addressbook.model.ContactModelPartial;
030: import org.columba.addressbook.model.GroupModelPartial;
031: import org.columba.addressbook.model.BasicModelPartial;
032: import org.columba.addressbook.model.IGroupModel;
033: import org.columba.addressbook.model.IGroupModelPartial;
034: import org.columba.addressbook.model.IBasicModelPartial;
035:
036: public class AddressCollector implements IAddressCollector {
037:
038: private Hashtable _adds = new Hashtable();
039:
040: private static AddressCollector instance = new AddressCollector();
041:
042: private AddressCollector() {
043: }
044:
045: public static AddressCollector getInstance() {
046: return instance;
047: }
048:
049: /**
050: * Add all contacts and group items to hashmap.
051: *
052: * @param uid
053: * selected folder uid
054: * @param includeGroup
055: * add groups if true. No groups, otherwise.
056: */
057: public void addAllContacts(String uid, boolean includeGroup) {
058: if (uid == null)
059: throw new IllegalArgumentException("uid == null");
060:
061: List<IBasicModelPartial> list = new Vector<IBasicModelPartial>();
062:
063: try {
064: AbstractFolder folder = (AbstractFolder) AddressbookTreeModel
065: .getInstance().getFolder(uid);
066: if (folder == null)
067: return;
068:
069: list.addAll(folder.getHeaderItemList());
070:
071: if (includeGroup) {
072: for (int i = 0; i < folder.getChildCount(); i++) {
073: IGroupFolder groupFolder = (IGroupFolder) folder
074: .getChildAt(i);
075: IGroupModel group = groupFolder.getGroup();
076:
077: IGroupModelPartial groupPartial = ContactModelFactory
078: .createGroupPartial(group, folder);
079:
080: list.add(groupPartial);
081: }
082: }
083:
084: } catch (Exception e) {
085:
086: e.printStackTrace();
087: }
088:
089: Iterator it = list.iterator();
090: while (it.hasNext()) {
091: BasicModelPartial headerItem = (BasicModelPartial) it
092: .next();
093:
094: if (headerItem.isContact()) {
095: // contacts item
096: ContactModelPartial item = (ContactModelPartial) headerItem;
097:
098: addAddress(item.getName(), item);
099: addAddress(item.getLastname(), item);
100: addAddress(item.getFirstname(), item);
101: addAddress(item.getAddress(), item);
102: } else {
103: if (includeGroup) {
104: // group item
105: GroupModelPartial item = (GroupModelPartial) headerItem;
106:
107: addAddress(item.getName(), item);
108: }
109: }
110: }
111: }
112:
113: public void addAddress(String add, IBasicModelPartial item) {
114: if (add != null) {
115: _adds.put(add, item);
116: }
117: }
118:
119: public Object[] getAddresses() {
120: return _adds.keySet().toArray();
121: }
122:
123: public IBasicModelPartial getHeaderItem(String add) {
124: return (IBasicModelPartial) _adds.get(add);
125: }
126:
127: public void clear() {
128: _adds.clear();
129: }
130:
131: /**
132: * @see org.frappucino.addresscombobox.ItemProvider#getMatchingItems(java.lang.String)
133: */
134: public Object[] getMatchingItems(String s) {
135: Object[] items = getAddresses();
136:
137: Vector v = new Vector();
138: // for each JComboBox item
139: for (int k = 0; k < items.length; k++) {
140: // to lower case
141: String item = items[k].toString().toLowerCase();
142: // compare if item starts with str
143: if (item.startsWith(s.toLowerCase())) {
144: v.add(item);
145: }
146: }
147: return v.toArray();
148: }
149: }
|