1 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 ; 46 47 48 54 55 public class MultipartViewer extends JPanel implements CommandObject { 56 57 protected DataHandler dh = null; 58 protected String verb = null; 59 60 public MultipartViewer() { 61 super(new GridBagLayout()); 62 } 63 64 65 public void setCommandContext(String verb, DataHandler dh) throws IOException { 66 this.verb = verb; 67 this.dh = dh; 68 69 Object 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 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 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 try { 100 int count = mp.getCount(); 101 102 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(int i = 1; i < count; i++) { BodyPart curr = mp.getBodyPart(i); 114 String 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 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 content) { 165 String 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 |