KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > pooka > vcard > VcardAddressBook


1 package net.suberic.pooka.vcard;
2 import net.suberic.pooka.*;
3 import javax.mail.internet.InternetAddress JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.io.*;
6
7 /**
8  * An AddressBook which uses Vcards.
9  */

10 public class VcardAddressBook implements AddressBook, AddressMatcher {
11
12   String JavaDoc addressBookID;
13
14   String JavaDoc fileName;
15   Vcard[] orderedList;
16   ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
17
18   int sortingMethod;
19
20   /**
21    * Creates a new VcardAddressBook.
22    */

23   public VcardAddressBook() {
24
25   }
26
27   /**
28    * Creates a new VcardAddressBook from the given Vcard. It uses the
29    * file represented by the given pFileName as the source for the
30    * addresses.
31    */

32   public VcardAddressBook(String JavaDoc pFileName) throws java.text.ParseException JavaDoc, java.io.IOException JavaDoc {
33     fileName = pFileName;
34
35     loadAddressBook();
36
37   }
38
39   public void configureAddressBook(String JavaDoc newAddressBookID) {
40     addressBookID = newAddressBookID;
41     fileName = Pooka.getProperty("AddressBook." + addressBookID + ".filename", "");
42
43     try {
44       loadAddressBook();
45     } catch (Exception JavaDoc e) {
46       System.out.println(e);
47       e.printStackTrace();
48     }
49   }
50
51   /**
52    * Loads the AddressBook from the saved filename.
53    */

54   public void loadAddressBook() throws java.text.ParseException JavaDoc, java.io.IOException JavaDoc {
55
56     InputStream is = Pooka.getResourceManager().getInputStream(fileName);
57     if (is != null) {
58       BufferedReader reader = new BufferedReader(new InputStreamReader(is));
59       for(Vcard newCard = Vcard.parse(reader); newCard != null; newCard = Vcard.parse(reader)) {
60         insertIntoList(newCard);
61       }
62     }
63
64     /*
65       File f = new File(fileName);
66
67       if (f.exists()) {
68       BufferedReader reader = new BufferedReader(new FileReader(f));
69       for(Vcard newCard = Vcard.parse(reader); newCard != null; newCard = Vcard.parse(reader)) {
70       insertIntoList(newCard);
71       }
72       } else {
73       f.createNewFile();
74       }
75     */

76
77
78     sortList();
79   }
80
81   /**
82    * Inserts the given Vcard into the ordered list.
83    */

84   protected void insertIntoList(Vcard newCard) {
85     arrayList.add(newCard);
86   }
87
88   /**
89    * Adds the given Vcard to the address book.
90    */

91   public void addAddress(AddressBookEntry newAddress) {
92     if (newAddress instanceof Vcard) {
93       Vcard newCard = (Vcard) newAddress;
94       Vcard[] newList = new Vcard[orderedList.length + 1];
95       int searchResult = java.util.Arrays.binarySearch(orderedList, newCard);
96       if (searchResult < 0) {
97         int insertLocation = (searchResult + 1) * -1;
98         if (insertLocation > 0)
99           System.arraycopy(orderedList, 0, newList, 0, insertLocation);
100         newList[insertLocation] = newCard;
101         if (orderedList.length - insertLocation > 0)
102           System.arraycopy(orderedList, insertLocation, newList, insertLocation + 1, orderedList.length - insertLocation);
103
104         orderedList = newList;
105         try {
106           saveAddressBook();
107         } catch (java.io.IOException JavaDoc ioe) {
108           Pooka.getUIFactory().showError(Pooka.getProperty("error.savingVcard", "Error saving Address Book"), ioe);
109         }
110       }
111     }
112   }
113
114   /**
115    * Removes the entry from the AddressBook.
116    */

117   public void removeAddress(AddressBookEntry removeAddress) {
118     if (removeAddress instanceof Vcard) {
119       Vcard removeCard = (Vcard) removeAddress;
120       Vcard[] newList = new Vcard[orderedList.length - 1];
121       int searchResult = java.util.Arrays.binarySearch(orderedList, removeCard);
122       if (searchResult >= 0) {
123         if (searchResult > 0)
124           System.arraycopy(orderedList, 0, newList, 0, searchResult);
125         if (orderedList.length - searchResult > 1)
126           System.arraycopy(orderedList, searchResult + 1, newList, searchResult, orderedList.length - searchResult - 1);
127
128         orderedList = newList;
129         try {
130           saveAddressBook();
131         } catch (java.io.IOException JavaDoc ioe) {
132           Pooka.getUIFactory().showError(Pooka.getProperty("error.savingVcard", "Error saving Address Book"), ioe);
133         }
134       }
135     }
136   }
137
138
139   /**
140    * Saves the list.
141    */

142   public void saveAddressBook() throws java.io.IOException JavaDoc {
143
144     /*
145       File f = new File(fileName);
146       if (f.exists()) {
147       BufferedWriter writer = new BufferedWriter(new FileWriter(f));
148       for(int i = 0; i < orderedList.length; i++) {
149       orderedList[i].write(writer);
150       }
151       writer.flush();
152       writer.close();
153       }
154     */

155
156     OutputStream os = Pooka.getResourceManager().getOutputStream(fileName);
157     if (os != null) {
158       BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os));
159       for(int i = 0; i < orderedList.length; i++) {
160         orderedList[i].write(writer);
161       }
162       writer.flush();
163       writer.close();
164     }
165
166   }
167
168   /**
169    * Sorts the list.
170    */

