KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FolderModel


1 /*
2  * @(#)FolderModel.java 1.13 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import javax.mail.*;
40 import java.util.Date JavaDoc;
41 import javax.swing.table.AbstractTableModel JavaDoc;
42
43 /**
44  * Maps the messages in a Folder to the Swing's Table Model
45  *
46  * @version 1.13, 01/05/23
47  * @author Christopher Cotton
48  * @author Bill Shannon
49  */

50
51 public class FolderModel extends AbstractTableModel JavaDoc {
52     
53     Folder folder;
54     Message[] messages;
55
56     String JavaDoc[] columnNames = { "Date", "From", "Subject"};
57     Class JavaDoc[] columnTypes = { String JavaDoc.class, String JavaDoc.class, String JavaDoc.class };
58
59     public void setFolder(Folder what) throws MessagingException {
60     if (what != null) {
61
62         // opened if needed
63
if (!what.isOpen()) {
64         what.open(Folder.READ_WRITE);
65         }
66     
67         // get the messages
68
messages = what.getMessages();
69         cached = new String JavaDoc[messages.length][];
70     } else {
71         messages = null;
72         cached = null;
73     }
74     // close previous folder and switch to new folder
75
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     //---------------------
86
// Implementation of the TableModel methods
87
//---------------------
88

89     public String JavaDoc getColumnName(int column) {
90     return columnNames[column];
91     }
92     
93     public Class JavaDoc 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 JavaDoc getValueAt(int aRow, int aColumn) {
110     switch(aColumn) {
111     case 0: // date
112
case 1: // From String[] what = getCachedData(aRow);
113
case 2: // Subject
114
String JavaDoc[] 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 JavaDoc[][] cached;
127     
128     protected String JavaDoc[] getCachedData(int row) {
129     if (cached[row] == null) {
130         try{
131         Message m = messages[row];
132         
133         String JavaDoc[] theData = new String JavaDoc[4];
134         
135         // Date
136
Date JavaDoc date = m.getSentDate();
137         if (date == null) {
138             theData[0] = "Unknown";
139         } else {
140             theData[0] = date.toString();
141         }
142         
143         // From
144
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         // Subject
152
String JavaDoc 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