KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > MessageViewer


1 /*
2  * @(#)MessageViewer.java 1.16 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  * @(#)MessageViewer.java 1.16 01/05/23
40  *
41  * Copyright (c) 1997-1998 by Sun Microsystems, Inc.
42  * All Rights Reserved.
43  */

44
45 import java.awt.*;
46 import java.awt.event.*;
47 import javax.mail.*;
48 import javax.activation.*;
49 import java.util.Date JavaDoc;
50 import java.io.IOException JavaDoc;
51 import javax.swing.JPanel JavaDoc;
52
53 /**
54  * @version 1.16, 01/05/23
55  * @author Christopher Cotton
56  * @author Bill Shannon
57  */

58
59 public class MessageViewer extends JPanel JavaDoc implements CommandObject {
60     
61     Message displayed = null;
62     DataHandler dataHandler = null;
63     String JavaDoc verb = null;
64     Component mainbody;
65     TextArea headers;
66
67     public MessageViewer() {
68     this(null);
69     }
70     
71     public MessageViewer(Message what) {
72     // set our layout
73
super(new GridBagLayout());
74
75     // add the toolbar
76
addToolbar();
77
78     GridBagConstraints gb = new GridBagConstraints();
79     gb.gridwidth = GridBagConstraints.REMAINDER;
80     gb.fill = GridBagConstraints.BOTH;
81     gb.weightx = 1.0;
82     gb.weighty = 0.0;
83
84     // add the headers
85
headers = new TextArea("", 4, 80, TextArea.SCROLLBARS_NONE);
86     headers.setEditable(false);
87     add(headers, gb);
88
89     // now display our message
90
setMessage(what);
91     }
92     
93     /**
94      * sets the current message to be displayed in the viewer
95      */

96     public void setMessage(Message what) {
97     displayed = what;
98
99     if (mainbody != null)
100         remove(mainbody);
101
102     if (what != null) {
103         loadHeaders();
104         mainbody = getBodyComponent();
105     } else {
106         headers.setText("");
107         TextArea dummy = new TextArea("", 24, 80, TextArea.SCROLLBARS_NONE);
108         dummy.setEditable(false);
109         mainbody = dummy;
110     }
111
112     // add the main body
113
GridBagConstraints gb = new GridBagConstraints();
114     gb.gridwidth = GridBagConstraints.REMAINDER;
115     gb.fill = GridBagConstraints.BOTH;
116     gb.weightx = 1.0;
117     gb.weighty = 1.0;
118     add(mainbody, gb);
119
120     invalidate();
121     validate();
122     }
123
124     protected void addToolbar() {
125     GridBagConstraints gb = new GridBagConstraints();
126     gb.gridheight = 1;
127     gb.gridwidth = 1;
128     gb.fill = GridBagConstraints.NONE;
129     gb.anchor = GridBagConstraints.WEST;
130     gb.weightx = 0.0;
131     gb.weighty = 0.0;
132     gb.insets = new Insets(4,4,4,4);
133
134     // structure button
135
gb.gridwidth = GridBagConstraints.REMAINDER; // only for the last one
136
Button b = new Button("Structure");
137     b.addActionListener( new StructureAction());
138     add(b, gb);
139     }
140
141     protected void loadHeaders() {
142     // setup what we want in our viewer
143
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
144
145     // date
146
sb.append("Date: ");
147     try {
148         Date JavaDoc duh = displayed.getSentDate();
149         if (duh != null) {
150         sb.append(duh.toString());
151         } else {
152         sb.append("Unknown");
153         }
154         
155         sb.append("\n");
156
157         // from
158
sb.append("From: ");
159         Address[] adds = displayed.getFrom();
160         if (adds != null && adds.length > 0) {
161         sb.append(adds[0].toString());
162         }
163         sb.append("\n");
164     
165         // to
166
sb.append("To: ");
167         adds = displayed.getRecipients(Message.RecipientType.TO);
168         if (adds != null && adds.length > 0) {
169         sb.append(adds[0].toString());
170         }
171         sb.append("\n");
172
173         // subject
174
sb.append("Subject: ");
175         sb.append(displayed.getSubject());
176         
177         headers.setText(sb.toString());
178     } catch (MessagingException me) {
179         headers.setText("");
180     }
181     }
182
183     protected Component getBodyComponent() {
184     //------------
185
// now get a content viewer for the main type...
186
//------------
187
try {
188         DataHandler dh = displayed.getDataHandler();
189         CommandInfo ci = dh.getCommand("view");
190         if (ci == null) {
191         throw new MessagingException("view command failed on: " +
192                          displayed.getContentType());
193         }
194     
195         Object JavaDoc bean = dh.getBean(ci);
196         if (bean instanceof Component) {
197         return (Component)bean;
198         } else {
199         throw new MessagingException("bean is not a component " +
200                          bean.getClass().toString());
201         }
202     } catch (MessagingException me) {
203         return new Label(me.toString());
204     }
205     }
206     
207     /**
208      * the CommandObject method to accept our DataHandler
209      * @param dh the datahandler used to get the content
210      */

211     public void setCommandContext(String JavaDoc verb,
212                   DataHandler dh) throws IOException JavaDoc {
213     this.verb = verb;
214     dataHandler = dh;
215     
216     Object JavaDoc o = dh.getContent();
217     if (o instanceof Message) {
218         setMessage((Message)o);
219     }
220     else {
221         System.out.println(
222         "MessageViewer - content not a Message object, " + o);
223         if (o != null){
224         System.out.println(o.getClass().toString());
225         }
226     }
227     }
228
229
230     class StructureAction implements ActionListener {
231     StringBuffer JavaDoc sb;
232     
233     public void actionPerformed(ActionEvent e) {
234         System.out.println("\n\nMessage Structure");
235         dumpPart("", displayed);
236     }
237
238     protected void dumpPart(String JavaDoc prefix, Part p) {
239         try {
240         System.out.println(prefix + "----------------");
241         System.out.println(prefix +
242                    "Content-Type: " + p.getContentType());
243         System.out.println(prefix +
244                    "Class: " + p.getClass().toString());
245                 
246         Object JavaDoc o = p.getContent();
247         if (o == null) {
248             System.out.println(prefix + "Content: is null");
249         } else {
250             System.out.println(prefix +
251                        "Content: " + o.getClass().toString());
252         }
253         
254         if (o instanceof Multipart) {
255             String JavaDoc newpref = prefix + "\t";
256             Multipart mp = (Multipart)o;
257             int count = mp.getCount();
258             for (int i = 0; i < count; i++) {
259             dumpPart(newpref, mp.getBodyPart(i));
260             }
261         }
262         } catch (MessagingException e) {
263         e.printStackTrace();
264         } catch (IOException JavaDoc ioex) {
265         System.out.println("Cannot get content" + ioex.getMessage());
266         }
267     }
268     }
269 }
270
Popular Tags