171   protected void sortList() {
172     orderedList = new Vcard[arrayList.size()];
173     orderedList = (Vcard[]) arrayList.toArray(orderedList);
174     java.util.Arrays.sort(orderedList);
175   }
176
177   /**
178    * Gets the AddressMatcher for this AddressBook.
179    */

180   public AddressMatcher getAddressMatcher() {
181     return this;
182   }
183
184   /**
185    * Returns all of the InternetAddresses which match the given String.
186    */

187   public AddressBookEntry[] match(String JavaDoc matchString) {
188     return match(matchString, false);
189   }
190
191   /**
192    * Returns all of the InternetAddresses which match the given String.
193    */

194   public AddressBookEntry[] matchExactly(String JavaDoc matchString) {
195     return match(matchString, true);
196   }
197
198   /**
199    * Returns all of the InternetAddresses which match the given String.
200    */

201   public AddressBookEntry[] match(String JavaDoc matchString, boolean exactly) {
202     if (orderedList.length < 1)
203       return new AddressBookEntry[0];
204
205     int value = java.util.Arrays.binarySearch(orderedList, matchString);
206     // now get all the matches, if any.
207
if (value < 0) {
208       return new AddressBookEntry[0];
209     }
210
211     if (orderedList[value].compareTo(matchString) == 0) {
212       if (exactly) {
213         // FIXME assume for now only one match.
214
String JavaDoc valueExact = orderedList[value].getPersonalName();
215         if (valueExact.equalsIgnoreCase(matchString)) {
216           return new AddressBookEntry[] { orderedList[value] };
217         } else
218           return new AddressBookEntry[0];
219       }
220
221       // get all the matches.
222
int minimum = value;
223       while (minimum > 0 && (orderedList[minimum - 1].compareTo(matchString) == 0))
224         minimum--;
225
226
227       int maximum = value;
228       while (maximum < orderedList.length -1 && (orderedList[maximum + 1].compareTo(matchString) == 0))
229         maximum++;
230
231       AddressBookEntry[] returnValue = new AddressBookEntry[maximum - minimum + 1];
232
233       for(int i = 0; i < returnValue.length; i++) {
234         returnValue[i] = orderedList[minimum + i];
235       }
236
237       return returnValue;
238     } else {
239       return new AddressBookEntry[0];
240     }
241     //return binarySearch(matchString, 0, orderedList.size());
242
}
243
244   /**
245    * Returns all of the InternetAddresses whose FirstName matches the given
246    * String.
247    */

248   public AddressBookEntry[] matchFirstName(String JavaDoc matchString) {
249     return match(matchString);
250   }
251
252   /**
253    * Returns all of the InternetAddresses whose LastName matches the given
254    * String.
255    */

256   public AddressBookEntry[] matchLastName(String JavaDoc matchString) {
257     return match(matchString);
258   }
259
260   /**
261    * Returns all of the InternetAddresses whose email addresses match the
262    * given String.
263    */

264   public AddressBookEntry[] matchEmailAddress(String JavaDoc matchString) {
265     return match(matchString);
266   }
267
268   /**
269    * Returns the InternetAddress which follows the given String alphabetically.
270    */

271   public AddressBookEntry getNextMatch(String JavaDoc matchString) {
272     if (orderedList.length < 1)
273       return null;
274
275     int value = java.util.Arrays.binarySearch(orderedList, matchString);
276     // now get all the matches, if any.
277
if (value < 0) {
278       value = (value + 1) * -1;
279     } else {
280       // if we got a match, we want to return the next one.
281
value = value + 1;
282     }
283     if (value >= orderedList.length) {
284       return orderedList[orderedList.length - 1];
285     } else {
286       return orderedList[value];
287     }
288   }
289
290   /**
291    * Returns the InternetAddress which precedes the given String
292    * alphabetically.
293    */

294   public AddressBookEntry getPreviousMatch(String JavaDoc matchString) {
295     if (orderedList.length < 1)
296       return null;
297
298     int value = java.util.Arrays.binarySearch(orderedList, matchString);
299     // now get all the matches, if any.
300
if (value < 0) {
301       value = (value + 2) * -1;
302     } else {
303       // if we got a match, we want to return the previous one.
304
value = value - 1;
305     }
306     if (value < 0) {
307       return orderedList[0];
308     } else {
309       return orderedList[value];
310     }
311   }
312
313   /**
314    * Returns the AddressBookID.
315    */

316   public String JavaDoc getAddressBookID() {
317     return addressBookID;
318   }
319
320   /**
321    * Creates a new, empty AddressBookEntry.
322    */

323   public AddressBookEntry newAddressBookEntry() {
324     return new Vcard(new java.util.Properties JavaDoc());
325   }
326
327 }
328
Popular Tags