1 package SnowMailClient.model; 2 3 4 import SnowMailClient.SnowMailClientApp; 5 import SnowMailClient.model.folders.*; 6 import SnowMailClient.model.events.*; 7 import snow.utils.storage.*; 8 import SnowMailClient.utils.*; 9 import snow.utils.gui.*; 10 11 import snow.sortabletable.*; 12 import SnowMailClient.Language.Language; 13 14 15 import java.util.*; 16 import java.util.regex.*; 17 import java.text.*; 18 import javax.swing.table.*; 19 import javax.swing.SwingUtilities ; 20 import java.awt.EventQueue ; 21 22 23 27 public final class MailFolder extends FineGrainTableModel 28 implements Vectorizable, MailMessageChangeListener{ 30 private final AppProperties properties = new AppProperties(); 31 private final Vector<MailMessage> messages = new Vector<MailMessage>(); 32 private static final String [] COLUMN_NAMES = new String []{ 33 Language.translate("From"), 34 Language.translate("To"), 35 Language.translate("Subject"), 36 Language.translate("Date"), 37 Language.translate("Size"), 38 Language.translate("SPAM") 39 }; 40 41 42 int[] COLUMN_PREFERED_SIZES = new int[] { 12, 12, 12, 6, 6, 4 }; 43 public int getPreferredColumnWidth(int column) 44 { 45 if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column]; 46 return -1; 47 } 48 49 50 FolderTreeNode node; 52 53 private boolean contentHasChanged = false; 55 56 private SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yy HH':'mm", Language.getInstance().getLocale()); 57 58 59 private int sortedColumnInView = 3; private boolean ascendingSortOrder = true; 61 62 public MailFolder(FolderTreeNode node) 63 { 64 this.node = node; 65 } 67 68 public String getFolderName() 69 { 70 return node.getFolderName(); 71 } 72 73 public void addMessage(final MailMessage m) 74 { 75 if(!SwingUtilities.isEventDispatchThread()) 76 { 77 new Throwable ("Must be EDT").printStackTrace(); 78 } 79 80 fireTableModelWillChange(); 81 messages.addElement(m); 82 m.addChangeListener(this); 83 contentHasChanged = true; 84 fireTableDataChanged(); 85 fireTableModelHasChanged(); 86 87 this.notifyMailFolderListener_of_FolderSizeChanged(); 88 } 89 90 91 public void removeMessage(final MailMessage m) 92 { 93 if(!SwingUtilities.isEventDispatchThread()) 94 { 95 new Throwable ("Must be EDT").printStackTrace(); 96 } 97 98 fireTableModelWillChange(); 99 messages.removeElement(m); 100 m.removeChangeListener(this); 101 contentHasChanged = true; 102 fireTableDataChanged(); 103 fireTableModelHasChanged(); 104 105 this.notifyMailFolderListener_of_FolderSizeChanged(); 106 } 107 108 109 public MailMessage getMessageAt(int i) 110 { 111 return messages.elementAt(i); 112 } 113 114 116 public int getNumberOfNewMails() 117 { 118 int n=0; 119 for(MailMessage mess: messages) 120 { 121 if(mess.getIsNew()) n+=1; 122 } 123 return n; 124 } 125 126 127 130 141 142 143 147 public Vector<MailMessage> getAllMessages() 148 { 149 Vector<MailMessage> vm = new Vector<MailMessage>(); 150 vm.addAll(messages); 151 return vm; 152 } 153 154 155 158 public int getRowCount() { return messages.size(); } 159 public int getColumnCount() { return COLUMN_NAMES.length; } 160 public Object getValueAt(int row, int column) 161 { 162 MailMessage mess = messages.elementAt(row); 163 if(column==0) 164 { 165 Address from = mess.getFromAddress(); 166 return from.getMailAddress(); 167 } 168 else if(column==1) 169 { 170 return mess.getToAddressesText(); 171 } 172 else if(column==2) 173 { 174 return mess.getSubject(); 175 } 176 else if(column==3) 177 { 178 long time = mess.getParsedMessageTime(); 179 if(time==0) 180 { 181 return mess.getDateFieldFormHeader(); 182 } 183 else 184 { 185 return dateFormat.format(new Date(time)); 186 } 187 } 188 else if(column==4) 189 { 190 return MailMessageUtils.formatSize(mess.getSize()); 191 } 192 else if(column==5) 193 { 194 return ""; 200 } 201 202 return "?"; 203 } 204 205 206 public String getColumnName(int col) 207 { 208 return COLUMN_NAMES[col]; 209 } 210 211 public boolean hitForTextSearch(int row, String txt, Pattern p) 212 { 213 boolean foundInTable = super.hitForTextSearch(row, txt, p); 215 if(foundInTable==true) return true; 216 217 MailMessage mess = messages.elementAt(row); 219 return mess.searchText(txt.toUpperCase()); 220 } 221 222 public int compareForColumnSort(int pos1, int pos2, int col) 223 { 224 MailMessage mess1 = messages.elementAt(pos1); 225 MailMessage mess2 = messages.elementAt(pos2); 226 227 if(col==3) { 229 return this.compareDoubles(mess1.getParsedMessageTime(), mess2.getParsedMessageTime()); 230 } 231 232 if(col==4) { 234 return this.compareInts(mess1.getSize(), mess2.getSize()); 235 } 236 237 if(col==5) { 239 240 if(mess1.getIsSPAM() && !mess2.getIsSPAM()) return -1; 241 if(!mess1.getIsSPAM() && mess2.getIsSPAM()) return 1; 242 244 int cd = this.compareDoubles(mess1.getSPAMProbability(), mess2.getSPAMProbability()); 245 return cd; 246 } 247 248 String val1 = (String ) getValueAt(pos1, col); 250 String val2 = (String ) getValueAt(pos2, col); 251 return val1.compareTo(val2); 252 } 253 254 259 public void setContentHasChanged(boolean notify) 260 { 261 263 contentHasChanged = true; 264 if(notify) 265 { 266 this.notifyMailFolderListener_of_ContentEdited(null); 267 } 268 } 269 270 public void setContentWasStored() 271 { 272 contentHasChanged = false; 273 this.notifyMailFolderListener_of_FolderStored(); 274 } 275 276 public boolean hasContentChanged() 277 { 278 return contentHasChanged; 279 } 280 281 282 public void saveSortedColumnFromView( int sortedColumnInView, 283 boolean ascendingSortOrder ) 284 { 285 this.sortedColumnInView = sortedColumnInView; 286 this.ascendingSortOrder = ascendingSortOrder; 287 288 } 289 290 public int getSortedColumn() 291 { 292 return sortedColumnInView; 293 } 294 295 public boolean getIsSortingAscendingOrder() 296 { 297 return ascendingSortOrder; 298 } 299 300 public Vector<Object > getVectorRepresentation() throws VectorizeException 303 { 304 Vector<Object > rep = new Vector<Object >(); 305 rep.addElement(1); rep.addElement(properties.getVectorRepresentation()); 308 Vector<Object > messagesVec = new Vector<Object >(); 309 rep.addElement(messagesVec); for(MailMessage m: messages) 311 { 312 messagesVec.addElement( m.getVectorRepresentation() ); 313 } 314 rep.addElement(this.sortedColumnInView); rep.addElement(this.ascendingSortOrder); return rep; 317 } 318 319 @SuppressWarnings ("unchecked") 320 public void createFromVectorRepresentation(Vector<Object > v) throws VectorizeException 321 { 322 int version = (Integer ) v.elementAt(0); if(version==1) 324 { 325 properties.createFromVectorRepresentation((Vector) v.elementAt(1)); 327 Vector<Vector> messVec = (Vector<Vector>) v.elementAt(2); messages.removeAllElements(); 329 for(Vector mv: messVec) 330 { 331 try 332 { 333 MailMessage mess = new MailMessage(); 334 mess.createFromVectorRepresentation(mv ); 335 messages.addElement(mess); 336 mess.addChangeListener(this); 337 } 338 catch(Exception e) 339 { 340 System.out.println("Cannot read a mail in folder "+this.getFolderName()+" "+e.getMessage()); 341 } 343 } 344 if(v.size()>3) 345 { 346 this.sortedColumnInView = (Integer ) v.elementAt(3); 347 this.ascendingSortOrder = (Boolean ) v.elementAt(4); 348 } 349 350 } 351 else 352 { 353 throw new VectorizeException(Language.translate("bad version %", ""+version)); 354 } 355 } 356 357 public void setRowSelection(int row, boolean isSelected) 360 { 361 messages.elementAt(row).selectedInView = isSelected; 362 } 363 364 public boolean isRowSelected(int row) 365 { 366 return messages.elementAt(row).selectedInView; 367 } 368 369 public void setSelectedMessage(MailMessage mess) 370 { 371 if(mess==null) return; 372 for(MailMessage mi : messages) 373 { 374 mi.selectedInView = (mi==mess); 375 } 376 } 377 378 379 386 public void mailMessageChanged(MailMessage source, MailMessageChangeType type, String detail) 387 { 388 if(type == MailMessageChangeType.IN_EDITION) 389 { 390 this.setContentHasChanged(false); 392 } 393 else 394 { 395 397 this.setContentHasChanged(true); 399 } 400 } 401 402 403 Vector<MailFolderListener> mailFolderListener = new Vector<MailFolderListener>(); 406 407 409 public void addMailFolderListener(MailFolderListener mfn) 410 { 411 mailFolderListener.addElement(mfn); 412 } 413 414 415 public void removeMailFolderListener(MailFolderListener mfn) 416 { 417 mailFolderListener.removeElement(mfn); 418 } 419 420 422 public void notifyMailFolderListener_of_FolderSizeChanged() 423 { 424 MailFolderListener[] mfl = null; 428 synchronized(mailFolderListener) 429 { 430 mfl = mailFolderListener.toArray(new MailFolderListener[mailFolderListener.size()]); 431 } 432 433 for(MailFolderListener mfn : mfl) 434 { 435 mfn.folderSizeChanged(); 436 } 437 if(SnowMailClientApp.getInstance().getFoldersView()!=null) 439 { 440 SnowMailClientApp.getInstance().getFoldersView().repaint(); 441 } 442 } 443 444 public void notifyMailFolderListener_of_FolderStored() 445 { 446 MailFolderListener[] mfl = null; 450 synchronized(mailFolderListener) 451 { 452 mfl = mailFolderListener.toArray(new MailFolderListener[mailFolderListener.size()]); 453 } 454 455 for(MailFolderListener mfn : mfl) 456 { 457 mfn.folderStored(); 458 } 459 460 if(SnowMailClientApp.getInstance().getFoldersView()!=null) 461 { 462 SnowMailClientApp.getInstance().getFoldersView().repaint(); 463 } 464 } 465 466 public void notifyMailFolderListener_of_ContentEdited(MailMessageChangeListener.MailMessageChangeType change) 467 { 468 MailFolderListener[] mfl = null; 472 synchronized(mailFolderListener) 473 { 474 mfl = mailFolderListener.toArray(new MailFolderListener[mailFolderListener.size()]); 475 } 476 477 for(MailFolderListener mfn : mfl) 478 { 479 mfn.folderContentEdited(change); 480 } 481 SnowMailClientApp.getInstance().getFoldersView().repaint(); 482 } 483 484 public void notifyMailFolderListener_of_FolderClosed() 485 { 486 MailFolderListener[] mfl = null; 490 synchronized(mailFolderListener) 491 { 492 mfl = mailFolderListener.toArray(new MailFolderListener[mailFolderListener.size()]); 493 } 494 495 for(MailFolderListener mfn : mfl) 496 { 497 mfn.folderClosed(); 498 } 499 SnowMailClientApp.getInstance().getFoldersView().repaint(); 500 } 501 502 503 504 } | Popular Tags |