KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.*;
24 import java.io.*;
25 import java.util.*;
26 import javax.mail.*;
27 import javax.swing.*;
28
29 /** A dialog containing list of attached files */
30 final class AttachmentFrame extends JFrame
31 {
32     /** Language resource */
33     private ResourceBundle msgBundle;
34     
35     private JPanel panel;
36     
37     private JPopupMenu filePopup;
38     private JMenuItem openMenuItem;
39     private JMenuItem saveMenuItem;
40     
41     private JList list;
42     private JScrollPane listPane;
43     
44     private Vector names;
45     
46     /** The message which contains the files */
47     private Message mail;
48     
49     private AttachmentListener listener;
50     
51     /** Constructor
52      * @param mail the message
53      * @param msgBundle language resource
54      */

55     protected AttachmentFrame(Message mail, ResourceBundle msgBundle)
56     {
57         super(msgBundle.getString("Attachment.frameTitle"));
58         
59         this.msgBundle = msgBundle;
60         
61         listener = new AttachmentListener();
62         
63         this.mail = mail;
64         
65         panel = new JPanel();
66         
67         names = new Vector();
68         names = getFiles(mail, names);
69         
70         list = new JList(names);
71         list.addMouseListener(listener);
72         listPane = new JScrollPane(list);
73         panel.add(listPane);
74         
75         filePopup = new JPopupMenu();
76         
77         openMenuItem = new JMenuItem(msgBundle.getString("common.open"));
78         openMenuItem.addActionListener(listener);
79         filePopup.add(openMenuItem);
80         
81         saveMenuItem = new JMenuItem(msgBundle.getString("common.save"));
82         saveMenuItem.addActionListener(listener);
83         filePopup.add(saveMenuItem);
84         
85         setContentPane(panel);
86         setSize(400, 200);
87         setVisible(true);
88     }
89     
90     /** Recursive method to get the content of a mail part
91      * @param p part to be examined
92      * @param names current names
93      * @return vector containing every files name
94      */

95     private Vector getFiles(Part p, Vector names)
96     {
97         try
98         {
99             if(p.isMimeType("multipart/mixed") || p.isMimeType("multipart/related") || p.isMimeType("multipart/signed") || p.isMimeType("multipart/report"))
100             {
101                 Multipart mp = (Multipart)p.getContent();
102                 
103                 for(int i = 0; i < mp.getCount(); i++)
104                     names = getFiles(mp.getBodyPart(i), names);
105             }
106             
107             else if(p.isMimeType("multipart/alternative"))
108             {
109                 Multipart mp = (Multipart)p.getContent();
110                 BodyPart bp = null;
111                 
112                 for(int i = 0; i < mp.getCount(); i++)
113                 {
114                     bp = mp.getBodyPart(i);
115                     
116                     if(bp.isMimeType("text/html"))
117                     {
118                         names.add("HTML version");
119                         break;
120                     }
121                 }
122             }
123             
124             else if(p.isMimeType("message/rfc822"))
125             {
126                 Message m = (Message)p.getContent();
127                 names = getFiles(m, names);
128             }
129             
130             else
131             {
132                 String JavaDoc name = p.getFileName();
133                 
134                 if(name != null)
135                 {
136                     
137                     int size = p.getSize();
138                     
139                     String JavaDoc sizeString = new String JavaDoc();
140                     
141                     if(size >= 1000000)
142                         sizeString = (size / 1000000) + " Mb";
143                     
144                     else if(size >= 1000)
145                         sizeString = (size / 1000) + " kb";
146                     
147                     else
148                         sizeString = size + " b";
149                     
150                     names.add(name + " (" + sizeString + ")");
151                 }
152                 
153                 else if(p.isMimeType("text/html"))
154                     names.add("HTML version");
155                 
156                 else if(p.isMimeType("application/pgp-signature"))
157                     names.add("PGP");
158             }
159         }
160         
161         catch(Exception JavaDoc e)
162         {
163             e.printStackTrace();
164         }
165         
166         return(names);
167     }
168     
169     /** Recursive method to get the content of each part
170      * @param mp multipart to be examined
171      * @param name name of the file
172      * @return sub-part
173      */

174     private Part getBodyPart(Multipart mp, String JavaDoc name)
175     {
176         try
177         {
178             for(int i = 0; i < mp.getCount(); i++)
179             {
180                 BodyPart bp = mp.getBodyPart(i);
181                 
182                 BodyPart result = null;
183                 
184                 if(bp.isMimeType("multipart/mixed") || bp.isMimeType("multipart/alternative"))
185                     result = (BodyPart)getBodyPart((Multipart)bp.getContent(), name);
186                 
187                 if(result != null)
188                     return(result);
189                 
190                 if(name.compareTo("HTML version") == 0)
191                     if(bp.isMimeType("text/html"))
192                         return(bp);
193                     
194                     else if(name.compareTo("PGP") == 0)
195                         if(bp.isMimeType("application/pgp-signature"))
196                             return(bp);
197                         
198                 if(bp.isMimeType("message/rfc822"))
199                 {
200                     Message newMail = (Message)bp.getContent();
201                     
202                     if(newMail.isMimeType("text/html"))
203                         return((Part)bp.getContent());
204                     
205                     else if(newMail.getContent() instanceof Multipart)
206                     {
207                         Multipart mp2 = (Multipart)newMail.getContent();
208                         
209                         BodyPart bp2 = (BodyPart)getBodyPart(mp2, name);
210                         
211                         if(bp2 != null)
212                             return(bp2);
213                     }
214                 }
215                 
216                 
217                 String JavaDoc n = bp.getFileName();
218                 
219                 if(n != null)
220                     if(name.startsWith(n))
221                         return(bp);
222             }
223         }
224         
225         catch(UnsupportedEncodingException encodE)
226         {
227             JOptionPane.showMessageDialog(null, encodE.getMessage(), "UnsupportedEncodingException", JOptionPane.ERROR_MESSAGE);
228             return(null);
229         }
230         
231         catch(Exception JavaDoc e)
232         {
233             e.printStackTrace();
234         }
235         
236         return(null);
237     }
238     
239     /** Method to view the content of an attached file, if possible */
240     private void view()
241     {
242         try
243         {
244             Object JavaDoc o = mail.getContent();
245             
246             if(o instanceof Multipart)
247             {
248                 int i = list.getSelectedIndex();
249                 
250                 if(i != -1)
251                 {
252                     String JavaDoc name = (String JavaDoc)list.getSelectedValue();
253                     
254                     Multipart mp = (Multipart)mail.getContent();
255                     
256                     BodyPart bp = (BodyPart)getBodyPart(mp, name);
257                     
258                     if(bp == null)
259                         return;
260                     
261                     String JavaDoc realName = bp.getFileName();
262                     
263                     if(realName == null)
264                         realName = "attach.html";
265                     
266                     if(bp.isMimeType("image/gif") || bp.isMimeType("image/jpeg") || bp.isMimeType("image/jpg") || bp.isMimeType("image/pjpeg") || bp.isMimeType("image/x-png"))
267                     {
268                         File f = getIt(realName.toString(), bp);
269                         
270                         ImageIcon img = new ImageIcon(f.getPath());
271                         JLabel label = new JLabel(img);
272                         
273                         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(msgBundle.getString("AttachedFile.frameTitle"));
274                         buf.append(" ").append(realName);
275                         
276                         new AttachedFileDialog(buf.toString(), label, msgBundle);
277                         
278                         f.delete();
279                     }
280                     
281                     else if(bp.isMimeType("text/plain"))
282                     {
283                         File f = getIt(realName, bp);
284                         
285                         JTextArea area = new JTextArea(30, 35);
286                         area.setEditable(false);
287                         
288                         BufferedReader in = new BufferedReader(new FileReader(f));
289                         
290                         String JavaDoc line = new String JavaDoc();
291                         
292                         while((line = in.readLine()) != null)
293                             area.append(line + "\n");
294                         
295                         in.close();
296                         
297                         area.setCaretPosition(0);
298                         
299                         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(msgBundle.getString("AttachedFile.frameTitle"));
300                         buf.append(" ").append(realName);
301                         
302                         new AttachedFileDialog(buf.toString(), area, msgBundle);
303                         
304                         f.delete();
305                     }
306                     
307                     else if(bp.isMimeType("text/html"))
308                     {
309                         JEditorPane editorPane = new JEditorPane("text/html", (String JavaDoc)bp.getContent());
310                         editorPane.setEditable(false);
311                         
312                         new AttachedFileDialog(realName, editorPane, msgBundle);
313                     }
314                     
315                     else
316                     {
317                         String JavaDoc type[] = bp.getHeader("Content-Type");
318                         
319                         JOptionPane.showMessageDialog(null, msgBundle.getString("Attachment.undisplayableFilePart1Label") + " \"" + type[0] + "\" " + msgBundle.getString("Attachment.undisplayableFilePart2Label"), "Attachment", JOptionPane.ERROR_MESSAGE);
320                     }
321                 }
322             }
323             
324             else
325             {
326                 JEditorPane editorPane = new JEditorPane("text/html", (String JavaDoc)mail.getContent());
327                 editorPane.setEditable(false);
328                 
329                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc(msgBundle.getString("AttachedFile.frameTitle"));
330                 buf.append(" attach.html");
331                 new AttachedFileDialog(buf.toString(), editorPane, msgBundle);
332             }
333         }
334         
335         catch(Exception JavaDoc ex)
336         {
337             ex.printStackTrace();
338         }
339     }
340     
341     /** Allows to save an attached file */
342     private void save()
343     {
344         int i = list.getSelectedIndex();
345         
346         if(i != -1)
347         {
348             try
349             {
350                 String JavaDoc name = (String JavaDoc)list.getSelectedValue();
351                 
352                 Part bp = null;
353                 String JavaDoc realName = new String JavaDoc();
354                 
355                 Object JavaDoc o = mail.getContent();
356                 
357                 if(o instanceof Multipart)
358                 {
359                     Multipart mp = (Multipart)mail.getContent();
360                     bp = getBodyPart(mp, name);
361                     
362                     if(bp == null)
363                         return;
364                     
365                     realName = bp.getFileName();
366                     
367                     if(realName == null)
368                     {
369                         if(name.compareTo("HTML version") == 0)
370                             realName = "attach.html";
371                         
372                         else if(name.compareTo("PGP") == 0)
373                             realName = "sign.pgp";
374                     }
375                 }
376                 
377                 else
378                 {
379                     bp = (Part)mail;
380                     realName = "attach.html";
381                 }
382                 
383                 JFileChooser fc = new JFileChooser();
384                 
385                 String JavaDoc newName = new String JavaDoc();
386                 
387                 fc.setSelectedFile(new File(realName));
388                 
389                 int val = fc.showSaveDialog(null);
390                 
391                 if(val == JFileChooser.APPROVE_OPTION)
392                 {
393                     File file = fc.getSelectedFile();
394                     
395                     if(file != null)
396                     {
397                         try
398                         {
399                             while(file.exists())
400                             {
401                                 int choice = JOptionPane.showConfirmDialog(null, msgBundle.getString("Attachment.fileExistsLabel"), "Attachment", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
402                                 
403                                 if(choice == JOptionPane.YES_OPTION)
404                                     break;
405                                 
406                                 val = fc.showSaveDialog(null);
407                                 
408                                 if(val == JFileChooser.APPROVE_OPTION)
409                                     file = fc.getSelectedFile();
410                                 
411                                 else
412                                     return;
413                             }
414                             
415                             InputStream in = bp.getInputStream();
416                             FileOutputStream out = new FileOutputStream(file);
417                             
418                             int nb;
419                             byte b[];
420                             
421                             b = new byte[256];
422                             nb = in.read(b);
423                             
424                             while(nb == 256)
425                             {
426                                 out.write(b, 0, nb);
427                                 b = new byte[256];
428                                 nb = in.read(b);
429                             }
430                             
431                             if(nb != -1)
432                             {
433                                 out.write(b, 0, nb);
434                             }
435                             
436                             in.close();
437                             out.close();
438                         }
439                         
440                         catch(Exception JavaDoc ex)
441                         {
442                             ex.printStackTrace();
443                         }
444                     }
445                 }
446             }
447             
448             catch(Exception JavaDoc ex)
449             {
450                 ex.printStackTrace();
451             }
452         }
453     }
454     
455     /** Creates a temp file, to view it
456      * @param name name of the original file
457      * @param bp part of the mail containing it
458      * @return temporary file
459      */

460     private File getIt(String JavaDoc name, BodyPart bp)
461     {
462         try
463         {
464             File f = File.createTempFile(name, null);
465             
466             InputStream in = bp.getInputStream();
467             FileOutputStream out = new FileOutputStream(f);
468             
469             int nb;
470             byte b[];
471             
472             do
473             {
474                 b = new byte[256];
475                 nb = in.read(b);
476                 out.write(b, 0, nb);
477             } while(nb != -1 && nb == 256);
478             
479             in.close();
480             out.close();
481             
482             return(f);
483         }
484         
485         catch(Exception JavaDoc e)
486         {
487             e.printStackTrace();
488         }
489         
490         return(null);
491     }
492     
493     /** Listener for this class */
494     private final class AttachmentListener extends MouseAdapter implements ActionListener
495     {
496         /** Method invoked when the user clicks
497          * @param e event triggered
498          */

499         public final void mousePressed(MouseEvent e)
500         {
501             if(e.isPopupTrigger())
502             {
503                 if(list.getSelectedIndex() != -1)
504                     filePopup.show(e.getComponent(), e.getX(), e.getY());
505             }
506             
507             else
508             {
509                 int mods = e.getModifiers();
510                 
511                 if(mods == 4)
512                 {
513                     if(list.getSelectedIndex() != -1)
514                         filePopup.show(e.getComponent(), e.getX(), e.getY());
515                     
516                 }
517                 
518                 /** Left double-click */
519                 else if(mods == 16 && e.getClickCount() == 2)
520                 {
521                     if(list.getSelectedIndex() != -1)
522                         view();
523                 }
524             }
525         }
526         
527         /** This method is invoked when an event is triggered
528          * @param e event
529          */

530         public final void actionPerformed(ActionEvent e)
531         {
532             JMenuItem m = (JMenuItem)e.getSource();
533             
534             if(m == openMenuItem)
535                 view();
536             
537             else if(m == saveMenuItem)
538                 save();
539         }
540     }
541 }
542
Popular Tags