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.mail.gui.composer.util;
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.facade.IContactItem;
026: import org.columba.addressbook.facade.IGroupItem;
027: import org.columba.addressbook.facade.IHeaderItem;
028:
029: public class AddressCollector {
030:
031: private Hashtable _adds = new Hashtable();
032: private static AddressCollector instance = new AddressCollector();
033:
034: private AddressCollector() {
035: }
036:
037: public static AddressCollector getInstance() {
038: return instance;
039: }
040:
041: /**
042: * Add all contacts and group items to hashmap.
043: *
044: * @param uid selected folder uid
045: * @param includeGroup add groups if true. No groups, otherwise.
046: */
047: public void addAllContacts(List<IHeaderItem> list,
048: boolean includeGroup) {
049: if (list == null)
050: throw new IllegalArgumentException("list == null");
051:
052: Iterator<IHeaderItem> it = list.iterator();
053: while (it.hasNext()) {
054: IHeaderItem headerItem = it.next();
055:
056: if (headerItem.isContact()) {
057: // contacts item
058: IContactItem item = (IContactItem) headerItem;
059:
060: addAddress(item.getName(), item);
061: addAddress(item.getFirstName(), item);
062: addAddress(item.getLastName(), item);
063: addAddress(item.getEmailAddress(), item);
064: } else {
065: if (includeGroup) {
066: // group item
067: IGroupItem item = (IGroupItem) headerItem;
068:
069: addAddress(item.getName(), item);
070: }
071: }
072: }
073: }
074:
075: public void addAddress(String add, IHeaderItem item) {
076: if (add != null) {
077: _adds.put(add, item);
078: }
079: }
080:
081: public Object[] getAddresses() {
082: return _adds.keySet().toArray();
083: }
084:
085: public IHeaderItem getHeaderItem(String add) {
086: return (IHeaderItem) _adds.get(add);
087: }
088:
089: public void clear() {
090: _adds.clear();
091: }
092:
093: /**
094: * @see org.frappucino.addresscombobox.ItemProvider#getMatchingItems(java.lang.String)
095: */
096: public Object[] getMatchingItems(String s) {
097: Object[] items = getAddresses();
098:
099: Vector v = new Vector();
100: // for each JComboBox item
101: for (int k = 0; k < items.length; k++) {
102: // to lower case
103: String item = items[k].toString().toLowerCase();
104: // compare if item starts with str
105: if (item.startsWith(s.toLowerCase())) {
106: v.add(item);
107: }
108: }
109: return v.toArray();
110: }
111: }
|