KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > MessageListTable


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package echo2example.email;
31
32 import java.io.Serializable JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.EventListener JavaDoc;
35 import java.util.EventObject JavaDoc;
36
37 import javax.mail.Address JavaDoc;
38 import javax.mail.Folder JavaDoc;
39 import javax.mail.Message JavaDoc;
40 import javax.mail.MessagingException JavaDoc;
41 import javax.mail.internet.InternetAddress JavaDoc;
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 /**
54  * A selectable <code>Table</code> which displays a list of messages.
55  */

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    /**
63      * An event describing a message selection.
64      */

65     public class MessageSelectionEvent extends EventObject JavaDoc {
66         
67         private Message JavaDoc message;
68         
69         /**
70          * Creates a <code>MessageSelectionEvent</code>.
71          *
72          * @param message the selected message
73          */

74         public MessageSelectionEvent(Message JavaDoc message) {
75             super(MessageListTable.this);
76             this.message = message;
77         }
78         
79         /**
80          * Returns the selected message.
81          *
82          * @return the selected message
83          */

84         public Message JavaDoc getMessage() {
85             return message;
86         }
87     }
88     
89     /**
90      * A listener interface for receiving notification of message selections.
91      */

92     public static interface MessageSelectionListener extends EventListener JavaDoc, Serializable JavaDoc {
93         
94         /**
95          * Invoked when a message is selected.
96          *
97          * @param e an event describing the selection
98          */

99         public void messageSelected(MessageSelectionEvent e);
100     }
101
102     /**
103      * A <code>TableModel</code> that pulls message header data from the
104      * mail server.
105      */

106     private AbstractTableModel messageTableModel = new AbstractTableModel() {
107         
108         /**
109          * @see nextapp.echo2.app.table.TableModel#getColumnCount()
110          */

111         public int getColumnCount() {
112             return 3;
113         }
114
115         /**
116          * @see nextapp.echo2.app.table.TableModel#getRowCount()
117          */

118         public int getRowCount() {
119             return displayedMessages == null ? 0 : displayedMessages.length;
120         }
121         
122         /**
123          * @see nextapp.echo2.app.table.TableModel#getValueAt(int, int)
124          */

125         public Object JavaDoc getValueAt(int column, int row) {
126             try {
127                 switch (column) {
128                 case COLUMN_FROM:
129                     // Return the sender's address.
130
Address JavaDoc[] from = displayedMessages[row].getFrom();
131                     if (from != null) {
132                         InternetAddress JavaDoc fromAddress = ((InternetAddress JavaDoc) from[0]);
133                         String JavaDoc personal = fromAddress.getPersonal();
134                         return personal == null ? fromAddress.getAddress() : personal;
135                     } else {
136                         return Messages.getString("MessageListTable.UnknownSenderText");
137                     }
138                 case COLUMN_SUBJECT:
139                     // Return the subject.
140
String JavaDoc 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 JavaDoc("Invalid column.");
149                 }
150             } catch (MessagingException JavaDoc ex) {
151                 // Generally should not occur.
152
return null;
153             }
154         }
155         
156         /**
157          * @see nextapp.echo2.app.table.TableModel#getColumnName(int)
158          */

159         public String JavaDoc 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 JavaDoc("Invalid column.");
165             }
166         }
167     };
168     
169     /**
170      * A renderer for the header data contained in the table of messages.
171      */

172     private TableCellRenderer messageTableCellRenderer = new TableCellRenderer() {
173         
174         /**
175          * @see nextapp.echo2.app.table.TableCellRenderer#getTableCellRendererComponent(
176          * nextapp.echo2.app.Table, java.lang.Object, int, int)
177          */

178         public Component getTableCellRendererComponent(Table table, Object JavaDoc value, int column, int row) {
179             Label label;
180             switch (column) {
181             case COLUMN_DATE:
182                 label = new Label(Messages.formatDateTimeMedium((Date JavaDoc) value));
183                 label.setLineWrap(false);
184                 break;
185             case COLUMN_SUBJECT:
186                 label = new Label(value == null ? (String JavaDoc) null : MessageUtil.clean(value.toString(), 25, 50));
187                 break;
188             case COLUMN_FROM:
189                 label = new Label(value == null ? (String JavaDoc) null : MessageUtil.clean(value.toString(), 30, 65));
190                 break;
191             default:
192                 throw new IndexOutOfBoundsException JavaDoc();
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         /**
206          * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
207          */

208         public void actionPerformed(ActionEvent e) {
209             fireMessageSelection();
210         }
211     };
212     
213     private Message JavaDoc[] displayedMessages;
214     private Folder JavaDoc folder;
215     private int totalMessages;
216     private int pageIndex;
217      
218     /**
219      * Creates a new <code>MessageListTable</code>.
220      */

221     public MessageListTable() {
222         super();
223         addActionListener(tableActionListener);
224         setStyleName("MessageListTable.Table");
225         setModel(messageTableModel);
226         setDefaultRenderer(Object JavaDoc.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     /**
235      * Adds a <code>MessageSelectionListener</code> to be notified of
236      * message selections.
237      *
238      * @param l the listener to add
239      */

240     public void addMessageSelectionListener(MessageSelectionListener l) {
241         getEventListenerList().addListener(MessageSelectionListener.class, l);
242     }
243
244     /**
245      * Notifies <code>MessageSelectionListener</code>s of a message selection.
246      */

247     private void fireMessageSelection() {
248         Message JavaDoc 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 JavaDoc[] listeners = getEventListenerList().getListeners(MessageSelectionListener.class);
257         for (int i = 0; i < listeners.length; ++i) {
258             ((MessageSelectionListener) listeners[i]).messageSelected(e);
259         }
260     }
261
262     /**
263      * Refreshes the current page.
264      */

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 JavaDoc ex) {
281             ((EmailApp) getApplicationInstance()).processFatalException(ex);
282         }
283     }
284
285     /**
286      * Removes a <code>MessageSelectionListener</code> from being notified of
287      * message selections.
288      *
289      * @param l the listener to remove
290      */

291     public void removeMessageSelectionListener(MessageSelectionListener l) {
292         getEventListenerList().removeListener(MessageSelectionListener.class, l);
293     }
294
295     /**
296      * Sets the displayed folder.
297      *
298      * @param newValue The new <code>Folder</code> to display.
299      */

300     public void setFolder(Folder JavaDoc 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 JavaDoc ex) {
313             ((EmailApp) getApplicationInstance()).processFatalException(ex);
314         }
315         getSelectionModel().clearSelection();
316         refresh();
317         fireMessageSelection();
318     }
319     
320     /**
321      * Sets the displayed page.
322      */

323     public void setPageIndex(int pageIndex) {
324         this.pageIndex = pageIndex;
325         getSelectionModel().clearSelection();
326         refresh();
327         fireMessageSelection();
328     }
329 }
Popular Tags