KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdic > desktop > Message


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20  
21 package org.jdesktop.jdic.desktop;
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29
30 /**
31  * This class represents a message structure.
32  *
33  * <p>
34  * It consists of several necessary message fields.
35  *
36  * @see Desktop#mail(Message)
37  */

38 public class Message {
39   
40     /**
41      * Name of the message "To" list field.
42      */

43     private List JavaDoc toAddrs;
44   
45     /**
46      * Name of the "Cc" list field.
47      */

48     private List JavaDoc ccAddrs;
49   
50     /**
51      * Name of the "Bcc" list field.
52      */

53     private List JavaDoc bccAddrs;
54   
55     /**
56      * Name of the message "Subject" field.
57      */

58     private String JavaDoc subject;
59   
60     /**
61      * Name of the message "Body" field.
62      */

63     private String JavaDoc body;
64   
65     /**
66      * Name of the message attachment list field.
67      */

68     private List JavaDoc attachments;
69
70     /**
71      * Constructor of a <code>Message</code> object.
72      */

73     public Message() {
74         toAddrs= new ArrayList JavaDoc();
75         ccAddrs = new ArrayList JavaDoc();
76         bccAddrs = new ArrayList JavaDoc();
77         attachments = new ArrayList JavaDoc();
78     }
79
80     /**
81      * Gets an iterator of the message "To" address list.
82      *
83      * @return an <code>Iterator</code> object of the message "To" address list.
84      */

85     public Iterator JavaDoc getToAddrs() {
86         return toAddrs.iterator();
87     }
88
89     /**
90      * Sets the message "To" address list.
91      *
92      * @param atoList an email address list for the "To" field.
93      */

94     public void setToAddrs(List JavaDoc atoList) {
95         toAddrs.clear();
96
97         if(atoList == null)
98             return;
99         Iterator JavaDoc iter = atoList.iterator();
100         if(iter == null)
101             return;
102         while(iter.hasNext())
103             toAddrs.add(iter.next());
104     }
105
106     /**
107      * Gets an iterator of the message "Cc" address list.
108      *
109      * @return an <code>Iterator</code> object of the message "Cc" address list.
110      */

111     public Iterator JavaDoc getCcAddrs() {
112         return ccAddrs.iterator();
113     }
114   
115     /**
116      * Sets the message "Cc" address list.
117      *
118      * @param accList an email address list for the "Cc" field.
119      */

120     public void setCcAddrs(List JavaDoc accList) {
121         ccAddrs.clear();
122
123         if(accList == null)
124             return;
125         Iterator JavaDoc iter = accList.iterator();
126         if(iter == null)
127             return;
128         while(iter.hasNext())
129             ccAddrs.add(iter.next());
130     }
131
132     /**
133      * Gets an iterator of the message "Bcc" address list.
134      *
135      * @return an <code>Iterator</code> object of the message "Bcc" address list.
136      */

137     public Iterator JavaDoc getBccAddrs() {
138         return bccAddrs.iterator();
139     }
140   
141     /**
142      * Sets the message "Bcc" address list.
143      *
144      * @param abccList an email address list for the "Bcc" field.
145      */

146     public void setBccAddrs(List JavaDoc abccList) {
147         bccAddrs.clear();
148
149         if(abccList == null)
150             return;
151         Iterator JavaDoc iter = abccList.iterator();
152         if(iter == null)
153             return;
154         while(iter.hasNext())
155             bccAddrs.add(iter.next());
156     }
157
158
159     /**
160      * Gets the "Subject" field of the message.
161      *
162      * @return the value of the "Subject" field.
163      */

164     public String JavaDoc getSubject() {
165         return subject;
166     }
167   
168     /**
169      * Sets the message "Subject" field.
170      *
171      * @param asubject a string for the "Subject" field.
172      *
173      */

174     public void setSubject(String JavaDoc asubject) {
175         subject = asubject;
176     }
177   
178     /**
179      * Gets the "Body" field of the message.
180      *
181      * @return the value of the "Body" field.
182      */

183     public String JavaDoc getBody() {
184         return body;
185     }
186   
187     /**
188      * Sets the message "Body" field.
189      *
190      * @param abody a string for the "Body" field.
191      */

192     public void setBody(String JavaDoc abody) {
193         body = abody;
194     }
195   
196     /**
197      * Gets an iterator of the message "Attachment" file list.
198      *
199      * @return an <code>Iterator</code> object of the message "Attachment" file
200      * list.
201      */

202     public Iterator JavaDoc getAttachments() {
203         return attachments.iterator();
204     }
205   
206     /**
207      * Sets the message "Attachments" field.
208      *
209      * @param attachList the given attachment list, whose elements are the
210      * abosolute paths of files to be attached.
211      * @throws IOException if any of the attached files is not readable.
212      */

213     public void setAttachments(List JavaDoc attachList) throws IOException JavaDoc {
214         boolean hasUnreadable = false;
215         boolean hasDuplicated = false;
216         List JavaDoc unattachedlist = new ArrayList JavaDoc();
217         String JavaDoc unattach_filenames = "";
218         attachments.clear();
219
220         if(attachList == null)
221             return;
222         Iterator JavaDoc iter = attachList.iterator();
223         if(iter == null)
224             return;
225         while(iter.hasNext()) {
226             String JavaDoc filename = (String JavaDoc)iter.next();
227             Iterator JavaDoc inner_iter = attachments.iterator();
228             if(inner_iter != null) {
229                 while(inner_iter.hasNext()) {
230                     if(hasDuplicated
231                             = filename.equals((String JavaDoc)(inner_iter.next())))
232                         break;
233                 }
234             }
235             if(hasDuplicated)
236                 continue;
237             File JavaDoc attfile = new File JavaDoc(filename);
238             
239             // add the file to attachments if it is a readable file.
240
if (attfile.canRead() && !attfile.isHidden()) {
241                 if (attfile.isFile())
242                     attachments.add(attfile.getAbsolutePath());
243             }
244             
245             // continue to add other files in the list if the current one
246
// unreadbale.
247
else {
248                 hasUnreadable = true;
249                 unattachedlist.add(attfile.getAbsolutePath());
250                 continue;
251             }
252         }
253
254         // throw IOException if there are any unreadable files.
255
if(hasUnreadable) {
256             Iterator JavaDoc unattach_iter = unattachedlist.iterator();
257             if (unattach_iter != null) {
258                 while(unattach_iter.hasNext()) {
259                      unattach_filenames += (String JavaDoc)unattach_iter.next() + "\n";
260                 }
261                 throw new IOException JavaDoc("Following files cannot be added to " +
262                         "the attachments:\n" + unattach_filenames);
263             }
264         }
265     }
266 }
267
Popular Tags