KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echo2example > email > ComposeWindow


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.util.StringTokenizer JavaDoc;
33
34 import javax.mail.Message JavaDoc;
35 import javax.mail.MessagingException JavaDoc;
36 import javax.mail.Transport JavaDoc;
37 import javax.mail.internet.AddressException JavaDoc;
38 import javax.mail.internet.InternetAddress JavaDoc;
39 import javax.mail.internet.MimeMessage JavaDoc;
40
41 import nextapp.echo2.app.Button;
42 import nextapp.echo2.app.Column;
43 import nextapp.echo2.app.Extent;
44 import nextapp.echo2.app.Grid;
45 import nextapp.echo2.app.Insets;
46 import nextapp.echo2.app.Label;
47 import nextapp.echo2.app.Row;
48 import nextapp.echo2.app.SplitPane;
49 import nextapp.echo2.app.TextArea;
50 import nextapp.echo2.app.TextField;
51 import nextapp.echo2.app.WindowPane;
52 import nextapp.echo2.app.event.ActionEvent;
53 import nextapp.echo2.app.event.ActionListener;
54 import nextapp.echo2.app.event.WindowPaneEvent;
55 import nextapp.echo2.app.event.WindowPaneListener;
56
57 /**
58  * The message composition window.
59  */

60 public class ComposeWindow extends WindowPane {
61     
62     private static final Extent DIALOG_OFFSET_X = new Extent(80);
63     private static final Extent DIALOG_OFFSET_Y = new Extent(26);
64     
65     /**
66      * Adds recipient e-mail addresses contained in a comma-delimited string
67      * to an outgoing e-mail <code>Message</code>.
68      *
69      * @param message the <code>Message</code> object to which the addresses
70      * will be added
71      * @param recipientType the type of recipient to which the addresses
72      * should be added (to, cc, or bcc)
73      * @param recipients a comma-delimited string of recipient addresses
74      */

75     private static void addRecipients(Message JavaDoc message, Message.RecipientType JavaDoc recipientType, String JavaDoc recipients)
76     throws AddressException JavaDoc, MessagingException JavaDoc {
77         if (recipients != null && recipients.trim().length() > 0) {
78             // Tokenize the recipient string based on commas.
79
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(recipients, ",");
80             while (tokenizer.hasMoreTokens()) {
81                 // Add each recipient.
82
String JavaDoc recipient = tokenizer.nextToken();
83                 message.addRecipient(recipientType, new InternetAddress JavaDoc(recipient));
84             }
85         }
86     }
87
88     private TextField toField;
89     private TextField ccField;
90     private TextField bccField;
91     private TextField subjectField;
92     private TextArea messageField;
93
94     /**
95      * Creates a new <code>ComposeWindow</code>.
96      *
97      * @param replyMessage the message being replied to, or null if composing
98      * a new message.
99      */

100     public ComposeWindow(Message JavaDoc replyMessage) {
101         super(Messages.getString("ComposeWindow.Title"), new Extent(600), new Extent(480));
102         setResizable(false);
103         setDefaultCloseOperation(WindowPane.DO_NOTHING_ON_CLOSE);
104         setStyleName("Default");
105         
106         addWindowPaneListener(new WindowPaneListener() {
107             public void windowPaneClosing(WindowPaneEvent e) {
108                 processDiscard();
109             }
110         });
111         
112         SplitPane mainPane = new SplitPane(SplitPane.ORIENTATION_VERTICAL, new Extent(32));
113         add(mainPane);
114         
115         Row controlPane = new Row();
116         controlPane.setStyleName("ControlPane");
117         mainPane.add(controlPane);
118         
119         Button sendButton = new Button(Messages.getString("ComposeWindow.SendButton"),
120                 Styles.ICON_24_YES);
121         sendButton.setStyleName("ControlPane.Button");
122         sendButton.addActionListener(new ActionListener() {
123             public void actionPerformed(ActionEvent e) {
124                 if (sendMessage()) {
125                     ((EmailApp) getApplicationInstance()).getDefaultWindow().getContent().remove(ComposeWindow.this);
126                 }
127             }
128         });
129         controlPane.add(sendButton);
130
131         Button cancelButton = new Button(Messages.getString("ComposeWindow.DiscardButton"),
132                 Styles.ICON_24_NO);
133         cancelButton.setStyleName("ControlPane.Button");
134         cancelButton.addActionListener(new ActionListener() {
135             public void actionPerformed(ActionEvent e) {
136                 processDiscard();
137             }
138         });
139         controlPane.add(cancelButton);
140
141         Column layoutColumn = new Column();
142         layoutColumn.setCellSpacing(new Extent(10));
143         layoutColumn.setInsets(new Insets(10));
144         mainPane.add(layoutColumn);
145         
146         Grid headerGrid = new Grid();
147         headerGrid.setInsets(new Insets(0, 2));
148         layoutColumn.add(headerGrid);
149         
150         Label label;
151         
152         label = new Label(Messages.getString("Message.PromptLabel.To"));
153         headerGrid.add(label);
154         
155         toField = new TextField();
156         toField.setStyleName("Default");
157         toField.setWidth(new Extent(450));
158         headerGrid.add(toField);
159         
160         label = new Label(Messages.getString("Message.PromptLabel.Cc"));
161         headerGrid.add(label);
162         
163         ccField = new TextField();
164         ccField.setStyleName("Default");
165         ccField.setWidth(new Extent(450));
166         headerGrid.add(ccField);
167         
168         label = new Label(Messages.getString("Message.PromptLabel.Bcc"));
169         headerGrid.add(label);
170         
171         bccField = new TextField();
172         bccField.setStyleName("Default");
173         bccField.setWidth(new Extent(450));
174         headerGrid.add(bccField);
175         
176         label = new Label(Messages.getString("Message.PromptLabel.Subject"));
177         headerGrid.add(label);
178         
179         subjectField = new TextField();
180         subjectField.setStyleName("Default");
181         subjectField.setWidth(new Extent(450));
182         headerGrid.add(subjectField);
183         
184         messageField = new TextArea();
185         messageField.setStyleName("Default");
186         messageField.setWidth(new Extent(520));
187         messageField.setHeight(new Extent(18, Extent.EM));
188         layoutColumn.add(messageField);
189         
190         if (replyMessage == null) {
191             EmailApp.getActive().setFocusedComponent(toField);
192         } else {
193             EmailApp.getActive().setFocusedComponent(messageField);
194             try {
195                 toField.setText(MessageUtil.clean(replyMessage.getFrom()[0].toString(), -1, -1));
196                 subjectField.setText(MessageUtil.clean(replyMessage.getSubject(), -1, -1));
197             } catch (MessagingException JavaDoc ex) {
198                 EmailApp.getApp().processFatalException(ex);
199             }
200         }
201     }
202     
203     /**
204      * Handles a user request to discard the message being composed.
205      */

206     private void processDiscard() {
207         if (messageField.getText().trim().length() > 0) {
208             MessageDialog confirmDialog = new MessageDialog(Messages.getString("ComposeWindow.ConfirmDiscard.Title"),
209                     Messages.getString("ComposeWindow.ConfirmDiscard.Message"), MessageDialog.TYPE_CONFIRM,
210                     MessageDialog.CONTROLS_YES_NO);
211             confirmDialog.setPositionX(Extent.add(getPositionX(), DIALOG_OFFSET_X));
212             confirmDialog.setPositionY(Extent.add(getPositionY(), DIALOG_OFFSET_Y));
213             confirmDialog.addActionListener(new ActionListener() {
214                 public void actionPerformed(ActionEvent e) {
215                     if (MessageDialog.COMMAND_OK.equals(e.getActionCommand())) {
216                         getParent().remove(ComposeWindow.this);
217                     }
218                 }
219             });
220             getApplicationInstance().getDefaultWindow().getContent().add(confirmDialog);
221         } else {
222             getParent().remove(this);
223         }
224     }
225
226     /**
227      * Sends the message.
228      * This message will display an error dialog if the message cannot be
229      * sent.
230      *
231      * @return True if the message was sent.
232      */

233     private boolean sendMessage() {
234         MimeMessage JavaDoc message = new MimeMessage JavaDoc(((EmailApp) getApplicationInstance()).getMailSession());
235         try {
236             // Validate.
237
String JavaDoc errorMessage = null;
238             if (toField.getText().length() == 0) {
239                 errorMessage = Messages.getString("ComposeWindow.SendError.NoToField");
240             } else if (subjectField.getText().length() == 0) {
241                 errorMessage = Messages.getString("ComposeWindow.SendError.NoSubjectField");
242             } else if (messageField.getText().length() == 0) {
243                 errorMessage = Messages.getString("ComposeWindow.SendError.NoMessageContent");
244             }
245             if (errorMessage != null) {
246                 MessageDialog messageDialog = new MessageDialog(Messages.getString("ComposeWindow.SendError.Title"),
247                         errorMessage, MessageDialog.TYPE_ERROR, MessageDialog.CONTROLS_OK);
248                 messageDialog.setPositionX(Extent.add(getPositionX(), DIALOG_OFFSET_X));
249                 messageDialog.setPositionY(Extent.add(getPositionY(), DIALOG_OFFSET_Y));
250                 getApplicationInstance().getDefaultWindow().getContent().add(messageDialog);
251                 return false;
252             }
253             
254             // Set sender address.
255
message.setFrom(new InternetAddress JavaDoc(((EmailApp) getApplicationInstance()).getEmailAddress()));
256             
257             // Add recipients contained in recipient text fields.
258
addRecipients(message, Message.RecipientType.TO, toField.getText());
259             addRecipients(message, Message.RecipientType.CC, ccField.getText());
260             addRecipients(message, Message.RecipientType.BCC, bccField.getText());
261             
262             // Set the subject and content of the message.
263
message.setSubject(subjectField.getText());
264             message.setText(messageField.getText());
265             
266             // Attempt to send the message.
267
if (!EmailApp.FAUX_MODE) {
268                 Transport.send(message);
269             }
270             return true;
271         } catch (AddressException JavaDoc ex) {
272             // Process an exception pertaining to an invalid recipient e-mail address: Raise an error.
273
MessageDialog messageDialog = new MessageDialog(Messages.getString("ComposeWindow.SendError.Title"),
274                     Messages.getString("ComposeWindow.SendError.InvalidAddress"), MessageDialog.TYPE_ERROR,
275                     MessageDialog.CONTROLS_OK);
276             messageDialog.setPositionX(Extent.add(getPositionX(), DIALOG_OFFSET_X));
277             messageDialog.setPositionY(Extent.add(getPositionY(), DIALOG_OFFSET_Y));
278             getApplicationInstance().getDefaultWindow().getContent().add(messageDialog);
279             return false;
280         } catch (MessagingException JavaDoc ex) {
281             // Handle a fatal exception.
282
EmailApp.getApp().processFatalException(ex);
283             return false;
284         }
285     }
286 }
287
Popular Tags