KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > MessagePane


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.IOException JavaDoc;
33 import java.text.CharacterIterator JavaDoc;
34 import java.text.StringCharacterIterator JavaDoc;
35 import java.util.ArrayList JavaDoc;
36 import java.util.Iterator JavaDoc;
37 import java.util.List JavaDoc;
38
39 import javax.mail.Address JavaDoc;
40 import javax.mail.BodyPart JavaDoc;
41 import javax.mail.Message JavaDoc;
42 import javax.mail.MessagingException JavaDoc;
43 import javax.mail.Multipart JavaDoc;
44
45 import nextapp.echo2.app.Column;
46 import nextapp.echo2.app.Component;
47 import nextapp.echo2.app.Extent;
48 import nextapp.echo2.app.Font;
49 import nextapp.echo2.app.Grid;
50 import nextapp.echo2.app.Insets;
51 import nextapp.echo2.app.Label;
52 import nextapp.echo2.app.layout.ColumnLayoutData;
53
54 /**
55  * A <code>Component</code> which displays a single <code>Message</code>.
56  */

57 public class MessagePane extends Column {
58     
59     private static final ColumnLayoutData SPACER_COLUMN_LAYOUT_DATA;
60     static {
61         SPACER_COLUMN_LAYOUT_DATA = new ColumnLayoutData();
62         SPACER_COLUMN_LAYOUT_DATA.setHeight(new Extent(15));
63     }
64     
65     private Label toFieldPromptLabel;
66     private Label toFieldValueLabel;
67     private Label ccFieldPromptLabel;
68     private Label ccFieldValueLabel;
69     private Label bccFieldPromptLabel;
70     private Label bccFieldValueLabel;
71     private Label subjectFieldValueLabel;
72     private Column messageColumn;
73     
74     /**
75      * Creates a new <code>MessagePane</code>.
76      */

77     public MessagePane() {
78         super();
79         setVisible(false);
80         
81         setCellSpacing(new Extent(10));
82         setInsets(new Insets(3));
83         
84         Grid headerGrid = new Grid();
85         headerGrid.setStyleName("Message.HeaderGrid");
86         add(headerGrid);
87
88         toFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.To"));
89         toFieldPromptLabel.setStyleName("Message.HeaderGridPrompt");
90         headerGrid.add(toFieldPromptLabel);
91         
92         toFieldValueLabel = new Label();
93         headerGrid.add(toFieldValueLabel);
94         
95         ccFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.Cc"));
96         ccFieldPromptLabel.setStyleName("Message.HeaderGridPrompt");
97         headerGrid.add(ccFieldPromptLabel);
98         
99         ccFieldValueLabel = new Label();
100         headerGrid.add(ccFieldValueLabel);
101         
102         bccFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.Bcc"));
103         bccFieldPromptLabel.setStyleName("Message.HeaderGridPrompt");
104         headerGrid.add(bccFieldPromptLabel);
105         
106         bccFieldValueLabel = new Label();
107         headerGrid.add(bccFieldValueLabel);
108         
109         Label subjectFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.Subject"));
110         subjectFieldPromptLabel.setStyleName("Message.HeaderGridPrompt");
111         headerGrid.add(subjectFieldPromptLabel);
112         
113         subjectFieldValueLabel = new Label();
114         headerGrid.add(subjectFieldValueLabel);
115         
116         messageColumn = new Column();
117         messageColumn.setFont(new Font(Font.MONOSPACE, Font.PLAIN, null));
118         add(messageColumn);
119     }
120
121     /**
122      * Returns the recipients of a message as a comma-delimited String.
123      *
124      * @param message the <code>Message</code>
125      * @param type the recipient type
126      */

127     private String JavaDoc formatRecipients(Message JavaDoc message, Message.RecipientType JavaDoc type)
128     throws MessagingException JavaDoc {
129         Address JavaDoc[] recipients = message.getRecipients(type);
130         if (recipients == null || recipients.length == 0) {
131             return null;
132         }
133         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
134         for (int recipientIndex = 0; recipientIndex < recipients.length; ++recipientIndex) {
135             sb.append(recipients[recipientIndex].toString());
136             if (recipientIndex < recipients.length - 1) {
137                 // Separate recipient names with a comma.
138
sb.append(", ");
139             }
140         }
141         return sb.toString();
142     }
143     
144     /**
145      * Returns an array of components containing the parts of a multipart
146      * message.
147      *
148      * @param message The message to parse.
149      * @return An array of components, each containing a part of the message.
150      */

151     private Component[] renderMessageContent(Message JavaDoc message)
152     throws MessagingException JavaDoc {
153         try {
154             Object JavaDoc content = message.getContent();
155             if (content instanceof String JavaDoc) {
156                 // Content is a string, return it enclosed in a Label.
157
return new Component[]{renderMessageText((String JavaDoc) content)};
158             } else if (content instanceof Multipart JavaDoc) {
159                 // Content is multi-part, parse each part.
160
Multipart JavaDoc multipart = (Multipart JavaDoc) content;
161                 Component[] data = new Component[multipart.getCount()];
162                 // Iterate through parts.
163
for (int index = 0; index < data.length; ++index) {
164                     BodyPart JavaDoc part = multipart.getBodyPart(index);
165                     Object JavaDoc partContent = part.getContent();
166                     if (partContent instanceof String JavaDoc) {
167                         // Part content is a string, add it to returned array of Components as a Label.
168
data[index] = renderMessageText((String JavaDoc) partContent);
169                     } else {
170                         // Part content is not a string, add it to returned array as a Label containing its content type.
171
data[index] = new Label(part.getContentType());
172                     }
173                 }
174                 return data;
175             } else {
176                 // Unhandled type, should not generally occur.
177
return new Component[]{new Label(Messages.getString("Messages.UnableToParseError"))};
178             }
179         } catch (IOException JavaDoc ex) {
180             // Generally should not occur.
181
return new Component[]{new Label(Messages.getString("Messages.UnableToParseError"))};
182         }
183     }
184     
185     /**
186      * Renders a portion of message text into a <code>Component</code>s.
187      * If the text contains newlines, the returned <code>Component is
188      * a <code>Column</code> containing <code>Label</code>s. If the text
189      * does not contain newlines, a single <code>Label</code> is returned.
190      *
191      * @param text the text to render
192      * @return a <code>Component</code> representation of the text
193      */

194     private Component renderMessageText(String JavaDoc text) {
195         List JavaDoc componentList = new ArrayList JavaDoc();
196         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
197         CharacterIterator JavaDoc ci = new StringCharacterIterator JavaDoc(text);
198         char ch = ci.first();
199         while (ch != CharacterIterator.DONE) {
200             if (ch == '\n') {
201                 String JavaDoc labelText = out.toString();
202                 Label label = new Label(labelText);
203                 if (labelText.trim().length() == 0) {
204                     label.setLayoutData(SPACER_COLUMN_LAYOUT_DATA);
205                 }
206                 componentList.add(label);
207                 out = new StringBuffer JavaDoc();
208             } else if (ch >= 0x20) {
209                 out.append(ch);
210             }
211             ch = ci.next();
212         }
213         Label label = new Label(out.toString());
214         componentList.add(label);
215         
216         if (componentList.size() == 1) {
217             return label;
218         } else {
219             Column column = new Column();
220             Iterator JavaDoc it = componentList.iterator();
221             while (it.hasNext()) {
222                 column.add((Component) it.next());
223             }
224             return column;
225         }
226     }
227
228     /**
229      * Sets the displayed <code>Message</code>.
230      *
231      * @param message the <code>Message</code> to display
232      */

233     public void setMessage(Message JavaDoc message)
234     throws MessagingException JavaDoc {
235         if (message == null) {
236             setVisible(false);
237         } else {
238             setVisible(true);
239             updateRecipientData(message, Message.RecipientType.TO, toFieldPromptLabel, toFieldValueLabel);
240             updateRecipientData(message, Message.RecipientType.CC, ccFieldPromptLabel, ccFieldValueLabel);
241             updateRecipientData(message, Message.RecipientType.BCC, bccFieldPromptLabel, bccFieldValueLabel);
242             subjectFieldValueLabel.setText(MessageUtil.clean(message.getSubject(), -1, -1));
243         }
244         
245         messageColumn.removeAll();
246         if (message != null) {
247             Component[] messageComponents = renderMessageContent(message);
248             for (int i = 0; i < messageComponents.length; ++i) {
249                 messageColumn.add(messageComponents[i]);
250             }
251         }
252     }
253
254     /**
255      * Updates the visual presentation of recipient information,
256      * showing recipient types that are present and hiding those
257      * which are not.
258      */

259     private void updateRecipientData(Message JavaDoc message, Message.RecipientType JavaDoc type,
260             Label promptLabel, Label valueLabel)
261     throws MessagingException JavaDoc {
262         String JavaDoc recipients = MessageUtil.clean(formatRecipients(message, type), -1, -1);
263         if (recipients == null) {
264             promptLabel.setVisible(false);
265             valueLabel.setVisible(false);
266             valueLabel.setText(null);
267         } else {
268             promptLabel.setVisible(true);
269             valueLabel.setVisible(true);
270             valueLabel.setText(recipients);
271         }
272     }
273 }
274
Popular Tags