1 package net.suberic.pooka.vcard; 2 import net.suberic.pooka.*; 3 import javax.mail.internet.InternetAddress ; 4 import java.util.ArrayList ; 5 import java.io.*; 6 7 10 public class VcardAddressBook implements AddressBook, AddressMatcher { 11 12 String addressBookID; 13 14 String fileName; 15 Vcard[] orderedList; 16 ArrayList arrayList = new ArrayList (); 17 18 int sortingMethod; 19 20 23 public VcardAddressBook() { 24 25 } 26 27 32 public VcardAddressBook(String pFileName) throws java.text.ParseException , java.io.IOException { 33 fileName = pFileName; 34 35 loadAddressBook(); 36 37 } 38 39 public void configureAddressBook(String newAddressBookID) { 40 addressBookID = newAddressBookID; 41 fileName = Pooka.getProperty("AddressBook." + addressBookID + ".filename", ""); 42 43 try { 44 loadAddressBook(); 45 } catch (Exception e) { 46 System.out.println(e); 47 e.printStackTrace(); 48 } 49 } 50 51 54 public void loadAddressBook() throws java.text.ParseException , java.io.IOException { 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 76 77 78 sortList(); 79 } 80 81 84 protected void insertIntoList(Vcard newCard) { 85 arrayList.add(newCard); 86 } 87 88 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 ioe) { 108 Pooka.getUIFactory().showError(Pooka.getProperty("error.savingVcard", "Error saving Address Book"), ioe); 109 } 110 } 111 } 112 } 113 114 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 ioe) { 132 Pooka.getUIFactory().showError(Pooka.getProperty("error.savingVcard", "Error saving Address Book"), ioe); 133 } 134 } 135 } 136 } 137 138 139 142 public void saveAddressBook() throws java.io.IOException { 143 144 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 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 180 public AddressMatcher getAddressMatcher() { 181 return this; 182 } 183 184 187 public AddressBookEntry[] match(String matchString) { 188 return match(matchString, false); 189 } 190 191 194 public AddressBookEntry[] matchExactly(String matchString) { 195 return match(matchString, true); 196 } 197 198 201 public AddressBookEntry[] match(String matchString, boolean exactly) { 202 if (orderedList.length < 1) 203 return new AddressBookEntry[0]; 204 205 int value = java.util.Arrays.binarySearch(orderedList, matchString); 206 if (value < 0) { 208 return new AddressBookEntry[0]; 209 } 210 211 if (orderedList[value].compareTo(matchString) == 0) { 212 if (exactly) { 213 String 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 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 } 243 244 248 public AddressBookEntry[] matchFirstName(String matchString) { 249 return match(matchString); 250 } 251 252 256 public AddressBookEntry[] matchLastName(String matchString) { 257 return match(matchString); 258 } 259 260 264 public AddressBookEntry[] matchEmailAddress(String matchString) { 265 return match(matchString); 266 } 267 268 271 public AddressBookEntry getNextMatch(String matchString) { 272 if (orderedList.length < 1) 273 return null; 274 275 int value = java.util.Arrays.binarySearch(orderedList, matchString); 276 if (value < 0) { 278 value = (value + 1) * -1; 279 } else { 280 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 294 public AddressBookEntry getPreviousMatch(String matchString) { 295 if (orderedList.length < 1) 296 return null; 297 298 int value = java.util.Arrays.binarySearch(orderedList, matchString); 299 if (value < 0) { 301 value = (value + 2) * -1; 302 } else { 303 value = value - 1; 305 } 306 if (value < 0) { 307 return orderedList[0]; 308 } else { 309 return orderedList[value]; 310 } 311 } 312 313 316 public String getAddressBookID() { 317 return addressBookID; 318 } 319 320 323 public AddressBookEntry newAddressBookEntry() { 324 return new Vcard(new java.util.Properties ()); 325 } 326 327 } 328 | Popular Tags |