KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > mail > ConfirmSendDialog


1 /*
2  * ConfirmSendDialog.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: ConfirmSendDialog.java,v 1.4 2004/08/27 16:33:33 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j.mail;
23
24 import java.awt.event.ActionEvent JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import javax.swing.Box JavaDoc;
27 import javax.swing.BoxLayout JavaDoc;
28 import javax.swing.JPanel JavaDoc;
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 JavaDoc fromKey = "confirmSend.from";
45     private static final String JavaDoc bccAddSenderKey = "confirmSend.bccAddSender";
46     private static final String JavaDoc bccAddOtherKey = "confirmSend.bccAddOther";
47     private static final String JavaDoc bccOtherKey = "confirmSend.bccOther";
48     private static final String JavaDoc 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 JavaDoc from;
65     private String JavaDoc smtp;
66     private boolean bccAddSender;
67     private boolean bccAddOther;
68     private String JavaDoc 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 JavaDoc panel = new JPanel JavaDoc();
101         panel.setAlignmentX(LEFT_ALIGNMENT);
102         panel.setLayout(new BoxLayout JavaDoc(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 JavaDoc getFrom()
132     {
133         return from;
134     }
135
136     public String JavaDoc 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 JavaDoc 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         // Validation.
164
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         // Save state.
188
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 JavaDoc e)
202     {
203         // Treat the user's mapping(s) for the send command like Enter.
204
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 JavaDoc e)
215     {
216         String JavaDoc 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