1 21 22 package org.armedbear.j.mail; 23 24 import java.awt.event.ActionEvent ; 25 import java.awt.event.KeyEvent ; 26 import javax.swing.Box ; 27 import javax.swing.BoxLayout ; 28 import javax.swing.JPanel ; 29 import org.armedbear.j.AbstractDialog; 30 import org.armedbear.j.CheckBox; 31 import org.armedbear.j.Editor; 32 import org.armedbear.j.History; 33 import org.armedbear.j.HistoryTextField; 34 import org.armedbear.j.KeyMapping; 35 import org.armedbear.j.Label; 36 import org.armedbear.j.MessageDialog; 37 import org.armedbear.j.Property; 38 import org.armedbear.j.SessionProperties; 39 40 public final class ConfirmSendDialog extends AbstractDialog 41 { 42 private static final int TEXTFIELD_WIDTH = 22; 43 44 private static final String fromKey = "confirmSend.from"; 45 private static final String bccAddSenderKey = "confirmSend.bccAddSender"; 46 private static final String bccAddOtherKey = "confirmSend.bccAddOther"; 47 private static final String bccOtherKey = "confirmSend.bccOther"; 48 private static final String smtpKey = "confirmSend.smtp"; 49 50 private final Editor editor; 51 private final SendMail sm; 52 private final SessionProperties sessionProperties; 53 54 private final HistoryTextField fromTextField; 55 private final CheckBox bccAddSenderCheckBox; 56 private final CheckBox bccAddOtherCheckBox; 57 private final HistoryTextField bccOtherTextField; 58 private final HistoryTextField smtpTextField; 59 60 private final History fromHistory; 61 private final History bccOtherHistory; 62 private final History smtpHistory; 63 64 private String from; 65 private String smtp; 66 private boolean bccAddSender; 67 private boolean bccAddOther; 68 private String bccOther; 69 70 public ConfirmSendDialog(Editor editor, SendMail sm) 71 { 72 super(editor, "Confirm Send", true); 73 this.editor = editor; 74 this.sm = sm; 75 sessionProperties = Editor.getSessionProperties(); 76 Label label = new Label("From:"); 77 label.setDisplayedMnemonic('F'); 78 fromTextField = new HistoryTextField(TEXTFIELD_WIDTH); 79 fromHistory = new History(fromKey); 80 fromTextField.setHistory(fromHistory); 81 if (fromHistory.size() == 0) { 82 MailAddress ma = Mail.getUserMailAddress(); 83 if (ma != null) 84 fromHistory.append(ma.toString()); 85 } 86 fromTextField.recallLast(); 87 addLabelAndTextField(label, fromTextField); 88 addVerticalStrut(); 89 bccAddSenderCheckBox = new CheckBox("Bcc sender", 90 sessionProperties.getBooleanProperty(bccAddSenderKey, false)); 91 bccAddSenderCheckBox.setMnemonic('S'); 92 bccAddSenderCheckBox.addKeyListener(this); 93 mainPanel.add(bccAddSenderCheckBox); 94 bccAddOtherCheckBox = new CheckBox("Bcc other:", 95 sessionProperties.getBooleanProperty(bccAddOtherKey, false)); 96 bccAddOtherCheckBox.setMnemonic('O'); 97 bccAddOtherCheckBox.addKeyListener(this); 98 bccAddOtherCheckBox.addActionListener(this); 99 mainPanel.add(bccAddOtherCheckBox); 100 JPanel panel = new JPanel (); 101 panel.setAlignmentX(LEFT_ALIGNMENT); 102 panel.setLayout(new BoxLayout (panel, BoxLayout.X_AXIS)); 103 panel.add(Box.createHorizontalStrut(17)); 104 bccOtherTextField = new HistoryTextField(TEXTFIELD_WIDTH); 105 bccOtherHistory = new History(bccOtherKey); 106 bccOtherTextField.setHistory(bccOtherHistory); 107 bccOtherTextField.recallLast(); 108 bccOtherTextField.addKeyListener(this); 109 bccOtherTextField.setEnabled(bccAddOtherCheckBox.isSelected()); 110 panel.add(bccOtherTextField); 111 mainPanel.add(panel); 112 addVerticalStrut(); 113 smtpTextField = new HistoryTextField(TEXTFIELD_WIDTH); 114 smtpHistory = new History(smtpKey); 115 if (smtpHistory.size() == 0) { 116 smtp = Editor.preferences().getStringProperty(Property.SMTP); 117 if (smtp != null) 118 smtpHistory.append(smtp); 119 } 120 smtpTextField.setHistory(smtpHistory); 121 smtpTextField.recallLast(); 122 label = new Label("SMTP server:"); 123 label.setDisplayedMnemonic('M'); 124 addLabelAndTextField(label, smtpTextField); 125 addVerticalStrut(); 126 addOKCancel(); 127 pack(); 128 fromTextField.requestFocus(); 129 } 130 131 public String getFrom() 132 { 133 return from; 134 } 135 136 public String getSmtp() 137 { 138 return smtp; 139 } 140 141 public boolean bccAddSender() 142 { 143 return bccAddSender; 144 } 145 146 public boolean bccAddOther() 147 { 148 return bccAddOther; 149 } 150 151 public String getBccOther() 152 { 153 return bccOther; 154 } 155 156 protected void ok() 157 { 158 from = fromTextField.getText().trim(); 159 bccAddSender = bccAddSenderCheckBox.isSelected(); 160 bccAddOther = bccAddOtherCheckBox.isSelected(); 161 bccOther = bccOtherTextField.getText().trim(); 162 smtp = smtpTextField.getText().trim(); 163 if (from.length() == 0 || from.indexOf('@') < 0) { 165 MessageDialog.showMessageDialog( 166 "You must enter a valid \"From\" address", 167 "Error"); 168 fromTextField.requestFocus(); 169 return; 170 } 171 if (bccAddOther) { 172 if (bccOther.length() == 0 || bccOther.indexOf('@') < 0) { 173 MessageDialog.showMessageDialog( 174 "You must enter a valid \"Bcc\" address", 175 "Error"); 176 bccOtherTextField.requestFocus(); 177 return; 178 } 179 } 180 if (smtp.length() == 0) { 181 MessageDialog.showMessageDialog( 182 "You must specify the SMTP server", 183 "Error"); 184 smtpTextField.requestFocus(); 185 return; 186 } 187 fromHistory.append(from); 189 fromHistory.save(); 190 sessionProperties.setBooleanProperty(bccAddSenderKey, bccAddSender); 191 sessionProperties.setBooleanProperty(bccAddOtherKey, bccAddOther); 192 if (bccOther.length() > 0) { 193 bccOtherHistory.append(bccOther); 194 bccOtherHistory.save(); 195 } 196 smtpHistory.append(smtp); 197 smtpHistory.save(); 198 dispose(); 199 } 200 201 public void keyPressed(KeyEvent e) 202 { 203 KeyMapping mapping = 205 editor.getKeyMapping(e.getKeyChar(), e.getKeyCode(), 206 e.getModifiers()); 207 if (mapping != null && mapping.getCommand() == "send") { 208 e.consume(); 209 enter(); 210 } else 211 super.keyPressed(e); 212 } 213 214 public void actionPerformed(ActionEvent e) 215 { 216 String cmd = e.getActionCommand(); 217 if (cmd != null && cmd.equals(bccAddOtherCheckBox.getText())) 218 bccOtherTextField.setEnabled(bccAddOtherCheckBox.isSelected()); 219 else 220 super.actionPerformed(e); 221 } 222 } 223 | Popular Tags |