1 package SnowMailClient.view.MessagesPreview; 2 3 import SnowMailClient.SnowMailClientApp; 4 import SnowMailClient.model.*; 5 import SnowMailClient.utils.storage.*; 6 import SnowMailClient.utils.*; 7 import snow.utils.gui.*; 8 import snow.sortabletable.*; 9 import SnowMailClient.Language.Language; 10 11 12 import java.util.*; 13 import java.text.*; 14 import javax.swing.table.*; 15 import javax.swing.SwingUtilities ; 16 17 19 public final class PreviewModel extends FineGrainTableModel 20 { 21 private final Vector<PreviewHeader> headers = new Vector<PreviewHeader>(); 22 private boolean hasBeenChangedByUser = false; 23 private static final String [] COLUMN_NAMES = new String []{ 24 Language.translate("State"), 25 Language.translate("From"), 26 Language.translate("To"), 27 Language.translate("To name"), Language.translate("Subject"), 29 Language.translate("Date"), 30 Language.translate("Size"), 31 Language.translate("SPAM") }; 32 33 private int[] COLUMN_PREFERED_SIZES = new int[] { 8, 15, 12, 8, 17, 7, 3, 2 }; 34 public int getPreferredColumnWidth(int column) 35 { 36 if(column>=0 && column<COLUMN_PREFERED_SIZES.length) return COLUMN_PREFERED_SIZES[column]; 37 return -1; 38 } 39 40 public PreviewModel() 41 { 42 super(); 43 } 44 45 48 public void addMessageHeader(final PreviewHeader h) 49 { 50 if(!SwingUtilities.isEventDispatchThread()) 51 { 52 new Throwable ("Must be called in the EDT").printStackTrace(); 53 } 54 55 AddressBook book = SnowMailClientApp.getInstance().getAddressBook(); 57 String from = h.getEntryValue("from", "<NO FROM FIELD>"); 58 String fromAddress = new Address(from).getMailAddress(); 59 boolean knownFrom =book.getAddress(fromAddress)!=null; 60 if(knownFrom) 61 { 62 h.setEntryOverwrite("PreviewModel_known_from", "true"); 63 } 64 65 if(SnowMailClientApp.getInstance().getSpamBook().getAddress(fromAddress)!=null) 66 { 67 h.setEntryOverwrite("PreviewModel_spam_from", "true"); 68 } 69 70 double p = SnowMailClientApp.getInstance().getWordStatistic().calculateSpamProbability(h).getProbability(); 71 if(p>=0 && p<=1) 72 { 73 h.setEntryOverwrite("PreviewModel_spam_prob", ""+p); 74 } 75 76 78 h.setEntryOverwrite("PreviewModel_to_name", ""); 79 80 81 String tos = h.getEntryValue("to", "") + " " +h.getEntryValue("cc", "") + " " + h.getEntryValue("bcc", ""); 82 int pos = tos.toUpperCase().indexOf( h.getAccount().getAddress().toUpperCase() ); 83 if( pos==-1) 84 { 85 if(!knownFrom) 86 { 87 h.setEntryOverwrite("PreviewModel_invalid_to", "true"); 88 } 89 } 90 else 91 { 92 String toName = tos.substring(0,pos).trim(); 93 pos = toName.lastIndexOf(","); 94 if(pos>=0) toName = toName.substring(pos+1, toName.length()).trim(); 95 if(toName.endsWith("<")) toName = toName.substring(0, toName.length()-1).trim(); 96 if(toName.endsWith("\"")) toName = toName.substring(0, toName.length()-1).trim(); 97 if(toName.startsWith("\"")) toName = toName.substring(1).trim(); 98 99 h.setEntryOverwrite("PreviewModel_to_name", toName); 100 } 102 103 104 105 fireTableModelWillChange(); 106 hasBeenChangedByUser = true; 107 108 synchronized(this.headers) 109 { 110 headers.add(h); 111 } 112 113 fireTableStructureChanged(); 114 fireTableModelHasChanged(); 115 } 116 117 119 public void removeMessageHeader(final PreviewHeader h) 120 { 121 if(!SwingUtilities.isEventDispatchThread()) 122 { 123 new Throwable ("Must be EDT").printStackTrace(); 124 } 125 126 fireTableModelWillChange(); 127 hasBeenChangedByUser = true; 128 129 synchronized(this.headers) 130 { 131 headers.remove(h); 132 } 133 134 fireTableStructureChanged(); 135 fireTableModelHasChanged(); 136 } 137 138 public PreviewHeader getMessageHeaderAt(int pos) 139 { 140 synchronized(this.headers) 141 { 142 return headers.get(pos); 143 } 144 } 145 146 public Vector<PreviewHeader> getMailsBeingDownloaded() 147 { 148 synchronized(this.headers) 149 { 150 Vector<PreviewHeader> h = new Vector<PreviewHeader>(); 151 for(int i=0; i<getRowCount(); i++) 152 { 153 PreviewHeader ph = getMessageHeaderAt(i); 154 if(ph.state==PreviewHeader.State.DownloadingMessage) 155 { 156 h.add(ph); 157 } 158 } 159 return h; 160 } 161 } 162 163 166 public Vector<PreviewHeader> getAllMailsNotAlreadyDownloaded_(boolean selectedOnly) 167 { 168 synchronized(this.headers) 169 { 170 Vector<PreviewHeader> h = new Vector<PreviewHeader>(); 171 for(int i=0; i<getRowCount(); i++) 172 { 173 PreviewHeader ph = getMessageHeaderAt(i); 174 if(selectedOnly && !this.isRowSelected(i)) 175 { 176 continue; 177 } 178 179 if(ph.state!=PreviewHeader.State.MessageDownloaded 180 && ph.state!=PreviewHeader.State.DownloadingMessage) 181 { 182 h.addElement(ph); 183 } 184 } 185 return h; 186 } 187 } 188 189 190 193 public int getRowCount() 194 { 195 synchronized(this.headers) 196 { 197 return headers.size(); 198 } 199 } 200 201 public int getColumnCount() { return COLUMN_NAMES.length; } 202 SimpleDateFormat dateFormat = new SimpleDateFormat("dd MMM yyyy HH':'mm", Locale.ENGLISH); 203 DecimalFormat format0 = new DecimalFormat("0.0"); 204 public Object getValueAt(int row, int column) 205 { 206 PreviewHeader h = getMessageHeaderAt(row); 207 if(column==0) 208 { 209 if(h.state==PreviewHeader.State.DownloadError) 210 { 211 return Language.translate("Error"); 212 } 213 if(h.state==PreviewHeader.State.DownloadInterrupted) return Language.translate("Interrupted"); 214 if(h.state==PreviewHeader.State.Deleted) return Language.translate("Deleted"); 215 if(h.state==PreviewHeader.State.Deleted) return Language.translate("Deleting")+"..."; 216 if(h.state==PreviewHeader.State.MessageNotDownloaded) return ""; 217 if(h.state==PreviewHeader.State.DownloadingMessage) 218 { 219 if(h.alreadyDownloadedSize==0) 220 { 221 return Language.translate("Downloading")+"..."; 222 } 223 return ""+format0.format((h.alreadyDownloadedSize / ((double) h.getSizeInBytes())*100.0))+" %"; 224 225 } 226 return ""; 227 228 } 229 else if(column==1) 230 { 231 return h.getEntryValue("from", "<NO FROM FIELD>"); 232 } 233 else if(column==2) 234 { 235 StringBuilder sb = new StringBuilder (); 236 sb.append(h.getEntryValue("to", "<NO TO FIELD>")); 237 if(h.hasEntry("cc")) 238 { 239 sb.append(", CC:"+ h.getEntryValue("cc", "?")); 240 } 241 if(h.hasEntry("bcc")) 242 { 243 sb.append(", BCC:"+ h.getEntryValue("bcc", "?")); 244 } 245 return sb.toString(); 246 } 247 else if(column==3) 248 { 249 return h.getEntryValue("PreviewModel_to_name", "<??>"); 250 } 251 else if(column==4) 252 { 253 return h.getEntryValue("subject", "<NO SUBJECT FIELD>"); 254 } 255 else if(column==5) 256 { 257 String dateS = h.getEntryValue("date", "<NO DATE FIELD>"); 258 try 259 { 260 Date date = MailMessageUtils.parseDateFromString(dateS); 261 return dateFormat.format(date); 262 } 263 catch(Exception e) 264 { 265 if(!dateS.equals("<NO DATE FIELD>")) 266 { 267 System.out.println("Cannot parse date "+dateS); 268 } 269 return dateS; 270 } 271 } 272 else if(column==6) 273 { 274 return h.getSize(); 275 } 276 else if(column==7) 277 { 278 return ""; 279 } 280 return "?"; 281 } 282 283 284 public String getColumnName(int col) 285 { 286 return COLUMN_NAMES[col]; 287 } 288 289 public int compareForColumnSort(int pos1, int pos2, int col) 290 { 291 PreviewHeader h1 = getMessageHeaderAt(pos1); 292 PreviewHeader h2 = getMessageHeaderAt(pos2); 293 if(col==5) 294 { 295 double p1 = h1.getParsedDate(); 296 double p2 = h2.getParsedDate(); 297 return this.compareDoubles(p1, p2); 298 } 299 else if(col==6) 300 { 301 double p1 = h1.getSizeInBytes(); 302 double p2 = h2.getSizeInBytes(); 303 return this.compareDoubles(p1,p2); 304 } 305 else if(col==7) 306 { 307 double p1 = Double.parseDouble(h1.getEntryValue("PreviewModel_spam_prob", "0.0")); 308 double p2 = Double.parseDouble(h2.getEntryValue("PreviewModel_spam_prob", "0.0")); 309 return this.compareDoubles(p1,p2); 310 } 311 else 312 { 313 return super.compareForColumnSort(pos1,pos2,col); 314 } 315 } 316 317 321 public void setRowSelection(int row, boolean isSelected) 322 { 323 PreviewHeader h = getMessageHeaderAt(row); 324 h.setEntryOverwrite("Preview_Selected", ""+isSelected); 325 } 326 327 public boolean isRowSelected(int row) 328 { 329 PreviewHeader h = getMessageHeaderAt(row); 330 return h.getEntryValue("Preview_Selected", "false").equals("true"); 331 } 332 333 public void clearRowSelection() 334 { 335 for(int i=0; i<this.getRowCount(); i++) 336 { 337 setRowSelection(i, false); 338 } 339 } 340 341 342 } | Popular Tags |