1 29 30 package echo2example.email; 31 32 import java.io.IOException ; 33 import java.text.CharacterIterator ; 34 import java.text.StringCharacterIterator ; 35 import java.util.ArrayList ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 39 import javax.mail.Address ; 40 import javax.mail.BodyPart ; 41 import javax.mail.Message ; 42 import javax.mail.MessagingException ; 43 import javax.mail.Multipart ; 44 45 import nextapp.echo2.app.Column; 46 import nextapp.echo2.app.Component; 47 import nextapp.echo2.app.Extent; 48 import nextapp.echo2.app.Font; 49 import nextapp.echo2.app.Grid; 50 import nextapp.echo2.app.Insets; 51 import nextapp.echo2.app.Label; 52 import nextapp.echo2.app.layout.ColumnLayoutData; 53 54 57 public class MessagePane extends Column { 58 59 private static final ColumnLayoutData SPACER_COLUMN_LAYOUT_DATA; 60 static { 61 SPACER_COLUMN_LAYOUT_DATA = new ColumnLayoutData(); 62 SPACER_COLUMN_LAYOUT_DATA.setHeight(new Extent(15)); 63 } 64 65 private Label toFieldPromptLabel; 66 private Label toFieldValueLabel; 67 private Label ccFieldPromptLabel; 68 private Label ccFieldValueLabel; 69 private Label bccFieldPromptLabel; 70 private Label bccFieldValueLabel; 71 private Label subjectFieldValueLabel; 72 private Column messageColumn; 73 74 77 public MessagePane() { 78 super(); 79 setVisible(false); 80 81 setCellSpacing(new Extent(10)); 82 setInsets(new Insets(3)); 83 84 Grid headerGrid = new Grid(); 85 headerGrid.setStyleName("Message.HeaderGrid"); 86 add(headerGrid); 87 88 toFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.To")); 89 toFieldPromptLabel.setStyleName("Message.HeaderGridPrompt"); 90 headerGrid.add(toFieldPromptLabel); 91 92 toFieldValueLabel = new Label(); 93 headerGrid.add(toFieldValueLabel); 94 95 ccFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.Cc")); 96 ccFieldPromptLabel.setStyleName("Message.HeaderGridPrompt"); 97 headerGrid.add(ccFieldPromptLabel); 98 99 ccFieldValueLabel = new Label(); 100 headerGrid.add(ccFieldValueLabel); 101 102 bccFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.Bcc")); 103 bccFieldPromptLabel.setStyleName("Message.HeaderGridPrompt"); 104 headerGrid.add(bccFieldPromptLabel); 105 106 bccFieldValueLabel = new Label(); 107 headerGrid.add(bccFieldValueLabel); 108 109 Label subjectFieldPromptLabel = new Label(Messages.getString("Message.PromptLabel.Subject")); 110 subjectFieldPromptLabel.setStyleName("Message.HeaderGridPrompt"); 111 headerGrid.add(subjectFieldPromptLabel); 112 113 subjectFieldValueLabel = new Label(); 114 headerGrid.add(subjectFieldValueLabel); 115 116 messageColumn = new Column(); 117 messageColumn.setFont(new Font(Font.MONOSPACE, Font.PLAIN, null)); 118 add(messageColumn); 119 } 120 121 127 private String formatRecipients(Message message, Message.RecipientType type) 128 throws MessagingException { 129 Address [] recipients = message.getRecipients(type); 130 if (recipients == null || recipients.length == 0) { 131 return null; 132 } 133 StringBuffer sb = new StringBuffer (); 134 for (int recipientIndex = 0; recipientIndex < recipients.length; ++recipientIndex) { 135 sb.append(recipients[recipientIndex].toString()); 136 if (recipientIndex < recipients.length - 1) { 137 sb.append(", "); 139 } 140 } 141 return sb.toString(); 142 } 143 144 151 private Component[] renderMessageContent(Message message) 152 throws MessagingException { 153 try { 154 Object content = message.getContent(); 155 if (content instanceof String ) { 156 return new Component[]{renderMessageText((String ) content)}; 158 } else if (content instanceof Multipart ) { 159 Multipart multipart = (Multipart ) content; 161 Component[] data = new Component[multipart.getCount()]; 162 for (int index = 0; index < data.length; ++index) { 164 BodyPart part = multipart.getBodyPart(index); 165 Object partContent = part.getContent(); 166 if (partContent instanceof String ) { 167 data[index] = renderMessageText((String ) partContent); 169 } else { 170 data[index] = new Label(part.getContentType()); 172 } 173 } 174 return data; 175 } else { 176 return new Component[]{new Label(Messages.getString("Messages.UnableToParseError"))}; 178 } 179 } catch (IOException ex) { 180 return new Component[]{new Label(Messages.getString("Messages.UnableToParseError"))}; 182 } 183 } 184 185 194 private Component renderMessageText(String text) { 195 List componentList = new ArrayList (); 196 StringBuffer out = new StringBuffer (); 197 CharacterIterator ci = new StringCharacterIterator (text); 198 char ch = ci.first(); 199 while (ch != CharacterIterator.DONE) { 200 if (ch == '\n') { 201 String labelText = out.toString(); 202 Label label = new Label(labelText); 203 if (labelText.trim().length() == 0) { 204 label.setLayoutData(SPACER_COLUMN_LAYOUT_DATA); 205 } 206 componentList.add(label); 207 out = new StringBuffer (); 208 } else if (ch >= 0x20) { 209 out.append(ch); 210 } 211 ch = ci.next(); 212 } 213 Label label = new Label(out.toString()); 214 componentList.add(label); 215 216 if (componentList.size() == 1) { 217 return label; 218 } else { 219 Column column = new Column(); 220 Iterator it = componentList.iterator(); 221 while (it.hasNext()) { 222 column.add((Component) it.next()); 223 } 224 return column; 225 } 226 } 227 228 233 public void setMessage(Message message) 234 throws MessagingException { 235 if (message == null) { 236 setVisible(false); 237 } else { 238 setVisible(true); 239 updateRecipientData(message, Message.RecipientType.TO, toFieldPromptLabel, toFieldValueLabel); 240 updateRecipientData(message, Message.RecipientType.CC, ccFieldPromptLabel, ccFieldValueLabel); 241 updateRecipientData(message, Message.RecipientType.BCC, bccFieldPromptLabel, bccFieldValueLabel); 242 subjectFieldValueLabel.setText(MessageUtil.clean(message.getSubject(), -1, -1)); 243 } 244 245 messageColumn.removeAll(); 246 if (message != null) { 247 Component[] messageComponents = renderMessageContent(message); 248 for (int i = 0; i < messageComponents.length; ++i) { 249 messageColumn.add(messageComponents[i]); 250 } 251 } 252 } 253 254 259 private void updateRecipientData(Message message, Message.RecipientType type, 260 Label promptLabel, Label valueLabel) 261 throws MessagingException { 262 String recipients = MessageUtil.clean(formatRecipients(message, type), -1, -1); 263 if (recipients == null) { 264 promptLabel.setVisible(false); 265 valueLabel.setVisible(false); 266 valueLabel.setText(null); 267 } else { 268 promptLabel.setVisible(true); 269 valueLabel.setVisible(true); 270 valueLabel.setText(recipients); 271 } 272 } 273 } 274 | Popular Tags |