1 package SnowMailClient.model; 2 3 import snow.utils.storage.*; 4 import snow.sortabletable.*; 5 import SnowMailClient.Language.Language; 6 7 import java.util.*; 8 import java.awt.EventQueue ; 9 import java.text.*; 10 11 public final class AddressBook extends FineGrainTableModel 12 { 13 private final Vector<Address> addresses = new Vector<Address>(); 14 15 private static final String [] COLUMN_NAMES = new String []{ 16 Language.translate("Name"), 17 Language.translate("Address"), 18 Language.translate("#Received from"), 19 Language.translate("#Sent to"), 20 Language.translate("Last use"), 21 Language.translate("Birthday"), 22 Language.translate("Phone"), 23 Language.translate("Street"), 24 Language.translate("City") }; 25 26 public static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy", Locale.ENGLISH); 27 28 boolean spamlist = false; 29 public AddressBook(boolean spamlist) 30 { 31 this.spamlist = spamlist; 32 } 34 36 public final void addAddress(final Address a) 37 { 38 addAddress(a, true); 39 } 40 41 44 public final void addAddress(final Address a, final boolean matchMail) 45 { 46 if(matchMail 47 && a.getMailAddress().length()>0 && this.hasAddress(a.getMailAddress())) return; 50 EventQueue.invokeLater(new Runnable () { public void run() 51 { 52 fireTableModelWillChange(); 53 addresses.addElement(a); 54 fireTableStructureChanged(); 55 fireTableModelHasChanged(); 56 }}); 57 } 58 59 public final void removeAddress(final Address a) 60 { 61 EventQueue.invokeLater(new Runnable () { public void run() 62 { 63 fireTableModelWillChange(); 64 addresses.removeElement(a); 65 fireTableStructureChanged(); 66 fireTableModelHasChanged(); 67 }}); 68 } 69 70 public final int getNumberOfAddresses() 71 { 72 return addresses.size(); 73 } 74 75 public final Address getAddressAt(int pos) 76 { 77 if(pos<0 || pos>=addresses.size()) return null; 78 79 return addresses.elementAt(pos); 80 } 81 82 84 public final Address getAddress(String mail) 85 { 86 for(Address a : addresses) 87 { 88 if(a.getMailAddress().equalsIgnoreCase(mail)) return a; 89 } 90 return null; 91 } 92 93 public final boolean hasAddress(String mail) 94 { 95 return getAddress(mail) != null; 96 } 97 98 100 public final void incrementMailsReceivedFromIfPresent(Address address) 101 { 102 Address a = getAddress(address.getMailAddress()); 103 if(a==null) return; 104 a.incrementMailsReceivedFrom_(); 105 } 106 107 109 public final void incrementMailsReceivedFrom_AddIfNotFound(Address address) 110 { 111 Address a = getAddress(address.getMailAddress()); 112 if(a==null) 113 { 114 a = address; 115 } 116 a.incrementMailsReceivedFrom_(); 117 } 118 119 122 public void incrementMailsSendedTo(Address address) 123 { 124 Address a = getAddress(address.getMailAddress()); 125 if(a==null) 126 { 127 a = address; 128 } 129 a.incrementMailsSendedTo_(); 130 } 131 132 135 public final int getRowCount() 136 { 137 return addresses.size(); 138 } 139 140 public final int getColumnCount() 141 { 142 if(spamlist) return 4; 143 return COLUMN_NAMES.length; 144 } 145 146 public final Object getValueAt(int row, int column) 147 { 148 if(row<0 || row>=addresses.size()) return "??"; 149 150 Address address = this.getAddressAt(row); 151 if(column==0) return address.getName(); 152 if(column==1) return address.getMailAddress(); 153 if(column==2) return address.getNumberOfMailsReceivedFrom(); 154 if(column==3) return address.getNumberOfMailsSendedTo(); 155 if(column==4) 156 { 157 if(address.getLastUse()==0) return ""; 158 return dateFormat.format(new Date(address.getLastUse())); 159 } 160 if(column==5) 161 { 162 if(address.getBirthday()==0) return ""; 163 return dateFormat.format(new Date(address.getBirthday())); 164 } 165 if(column==6) return address.getPhone(); 166 if(column==7) return address.getStreet(); 167 if(column==8) return address.getCity(); 168 169 return "?"; 170 } 171 172 public String getColumnName(int col) 173 { 174 return COLUMN_NAMES[col]; 175 } 176 177 181 182 public int compareForColumnSort(int pos1, int pos2, int col) 183 { 184 Address address1 = this.getAddressAt(pos1); 185 Address address2 = this.getAddressAt(pos2); 186 187 if(col==0) return address1.getName().compareTo(address2.getName()); 188 if(col==1) return address1.getMailAddress().compareTo(address2.getMailAddress()); 189 if(col==2) 190 { 191 return compareInts(address1.getNumberOfMailsReceivedFrom(), address2.getNumberOfMailsReceivedFrom()); 192 } 193 if(col==3) 194 { 195 return compareInts(address1.getNumberOfMailsSendedTo(), address2.getNumberOfMailsSendedTo()); 196 } 197 if(col==4) 198 { 199 return this.compareDoubles(address1.getLastUse(), address2.getLastUse()); 200 } 201 if(col==5) 202 { 203 return this.compareDoubles(address1.getBirthday(), address2.getBirthday()); 204 } 205 if(col==6) return address1.getPhone().compareTo(address2.getName()); 206 if(col==7) return address1.getStreet().compareTo(address2.getStreet()); 207 if(col==8) return address1.getCity().compareTo(address2.getCity()); 208 209 210 return 0; 212 } 213 214 public boolean isCellEditable(int row, int col) 215 { 216 return col<2 || col>=5; 217 } 218 219 221 public void setValueAt(Object value, int row, int col) 222 { 223 this.fireTableModelWillChange(); 224 225 Address address = this.getAddressAt(row); 226 if(col==0) 227 { 228 address.setName( (String ) value ); 229 } 230 else if(col==1) 231 { 232 address.setMailAddress( (String ) value ); 233 } 234 else if(col==5) 235 { 236 try 237 { 238 if(value.toString().length()==0) 239 { 240 address.setBirthday(0L); 241 } 242 else 243 { 244 Date date = dateFormat.parse( value.toString() ); 245 address.setBirthday(date.getTime()); 246 } 247 } 248 catch(Exception e) {} 249 } 250 else if(col==6) 251 { 252 address.setPhone( (String ) value ); 253 } 254 else if(col==7) 255 { 256 address.setStreet( (String ) value ); 257 } 258 else if(col==8) 259 { 260 address.setCity( (String ) value ); 261 } 262 263 264 265 this.fireTableDataChanged(); 266 this.fireTableModelHasChanged(); 267 } 268 269 270 271 public Class getColumnClass ( int column ) 272 { 273 if(column==4) return String .class; 274 if(column==5) return String .class; 275 return Object .class; 276 } 277 278 280 public void setSelectedAddresses(Vector<Address> ads) 281 { 282 clearRowSelection(); 283 for(Address ai: ads) 284 { 285 Address ad = this.getAddress(ai.getMailAddress()); 286 if(ad!=null) 287 { 288 ad.selected = true; 289 } 290 } 291 } 292 293 296 public void setRowSelection(int row, boolean isSelected) 297 { 298 Address item = this.getAddressAt(row); 299 item.selected = isSelected; 300 } 301 302 public boolean isRowSelected(int row) 303 { 304 Address item = this.getAddressAt(row); 305 return item.selected; 306 } 307 308 public void clearRowSelection() 309 { 310 for(Address a : addresses) 311 { 312 a.selected = false; 313 } 314 } 315 316 318 private void sortAddresses() 319 { 320 Collections.sort(addresses, new Comparator<Address>() 321 { 322 public int compare(Address a1, Address a2) 323 { 324 return a1.getMailAddress().compareToIgnoreCase(a2.getMailAddress()); 325 } 326 }); 327 } 328 329 330 @SuppressWarnings ("unchecked") 333 public void createFromVectorRepresentation (Vector<Object > v) throws VectorizeException 334 { 335 int version = (Integer ) v.elementAt(0); 336 if(version==1) 337 { 338 Vector<Vector> adv = (Vector) v.elementAt(1); 339 addresses.clear(); 340 for(Vector av: adv) 341 { 342 Address a = new Address(av); 343 addresses.addElement( a ); 344 } 345 sortAddresses(); 346 347 if(this.spamlist && addresses.size()>0) 349 { 350 String mad = this.getAddressAt(addresses.size()-1).getMailAddress(); 351 for(int i=addresses.size()-2; i>=0; i--) 352 { 353 String mad2 = this.getAddressAt(i).getMailAddress(); 354 if(mad2.equalsIgnoreCase(mad)) 355 { 356 addresses.removeElementAt(i); 357 } 358 else 359 { 360 mad = mad2; 361 } 362 } 363 } 364 } 365 else 366 { 367 throw new VectorizeException(Language.translate("Bad version %",""+version)); 368 } 369 } 370 371 public Vector<Object > getVectorRepresentation () throws VectorizeException 372 { 373 Vector<Object > v = new Vector<Object >(); 374 v.addElement(1); Vector<Object > adv = new Vector<Object >(); 376 v.addElement(adv); for(Address a: addresses) 378 { 379 adv.add( a.getVectorRepresentation() ); 380 } 381 382 return v; 383 } 384 385 386 } | Popular Tags |