1 29 30 package echo2example.email; 31 32 import java.io.Serializable ; 33 import java.util.Date ; 34 import java.util.EventListener ; 35 import java.util.EventObject ; 36 37 import javax.mail.Address ; 38 import javax.mail.Folder ; 39 import javax.mail.Message ; 40 import javax.mail.MessagingException ; 41 import javax.mail.internet.InternetAddress ; 42 43 import nextapp.echo2.app.Component; 44 import nextapp.echo2.app.Extent; 45 import nextapp.echo2.app.Label; 46 import nextapp.echo2.app.Table; 47 import nextapp.echo2.app.event.ActionEvent; 48 import nextapp.echo2.app.event.ActionListener; 49 import nextapp.echo2.app.table.AbstractTableModel; 50 import nextapp.echo2.app.table.TableCellRenderer; 51 import nextapp.echo2.app.table.TableColumnModel; 52 53 56 public class MessageListTable extends Table { 57 58 private static final int COLUMN_FROM = 0; 59 private static final int COLUMN_SUBJECT = 1; 60 private static final int COLUMN_DATE = 2; 61 62 65 public class MessageSelectionEvent extends EventObject { 66 67 private Message message; 68 69 74 public MessageSelectionEvent(Message message) { 75 super(MessageListTable.this); 76 this.message = message; 77 } 78 79 84 public Message getMessage() { 85 return message; 86 } 87 } 88 89 92 public static interface MessageSelectionListener extends EventListener , Serializable { 93 94 99 public void messageSelected(MessageSelectionEvent e); 100 } 101 102 106 private AbstractTableModel messageTableModel = new AbstractTableModel() { 107 108 111 public int getColumnCount() { 112 return 3; 113 } 114 115 118 public int getRowCount() { 119 return displayedMessages == null ? 0 : displayedMessages.length; 120 } 121 122 125 public Object getValueAt(int column, int row) { 126 try { 127 switch (column) { 128 case COLUMN_FROM: 129 Address [] from = displayedMessages[row].getFrom(); 131 if (from != null) { 132 InternetAddress fromAddress = ((InternetAddress ) from[0]); 133 String personal = fromAddress.getPersonal(); 134 return personal == null ? fromAddress.getAddress() : personal; 135 } else { 136 return Messages.getString("MessageListTable.UnknownSenderText"); 137 } 138 case COLUMN_SUBJECT: 139 String subject = displayedMessages[row].getSubject(); 141 if (subject != null && subject.length() > 40) { 142 subject = subject.substring(0, 40); 143 } 144 return subject; 145 case COLUMN_DATE: 146 return displayedMessages[row].getSentDate(); 147 default: 148 throw new IllegalArgumentException ("Invalid column."); 149 } 150 } catch (MessagingException ex) { 151 return null; 153 } 154 } 155 156 159 public String getColumnName(int column) { 160 switch (column) { 161 case COLUMN_FROM: return Messages.getString("MessageListTable.ColumnHeaderFrom"); 162 case COLUMN_SUBJECT: return Messages.getString("MessageListTable.ColumnHeaderSubject"); 163 case COLUMN_DATE: return Messages.getString("MessageListTable.ColumnHeaderDate"); 164 default: throw new IllegalArgumentException ("Invalid column."); 165 } 166 } 167 }; 168 169 172 private TableCellRenderer messageTableCellRenderer = new TableCellRenderer() { 173 174 178 public Component getTableCellRendererComponent(Table table, Object value, int column, int row) { 179 Label label; 180 switch (column) { 181 case COLUMN_DATE: 182 label = new Label(Messages.formatDateTimeMedium((Date ) value)); 183 label.setLineWrap(false); 184 break; 185 case COLUMN_SUBJECT: 186 label = new Label(value == null ? (String ) null : MessageUtil.clean(value.toString(), 25, 50)); 187 break; 188 case COLUMN_FROM: 189 label = new Label(value == null ? (String ) null : MessageUtil.clean(value.toString(), 30, 65)); 190 break; 191 default: 192 throw new IndexOutOfBoundsException (); 193 } 194 if (row % 2 == 0) { 195 label.setStyleName("MessageListTable.EvenRowLabel"); 196 } else { 197 label.setStyleName("MessageListTable.OddRowLabel"); 198 } 199 return label; 200 } 201 }; 202 203 private ActionListener tableActionListener = new ActionListener() { 204 205 208 public void actionPerformed(ActionEvent e) { 209 fireMessageSelection(); 210 } 211 }; 212 213 private Message [] displayedMessages; 214 private Folder folder; 215 private int totalMessages; 216 private int pageIndex; 217 218 221 public MessageListTable() { 222 super(); 223 addActionListener(tableActionListener); 224 setStyleName("MessageListTable.Table"); 225 setModel(messageTableModel); 226 setDefaultRenderer(Object .class, messageTableCellRenderer); 227 228 TableColumnModel columnModel = getColumnModel(); 229 columnModel.getColumn(0).setWidth(new Extent(35, Extent.PERCENT)); 230 columnModel.getColumn(1).setWidth(new Extent(40, Extent.PERCENT)); 231 columnModel.getColumn(2).setWidth(new Extent(25, Extent.PERCENT)); 232 } 233 234 240 public void addMessageSelectionListener(MessageSelectionListener l) { 241 getEventListenerList().addListener(MessageSelectionListener.class, l); 242 } 243 244 247 private void fireMessageSelection() { 248 Message message; 249 int selectedRow = getSelectionModel().getMinSelectedIndex(); 250 if (selectedRow == -1) { 251 message = null; 252 } else { 253 message = displayedMessages[selectedRow]; 254 } 255 MessageSelectionEvent e = new MessageSelectionEvent(message); 256 EventListener [] listeners = getEventListenerList().getListeners(MessageSelectionListener.class); 257 for (int i = 0; i < listeners.length; ++i) { 258 ((MessageSelectionListener) listeners[i]).messageSelected(e); 259 } 260 } 261 262 265 private void refresh() { 266 if (folder == null) { 267 displayedMessages = null; 268 messageTableModel.fireTableDataChanged(); 269 return; 270 } 271 try { 272 int firstMessage = pageIndex * EmailApp.MESSAGES_PER_PAGE + 1; 273 int lastMessage = firstMessage + EmailApp.MESSAGES_PER_PAGE - 1; 274 if (lastMessage > totalMessages) { 275 lastMessage = totalMessages; 276 } 277 278 displayedMessages = folder.getMessages(firstMessage, lastMessage); 279 messageTableModel.fireTableDataChanged(); 280 } catch (MessagingException ex) { 281 ((EmailApp) getApplicationInstance()).processFatalException(ex); 282 } 283 } 284 285 291 public void removeMessageSelectionListener(MessageSelectionListener l) { 292 getEventListenerList().removeListener(MessageSelectionListener.class, l); 293 } 294 295 300 public void setFolder(Folder newValue) { 301 try { 302 if (folder != null) { 303 folder.close(false); 304 folder = null; 305 } 306 folder = newValue; 307 if (folder != null) { 308 folder.open(Folder.READ_ONLY); 309 totalMessages = folder.getMessageCount(); 310 } 311 312 } catch (MessagingException ex) { 313 ((EmailApp) getApplicationInstance()).processFatalException(ex); 314 } 315 getSelectionModel().clearSelection(); 316 refresh(); 317 fireMessageSelection(); 318 } 319 320 323 public void setPageIndex(int pageIndex) { 324 this.pageIndex = pageIndex; 325 getSelectionModel().clearSelection(); 326 refresh(); 327 fireMessageSelection(); 328 } 329 } | Popular Tags |