1 38 39 import javax.mail.*; 40 import java.util.Date ; 41 import javax.swing.table.AbstractTableModel ; 42 43 50 51 public class FolderModel extends AbstractTableModel { 52 53 Folder folder; 54 Message[] messages; 55 56 String [] columnNames = { "Date", "From", "Subject"}; 57 Class [] columnTypes = { String .class, String .class, String .class }; 58 59 public void setFolder(Folder what) throws MessagingException { 60 if (what != null) { 61 62 if (!what.isOpen()) { 64 what.open(Folder.READ_WRITE); 65 } 66 67 messages = what.getMessages(); 69 cached = new String [messages.length][]; 70 } else { 71 messages = null; 72 cached = null; 73 } 74 if (folder != null) 76 folder.close(true); 77 folder = what; 78 fireTableDataChanged(); 79 } 80 81 public Message getMessage(int which) { 82 return messages[which]; 83 } 84 85 89 public String getColumnName(int column) { 90 return columnNames[column]; 91 } 92 93 public Class getColumnClass(int column) { 94 return columnTypes[column]; 95 } 96 97 98 public int getColumnCount() { 99 return columnNames.length; 100 } 101 102 public int getRowCount() { 103 if (messages == null) 104 return 0; 105 106 return messages.length; 107 } 108 109 public Object getValueAt(int aRow, int aColumn) { 110 switch(aColumn) { 111 case 0: case 1: case 2: String [] what = getCachedData(aRow); 115 if (what != null) { 116 return what[aColumn]; 117 } else { 118 return ""; 119 } 120 121 default: 122 return ""; 123 } 124 } 125 126 protected static String [][] cached; 127 128 protected String [] getCachedData(int row) { 129 if (cached[row] == null) { 130 try{ 131 Message m = messages[row]; 132 133 String [] theData = new String [4]; 134 135 Date date = m.getSentDate(); 137 if (date == null) { 138 theData[0] = "Unknown"; 139 } else { 140 theData[0] = date.toString(); 141 } 142 143 Address[] adds = m.getFrom(); 145 if (adds != null && adds.length != 0) { 146 theData[1] = adds[0].toString(); 147 } else { 148 theData[1] = ""; 149 } 150 151 String subject = m.getSubject(); 153 if (subject != null) { 154 theData[2] = subject; 155 } else { 156 theData[2] = "(No Subject)"; 157 } 158 159 cached[row] = theData; 160 } 161 catch (MessagingException e) { 162 e.printStackTrace(); 163 } 164 } 165 166 return cached[row]; 167 } 168 } 169 | Popular Tags |