Skip to content

Package: MultipartViewer$AttachmentViewer

MultipartViewer$AttachmentViewer

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