001: /*
002: * ConfirmSendDialog.java
003: *
004: * Copyright (C) 2002-2004 Peter Graves
005: * $Id: ConfirmSendDialog.java,v 1.4 2004/08/27 16:33:33 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j.mail;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.KeyEvent;
026: import javax.swing.Box;
027: import javax.swing.BoxLayout;
028: import javax.swing.JPanel;
029: import org.armedbear.j.AbstractDialog;
030: import org.armedbear.j.CheckBox;
031: import org.armedbear.j.Editor;
032: import org.armedbear.j.History;
033: import org.armedbear.j.HistoryTextField;
034: import org.armedbear.j.KeyMapping;
035: import org.armedbear.j.Label;
036: import org.armedbear.j.MessageDialog;
037: import org.armedbear.j.Property;
038: import org.armedbear.j.SessionProperties;
039:
040: public final class ConfirmSendDialog extends AbstractDialog {
041: private static final int TEXTFIELD_WIDTH = 22;
042:
043: private static final String fromKey = "confirmSend.from";
044: private static final String bccAddSenderKey = "confirmSend.bccAddSender";
045: private static final String bccAddOtherKey = "confirmSend.bccAddOther";
046: private static final String bccOtherKey = "confirmSend.bccOther";
047: private static final String smtpKey = "confirmSend.smtp";
048:
049: private final Editor editor;
050: private final SendMail sm;
051: private final SessionProperties sessionProperties;
052:
053: private final HistoryTextField fromTextField;
054: private final CheckBox bccAddSenderCheckBox;
055: private final CheckBox bccAddOtherCheckBox;
056: private final HistoryTextField bccOtherTextField;
057: private final HistoryTextField smtpTextField;
058:
059: private final History fromHistory;
060: private final History bccOtherHistory;
061: private final History smtpHistory;
062:
063: private String from;
064: private String smtp;
065: private boolean bccAddSender;
066: private boolean bccAddOther;
067: private String bccOther;
068:
069: public ConfirmSendDialog(Editor editor, SendMail sm) {
070: super (editor, "Confirm Send", true);
071: this .editor = editor;
072: this .sm = sm;
073: sessionProperties = Editor.getSessionProperties();
074: Label label = new Label("From:");
075: label.setDisplayedMnemonic('F');
076: fromTextField = new HistoryTextField(TEXTFIELD_WIDTH);
077: fromHistory = new History(fromKey);
078: fromTextField.setHistory(fromHistory);
079: if (fromHistory.size() == 0) {
080: MailAddress ma = Mail.getUserMailAddress();
081: if (ma != null)
082: fromHistory.append(ma.toString());
083: }
084: fromTextField.recallLast();
085: addLabelAndTextField(label, fromTextField);
086: addVerticalStrut();
087: bccAddSenderCheckBox = new CheckBox("Bcc sender",
088: sessionProperties.getBooleanProperty(bccAddSenderKey,
089: false));
090: bccAddSenderCheckBox.setMnemonic('S');
091: bccAddSenderCheckBox.addKeyListener(this );
092: mainPanel.add(bccAddSenderCheckBox);
093: bccAddOtherCheckBox = new CheckBox("Bcc other:",
094: sessionProperties.getBooleanProperty(bccAddOtherKey,
095: false));
096: bccAddOtherCheckBox.setMnemonic('O');
097: bccAddOtherCheckBox.addKeyListener(this );
098: bccAddOtherCheckBox.addActionListener(this );
099: 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()
117: .getStringProperty(Property.SMTP);
118: if (smtp != null)
119: smtpHistory.append(smtp);
120: }
121: smtpTextField.setHistory(smtpHistory);
122: smtpTextField.recallLast();
123: label = new Label("SMTP server:");
124: label.setDisplayedMnemonic('M');
125: addLabelAndTextField(label, smtpTextField);
126: addVerticalStrut();
127: addOKCancel();
128: pack();
129: fromTextField.requestFocus();
130: }
131:
132: public String getFrom() {
133: return from;
134: }
135:
136: public String getSmtp() {
137: return smtp;
138: }
139:
140: public boolean bccAddSender() {
141: return bccAddSender;
142: }
143:
144: public boolean bccAddOther() {
145: return bccAddOther;
146: }
147:
148: public String getBccOther() {
149: return bccOther;
150: }
151:
152: protected void ok() {
153: from = fromTextField.getText().trim();
154: bccAddSender = bccAddSenderCheckBox.isSelected();
155: bccAddOther = bccAddOtherCheckBox.isSelected();
156: bccOther = bccOtherTextField.getText().trim();
157: smtp = smtpTextField.getText().trim();
158: // Validation.
159: if (from.length() == 0 || from.indexOf('@') < 0) {
160: MessageDialog.showMessageDialog(
161: "You must enter a valid \"From\" address", "Error");
162: fromTextField.requestFocus();
163: return;
164: }
165: if (bccAddOther) {
166: if (bccOther.length() == 0 || bccOther.indexOf('@') < 0) {
167: MessageDialog.showMessageDialog(
168: "You must enter a valid \"Bcc\" address",
169: "Error");
170: bccOtherTextField.requestFocus();
171: return;
172: }
173: }
174: if (smtp.length() == 0) {
175: MessageDialog.showMessageDialog(
176: "You must specify the SMTP server", "Error");
177: smtpTextField.requestFocus();
178: return;
179: }
180: // Save state.
181: fromHistory.append(from);
182: fromHistory.save();
183: sessionProperties.setBooleanProperty(bccAddSenderKey,
184: bccAddSender);
185: sessionProperties.setBooleanProperty(bccAddOtherKey,
186: bccAddOther);
187: if (bccOther.length() > 0) {
188: bccOtherHistory.append(bccOther);
189: bccOtherHistory.save();
190: }
191: smtpHistory.append(smtp);
192: smtpHistory.save();
193: dispose();
194: }
195:
196: public void keyPressed(KeyEvent e) {
197: // Treat the user's mapping(s) for the send command like Enter.
198: KeyMapping mapping = editor.getKeyMapping(e.getKeyChar(), e
199: .getKeyCode(), e.getModifiers());
200: if (mapping != null && mapping.getCommand() == "send") {
201: e.consume();
202: enter();
203: } else
204: super .keyPressed(e);
205: }
206:
207: public void actionPerformed(ActionEvent e) {
208: String cmd = e.getActionCommand();
209: if (cmd != null && cmd.equals(bccAddOtherCheckBox.getText()))
210: bccOtherTextField.setEnabled(bccAddOtherCheckBox
211: .isSelected());
212: else
213: super.actionPerformed(e);
214: }
215: }
|