KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.*;
26 import java.text.*;
27 import java.util.*;
28 import javax.mail.*;
29 import javax.mail.event.*;
30 import javax.mail.internet.*;
31 import javax.swing.*;
32 import javax.swing.event.*;
33 import javax.swing.tree.*;
34
35 import org.lucane.applications.jmail.JMailPlugin;
36 import org.lucane.client.widgets.HTMLEditor;
37
38 /** The main panel of JMail */
39 final class MainPanel extends JPanel {
40     /** Language resource */
41     private ResourceBundle msgBundle;
42     
43     /** Horizontal split */
44     private JSplitPane splitPane1;
45     
46     /** Vertical split */
47     private JSplitPane splitPane2;
48     
49     private JToolBar toolBar;
50     
51     private JButton verify;
52     
53     private JButton send;
54     private JButton reply;
55     private JButton replyall;
56     private JButton forward;
57     private JButton print;
58     private JButton delete;
59     
60     private JScrollPane foldersPane;
61     private JTree foldersTree;
62     
63     private JScrollPane subjectsPane;
64     private SortableTable subjectsTable;
65     
66     private Object JavaDoc msgList[][];
67     
68     /** Columns names */
69     private String JavaDoc columns[];
70     
71     private ListSelectionModel listSelect;
72     
73     private Message mails[];
74     private Message current;
75     
76     /** Panel used when a mail is selected */
77     private JPanel readPanel;
78     
79     private JLabel from;
80     private JLabel subject;
81     
82     private JButton attachment;
83     
84     /** Textarea for the mail body */
85     private HTMLEditor area;
86     
87     private JScrollPane readPane;
88     
89     /** Mail body */
90     private StringBuffer JavaDoc content;
91     
92     /** Current folder */
93     private String JavaDoc folder;
94     
95     private JMailWindow parent;
96     
97     /** User profile */
98     private Profile profile;
99     
100     private DateFormat df;
101     //private FormattedDateRenderer renderer;
102

103     private MainPanelListener listener;
104     
105     private JPopupMenu folderPopup;
106     private JMenuItem folderNewItem;
107     private JMenuItem folderDeleteItem;
108     
109     private JPopupMenu msgPopup;
110     private JMenuItem msgOpenItem;
111     private JMenuItem msgSourceItem;
112     private JMenuItem msgSaveItem;
113     private JMenuItem msgPrintItem;
114     private JMenuItem msgReplyItem;
115     private JMenuItem msgReplyallItem;
116     private JMenuItem msgForwardItem;
117     private JMenuItem msgMoveItem;
118     private JMenuItem msgCopyItem;
119     private JMenuItem msgDeleteItem;
120     
121     private String JavaDoc currentId;
122     
123     /** The connection to the mail server */
124     private Store store;
125     
126     private Folder currentFolder;
127     
128     private JMailPlugin plugin;
129     
130     /**
131      * Constructor
132      *
133      * @param window
134      * windows which owns this panel
135      * @param profile
136      * user profile
137      * @param msgBundle
138      * language resource
139      */

140     protected MainPanel(
141             JMailPlugin plugin,
142             JMailWindow window,
143             Profile profile,
144             ResourceBundle msgBundle) {
145         this.plugin = plugin;
146         this.msgBundle = msgBundle;
147         
148         listener = new MainPanelListener();
149         
150         currentId = new String JavaDoc();
151         
152         setLayout(new BorderLayout());
153         
154         df = DateFormat.getInstance();
155         df.setLenient(true);
156         
157         parent = window;
158         this.profile = profile;
159         
160         /** Build the toolbar */
161         toolBar = new JToolBar();
162         toolBar.setFloatable(true);
163         
164         verify = new JButton(plugin.getImageIcon("check.png"));
165         verify.setToolTipText(msgBundle.getString("MainPanel.verifyLabel"));
166         verify.addActionListener(listener);
167         
168         toolBar.add(verify);
169         
170         send = new JButton(plugin.getImageIcon("new.png"));
171         send.setToolTipText(msgBundle.getString("MainPanel.sendLabel"));
172         send.addActionListener(listener);
173         
174         toolBar.add(send);
175         
176         reply = new JButton(plugin.getImageIcon("reply.png"));
177         reply.setToolTipText(msgBundle.getString("MainPanel.replyLabel"));
178         reply.setEnabled(false);
179         reply.addActionListener(listener);
180         
181         toolBar.add(reply);
182         
183         replyall = new JButton(plugin.getImageIcon("replyall.png"));
184         replyall.setToolTipText(msgBundle.getString("common.replyAll"));
185         replyall.setEnabled(false);
186         replyall.addActionListener(listener);
187         
188         toolBar.add(replyall);
189         
190         forward = new JButton(plugin.getImageIcon("forward.png"));
191         forward.setToolTipText(msgBundle.getString("MainPanel.forwardLabel"));
192         forward.setEnabled(false);
193         forward.addActionListener(listener);
194         
195         toolBar.add(forward);
196         
197         toolBar.addSeparator();
198         
199         print = new JButton(plugin.getImageIcon("print.png"));
200         print.setToolTipText(msgBundle.getString("MainPanel.printLabel"));
201         print.setEnabled(false);
202         
203         toolBar.add(print);
204         
205         delete = new JButton(plugin.getImageIcon("delete.png"));
206         delete.setToolTipText(msgBundle.getString("MainPanel.deleteLabel"));
207         delete.setEnabled(false);
208         delete.addActionListener(listener);
209         
210         toolBar.add(delete);
211         
212         add("North", toolBar);
213         
214         currentFolder = null;
215         store = MailClient.connect(profile);
216         
217         if (store == null) {
218             DefaultMutableTreeNode top = new DefaultMutableTreeNode();
219             top.add(new DefaultMutableTreeNode());
220             foldersTree = new JTree(top);
221         } else
222             foldersTree = MailClient.getFolders(store, profile);
223         
224         foldersTree.setCellRenderer(new FolderRenderer(plugin));
225         int nb = foldersTree.getRowCount();
226         
227         for (int i = 0; i < nb; i++)
228             foldersTree.expandRow(i);
229         
230         foldersTree.getSelectionModel().setSelectionMode(
231                 TreeSelectionModel.SINGLE_TREE_SELECTION);
232         
233         if (store != null)
234             foldersTree.addTreeSelectionListener(listener);
235         
236         foldersTree.putClientProperty("JTree.lineStyle", "Angled");
237         foldersPane = new JScrollPane(foldersTree);
238         foldersPane.setPreferredSize(new Dimension(150, 500));
239         
240         foldersTree.addMouseListener(listener);
241         
242         folderPopup = new JPopupMenu();
243         
244         folderNewItem =
245             new JMenuItem(msgBundle.getString("MainPanel.folderNewItemLabel"));
246         folderNewItem.addActionListener(listener);
247         folderPopup.add(folderNewItem);
248         
249         folderDeleteItem =
250             new JMenuItem(
251                     msgBundle.getString("common.delete"));
252         folderDeleteItem.addActionListener(listener);
253         folderPopup.add(folderDeleteItem);
254         
255         if (profile.getType().compareTo("pop3") == 0) {
256             folderDeleteItem.setEnabled(false);
257             folderNewItem.setEnabled(false);
258         }
259         
260         TreeModel model = foldersTree.getModel();
261         DefaultMutableTreeNode firstChild =
262             (DefaultMutableTreeNode) model.getChild(model.getRoot(), 0);
263         folder = (String JavaDoc) firstChild.getUserObject();
264         
265         /** Build the table */
266         columns = new String JavaDoc[3];
267         columns[0] = msgBundle.getString("MainPanel.columns[0]Label");
268         columns[1] = msgBundle.getString("MainPanel.columns[1]Label");
269         columns[2] = msgBundle.getString("MainPanel.columns[2]Label");
270         
271         subjectsTable = new SortableTable();
272         subjectsTable.setDefaultRenderer(
273                 Object JavaDoc.class,
274                 new ColorableCellRenderer(df, this));
275         subjectsTable.setData(new Object JavaDoc[0][0], columns);
276         
277         subjectsTable.addListSelectionListener(listener);
278         subjectsTable.addMouseListener(listener);
279         
280         msgPopup = new JPopupMenu();
281         
282         msgOpenItem =
283             new JMenuItem(msgBundle.getString("common.open"));
284         msgOpenItem.addActionListener(listener);
285         msgPopup.add(msgOpenItem);
286         
287         msgSourceItem =
288             new JMenuItem(msgBundle.getString("MainPanel.msgSourceItemLabel"));
289         msgSourceItem.addActionListener(listener);
290         msgPopup.add(msgSourceItem);
291         
292         msgSaveItem =
293             new JMenuItem(msgBundle.getString("common.save"));
294         msgSaveItem.addActionListener(listener);
295         msgPopup.add(msgSaveItem);
296         
297         msgPrintItem =
298             new JMenuItem(msgBundle.getString("common.print"));
299         msgPrintItem.setEnabled(false);
300         msgPopup.add(msgPrintItem);
301         
302         msgPopup.addSeparator();
303         
304         msgReplyItem =
305             new JMenuItem(msgBundle.getString("common.reply"));
306         msgReplyItem.addActionListener(listener);
307         msgPopup.add(msgReplyItem);
308         
309         msgReplyallItem =
310             new JMenuItem(
311                     msgBundle.getString("common.replyAll"));
312         msgReplyallItem.addActionListener(listener);
313         msgPopup.add(msgReplyallItem);
314         
315         msgForwardItem =
316             new JMenuItem(msgBundle.getString("MainPanel.msgForwardItemLabel"));
317         msgForwardItem.addActionListener(listener);
318         msgPopup.add(msgForwardItem);
319         
320         msgPopup.addSeparator();
321         
322         msgMoveItem =
323             new JMenuItem(msgBundle.getString("MainPanel.msgMoveItemLabel"));
324         msgMoveItem.addActionListener(listener);
325         msgPopup.add(msgMoveItem);
326         
327         msgCopyItem =
328             new JMenuItem(msgBundle.getString("MainPanel.msgCopyItemLabel"));
329         msgCopyItem.addActionListener(listener);
330         msgPopup.add(msgCopyItem);
331         
332         msgDeleteItem =
333             new JMenuItem(msgBundle.getString("common.delete"));
334         msgDeleteItem.addActionListener(listener);
335         msgPopup.add(msgDeleteItem);
336         
337         subjectsPane = new JScrollPane(subjectsTable);
338         subjectsPane.setPreferredSize(new Dimension(100, 100));
339         
340         readPanel = new JPanel();
341         readPanel.setLayout(new BoxLayout(readPanel, BoxLayout.Y_AXIS));
342         from = new JLabel(msgBundle.getString("MainPanel.fromLabel"));
343         readPanel.add(from);
344         subject = new JLabel(msgBundle.getString("MainPanel.subjectLabel"));
345         readPanel.add(subject);
346         
347         attachment =
348             new JButton(msgBundle.getString("MainPanel.attachmentLabel"));
349         attachment.setVisible(false);
350         attachment.addActionListener(listener);
351         readPanel.add(attachment);
352         
353         content = new StringBuffer JavaDoc(msgBundle.getString("MainPanel.noMsgLabel"));
354         area = new HTMLEditor();
355         area.setEditable(false);
356         area.setToolbarVisible(false);
357         area.setText(content.toString());
358         area.setEditable(false);
359         readPane = new JScrollPane(area);
360         readPanel.add(readPane);
361         
362         splitPane1 =
363             new JSplitPane(JSplitPane.VERTICAL_SPLIT, subjectsPane, readPanel);
364         splitPane1.setOneTouchExpandable(true);
365         splitPane1.setContinuousLayout(true);
366         
367         splitPane2 =
368             new JSplitPane(
369                     JSplitPane.HORIZONTAL_SPLIT,
370                     foldersPane,
371                     splitPane1);
372         splitPane2.setOneTouchExpandable(true);
373         splitPane2.setContinuousLayout(true);
374         
375         add("Center", splitPane2);
376         
377         
378         foldersTree.setSelectionRow(1);
379     }
380     
381     /** Disconnects from the mail server */
382     protected final void disconnect() {
383         try {
384             if (currentFolder != null)
385                 if (currentFolder.isOpen())
386                     currentFolder.close(true);
387                 
388             if (store != null)
389                 store.close();
390         } catch (Exception JavaDoc ex) {
391             ex.printStackTrace();
392         }
393     }
394     
395     /** Display the selected mail */
396     private void displayMsg() {
397         setButtonsEnabled(true);
398         
399         String JavaDoc id = (String JavaDoc) subjectsTable.getSelectedRowIndex();
400         
401         if (id == null) {
402             content = new StringBuffer JavaDoc("*IDless* message :-(");
403             area.setText(content.toString());
404             
405             area.getEditorPane().setCaretPosition(0);
406             
407             current = mails[0];
408             
409             return;
410         }
411         
412         try {
413             String JavaDoc currentSubject = null;
414             Address a[];
415             
416             for (int i = 0; i < mails.length; i++) {
417                 currentId = ((MimeMessage) mails[i]).getMessageID();
418                 
419                 if (currentId == null)
420                     continue;
421                 
422                 if (id.compareTo(currentId) == 0) {
423                     current = mails[i];
424                     currentSubject = current.getSubject();
425                     
426                     if (currentSubject == null)
427                         currentSubject = new String JavaDoc();
428                     
429                     subject.setText(
430                             msgBundle.getString("MainPanel.subjectLabel")
431                             + " "
432                             + currentSubject);
433                     
434                     a = current.getFrom();
435                     
436                     if (a != null && a[0] != null)
437                         from.setText(
438                                 msgBundle.getString("MainPanel.fromLabel")
439                                 + " "
440                                 + a[0].toString());
441                     
442                     break;
443                 }
444             }
445         } catch (Exception JavaDoc e) {
446             e.printStackTrace();
447         }
448         
449         attachment.setVisible(false);
450         content = getMailContent(current);
451         System.out.println("displayMsg: " + content);
452         
453         area.setText(content.toString());
454         area.getEditorPane().setCaretPosition(0);
455         
456         parent.setMenuItemsEnabled(true);
457     }
458     
459     boolean isFlagSet(int row, Flags.Flag flag) {
460         boolean b = false;
461         
462         try {
463             b = mails[row].isSet(flag);
464         } catch (Exception JavaDoc e) {
465             e.printStackTrace();
466         }
467         
468         return (b);
469     }
470     
471     void checkConnect() {
472         try {
473             if (current != null)
474                 current.getContent();
475         } catch (Exception JavaDoc e) {
476             try {
477                 if (currentFolder != null)
478                     if (currentFolder.isOpen())
479                         currentFolder.close(true);
480                     
481                 if (store != null) {
482                     currentFolder = store.getFolder(folder);
483                     currentFolder.open(Folder.READ_WRITE);
484                     
485                     currentFolder.addMessageCountListener(listener);
486                 }
487             } catch (FolderNotFoundException exception) {
488                 JOptionPane.showMessageDialog(
489                         null,
490                         exception.getMessage(),
491                         "FolderNotFoundException",
492                         JOptionPane.ERROR_MESSAGE);
493                 
494                 TreePath path =
495                     new TreePath(new DefaultMutableTreeNode("INBOX"));
496                 foldersTree.setSelectionPath(path);
497                 
498                 return;
499             } catch (ReadOnlyFolderException readException) {
500                 try {
501                     currentFolder.open(Folder.READ_ONLY);
502                 } catch (Exception JavaDoc ex) {
503                     ex.printStackTrace();
504                 }
505             } catch (MessagingException me) {
506                 JOptionPane.showMessageDialog(
507                         null,
508                         me.getMessage(),
509                         "MessagingException",
510                         JOptionPane.ERROR_MESSAGE);
511                 return;
512             } catch (Exception JavaDoc ex) {
513                 ex.printStackTrace();
514             }
515         }
516     }
517     
518     private String JavaDoc cleanHtml(String JavaDoc base)
519     {
520         int body = base.toLowerCase().indexOf("<body>");
521         if(body > 0)
522             base = "<html>" + base.substring(body);
523         
524         return base;
525     }
526     
527     /**
528      * Recursive function to get a mail part's content
529      *
530      * @param current
531      * part to be examined
532      * @return content of this part
533      */

534     protected final StringBuffer JavaDoc getMailContent(Part current) {
535         checkConnect();
536         
537         StringBuffer JavaDoc content2 = new StringBuffer JavaDoc();
538         
539         try {
540             if (current.isMimeType("text/plain"))
541                 content2.append(((String JavaDoc) current.getContent()).replaceAll("\\n", "<br>"));
542             
543             else if (current.isMimeType("text/html")) {
544                 String JavaDoc text = (String JavaDoc)current.getContent();
545                 text = cleanHtml(text);
546
547                 content2.append(text);
548             } else if (current.isMimeType("multipart/alternative")) {
549                 Multipart mp = (Multipart) current.getContent();
550                 BodyPart bp = null;
551                 
552                 String JavaDoc text = "";
553                 for (int i = 0; i < mp.getCount(); i++) {
554                     bp = mp.getBodyPart(i);
555                     
556                     if(bp.isMimeType("text/html")) {
557                         text = (String JavaDoc)bp.getContent();
558                         text = cleanHtml(text);
559                     }
560                     else if (bp.isMimeType("text/plain") && text.length() == 0) {
561                         text = ((String JavaDoc)bp.getContent()).replaceAll("\\n", "<br>");
562                     }
563                     else
564                         attachment.setVisible(true);
565                 }
566                 content2.append(text);
567             } else if (
568                     current.isMimeType("multipart/mixed")
569                     || current.isMimeType("multipart/related")
570                     || current.isMimeType("multipart/signed")) {
571                 boolean done = false;
572                 
573                 Multipart mp = (Multipart) current.getContent();
574                 BodyPart bp0 = mp.getBodyPart(0);
575                 content2.append(getMailContent(bp0));
576                 
577                 BodyPart bp = null;
578                 
579                 for (int i = 1; i < mp.getCount(); i++) {
580                     bp = mp.getBodyPart(i);
581                     
582                     if (bp.isMimeType("message/rfc822")
583                             || bp.isMimeType("multipart/alternative")) {
584                         if (bp.isMimeType("message/rfc822")) {
585                             content2.append(
586                                     "\n\n"
587                                     + msgBundle.getString(
588                                     "MainPanel.forwardedMail")
589                                     + "\n");
590                             content2.append(
591                                     getMailContent((Message) bp.getContent()));
592                         }
593                         
594                         if (bp.isMimeType("multipart/alternative"))
595                             content2.append(getMailContent(bp));
596                     } else
597                         attachment.setVisible(true);
598                 }
599             } else if (current.isMimeType("message/rfc822")) {
600                 content2.append(
601                         "\n\n"
602                         + msgBundle.getString("MainPanel.forwardedMail")
603                         + "\n");
604                 content2.append(getMailContent((Message) current.getContent()));
605             } else if (current.isMimeType("multipart/report")) {
606                 boolean done = false;
607                 
608                 Multipart mp = (Multipart) current.getContent();
609                 
610                 BodyPart bp0 = mp.getBodyPart(0);
611                 
612                 content2.append(getMailContent(bp0));
613                 
614                 BodyPart bp = null;
615                 
616                 for (int i = 1; i < mp.getCount(); i++) {
617                     bp = mp.getBodyPart(i);
618                     
619                     if (bp.isMimeType("message/rfc822")
620                             || bp.isMimeType("multipart/alternative")) {
621                         if (bp.isMimeType("message/rfc822")) {
622                             content2.append(
623                                     "\n\n"
624                                     + msgBundle.getString(
625                                     "MainPanel.forwardedMail")
626                                     + "\n");
627                             content2.append(
628                                     getMailContent((Message) bp.getContent()));
629                         }
630                         
631                         if (bp.isMimeType("multipart/alternative"))
632                             content2.append(getMailContent(bp));
633                     }
634                 }
635             } else
636                 content2.append(
637                         msgBundle.getString("MainPanel.unreadableMsgLabel"));
638         } catch (UnsupportedEncodingException encodE) {
639             JOptionPane.showMessageDialog(
640                     null,
641                     encodE.getMessage(),
642                     "UnsupportedEncodingException",
643                     JOptionPane.ERROR_MESSAGE);
644             content2.append(
645                     msgBundle.getString("MainPanel.unreadableMsgLabel"));
646         } catch (Exception JavaDoc e) {
647             e.printStackTrace();
648         }
649         
650         return (content2);
651     }
652     
653     /** Read the selected mail */
654     private void read() {
655         new ReadMessageFrame(
656                 plugin,
657                 store,
658                 currentFolder,
659                 this,
660                 profile,
661                 current,
662                 getMailContent(current).toString(),
663                 msgBundle,
664                 attachment.isVisible());
665     }
666     
667     /** Delete the selected mail */
668     protected final void delete() {
669         try {
670             Object JavaDoc[] indexes = subjectsTable.getSelectedRowsIndexes();
671             
672             boolean b;
673             
674             for (int i = 0; i < indexes.length; i++)
675                 b =
676                     MailClient.deleteMessage(
677                             store,
678                             currentFolder,
679                             (String JavaDoc) indexes[i]);
680             
681             update();
682             
683             content =
684                 new StringBuffer JavaDoc(msgBundle.getString("MainPanel.noMsgLabel"));
685             area.setText(content.toString());
686             
687             /*
688              * boolean b = MailClient.deleteMessage(store, currentFolder,
689              * currentId);
690              *
691              * if(b) { JOptionPane.showMessageDialog(null,
692              * msgBundle.getString("MainPanel.msgDeletedLabel"), "MainPanel",
693              * JOptionPane.INFORMATION_MESSAGE); update();
694              *
695              * content = new
696              * StringBuffer(msgBundle.getString("MainPanel.noMsgLabel"));
697              * area.setText(content.toString()); }
698              */

699         } catch (Exception JavaDoc e) {
700             e.printStackTrace();
701         }
702     }
703     
704     /** Brings the "New Folder" dialog */
705     protected final void newFolder() {
706         DefaultMutableTreeNode n =
707             (DefaultMutableTreeNode) foldersTree.getLastSelectedPathComponent();
708         
709         if (n != null) {
710             TreePath t = new TreePath(n);
711             int row = foldersTree.getRowForPath(t);
712             
713             new NewFolderFrame(store, profile, row, msgBundle);
714         } else
715             new NewFolderFrame(store, profile, msgBundle);
716         
717         foldersTree.treeDidChange();
718     }
719     
720     /** Update the table */
721     protected final void update() {
722         DefaultMutableTreeNode n =
723             (DefaultMutableTreeNode) foldersTree.getLastSelectedPathComponent();
724         
725         if(n != null && n.isRoot())
726             return;
727         
728         if (n == null)
729         {
730             TreePath path = new TreePath(new DefaultMutableTreeNode("INBOX"));
731             foldersTree.setSelectionPath(path);
732             
733             n =
734                 (DefaultMutableTreeNode) foldersTree
735                 .getLastSelectedPathComponent();
736         }
737         
738         folder = n.toString();
739         
740         mails = null;
741         
742         try {
743             if (currentFolder != null)
744             {
745                 try {
746                     if (currentFolder.isOpen())
747                         currentFolder.close(true);
748                 } catch(Exception JavaDoc e) {
749                     //probably already closed, race condition...
750
}
751             }
752             
753             if (store != null) {
754                 currentFolder = store.getFolder(folder);
755                 if(currentFolder.getType() == Folder.HOLDS_FOLDERS)
756                     return;
757                 
758                 currentFolder.open(Folder.READ_WRITE);
759                 currentFolder.addMessageCountListener(listener);
760                 mails = currentFolder.getMessages();
761             }
762         } catch (FolderNotFoundException exception) {
763             JOptionPane.showMessageDialog(
764                     null,
765                     exception.getMessage(),
766                     "FolderNotFoundException",
767                     JOptionPane.ERROR_MESSAGE);
768             
769             TreePath path = new TreePath(new DefaultMutableTreeNode("INBOX"));
770             foldersTree.setSelectionPath(path);
771             
772             n =
773                 (DefaultMutableTreeNode) foldersTree
774                 .getLastSelectedPathComponent();
775             
776             return;
777         } catch (ReadOnlyFolderException readException) {
778             try {
779                 currentFolder.open(Folder.READ_ONLY);
780                 mails = currentFolder.getMessages();
781             } catch (Exception JavaDoc ex) {
782                 ex.printStackTrace();
783             }
784         } catch (MessagingException e) {
785             JOptionPane.showMessageDialog(
786                     null,
787                     e.getMessage(),
788                     "MessagingException",
789                     JOptionPane.ERROR_MESSAGE);
790             return;
791         } catch (Exception JavaDoc e) {
792             e.printStackTrace();
793         }
794         
795         if (mails == null)
796             mails = new Message[0];
797         
798         msgList = null;
799         msgList = new Object JavaDoc[mails.length][3];
800         
801         String JavaDoc ids[] = new String JavaDoc[mails.length];
802         
803         Message m = null;
804         
805         try {
806             if (folder.compareTo("INBOX") == 0
807                     || folder.compareTo("inbox") == 0) {
808                 for (int i = 0; i < mails.length; i++) {
809                     m = mails[i];
810                     
811                     if (!m.isSet(Flags.Flag.SEEN)) {
812                         //XXX this makes an infinite loop!
813
//update();
814
}
815                 }
816             }
817             
818             InternetAddress[] a;
819             String JavaDoc name;
820             Date d;
821             
822             for (int i = 0; i < mails.length; i++) {
823                 m = mails[i];
824                 
825                 a = (InternetAddress[]) m.getFrom();
826                 
827                 if (a != null && a.length > 0) {
828                     name = a[0].getPersonal();
829                     
830                     if (name == null || name.compareTo("") == 0)
831                         name = a[0].getAddress();
832                     
833                     msgList[i][0] = (Object JavaDoc) name;
834                     
835                     if (msgList[i][0] == null)
836                         msgList[i][0] = (Object JavaDoc) new String JavaDoc();
837                 }
838                 
839                 msgList[i][1] = (Object JavaDoc) m.getSubject();
840                 
841                 if (msgList[i][1] == null)
842                     msgList[i][1] = (Object JavaDoc) new String JavaDoc();
843                 
844                 d = m.getSentDate();
845                 
846                 if (d != null)
847                     msgList[i][2] = (Object JavaDoc) d;
848                 
849                 else
850                     msgList[i][2] = (Object JavaDoc) new Date();
851                 
852                 ids[i] = ((MimeMessage) m).getMessageID();
853             }
854         } catch (Exception JavaDoc e) {
855             e.printStackTrace();
856         }
857         
858         subjectsTable.setIndexes(ids);
859         subjectsTable.setData(msgList, columns);
860         subjectsTable.removeListeners();
861         subjectsTable.addMouseListenerToHeaderInTable();
862         
863         //subjectsTable.setCellRenderer(msgBundle.getString("MainPanel.columns[2]Label"),
864
// new FormattedDateRenderer(df, this));
865

866     }
867     
868     /** Write a new mail */
869     private void send() {
870         SendMessageDialog sm =
871             new SendMessageDialog(plugin, profile, msgBundle);
872         sm.dispose();
873     }
874     
875     /** Reply to the selected mail */
876     protected final void reply() {
877         try {
878             Address a[] = current.getReplyTo();
879             
880             String JavaDoc currentSubject = current.getSubject();
881             
882             if (currentSubject == null)
883                 currentSubject = "";
884             
885             currentSubject = "Re: " + currentSubject;
886             
887             SendMessageDialog sm =
888                 new SendMessageDialog(
889                         plugin,
890                         profile,
891                         a[0].toString(),
892                         currentSubject,
893                         getMailContent(current).toString(),
894                         msgBundle);
895             
896             if (sm.sentMail())
897                 current.setFlag(Flags.Flag.ANSWERED, true);
898             
899             sm.dispose();
900         } catch (Exception JavaDoc e) {
901             e.printStackTrace();
902         }
903     }
904     
905     /** Reply to all */
906     protected final void replyall() {
907         try {
908             Address a[] = current.getReplyTo();
909             
910             String JavaDoc currentSubject = current.getSubject();
911             
912             if (currentSubject == null)
913                 currentSubject = "";
914             
915             currentSubject = "Re: " + currentSubject;
916             
917             Address to[] = current.getRecipients(Message.RecipientType.TO);
918             Address cc[] = current.getRecipients(Message.RecipientType.CC);
919             
920             String JavaDoc copies[] = new String JavaDoc[0];
921             
922             if (cc != null) {
923                 if (to != null) {
924                     copies = new String JavaDoc[to.length + cc.length];
925                     
926                     int i;
927                     
928                     for (i = 0; i < to.length; i++)
929                         copies[i] = to[i].toString();
930                     
931                     for (int j = 0; j < cc.length; j++)
932                         copies[i++] = cc[j].toString();
933                 } else {
934                     copies = new String JavaDoc[cc.length];
935                     
936                     for (int i = 0; i < cc.length; i++)
937                         copies[i] = cc[i].toString();
938                 }
939             } else if (to != null) {
940                 copies = new String JavaDoc[to.length];
941                 
942                 for (int i = 0; i < to.length; i++)
943                     copies[i] = to[i].toString();
944             }
945             
946             SendMessageDialog sm =
947                 new SendMessageDialog(
948                         plugin,
949                         profile,
950                         a[0].toString(),
951                         copies,
952                         currentSubject,
953                         getMailContent(current).toString(),
954                         msgBundle);
955             
956             if (sm.sentMail())
957                 current.setFlag(Flags.Flag.ANSWERED, true);
958             
959             sm.dispose();
960         } catch (AddressException addressEx) {
961         } catch (Exception JavaDoc e) {
962             e.printStackTrace();
963         }
964     }
965     
966     /** Forward the selected mail */
967     protected final void forward() {
968         try {
969             String JavaDoc currentSubject = current.getSubject();
970             
971             if (currentSubject == null)
972                 currentSubject = "";
973             
974             currentSubject = "Fwd : " + currentSubject;
975             
976             SendMessageDialog sm =
977                 new SendMessageDialog(
978                         plugin,
979                         profile,
980                         currentSubject,
981                         getMailContent(current).toString(),
982                         msgBundle);
983             sm.dispose();
984         } catch (Exception JavaDoc e) {
985             e.printStackTrace();
986         }
987     }
988     
989     /** Move the selected mail in another folder */
990     protected final void moveMsg() {
991         new MoveMessageFrame(
992                 store,
993                 currentFolder,
994                 profile,
995                 folder,
996                 currentId,
997                 0,
998                 msgBundle);
999         update();
1000    }
1001    
1002    /* Copy the selected mail in another folder */
1003    protected final void copyMsg() {
1004        new MoveMessageFrame(
1005                store,
1006                currentFolder,
1007                profile,
1008                folder,
1009                currentId,
1010                1,
1011                msgBundle);
1012    }
1013    
1014    /**
1015     * Activate or deactivate the buttons which are related to a mail
1016     *
1017     * @param b
1018     * activate or deactivate
1019     */

1020    private void setButtonsEnabled(boolean b) {
1021        reply.setEnabled(b);
1022        replyall.setEnabled(b);
1023        forward.setEnabled(b);
1024        delete.setEnabled(b);
1025    }
1026    
1027    /** Deletes the selected folder */
1028    protected final void deleteFolder() {
1029        DefaultMutableTreeNode n =
1030            (DefaultMutableTreeNode) foldersTree.getLastSelectedPathComponent();
1031        
1032        if (n != null) {
1033            if (currentFolder.getFullName().compareTo(n.toString()) == 0) {
1034                if (!MailClient.deleteFolder(currentFolder))
1035                    JOptionPane.showMessageDialog(
1036                            null,
1037                            msgBundle.getString("MainPanel.msgNotDeletedLabel"),
1038                            "MainPanel.deleteFolder",
1039                            JOptionPane.ERROR_MESSAGE);
1040            } else {
1041                try {
1042                    Folder fold = store.getFolder(n.toString());
1043                    
1044                    if (!MailClient.deleteFolder(fold))
1045                        JOptionPane.showMessageDialog(
1046                                null,
1047                                msgBundle.getString("MainPanel.msgNotDeletedLabel"),
1048                                "MainPanel.deleteFolder",
1049                                JOptionPane.ERROR_MESSAGE);
1050                } catch (Exception JavaDoc e) {
1051                    e.printStackTrace();
1052                }
1053            }
1054            
1055            MutableTreeNode parent = (MutableTreeNode) n.getParent();
1056            
1057            if (parent != null) {
1058                DefaultTreeModel model =
1059                    (DefaultTreeModel) foldersTree.getModel();
1060                model.removeNodeFromParent(n);
1061            }
1062        }
1063    }
1064    
1065    /** Saves the current mail in a text file */
1066    protected final void save() {
1067        JFileChooser fc = new JFileChooser();
1068        
1069        int val = fc.showSaveDialog(null);
1070        
1071        if (val == JFileChooser.APPROVE_OPTION) {
1072            File file = fc.getSelectedFile();
1073            
1074            if (file != null) {
1075                try {
1076                    int choice;
1077                    
1078                    while (file.exists()) {
1079                        choice =
1080                            JOptionPane.showConfirmDialog(
1081                                    null,
1082                                    msgBundle.getString(
1083                                    "MainPanel.fileExistsLabel"),
1084                                    "MainPanel",
1085                                    JOptionPane.YES_NO_OPTION,
1086                                    JOptionPane.QUESTION_MESSAGE);
1087                        
1088                        if (choice == JOptionPane.YES_OPTION)
1089                            break;
1090                        
1091                        val = fc.showSaveDialog(null);
1092                        
1093                        if (val == JFileChooser.APPROVE_OPTION)
1094                            file = fc.getSelectedFile();
1095                        
1096                        else
1097                            return;
1098                    }
1099                    
1100                    FileOutputStream out = new FileOutputStream(file);
1101                    current.writeTo(out);
1102                    out.close();
1103                } catch (Exception JavaDoc ex) {
1104                    ex.printStackTrace();
1105                }
1106            }
1107        }
1108    }
1109    
1110    /** Displays the current mail's source in a dialog */
1111    private void displayMsgSource() {
1112        try {
1113            new MessageSourceFrame(current.getSubject(), current);
1114        } catch (Exception JavaDoc e) {
1115            e.printStackTrace();
1116        }
1117    }
1118    
1119    /**
1120     * Tells if the "Attachment" button is visible or not
1121     *
1122     * @return boolean telling wether the button is visible or not
1123     */

1124    protected final boolean isAttachmentVisible() {
1125        return (attachment.isVisible());
1126    }
1127    
1128    /**
1129     * Returns the current folder
1130     *
1131     * @return current folder
1132     */

1133    protected final Folder getCurrentFolder() {
1134        return (currentFolder);
1135    }
1136    
1137    /**
1138     * Allows to get the connection object
1139     *
1140     * @return the connection
1141     */

1142    protected final Store getStore() {
1143        return (store);
1144    }
1145    
1146    /**
1147     * Gets currently selected mail
1148     *
1149     * @return the mail
1150     */

1151    protected final Message getCurrent() {
1152        return (current);
1153    }
1154    
1155    /**
1156     * Returns the parent <code>JMailWindow</code>
1157     *
1158     * @return the parent <code>JMailWindow</code>
1159     */

1160    protected JMailWindow getParentWindow() {
1161        return (parent);
1162    }
1163    
1164    /** Listener for this class */
1165    private final class MainPanelListener
1166    extends MouseAdapter
1167    implements
1168    ActionListener,
1169    ListSelectionListener,
1170    TreeSelectionListener,
1171    MessageCountListener {
1172        /**
1173         * Method called when the user clicks on a mouse button
1174         *
1175         * @param e
1176         * triggered event
1177         */

1178        public final void mousePressed(MouseEvent e) {
1179            if (e.getSource() == subjectsTable) {
1180                if (subjectsTable.contains(e.getX(), e.getY())) {
1181                    if (e.isPopupTrigger()) {
1182                        if (subjectsTable.getSelectedRow() != -1)
1183                            msgPopup.show(e.getComponent(), e.getX(), e.getY());
1184                    } else {
1185                        int mods = e.getModifiers();
1186                        
1187                        /** Right click */
1188                        if (mods == 4) {
1189                            if (subjectsTable.getSelectedRow() != -1)
1190                                msgPopup.show(
1191                                        e.getComponent(),
1192                                        e.getX(),
1193                                        e.getY());
1194                        }
1195                        
1196                        /** Left double-click */
1197                        else if (mods == 16 && e.getClickCount() == 2) {
1198                            read();
1199                        }
1200                    }
1201                }
1202            } else if (e.getSource() == foldersTree) {
1203                if (e.isPopupTrigger()) {
1204                    DefaultMutableTreeNode n =
1205                        (DefaultMutableTreeNode) foldersTree
1206                        .getLastSelectedPathComponent();
1207                    
1208                    if (n != null)
1209                        folderPopup.show(e.getComponent(), e.getX(), e.getY());
1210                } else {
1211                    int mods = e.getModifiers();
1212                    
1213                    /** Right click */
1214                    if (mods == 4) {
1215                        DefaultMutableTreeNode n =
1216                            (DefaultMutableTreeNode) foldersTree
1217                            .getLastSelectedPathComponent();
1218                        
1219                        if (n != null)
1220                            folderPopup.show(
1221                                    e.getComponent(),
1222                                    e.getX(),
1223                                    e.getY());
1224                    }
1225                }
1226            }
1227        }
1228        
1229        /**
1230         * This method is invoked when an event is triggered
1231         *
1232         * @param e
1233         * event
1234         */

1235        public final void actionPerformed(ActionEvent e) {
1236            AbstractButton b = (AbstractButton) e.getSource();
1237            
1238            if (b == verify)
1239                update();
1240            
1241            else if (b == send)
1242                send();
1243            
1244            else if (b == reply || b == msgReplyItem)
1245                reply();
1246            
1247            else if (b == replyall || b == msgReplyallItem)
1248                replyall();
1249            
1250            else if (b == forward || b == msgForwardItem)
1251                forward();
1252            
1253            else if (b == delete || b == msgDeleteItem)
1254                delete();
1255            
1256            else if (b == folderNewItem)
1257                newFolder();
1258            
1259            else if (b == folderDeleteItem)
1260                deleteFolder();
1261            
1262            else if (b == msgOpenItem)
1263                read();
1264            
1265            else if (b == msgSourceItem)
1266                displayMsgSource();
1267            
1268            else if (b == msgSaveItem)
1269                save();
1270            
1271            else if (b == msgMoveItem)
1272                moveMsg();
1273            
1274            else if (b == msgCopyItem)
1275                copyMsg();
1276            
1277            else if (b == attachment)
1278                new AttachmentFrame(current, msgBundle);
1279        }
1280        
1281        /**
1282         * Method invoked when the user clicks on a mail
1283         *
1284         * @param e
1285         * triggered event
1286         */

1287        public final void valueChanged(ListSelectionEvent e) {
1288            if (e.getValueIsAdjusting())
1289                return;
1290            
1291            ListSelectionModel lsm = (ListSelectionModel) e.getSource();
1292            
1293            if (lsm.isSelectionEmpty()) {
1294                setButtonsEnabled(false);
1295                
1296                parent.setMenuItemsEnabled(false);
1297                from.setText(msgBundle.getString("MainPanel.fromLabel"));
1298                subject.setText(msgBundle.getString("MainPanel.subjectLabel"));
1299                
1300                content =
1301                    new StringBuffer JavaDoc(
1302                            msgBundle.getString("MainPanel.noMsgLabel"));
1303                area.setText(content.toString());
1304                
1305                attachment.setVisible(false);
1306            } else if (subjectsTable.getSelectedRowCount() > 1) {
1307                setButtonsEnabled(false);
1308                delete.setEnabled(true);
1309                from.setText(msgBundle.getString("MainPanel.fromLabel"));
1310                subject.setText(msgBundle.getString("MainPanel.subjectLabel"));
1311                
1312                content =
1313                    new StringBuffer JavaDoc(
1314                            msgBundle.getString("MainPanel.noMsgLabel"));
1315                area.setText(content.toString());
1316                
1317                attachment.setVisible(false);
1318            } else
1319                displayMsg();
1320        }
1321        
1322        /**
1323         * Method called when the user clicks on a folder
1324         *
1325         * @param e
1326         * triggered event
1327         */

1328        public final void valueChanged(TreeSelectionEvent e) {
1329            DefaultMutableTreeNode n =
1330                (DefaultMutableTreeNode) foldersTree
1331                .getLastSelectedPathComponent();
1332            
1333            if (n == null) {
1334                parent.setFolderMenuItemsEnabled(false);
1335                return;
1336            }
1337            
1338            if (profile.getType().compareTo("pop3") != 0)
1339                parent.setFolderMenuItemsEnabled(true);
1340            
1341            update();
1342        }
1343        
1344        /**
1345         * Method called when a new mail is received
1346         *
1347         * @param ev
1348         * triggered event
1349         */

1350        public final void messagesAdded(MessageCountEvent ev) {
1351            update();
1352            getToolkit().beep();
1353        }
1354        
1355        /***********************************************************************
1356         * Method *supposedly Indeed, it is not used and is present only
1357         * because of the <code>FolderListener</code> implantation
1358         *
1359         * @param ev
1360         * triggered event
1361         */

1362        public final void messagesRemoved(MessageCountEvent ev) {
1363        }
1364    }
1365}
1366
Popular Tags