Skip to content

Package: MessageViewer$StructureAction

MessageViewer$StructureAction

nameinstructionbranchcomplexitylinemethod
MessageViewer.StructureAction(MessageViewer)
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
actionPerformed(ActionEvent)
M: 10 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
dumpPart(String, Part)
M: 116 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 22 C: 0
0%
M: 1 C: 0
0%

Coverage

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