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. import java.awt.*;
  33. import java.awt.event.*;
  34. import java.io.*;
  35. import java.beans.*;
  36. import javax.activation.*;
  37. import javax.mail.*;
  38. import javax.swing.JPanel;
  39.  
  40.  
  41. /**
  42.  * A Viewer Bean for the type multipart/mixed
  43.  *
  44.  * @author      Christopher Cotton
  45.  */
  46.  
  47. public class MultipartViewer extends JPanel implements CommandObject {
  48.    
  49.     protected DataHandler       dh = null;
  50.     protected String            verb = null;
  51.    
  52.     public MultipartViewer() {
  53.         super(new GridBagLayout());
  54.     }
  55.  
  56.    
  57.     public void setCommandContext(String verb, DataHandler dh) throws IOException {
  58.         this.verb = verb;
  59.         this.dh = dh;
  60.        
  61.         // get the content, and hope it is a Multipart Object
  62.         Object content = dh.getContent();
  63.         if (content instanceof Multipart) {
  64.             setupDisplay((Multipart)content);
  65.         } else {
  66.             setupErrorDisplay(content);
  67.         }
  68.     }
  69.  
  70.     protected void setupDisplay(Multipart mp) {
  71.         // we display the first body part in a main frame on the left, and then
  72.         // on the right we display the rest of the parts as attachments
  73.  
  74.         GridBagConstraints gc = new GridBagConstraints();
  75.         gc.gridheight = GridBagConstraints.REMAINDER;
  76.         gc.fill = GridBagConstraints.BOTH;
  77.         gc.weightx = 1.0;
  78.         gc.weighty = 1.0;
  79.  
  80.         // get the first part
  81.         try {
  82.             BodyPart bp = mp.getBodyPart(0);
  83.             Component comp = getComponent(bp);
  84.             add(comp, gc);
  85.            
  86.         } catch (MessagingException me) {
  87.             add(new Label(me.toString()), gc);
  88.         }
  89.  
  90.         // see if there are more than one parts
  91.         try {
  92.             int count = mp.getCount();
  93.  
  94.             // setup how to display them
  95.             gc.gridwidth = GridBagConstraints.REMAINDER;
  96.             gc.gridheight = 1;
  97.             gc.fill = GridBagConstraints.NONE;
  98.             gc.anchor = GridBagConstraints.NORTH;
  99.             gc.weightx = 0.0;
  100.             gc.weighty = 0.0;
  101.             gc.insets = new Insets(4,4,4,4);
  102.  
  103.             // for each one we create a button with the content type
  104.             for(int i = 1; i < count; i++) { // we skip the first one
  105.                 BodyPart curr = mp.getBodyPart(i);
  106.                 String label = null;
  107.                 if (label == null) label = curr.getFileName();
  108.                 if (label == null) label = curr.getDescription();
  109.                 if (label == null) label = curr.getContentType();
  110.  
  111.                 Button but = new Button(label);
  112.                 but.addActionListener( new AttachmentViewer(curr));
  113.                 add(but, gc);
  114.             }
  115.            
  116.            
  117.         } catch(MessagingException me2) {
  118.             me2.printStackTrace();
  119.         }
  120.  
  121.     }
  122.  
  123.     protected Component getComponent(BodyPart bp) {
  124.  
  125.         try {
  126.             DataHandler dh = bp.getDataHandler();
  127.             CommandInfo ci = dh.getCommand("view");
  128.             if (ci == null) {
  129.                 throw new MessagingException(
  130.                     "view command failed on: " +
  131.                     bp.getContentType());
  132.             }
  133.            
  134.             Object bean = dh.getBean(ci);
  135.        
  136.             if (bean instanceof Component) {
  137.                 return (Component)bean;
  138.             } else {
  139.                 if (bean == null)
  140.                     throw new MessagingException(
  141.                         "bean is null, class " + ci.getCommandClass() +
  142.                         " , command " + ci.getCommandName());
  143.                 else
  144.                     throw new MessagingException(
  145.                         "bean is not a awt.Component" +
  146.                         bean.getClass().toString());
  147.             }
  148.         }
  149.         catch (MessagingException me) {
  150.             return new Label(me.toString());
  151.         }
  152.     }
  153.    
  154.  
  155.    
  156.     protected void setupErrorDisplay(Object content) {
  157.         String error;
  158.  
  159.         if (content == null)
  160.             error = "Content is null";
  161.         else
  162.             error = "Object not of type Multipart, content class = " +
  163.             content.getClass().toString();
  164.        
  165.         System.out.println(error);
  166.         Label lab = new Label(error);
  167.         add(lab);
  168.     }
  169.    
  170.     class AttachmentViewer implements ActionListener {
  171.        
  172.         BodyPart bp = null;
  173.        
  174.         public AttachmentViewer(BodyPart part) {
  175.             bp = part;
  176.         }
  177.        
  178.         public void actionPerformed(ActionEvent e) {
  179.             ComponentFrame f = new ComponentFrame(
  180.                 getComponent(bp), "Attachment");
  181.             f.pack();
  182.             f.show();
  183.         }
  184.     }
  185.  
  186. }
  187.