1 29 30 package echo2example.email; 31 32 import java.util.StringTokenizer ; 33 34 import javax.mail.Message ; 35 import javax.mail.MessagingException ; 36 import javax.mail.Transport ; 37 import javax.mail.internet.AddressException ; 38 import javax.mail.internet.InternetAddress ; 39 import javax.mail.internet.MimeMessage ; 40 41 import nextapp.echo2.app.Button; 42 import nextapp.echo2.app.Column; 43 import nextapp.echo2.app.Extent; 44 import nextapp.echo2.app.Grid; 45 import nextapp.echo2.app.Insets; 46 import nextapp.echo2.app.Label; 47 import nextapp.echo2.app.Row; 48 import nextapp.echo2.app.SplitPane; 49 import nextapp.echo2.app.TextArea; 50 import nextapp.echo2.app.TextField; 51 import nextapp.echo2.app.WindowPane; 52 import nextapp.echo2.app.event.ActionEvent; 53 import nextapp.echo2.app.event.ActionListener; 54 import nextapp.echo2.app.event.WindowPaneEvent; 55 import nextapp.echo2.app.event.WindowPaneListener; 56 57 60 public class ComposeWindow extends WindowPane { 61 62 private static final Extent DIALOG_OFFSET_X = new Extent(80); 63 private static final Extent DIALOG_OFFSET_Y = new Extent(26); 64 65 75 private static void addRecipients(Message message, Message.RecipientType recipientType, String recipients) 76 throws AddressException , MessagingException { 77 if (recipients != null && recipients.trim().length() > 0) { 78 StringTokenizer tokenizer = new StringTokenizer (recipients, ","); 80 while (tokenizer.hasMoreTokens()) { 81 String recipient = tokenizer.nextToken(); 83 message.addRecipient(recipientType, new InternetAddress (recipient)); 84 } 85 } 86 } 87 88 private TextField toField; 89 private TextField ccField; 90 private TextField bccField; 91 private TextField subjectField; 92 private TextArea messageField; 93 94 100 public ComposeWindow(Message replyMessage) { 101 super(Messages.getString("ComposeWindow.Title"), new Extent(600), new Extent(480)); 102 setResizable(false); 103 setDefaultCloseOperation(WindowPane.DO_NOTHING_ON_CLOSE); 104 setStyleName("Default"); 105 106 addWindowPaneListener(new WindowPaneListener() { 107 public void windowPaneClosing(WindowPaneEvent e) { 108 processDiscard(); 109 } 110 }); 111 112 SplitPane mainPane = new SplitPane(SplitPane.ORIENTATION_VERTICAL, new Extent(32)); 113 add(mainPane); 114 115 Row controlPane = new Row(); 116 controlPane.setStyleName("ControlPane"); 117 mainPane.add(controlPane); 118 119 Button sendButton = new Button(Messages.getString("ComposeWindow.SendButton"), 120 Styles.ICON_24_YES); 121 sendButton.setStyleName("ControlPane.Button"); 122 sendButton.addActionListener(new ActionListener() { 123 public void actionPerformed(ActionEvent e) { 124 if (sendMessage()) { 125 ((EmailApp) getApplicationInstance()).getDefaultWindow().getContent().remove(ComposeWindow.this); 126 } 127 } 128 }); 129 controlPane.add(sendButton); 130 131 Button cancelButton = new Button(Messages.getString("ComposeWindow.DiscardButton"), 132 Styles.ICON_24_NO); 133 cancelButton.setStyleName("ControlPane.Button"); 134 cancelButton.addActionListener(new ActionListener() { 135 public void actionPerformed(ActionEvent e) { 136 processDiscard(); 137 } 138 }); 139 controlPane.add(cancelButton); 140 141 Column layoutColumn = new Column(); 142 layoutColumn.setCellSpacing(new Extent(10)); 143 layoutColumn.setInsets(new Insets(10)); 144 mainPane.add(layoutColumn); 145 146 Grid headerGrid = new Grid(); 147 headerGrid.setInsets(new Insets(0, 2)); 148 layoutColumn.add(headerGrid); 149 150 Label label; 151 152 label = new Label(Messages.getString("Message.PromptLabel.To")); 153 headerGrid.add(label); 154 155 toField = new TextField(); 156 toField.setStyleName("Default"); 157 toField.setWidth(new Extent(450)); 158 headerGrid.add(toField); 159 160 label = new Label(Messages.getString("Message.PromptLabel.Cc")); 161 headerGrid.add(label); 162 163 ccField = new TextField(); 164 ccField.setStyleName("Default"); 165 ccField.setWidth(new Extent(450)); 166 headerGrid.add(ccField); 167 168 label = new Label(Messages.getString("Message.PromptLabel.Bcc")); 169 headerGrid.add(label); 170 171 bccField = new TextField(); 172 bccField.setStyleName("Default"); 173 bccField.setWidth(new Extent(450)); 174 headerGrid.add(bccField); 175 176 label = new Label(Messages.getString("Message.PromptLabel.Subject")); 177 headerGrid.add(label); 178 179 subjectField = new TextField(); 180 subjectField.setStyleName("Default"); 181 subjectField.setWidth(new Extent(450)); 182 headerGrid.add(subjectField); 183 184 messageField = new TextArea(); 185 messageField.setStyleName("Default"); 186 messageField.setWidth(new Extent(520)); 187 messageField.setHeight(new Extent(18, Extent.EM)); 188 layoutColumn.add(messageField); 189 190 if (replyMessage == null) { 191 EmailApp.getActive().setFocusedComponent(toField); 192 } else { 193 EmailApp.getActive().setFocusedComponent(messageField); 194 try { 195 toField.setText(MessageUtil.clean(replyMessage.getFrom()[0].toString(), -1, -1)); 196 subjectField.setText(MessageUtil.clean(replyMessage.getSubject(), -1, -1)); 197 } catch (MessagingException ex) { 198 EmailApp.getApp().processFatalException(ex); 199 } 200 } 201 } 202 203 206 private void processDiscard() { 207 if (messageField.getText().trim().length() > 0) { 208 MessageDialog confirmDialog = new MessageDialog(Messages.getString("ComposeWindow.ConfirmDiscard.Title"), 209 Messages.getString("ComposeWindow.ConfirmDiscard.Message"), MessageDialog.TYPE_CONFIRM, 210 MessageDialog.CONTROLS_YES_NO); 211 confirmDialog.setPositionX(Extent.add(getPositionX(), DIALOG_OFFSET_X)); 212 confirmDialog.setPositionY(Extent.add(getPositionY(), DIALOG_OFFSET_Y)); 213 confirmDialog.addActionListener(new ActionListener() { 214 public void actionPerformed(ActionEvent e) { 215 if (MessageDialog.COMMAND_OK.equals(e.getActionCommand())) { 216 getParent().remove(ComposeWindow.this); 217 } 218 } 219 }); 220 getApplicationInstance().getDefaultWindow().getContent().add(confirmDialog); 221 } else { 222 getParent().remove(this); 223 } 224 } 225 226 233 private boolean sendMessage() { 234 MimeMessage message = new MimeMessage (((EmailApp) getApplicationInstance()).getMailSession()); 235 try { 236 String errorMessage = null; 238 if (toField.getText().length() == 0) { 239 errorMessage = Messages.getString("ComposeWindow.SendError.NoToField"); 240 } else if (subjectField.getText().length() == 0) { 241 errorMessage = Messages.getString("ComposeWindow.SendError.NoSubjectField"); 242 } else if (messageField.getText().length() == 0) { 243 errorMessage = Messages.getString("ComposeWindow.SendError.NoMessageContent"); 244 } 245 if (errorMessage != null) { 246 MessageDialog messageDialog = new MessageDialog(Messages.getString("ComposeWindow.SendError.Title"), 247 errorMessage, MessageDialog.TYPE_ERROR, MessageDialog.CONTROLS_OK); 248 messageDialog.setPositionX(Extent.add(getPositionX(), DIALOG_OFFSET_X)); 249 messageDialog.setPositionY(Extent.add(getPositionY(), DIALOG_OFFSET_Y)); 250 getApplicationInstance().getDefaultWindow().getContent().add(messageDialog); 251 return false; 252 } 253 254 message.setFrom(new InternetAddress (((EmailApp) getApplicationInstance()).getEmailAddress())); 256 257 addRecipients(message, Message.RecipientType.TO, toField.getText()); 259 addRecipients(message, Message.RecipientType.CC, ccField.getText()); 260 addRecipients(message, Message.RecipientType.BCC, bccField.getText()); 261 262 message.setSubject(subjectField.getText()); 264 message.setText(messageField.getText()); 265 266 if (!EmailApp.FAUX_MODE) { 268 Transport.send(message); 269 } 270 return true; 271 } catch (AddressException ex) { 272 MessageDialog messageDialog = new MessageDialog(Messages.getString("ComposeWindow.SendError.Title"), 274 Messages.getString("ComposeWindow.SendError.InvalidAddress"), MessageDialog.TYPE_ERROR, 275 MessageDialog.CONTROLS_OK); 276 messageDialog.setPositionX(Extent.add(getPositionX(), DIALOG_OFFSET_X)); 277 messageDialog.setPositionY(Extent.add(getPositionY(), DIALOG_OFFSET_Y)); 278 getApplicationInstance().getDefaultWindow().getContent().add(messageDialog); 279 return false; 280 } catch (MessagingException ex) { 281 EmailApp.getApp().processFatalException(ex); 283 return false; 284 } 285 } 286 } 287 | Popular Tags |