Skip to content

Package: MessageViewer

MessageViewer

nameinstructionbranchcomplexitylinemethod
MessageViewer()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
MessageViewer(Message)
M: 54 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 15 C: 0
0%
M: 1 C: 0
0%
addToolbar()
M: 50 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 13 C: 0
0%
M: 1 C: 0
0%
getBodyComponent()
M: 55 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 12 C: 0
0%
M: 1 C: 0
0%
loadHeaders()
M: 99 C: 0
0%
M: 10 C: 0
0%
M: 6 C: 0
0%
M: 24 C: 0
0%
M: 1 C: 0
0%
setCommandContext(String, DataHandler)
M: 35 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 9 C: 0
0%
M: 1 C: 0
0%
setMessage(Message)
M: 63 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 19 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
3: *
4: * This program and the accompanying materials are made available under the
5: * terms of the Eclipse Distribution License v. 1.0, which is available at
6: * http://www.eclipse.org/org/documents/edl-v10.php.
7: *
8: * SPDX-License-Identifier: BSD-3-Clause
9: */
10:
11: import java.awt.*;
12: import java.awt.event.*;
13: import jakarta.mail.*;
14: import jakarta.activation.*;
15: import java.util.Date;
16: import java.io.IOException;
17: import javax.swing.JPanel;
18:
19: /**
20: * @author        Christopher Cotton
21: * @author        Bill Shannon
22: */
23:
24: public class MessageViewer extends JPanel implements CommandObject {
25:
26: Message        displayed = null;
27: DataHandler        dataHandler = null;
28: String        verb = null;
29: Component        mainbody;
30: TextArea        headers;
31:
32: public MessageViewer() {
33:         this(null);
34: }
35:
36: public MessageViewer(Message what) {
37:         // set our layout
38:         super(new GridBagLayout());
39:
40:         // add the toolbar
41:         addToolbar();
42:
43:         GridBagConstraints gb = new GridBagConstraints();
44:         gb.gridwidth = GridBagConstraints.REMAINDER;
45:         gb.fill = GridBagConstraints.BOTH;
46:         gb.weightx = 1.0;
47:         gb.weighty = 0.0;
48:
49:         // add the headers
50:         headers = new TextArea("", 4, 80, TextArea.SCROLLBARS_NONE);
51:         headers.setEditable(false);
52:         add(headers, gb);
53:
54:         // now display our message
55:         setMessage(what);
56: }
57:
58: /**
59: * sets the current message to be displayed in the viewer
60: */
61: public void setMessage(Message what) {
62:         displayed = what;
63:
64:•        if (mainbody != null)
65:          remove(mainbody);
66:
67:•        if (what != null) {
68:          loadHeaders();
69:          mainbody = getBodyComponent();
70:         } else {
71:          headers.setText("");
72:          TextArea dummy = new TextArea("", 24, 80, TextArea.SCROLLBARS_NONE);
73:          dummy.setEditable(false);
74:          mainbody = dummy;
75:         }
76:
77:         // add the main body
78:         GridBagConstraints gb = new GridBagConstraints();
79:         gb.gridwidth = GridBagConstraints.REMAINDER;
80:         gb.fill = GridBagConstraints.BOTH;
81:         gb.weightx = 1.0;
82:         gb.weighty = 1.0;
83:         add(mainbody, gb);
84:
85:         invalidate();
86:         validate();
87: }
88:
89: protected void addToolbar() {
90:         GridBagConstraints gb = new GridBagConstraints();
91:         gb.gridheight = 1;
92:         gb.gridwidth = 1;
93:         gb.fill = GridBagConstraints.NONE;
94:         gb.anchor = GridBagConstraints.WEST;
95:         gb.weightx = 0.0;
96:         gb.weighty = 0.0;
97:         gb.insets = new Insets(4,4,4,4);
98:
99:         // structure button
100:         gb.gridwidth = GridBagConstraints.REMAINDER; // only for the last one
101:         Button b = new Button("Structure");
102:         b.addActionListener( new StructureAction());
103:         add(b, gb);
104: }
105:
106: protected void loadHeaders() {
107:         // setup what we want in our viewer
108:         StringBuffer sb = new StringBuffer();
109:
110:         // date
111:         sb.append("Date: ");
112:         try {
113:          Date duh = displayed.getSentDate();
114:•         if (duh != null) {
115:                 sb.append(duh.toString());
116:          } else {
117:                 sb.append("Unknown");
118:          }
119:         
120:          sb.append("\n");
121:
122:          // from
123:          sb.append("From: ");
124:          Address[] adds = displayed.getFrom();
125:•         if (adds != null && adds.length > 0) {
126:                 sb.append(adds[0].toString());
127:          }
128:          sb.append("\n");
129:
130:          // to
131:          sb.append("To: ");
132:          adds = displayed.getRecipients(Message.RecipientType.TO);
133:•         if (adds != null && adds.length > 0) {
134:                 sb.append(adds[0].toString());
135:          }
136:          sb.append("\n");
137:
138:          // subject
139:          sb.append("Subject: ");
140:          sb.append(displayed.getSubject());
141:         
142:          headers.setText(sb.toString());
143:         } catch (MessagingException me) {
144:          headers.setText("");
145:         }
146: }
147:
148: protected Component getBodyComponent() {
149:         //------------
150:         // now get a content viewer for the main type...
151:         //------------
152:         try {
153:          DataHandler dh = displayed.getDataHandler();
154:          CommandInfo ci = dh.getCommand("view");
155:•         if (ci == null) {
156:                 throw new MessagingException("view command failed on: " +
157:                                          displayed.getContentType());
158:          }
159:
160:          Object bean = dh.getBean(ci);
161:•         if (bean instanceof Component) {
162:                 return (Component)bean;
163:          } else {
164:                 throw new MessagingException("bean is not a component " +
165:                                          bean.getClass().toString());
166:          }
167:         } catch (MessagingException me) {
168:          return new Label(me.toString());
169:         }
170: }
171:
172: /**
173: * the CommandObject method to accept our DataHandler
174: * @param dh        the datahandler used to get the content
175: */
176: public void setCommandContext(String verb,
177:                                  DataHandler dh) throws IOException {
178:         this.verb = verb;
179:         dataHandler = dh;
180:
181:         Object o = dh.getContent();
182:•        if (o instanceof Message) {
183:          setMessage((Message)o);
184:         }
185:         else {
186:          System.out.println(
187:                 "MessageViewer - content not a Message object, " + o);
188:•         if (o != null){
189:                 System.out.println(o.getClass().toString());
190:          }
191:         }
192: }
193:
194:
195: class StructureAction implements ActionListener {
196:         StringBuffer sb;
197:
198:         public void actionPerformed(ActionEvent e) {
199:          System.out.println("\n\nMessage Structure");
200:          dumpPart("", displayed);
201:         }
202:
203:         protected void dumpPart(String prefix, Part p) {
204:          try {
205:                 System.out.println(prefix + "----------------");
206:                 System.out.println(prefix +
207:                                  "Content-Type: " + p.getContentType());
208:                 System.out.println(prefix +
209:                                  "Class: " + p.getClass().toString());
210:                         
211:                 Object o = p.getContent();
212:                 if (o == null) {
213:                  System.out.println(prefix + "Content: is null");
214:                 } else {
215:                  System.out.println(prefix +
216:                                  "Content: " + o.getClass().toString());
217:                 }
218:
219:                 if (o instanceof Multipart) {
220:                  String newpref = prefix + "\t";
221:                  Multipart mp = (Multipart)o;
222:                  int count = mp.getCount();
223:                  for (int i = 0; i < count; i++) {
224:                         dumpPart(newpref, mp.getBodyPart(i));
225:                  }
226:                 }
227:          } catch (MessagingException e) {
228:                 e.printStackTrace();
229:          } catch (IOException ioex) {
230:                 System.out.println("Cannot get content" + ioex.getMessage());
231:          }
232:         }
233: }
234: }