KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MultipartViewer


1 /*
2  * @(#)MultipartViewer.java 1.14 01/05/23
3  *
4  * Copyright 1997-2000 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * - Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * - Redistribution in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * Neither the name of Sun Microsystems, Inc. or the names of contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * This software is provided "AS IS," without a warranty of any kind. ALL
22  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
23  * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
24  * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
25  * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES OR LIABILITIES
26  * SUFFERED BY LICENSEE AS A RESULT OF OR RELATING TO USE, MODIFICATION
27  * OR DISTRIBUTION OF THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
28  * SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
29  * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE
30  * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY,
31  * ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS
32  * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
33  *
34  * You acknowledge that Software is not designed, licensed or intended
35  * for use in the design, construction, operation or maintenance of any
36  * nuclear facility.
37  */

38
39 import java.awt.*;
40 import java.awt.event.*;
41 import java.io.*;
42 import java.beans.*;
43 import javax.activation.*;
44 import javax.mail.*;
45 import javax.swing.JPanel JavaDoc;
46
47
48 /**
49  * A Viewer Bean for the type multipart/mixed
50  *
51  * @version 1.14, 01/05/23
52  * @author Christopher Cotton
53  */

54
55 public class MultipartViewer extends JPanel JavaDoc implements CommandObject {
56     
57     protected DataHandler dh = null;
58     protected String JavaDoc verb = null;
59     
60     public MultipartViewer() {
61     super(new GridBagLayout());
62     }
63
64     
65     public void setCommandContext(String JavaDoc verb, DataHandler dh) throws IOException {
66     this.verb = verb;
67     this.dh = dh;
68     
69     // get the content, and hope it is a Multipart Object
70
Object JavaDoc content = dh.getContent();
71     if (content instanceof Multipart) {
72         setupDisplay((Multipart)content);
73     } else {
74         setupErrorDisplay(content);
75     }
76     }
77
78     protected void setupDisplay(Multipart mp) {
79     // we display the first body part in a main frame on the left, and then
80
// on the right we display the rest of the parts as attachments
81

82     GridBagConstraints gc = new GridBagConstraints();
83     gc.gridheight = GridBagConstraints.REMAINDER;
84     gc.fill = GridBagConstraints.BOTH;
85     gc.weightx = 1.0;
86     gc.weighty = 1.0;
87
88     // get the first part
89
try {
90         BodyPart bp = mp.getBodyPart(0);
91         Component comp = getComponent(bp);
92         add(comp, gc);
93         
94     } catch (MessagingException me) {
95         add(new Label(me.toString()), gc);
96     }
97
98     // see if there are more than one parts
99
try {
100         int count = mp.getCount();
101
102         // setup how to display them
103
gc.gridwidth = GridBagConstraints.REMAINDER;
104         gc.gridheight = 1;
105         gc.fill = GridBagConstraints.NONE;
106         gc.anchor = GridBagConstraints.NORTH;
107         gc.weightx = 0.0;
108         gc.weighty = 0.0;
109         gc.insets = new Insets(4,4,4,4);
110
111         // for each one we create a button with the content type
112
for(int i = 1; i < count; i++) { // we skip the first one
113
BodyPart curr = mp.getBodyPart(i);
114         String JavaDoc label = null;
115         if (label == null) label = curr.getFileName();
116         if (label == null) label = curr.getDescription();
117         if (label == null) label = curr.getContentType();
118
119         Button but = new Button(label);
120         but.addActionListener( new AttachmentViewer(curr));
121         add(but, gc);
122         }
123         
124         
125     } catch(MessagingException me2) {
126         me2.printStackTrace();
127     }
128
129     }
130
131     protected Component getComponent(BodyPart bp) {
132
133     try {
134         DataHandler dh = bp.getDataHandler();
135         CommandInfo ci = dh.getCommand("view");
136         if (ci == null) {
137         throw new MessagingException(
138             "view command failed on: " +
139             bp.getContentType());
140         }
141         
142         Object JavaDoc bean = dh.getBean(ci);
143     
144         if (bean instanceof Component) {
145         return (Component)bean;
146         } else {
147         if (bean == null)
148             throw new MessagingException(
149             "bean is null, class " + ci.getCommandClass() +
150             " , command " + ci.getCommandName());
151         else
152             throw new MessagingException(
153             "bean is not a awt.Component" +
154             bean.getClass().toString());
155         }
156     }
157     catch (MessagingException me) {
158         return new Label(me.toString());
159     }
160     }
161     
162
163     
164     protected void setupErrorDisplay(Object JavaDoc content) {
165     String JavaDoc error;
166
167     if (content == null)
168         error = "Content is null";
169     else
170         error = "Object not of type Multipart, content class = " +
171         content.getClass().toString();
172     
173     System.out.println(error);
174     Label lab = new Label(error);
175     add(lab);
176     }
177    
178     class AttachmentViewer implements ActionListener {
179     
180     BodyPart bp = null;
181     
182     public AttachmentViewer(BodyPart part) {
183         bp = part;
184     }
185     
186     public void actionPerformed(ActionEvent e) {
187         ComponentFrame f = new ComponentFrame(
188         getComponent(bp), "Attachment");
189         f.pack();
190         f.show();
191     }
192     }
193
194 }
195
Popular Tags