KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > util > AddressCollector


1 // The contents of this file are subject to the Mozilla Public License Version
2
// 1.1
3
//(the "License"); you may not use this file except in compliance with the
4
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
//
6
//Software distributed under the License is distributed on an "AS IS" basis,
7
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8
//for the specific language governing rights and
9
//limitations under the License.
10
//
11
//The Original Code is "The Columba Project"
12
//
13
//The Initial Developers of the Original Code are Frederik Dietz and Timo
14
// Stich.
15
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
16
//
17
//All Rights Reserved.
18
package org.columba.mail.gui.composer.util;
19
20 import java.util.Hashtable JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import org.columba.addressbook.facade.IContactItem;
26 import org.columba.addressbook.facade.IGroupItem;
27 import org.columba.addressbook.facade.IHeaderItem;
28
29 public class AddressCollector {
30
31     private Hashtable JavaDoc _adds = new Hashtable JavaDoc();
32     private static AddressCollector instance = new AddressCollector();
33
34     private AddressCollector() {
35     }
36     
37     public static AddressCollector getInstance() {
38         return instance;
39     }
40
41     /**
42      * Add all contacts and group items to hashmap.
43      *
44      * @param uid selected folder uid
45      * @param includeGroup add groups if true. No groups, otherwise.
46      */

47     public void addAllContacts(List JavaDoc<IHeaderItem> list, boolean includeGroup) {
48         if ( list == null ) throw new IllegalArgumentException JavaDoc("list == null");
49         
50         Iterator JavaDoc<IHeaderItem> it = list.iterator();
51         while (it.hasNext()) {
52             IHeaderItem headerItem = it.next();
53
54             if (headerItem.isContact()) {
55                 // contacts item
56
IContactItem item = (IContactItem) headerItem;
57
58                 addAddress(item.getName(), item);
59                 addAddress(item.getFirstName(), item);
60                 addAddress(item.getLastName(), item);
61                 addAddress(item.getEmailAddress(), item);
62             } else {
63                 if (includeGroup) {
64                     // group item
65
IGroupItem item = (IGroupItem) headerItem;
66
67                     addAddress(item.getName(), item);
68                 }
69             }
70         }
71     }
72
73     public void addAddress(String JavaDoc add, IHeaderItem item) {
74         if (add != null) {
75             _adds.put(add, item);
76         }
77     }
78
79     public Object JavaDoc[] getAddresses() {
80         return _adds.keySet().toArray();
81     }
82
83     public IHeaderItem getHeaderItem(String JavaDoc add) {
84         return (IHeaderItem) _adds.get(add);
85     }
86
87     public void clear() {
88         _adds.clear();
89     }
90
91     /**
92      * @see org.frappucino.addresscombobox.ItemProvider#getMatchingItems(java.lang.String)
93      */

94     public Object JavaDoc[] getMatchingItems(String JavaDoc s) {
95         Object JavaDoc[] items = getAddresses();
96
97         Vector JavaDoc v = new Vector JavaDoc();
98         // for each JComboBox item
99
for (int k = 0; k < items.length; k++) {
100             // to lower case
101
String JavaDoc item = items[k].toString().toLowerCase();
102             // compare if item starts with str
103
if (item.startsWith(s.toLowerCase())) {
104                 v.add(item);
105             }
106         }
107         return v.toArray();
108     }
109 }
Popular Tags