KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > jmail > base > ReadMessageFrame


1 package org.lucane.applications.jmail.base;
2
3 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * This file is part of JMail *
5  * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr> *
6  * *
7  * JMail is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * any later version. *
11  * *
12  * JMail is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License along *
18  * with JMail; if not, write to the Free Software Foundation, Inc., *
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20  * *
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.util.*;
26 import javax.mail.*;
27 import javax.mail.internet.*;
28 import javax.swing.*;
29
30 import org.lucane.applications.jmail.JMailPlugin;
31 import org.lucane.client.widgets.HTMLEditor;
32
33 /** Window displayed when reading a mail */
34 final class ReadMessageFrame extends JFrame
35 {
36     /** Language resource */
37     private ResourceBundle msgBundle;
38     
39     private JMenuBar menuBar;
40     
41     private JMenu fileMenu;
42     
43     private JMenuItem newMenuItem;
44     private JMenuItem moveMenuItem;
45     private JMenuItem copyMenuItem;
46     private JMenuItem deleteMenuItem;
47     private JMenuItem printMenuItem;
48     private JMenuItem closeMenuItem;
49     
50     private JMenu messageMenu;
51     private JMenuItem newMenuItem2;
52     private JMenuItem replyMenuItem;
53     private JMenuItem replyAllMenuItem;
54     private JMenuItem forwardMenuItem;
55     
56     private JPanel panel;
57     
58     private JToolBar toolBar;
59     
60     private JButton reply;
61     private JButton replyall;
62     private JButton forward;
63     private JButton print;
64     private JButton delete;
65     
66     private JLabel exp;
67     private JLabel dest;
68     private JLabel cc;
69     private JLabel date;
70     private JLabel subject;
71     
72     private JButton attachment;
73     
74     private HTMLEditor area;
75     private JPanel fields;
76     
77     private Profile profile;
78     
79     private Message mail;
80     
81     private ReadMessageListener listener;
82     
83     private MainPanel parent;
84     
85     private Store store;
86     private Folder currentFolder;
87     
88     private String JavaDoc content;
89     
90     private JMailPlugin plugin;
91     
92     /** Constructor
93      * @param store the connection
94      * @param currentFolder current folder
95      * @param parent the parent window
96      * @param profile user profile
97      * @param msg the mail to be read
98      * @param content mail body
99      * @param msgBungle language resource
100      * @param attachmentVisible tells wether the "Attachment" button has to be visible or not
101      */

102     public ReadMessageFrame(
103             JMailPlugin plugin,
104             Store store,
105             Folder currentFolder,
106             MainPanel parent,
107             Profile profile,
108             Message msg,
109             String JavaDoc content,
110             ResourceBundle msgBundle,
111             boolean attachmentVisible)
112     {
113         super();
114         this.plugin = plugin;
115         
116         this.msgBundle = msgBundle;
117         
118         this.content = content;
119         
120         this.store = store;
121         this.currentFolder = currentFolder;
122         
123         this.parent = parent;
124         
125         listener = new ReadMessageListener();
126         
127         String JavaDoc currentSubject = new String JavaDoc();
128         
129         try
130         {
131             currentSubject = msg.getSubject();
132         }
133         
134         catch (Exception JavaDoc e)
135         {
136             currentSubject = msgBundle.getString("ReadMessage.noSubjectLabel");
137         }
138         
139         if (currentSubject == null)
140             currentSubject = "";
141         
142         setTitle(currentSubject);
143         
144         this.profile = profile;
145         this.mail = msg;
146         
147         menuBar = new JMenuBar();
148         
149         fileMenu = new JMenu(msgBundle.getString("common.fileMenu"));
150         fileMenu.setMnemonic('F');
151         
152         newMenuItem = fileMenu.add(msgBundle.getString("ReadMessage.newMenuItemLabel"));
153         newMenuItem.addActionListener(listener);
154         
155         fileMenu.addSeparator();
156         
157         moveMenuItem = fileMenu.add(msgBundle.getString("ReadMessage.moveMenuItemLabel"));
158         moveMenuItem.setMnemonic('D');
159         moveMenuItem.addActionListener(listener);
160         
161         copyMenuItem = fileMenu.add(msgBundle.getString("ReadMessage.copyMenuItemLabel"));
162         copyMenuItem.addActionListener(listener);
163         
164         deleteMenuItem = fileMenu.add(msgBundle.getString("common.delete"));
165         deleteMenuItem.addActionListener(listener);
166         
167         fileMenu.addSeparator();
168         
169         printMenuItem = fileMenu.add(msgBundle.getString("common.print"));
170         printMenuItem.setEnabled(false);
171         printMenuItem.addActionListener(listener);
172         
173         fileMenu.addSeparator();
174         
175         closeMenuItem = fileMenu.add(msgBundle.getString("ReadMessage.closeMenuItemLabel"));
176         closeMenuItem.setMnemonic('F');
177         closeMenuItem.addActionListener(listener);
178         
179         fileMenu = menuBar.add(fileMenu);
180         
181         messageMenu = new JMenu(msgBundle.getString("common.mail"));
182         messageMenu.setMnemonic('M');
183         
184         newMenuItem2 = messageMenu.add(msgBundle.getString("common.new"));
185         newMenuItem2.addActionListener(listener);
186         
187         messageMenu.addSeparator();
188         
189         replyMenuItem = messageMenu.add(msgBundle.getString("common.reply"));
190         replyMenuItem.setAccelerator(KeyStroke.getKeyStroke('R', Event.CTRL_MASK));
191         replyMenuItem.setMnemonic('R');
192         replyMenuItem.addActionListener(listener);
193         
194         replyAllMenuItem = messageMenu.add(msgBundle.getString("common.replyAll"));
195         replyAllMenuItem.addActionListener(listener);
196         
197         forwardMenuItem = messageMenu.add(msgBundle.getString("ReadMessage.forwardMenuItemLabel"));
198         forwardMenuItem.addActionListener(listener);
199         
200         messageMenu = menuBar.add(messageMenu);
201         
202         setJMenuBar(menuBar);
203         
204         panel = new JPanel(new BorderLayout());
205         
206         toolBar = new JToolBar();
207         toolBar.setFloatable(true);
208         
209         reply = new JButton(plugin.getImageIcon("reply.png"));
210         reply.setToolTipText(msgBundle.getString("ReadMessage.replyLabel"));
211         reply.addActionListener(listener);
212         
213         toolBar.add(reply);
214         
215         replyall = new JButton(plugin.getImageIcon("replyall.png"));
216         replyall.setToolTipText(msgBundle.getString("common.replyAll"));
217         replyall.addActionListener(listener);
218         
219         toolBar.add(replyall);
220         
221         forward = new JButton(plugin.getImageIcon("forward.png"));
222         forward.setToolTipText(msgBundle.getString("ReadMessage.forwardLabel"));
223         forward.addActionListener(listener);
224         
225         toolBar.add(forward);
226         
227         toolBar.addSeparator();
228         
229         print = new JButton(plugin.getImageIcon("print.png"));
230         print.setEnabled(false);
231         print.setToolTipText(msgBundle.getString("ReadMessage.printLabel"));
232         print.addActionListener(listener);
233         
234         toolBar.add(print);
235         
236         delete = new JButton(plugin.getImageIcon("delete.png"));
237         delete.setToolTipText(msgBundle.getString("ReadMessage.deleteLabel"));
238         delete.addActionListener(listener);
239         
240         toolBar.add(delete);
241         
242         panel.add("North", toolBar);
243         
244         fields = new JPanel();
245         fields.setLayout(new BoxLayout(fields, BoxLayout.Y_AXIS));
246         
247         try
248         {
249             Address a[] = mail.getFrom();
250             exp = new JLabel(msgBundle.getString("ReadMessage.expLabel") + " " + a[0].toString());
251             fields.add(exp);
252             
253             a = mail.getRecipients(Message.RecipientType.TO);
254             
255             StringBuffer JavaDoc to = new StringBuffer JavaDoc(msgBundle.getString("ReadMessage.toLabel") + " ");
256             
257             if (a != null)
258             {
259                 to.append(a[0].toString());
260                 
261                 for (int i = 1; i < a.length; i++)
262                     to.append(", " + a[i].toString());
263             }
264             
265             dest = new JLabel(to.toString());
266             
267             fields.add(dest);
268             
269             a = mail.getRecipients(Message.RecipientType.CC);
270             
271             StringBuffer JavaDoc ccString = new StringBuffer JavaDoc("CC : ");
272             
273             if (a != null)
274             {
275                 ccString.append(a[0].toString());
276                 
277                 for (int i = 1; i < a.length; i++)
278                     ccString.append(", " + a[i].toString());
279                 
280                 cc = new JLabel(ccString.toString());
281                 fields.add(cc);
282             }
283             
284             Date d = mail.getSentDate();
285             
286             if (d != null)
287                 date = new JLabel(msgBundle.getString("ReadMessage.dateLabel") + " " + d.toString());
288             
289             else
290                 date = new JLabel(msgBundle.getString("ReadMessage.dateLabel"));
291             
292             fields.add(date);
293             
294             subject = new JLabel(msgBundle.getString("ReadMessage.subjectLabel") + " " + currentSubject);
295             fields.add(subject);
296             
297             area = new HTMLEditor();
298             area.setToolbarVisible(false);
299             area.setEditable(false);
300             
301             if (attachmentVisible)
302             {
303                 attachment = new JButton(msgBundle.getString("ReadMessage.attachmentLabel"));
304                 attachment.addActionListener(listener);
305                 fields.add(attachment);
306             }
307             
308             area.setText(content);
309             area.getEditorPane().setCaretPosition(0);
310             fields.add(area);
311         }
312         
313         catch (AddressException addressEx)
314         {
315             addressEx.printStackTrace();
316         }
317         
318         catch (Exception JavaDoc e)
319         {
320             e.printStackTrace();
321         }
322         
323         panel.add("Center", fields);
324         
325         setContentPane(panel);
326         
327         setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
328         setSize(500, 480);
329         
330         setVisible(true);
331     }
332     
333     /** Listener for this class */
334     private final class ReadMessageListener implements ActionListener
335     {
336         /** This method is invoked when an event is triggered
337          * @param e event
338          */

339         public final void actionPerformed(ActionEvent e)
340         {
341             AbstractButton b = (AbstractButton) e.getSource();
342             
343             if (b == newMenuItem || b == newMenuItem2)
344             {
345                 SendMessageDialog sm = new SendMessageDialog(plugin, profile, msgBundle);
346                 sm.dispose();
347             }
348             
349             else if (b == moveMenuItem)
350             {
351                 String JavaDoc folder = new String JavaDoc();
352                 
353                 try
354                 {
355                     folder = mail.getFolder().toString();
356                     new MoveMessageFrame(
357                             store,
358                             currentFolder,
359                             profile,
360                             folder,
361                             ((MimeMessage) mail).getMessageID(),
362                             0,
363                             msgBundle);
364                     parent.update();
365                     dispose();
366                 }
367                 
368                 catch (Exception JavaDoc ex)
369                 {
370                     ex.printStackTrace();
371                 }
372             }
373             
374             else if (b == copyMenuItem)
375             {
376                 String JavaDoc folder = new String JavaDoc();
377                 
378                 try
379                 {
380                     folder = mail.getFolder().toString();
381                     
382                     new MoveMessageFrame(
383                             store,
384                             currentFolder,
385                             profile,
386                             folder,
387                             ((MimeMessage) mail).getMessageID(),
388                             1,
389                             msgBundle);
390                 }
391                 
392                 catch (Exception JavaDoc ex)
393                 {
394                     ex.printStackTrace();
395                 }
396             }
397             
398             else if (b == deleteMenuItem || b == delete)
399             {
400                 try
401                 {
402                     boolean bool = MailClient.deleteMessage(store, currentFolder, ((MimeMessage) mail).getMessageID());
403                     
404                     if (bool)
405                     {
406                         JOptionPane.showMessageDialog(
407                                 null,
408                                 msgBundle.getString("ReadMessage.msgDeletedLabel"),
409                                 "ReadMessage",
410                                 JOptionPane.INFORMATION_MESSAGE);
411                         parent.update();
412                         dispose();
413                     }
414                 }
415                 
416                 catch (Exception JavaDoc ex)
417                 {
418                     ex.printStackTrace();
419                 }
420             }
421             
422             else if (b == printMenuItem || b == print)
423             {
424                 /** TODO : print code */
425             }
426             
427             else if (b == closeMenuItem)
428                 dispose();
429             
430             else if (b == replyMenuItem || b == reply)
431             {
432                 try
433                 {
434                     Address a[] = mail.getReplyTo();
435                     
436                     String JavaDoc currentSubject = mail.getSubject();
437                     
438                     if (currentSubject == null)
439                         currentSubject = "";
440                     
441                     currentSubject = "Re: " + currentSubject;
442                     
443                     String JavaDoc content = new String JavaDoc();
444                     
445                     if (mail.isMimeType("text/plain"))
446                         content = (String JavaDoc) mail.getContent();
447                     
448                     else
449                         content = msgBundle.getString("ReadMessage.unreadableMsgLabel");
450                     
451                     SendMessageDialog sm =
452                         new SendMessageDialog(plugin, profile, a[0].toString(), currentSubject, content, msgBundle);
453                     sm.dispose();
454                 }
455                 
456                 catch (Exception JavaDoc ex)
457                 {
458                     ex.printStackTrace();
459                 }
460             }
461             
462             else if (b == replyAllMenuItem || b == replyall)
463             {
464                 try
465                 {
466                     Address a[] = mail.getReplyTo();
467                     
468                     String JavaDoc currentSubject = mail.getSubject();
469                     
470                     if (currentSubject == null)
471                         currentSubject = "";
472                     
473                     currentSubject = "Re: " + currentSubject;
474                     
475                     String JavaDoc content = new String JavaDoc();
476                     
477                     if (mail.isMimeType("text/plain"))
478                         content = (String JavaDoc) mail.getContent();
479                     
480                     else
481                         content = msgBundle.getString("ReadMessage.unreadableMsgLabel");
482                     
483                     Address to[] = mail.getRecipients(Message.RecipientType.TO);
484                     
485                     Address cc[] = mail.getRecipients(Message.RecipientType.CC);
486                     
487                     String JavaDoc copies[] = new String JavaDoc[0];
488                     
489                     if (cc != null)
490                     {
491                         if (to != null)
492                         {
493                             copies = new String JavaDoc[to.length + cc.length];
494                             
495                             int i;
496                             
497                             for (i = 0; i < to.length; i++)
498                                 copies[i] = to[i].toString();
499                             
500                             for (int j = 0; j < cc.length; j++)
501                                 copies[i++] = cc[j].toString();
502                         }
503                         
504                         else
505                         {
506                             copies = new String JavaDoc[cc.length];
507                             
508                             for (int i = 0; i < cc.length; i++)
509                                 copies[i] = cc[i].toString();
510                         }
511                     }
512                     
513                     else if (to != null)
514                     {
515                         copies = new String JavaDoc[to.length];
516                         
517                         for (int i = 0; i < to.length; i++)
518                             copies[i] = to[i].toString();
519                     }
520                     
521                     SendMessageDialog sm =
522                         new SendMessageDialog(
523                                 plugin,
524                                 profile,
525                                 a[0].toString(),
526                                 copies,
527                                 currentSubject,
528                                 content,
529                                 msgBundle);
530                     sm.dispose();
531                 }
532                 
533                 catch (Exception JavaDoc ex)
534                 {
535                     ex.printStackTrace();
536                 }
537             }
538             
539             else if (b == forwardMenuItem || b == forward)
540             {
541                 try
542                 {
543                     String JavaDoc currentSubject = mail.getSubject();
544                     
545                     if (currentSubject == null)
546                         currentSubject = "";
547                     
548                     currentSubject = "Fwd: " + currentSubject;
549                     
550                     String JavaDoc content = new String JavaDoc();
551                     
552                     if (mail.isMimeType("text/plain"))
553                         content = (String JavaDoc) mail.getContent();
554                     else
555                         content = msgBundle.getString("ReadMessage.unreadableMsgLabel");
556                     
557                     SendMessageDialog sm = new SendMessageDialog(plugin, profile, currentSubject, content, msgBundle);
558                     sm.dispose();
559                 }
560                 
561                 catch (Exception JavaDoc ex)
562                 {
563                     ex.printStackTrace();
564                 }
565             }
566             
567             else if (b == attachment)
568                 new AttachmentFrame(mail, msgBundle);
569         }
570     }
571 }
572
Popular Tags