Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  *
  8.  *   - Redistributions of source code must retain the above copyright
  9.  *     notice, this list of conditions and the following disclaimer.
  10.  *
  11.  *   - Redistributions in binary form must reproduce the above copyright
  12.  *     notice, this list of conditions and the following disclaimer in the
  13.  *     documentation and/or other materials provided with the distribution.
  14.  *
  15.  *   - Neither the name of Sun Microsystems nor the names of its
  16.  *     contributors may be used to endorse or promote products derived
  17.  *     from this software without specific prior written permission.
  18.  *
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22.  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27.  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.  */
  31. /*
  32.  * Copyright (c) 1997-2007 by Sun Microsystems, Inc.
  33.  * All Rights Reserved.
  34.  */
  35.  
  36. import java.awt.*;
  37. import java.awt.event.*;
  38. import javax.mail.*;
  39. import javax.activation.*;
  40. import java.util.Date;
  41. import java.io.IOException;
  42. import javax.swing.JPanel;
  43.  
  44. /**
  45.  * @author      Christopher Cotton
  46.  * @author      Bill Shannon
  47.  */
  48.  
  49. public class MessageViewer extends JPanel implements CommandObject {
  50.    
  51.     Message     displayed = null;
  52.     DataHandler dataHandler = null;
  53.     String      verb = null;
  54.     Component   mainbody;
  55.     TextArea    headers;
  56.  
  57.     public MessageViewer() {
  58.         this(null);
  59.     }
  60.    
  61.     public MessageViewer(Message what) {
  62.         // set our layout
  63.         super(new GridBagLayout());
  64.  
  65.         // add the toolbar
  66.         addToolbar();
  67.  
  68.         GridBagConstraints gb = new GridBagConstraints();
  69.         gb.gridwidth = GridBagConstraints.REMAINDER;
  70.         gb.fill = GridBagConstraints.BOTH;
  71.         gb.weightx = 1.0;
  72.         gb.weighty = 0.0;
  73.  
  74.         // add the headers
  75.         headers = new TextArea("", 4, 80, TextArea.SCROLLBARS_NONE);
  76.         headers.setEditable(false);
  77.         add(headers, gb);
  78.  
  79.         // now display our message
  80.         setMessage(what);
  81.     }
  82.    
  83.     /**
  84.      * sets the current message to be displayed in the viewer
  85.      */
  86.     public void setMessage(Message what) {
  87.         displayed = what;
  88.  
  89.         if (mainbody != null)
  90.             remove(mainbody);
  91.  
  92.         if (what != null) {
  93.             loadHeaders();
  94.             mainbody = getBodyComponent();
  95.         } else {
  96.             headers.setText("");
  97.             TextArea dummy = new TextArea("", 24, 80, TextArea.SCROLLBARS_NONE);
  98.             dummy.setEditable(false);
  99.             mainbody = dummy;
  100.         }
  101.  
  102.         // add the main body
  103.         GridBagConstraints gb = new GridBagConstraints();
  104.         gb.gridwidth = GridBagConstraints.REMAINDER;
  105.         gb.fill = GridBagConstraints.BOTH;
  106.         gb.weightx = 1.0;
  107.         gb.weighty = 1.0;
  108.         add(mainbody, gb);
  109.  
  110.         invalidate();
  111.         validate();
  112.     }
  113.  
  114.     protected void addToolbar() {
  115.         GridBagConstraints gb = new GridBagConstraints();
  116.         gb.gridheight = 1;
  117.         gb.gridwidth = 1;
  118.         gb.fill = GridBagConstraints.NONE;
  119.         gb.anchor = GridBagConstraints.WEST;
  120.         gb.weightx = 0.0;
  121.         gb.weighty = 0.0;
  122.         gb.insets = new Insets(4,4,4,4);
  123.  
  124.         // structure button
  125.         gb.gridwidth = GridBagConstraints.REMAINDER; // only for the last one
  126.         Button b = new Button("Structure");
  127.         b.addActionListener( new StructureAction());
  128.         add(b, gb);
  129.     }
  130.  
  131.     protected void loadHeaders() {
  132.         // setup what we want in our viewer
  133.         StringBuffer sb = new StringBuffer();
  134.  
  135.         // date
  136.         sb.append("Date: ");
  137.         try {
  138.             Date duh = displayed.getSentDate();
  139.             if (duh != null) {
  140.                 sb.append(duh.toString());
  141.             } else {
  142.                 sb.append("Unknown");
  143.             }
  144.            
  145.             sb.append("\n");
  146.  
  147.             // from
  148.             sb.append("From: ");
  149.             Address[] adds = displayed.getFrom();
  150.             if (adds != null && adds.length > 0) {
  151.                 sb.append(adds[0].toString());
  152.             }
  153.             sb.append("\n");
  154.  
  155.             // to
  156.             sb.append("To: ");
  157.             adds = displayed.getRecipients(Message.RecipientType.TO);
  158.             if (adds != null && adds.length > 0) {
  159.                 sb.append(adds[0].toString());
  160.             }
  161.             sb.append("\n");
  162.  
  163.             // subject
  164.             sb.append("Subject: ");
  165.             sb.append(displayed.getSubject());
  166.            
  167.             headers.setText(sb.toString());
  168.         } catch (MessagingException me) {
  169.             headers.setText("");
  170.         }
  171.     }
  172.  
  173.     protected Component getBodyComponent() {
  174.         //------------
  175.         // now get a content viewer for the main type...
  176.         //------------
  177.         try {
  178.             DataHandler dh = displayed.getDataHandler();
  179.             CommandInfo ci = dh.getCommand("view");
  180.             if (ci == null) {
  181.                 throw new MessagingException("view command failed on: " +
  182.                                              displayed.getContentType());
  183.             }
  184.  
  185.             Object bean = dh.getBean(ci);
  186.             if (bean instanceof Component) {
  187.                 return (Component)bean;
  188.             } else {
  189.                 throw new MessagingException("bean is not a component " +
  190.                                              bean.getClass().toString());
  191.             }
  192.         } catch (MessagingException me) {
  193.             return new Label(me.toString());
  194.         }
  195.     }
  196.    
  197.     /**
  198.      * the CommandObject method to accept our DataHandler
  199.      * @param dh        the datahandler used to get the content
  200.      */
  201.     public void setCommandContext(String verb,
  202.                                   DataHandler dh) throws IOException {
  203.         this.verb = verb;
  204.         dataHandler = dh;
  205.  
  206.         Object o = dh.getContent();
  207.         if (o instanceof Message) {
  208.             setMessage((Message)o);
  209.         }
  210.         else {
  211.             System.out.println(
  212.                 "MessageViewer - content not a Message object, " + o);
  213.             if (o != null){
  214.                 System.out.println(o.getClass().toString());
  215.             }
  216.         }
  217.     }
  218.  
  219.  
  220.     class StructureAction implements ActionListener {
  221.         StringBuffer sb;
  222.  
  223.         public void actionPerformed(ActionEvent e) {
  224.             System.out.println("\n\nMessage Structure");
  225.             dumpPart("", displayed);
  226.         }
  227.  
  228.         protected void dumpPart(String prefix, Part p) {
  229.             try {
  230.                 System.out.println(prefix + "----------------");
  231.                 System.out.println(prefix +
  232.                                    "Content-Type: " + p.getContentType());
  233.                 System.out.println(prefix +
  234.                                    "Class: " + p.getClass().toString());
  235.                            
  236.                 Object o = p.getContent();
  237.                 if (o == null) {
  238.                     System.out.println(prefix + "Content:  is null");
  239.                 } else {
  240.                     System.out.println(prefix +
  241.                                        "Content: " + o.getClass().toString());
  242.                 }
  243.  
  244.                 if (o instanceof Multipart) {
  245.                     String newpref = prefix + "\t";
  246.                     Multipart mp = (Multipart)o;
  247.                     int count = mp.getCount();
  248.                     for (int i = 0; i < count; i++) {
  249.                         dumpPart(newpref, mp.getBodyPart(i));
  250.                     }
  251.                 }
  252.             } catch (MessagingException e) {
  253.                 e.printStackTrace();
  254.             } catch (IOException ioex) {
  255.                 System.out.println("Cannot get content" + ioex.getMessage());
  256.             }
  257.         }
  258.     }
  259. }
  260.