1 38 44 45 import java.awt.*; 46 import java.awt.event.*; 47 import javax.mail.*; 48 import javax.activation.*; 49 import java.util.Date ; 50 import java.io.IOException ; 51 import javax.swing.JPanel ; 52 53 58 59 public class MessageViewer extends JPanel implements CommandObject { 60 61 Message displayed = null; 62 DataHandler dataHandler = null; 63 String verb = null; 64 Component mainbody; 65 TextArea headers; 66 67 public MessageViewer() { 68 this(null); 69 } 70 71 public MessageViewer(Message what) { 72 super(new GridBagLayout()); 74 75 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 headers = new TextArea("", 4, 80, TextArea.SCROLLBARS_NONE); 86 headers.setEditable(false); 87 add(headers, gb); 88 89 setMessage(what); 91 } 92 93 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 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 gb.gridwidth = GridBagConstraints.REMAINDER; Button b = new Button("Structure"); 137 b.addActionListener( new StructureAction()); 138 add(b, gb); 139 } 140 141 protected void loadHeaders() { 142 StringBuffer sb = new StringBuffer (); 144 145 sb.append("Date: "); 147 try { 148 Date 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 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 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 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 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 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 211 public void setCommandContext(String verb, 212 DataHandler dh) throws IOException { 213 this.verb = verb; 214 dataHandler = dh; 215 216 Object 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 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 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 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 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 ioex) { 265 System.out.println("Cannot get content" + ioex.getMessage()); 266 } 267 } 268 } 269 } 270 | Popular Tags |