KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > addressbook > gui > autocomplete > 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.addressbook.gui.autocomplete;
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.folder.AbstractFolder;
26 import org.columba.addressbook.folder.IGroupFolder;
27 import org.columba.addressbook.gui.tree.AddressbookTreeModel;
28 import org.columba.addressbook.model.ContactModelFactory;
29 import org.columba.addressbook.model.ContactModelPartial;
30 import org.columba.addressbook.model.GroupModelPartial;
31 import org.columba.addressbook.model.BasicModelPartial;
32 import org.columba.addressbook.model.IGroupModel;
33 import org.columba.addressbook.model.IGroupModelPartial;
34 import org.columba.addressbook.model.IBasicModelPartial;
35
36 public class AddressCollector implements IAddressCollector {
37
38     private Hashtable JavaDoc _adds = new Hashtable JavaDoc();
39
40     private static AddressCollector instance = new AddressCollector();
41
42     private AddressCollector() {
43     }
44
45     public static AddressCollector getInstance() {
46         return instance;
47     }
48
49     /**
50      * Add all contacts and group items to hashmap.
51      *
52      * @param uid
53      * selected folder uid
54      * @param includeGroup
55      * add groups if true. No groups, otherwise.
56      */

57     public void addAllContacts(String JavaDoc uid, boolean includeGroup) {
58         if (uid == null)
59             throw new IllegalArgumentException JavaDoc("uid == null");
60
61         List JavaDoc<IBasicModelPartial> list = new Vector JavaDoc<IBasicModelPartial>();
62
63         try {
64             AbstractFolder folder = (AbstractFolder) AddressbookTreeModel
65                     .getInstance().getFolder(uid);
66             if (folder == null)
67                 return;
68
69             list.addAll(folder.getHeaderItemList());
70
71             if (includeGroup) {
72                 for (int i = 0; i < folder.getChildCount(); i++) {
73                     IGroupFolder groupFolder = (IGroupFolder) folder
74                             .getChildAt(i);
75                     IGroupModel group = groupFolder.getGroup();
76
77                     IGroupModelPartial groupPartial = ContactModelFactory.createGroupPartial(group, folder);
78
79                     list.add(groupPartial);
80                 }
81             }
82
83         } catch (Exception JavaDoc e) {
84
85             e.printStackTrace();
86         }
87
88         Iterator JavaDoc it = list.iterator();
89         while (it.hasNext()) {
90             BasicModelPartial headerItem = (BasicModelPartial) it.next();
91
92             if (headerItem.isContact()) {
93                 // contacts item
94
ContactModelPartial item = (ContactModelPartial) headerItem;
95
96                 addAddress(item.getName(), item);
97                 addAddress(item.getLastname(), item);
98                 addAddress(item.getFirstname(), item);
99                 addAddress(item.getAddress(), item);
100             } else {
101                 if (includeGroup) {
102                     // group item
103
GroupModelPartial item = (GroupModelPartial) headerItem;
104
105                     addAddress(item.getName(), item);
106                 }
107             }
108         }
109     }
110
111     public void addAddress(String JavaDoc add, IBasicModelPartial item) {
112         if (add != null) {
113             _adds.put(add, item);
114         }
115     }
116
117     public Object JavaDoc[] getAddresses() {
118         return _adds.keySet().toArray();
119     }
120
121     public IBasicModelPartial getHeaderItem(String JavaDoc add) {
122         return (IBasicModelPartial) _adds.get(add);
123     }
124
125     public void clear() {
126         _adds.clear();
127     }
128
129     /**
130      * @see org.frappucino.addresscombobox.ItemProvider#getMatchingItems(java.lang.String)
131      */

132     public Object JavaDoc[] getMatchingItems(String JavaDoc s) {
133         Object JavaDoc[] items = getAddresses();
134
135         Vector JavaDoc v = new Vector JavaDoc();
136         // for each JComboBox item
137
for (int k = 0; k < items.length; k++) {
138             // to lower case
139
String JavaDoc item = items[k].toString().toLowerCase();
140             // compare if item starts with str
141
if (item.startsWith(s.toLowerCase())) {
142                 v.add(item);
143             }
144         }
145         return v.toArray();
146     }
147 }
Popular Tags