KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > jmail > base > SendMessageDialog


1 package org.lucane.applications.jmail.base;
2
3 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
4  * This file is part of JMail *
5  * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr> *
6  * *
7  * JMail is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * any later version. *
11  * *
12  * JMail 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 along *
18  * with JMail; if not, write to the Free Software Foundation, Inc., *
19  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20  * *
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

22
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.io.*;
26 import java.util.*;
27 import javax.swing.*;
28
29 import org.lucane.applications.jmail.JMailPlugin;
30
31 /** Display the mail composition window */
32 final class SendMessageDialog extends JDialog {
33     /** Language resource */
34     private ResourceBundle msgBundle;
35
36     private JPanel panel;
37
38     private JMenuBar menuBar;
39
40     private JMenu fileMenu;
41     private JMenuItem newMenuItem;
42     private JMenuItem sendMenuItem;
43     private JMenuItem printMenuItem;
44     private JMenuItem closeMenuItem;
45
46     private JMenu viewMenu;
47     private JCheckBoxMenuItem ccMenuItem;
48     private JCheckBoxMenuItem bccMenuItem;
49
50     private JMenu insertMenu;
51     private JMenuItem attachMenuItem;
52     private JMenuItem textMenuItem;
53
54     private JMenu messageMenu;
55     private JMenuItem sendMsgMenuItem;
56
57     private JToolBar toolBar;
58
59     private JButton send;
60     private JButton attachment;
61
62     private JPanel fields;
63
64     private JButton dest;
65     private JTextField destTextField;
66
67     private JButton cc;
68     private JTextField ccTextField;
69
70     private JButton bcc;
71     private JTextField bccTextField;
72
73     private JLabel subject;
74     private JTextField subjectField;
75
76     private JLabel attach;
77     private JList files;
78     private Vector fileNames;
79     private JScrollPane attachPane;
80
81     private JTextArea content;
82     private JScrollPane contentPane;
83
84     private Profile profile;
85
86     private SendMessageListener listener;
87
88     private boolean sentMail;
89
90     private JMailPlugin plugin;
91
92     /**
93      * Default constructor
94      *
95      * @param profile
96      * user profile
97      * @param msgBundle
98      * language resource
99      */

100     protected SendMessageDialog(JMailPlugin plugin, Profile profile, ResourceBundle msgBundle) {
101         //super(msgBundle.getString("SendMessage.frameTitle"));
102
super(
103             (JFrame) null,
104             msgBundle.getString("SendMessage.frameTitle"),
105             true);
106
107         this.plugin = plugin;
108         this.msgBundle = msgBundle;
109         this.profile = profile;
110
111         init();
112
113         if (profile.getSignature() != null
114             && profile.getSignature().compareTo("") != 0)
115             insertTextFile(profile.getSignature());
116
117         setSize(455, 655);
118         pack();
119         setVisible(true);
120     }
121
122     /**
123      * Reply constructor
124      *
125      * @param profile
126      * user profile
127      * @param desti
128      * mail recipient
129      * @param subj
130      * mail subject
131      * @param mailContent
132      * body of the mail
133      * @param msgBundle
134      * language resource
135      */

136     protected SendMessageDialog(JMailPlugin plugin,
137         Profile profile,
138         String JavaDoc desti,
139         String JavaDoc subj,
140         String JavaDoc mailContent,
141         ResourceBundle msgBundle) {
142         //super(subj);
143
super((JFrame) null, subj, true);
144         this.plugin = plugin;
145
146         this.msgBundle = msgBundle;
147         this.profile = profile;
148
149         init();
150
151         destTextField.setText(desti);
152         subjectField.setText(subj);
153
154         content.setText(
155             msgBundle.getString("SendMessage.originalMsgLabel") + "\n");
156         content.append(mailContent);
157
158         int nb = content.getLineCount();
159
160         try {
161             for (int i = 0; i < nb; i++) {
162                 int x = content.getLineStartOffset(i);
163                 content.insert("> ", x);
164             }
165         } catch (Exception JavaDoc e) {
166             e.printStackTrace();
167         }
168
169         content.append("\n");
170
171         if (profile.getSignature() != null
172             && profile.getSignature().compareTo("") != 0)
173             insertTextFile(profile.getSignature());
174
175         content.setCaretPosition(0);
176
177         setSize(455, 655);
178         pack();
179         setVisible(true);
180     }
181
182     /**
183      * Forward constructor
184      *
185      * @param profile
186      * user profile
187      * @param subj
188      * mail subject
189      * @param mailContent
190      * mail body
191      * @param msgBundle
192      * language resource
193      */

194     protected SendMessageDialog(JMailPlugin plugin,
195         Profile profile,
196         String JavaDoc subj,
197         String JavaDoc mailContent,
198         ResourceBundle msgBundle) {
199         //super(subj);
200
super((JFrame) null, subj, true);
201
202         this.plugin = plugin;
203         this.msgBundle = msgBundle;
204         this.profile = profile;
205
206         init();
207
208         subject.setText(subj);
209
210         content.setText(
211             msgBundle.getString("SendMessage.originalMsgLabel") + "\n");
212         content.append(mailContent);
213
214         int nb = content.getLineCount();
215
216         try {
217             for (int i = 0; i < nb; i++) {
218                 int x = content.getLineStartOffset(i);
219                 content.insert("> ", x);
220             }
221         } catch (Exception JavaDoc e) {
222             e.printStackTrace();
223         }
224
225         content.append("\n");
226
227         if (profile.getSignature() != null
228             && profile.getSignature().compareTo("") != 0)
229             insertTextFile(profile.getSignature());
230
231         content.setCaretPosition(0);
232
233         setSize(455, 655);
234         pack();
235         setVisible(true);
236     }
237
238     /**
239      * Reply-all constructor
240      *
241      * @param profile
242      * user profile
243      * @param desti
244      * main recipient
245      * @param copies
246      * extra recipients
247      * @param subj
248      * mail subject
249      * @param mailContent
250      * mail body
251      * @param msgBundle
252      * language resource
253      */

254     protected SendMessageDialog(JMailPlugin plugin,
255         Profile profile,
256         String JavaDoc desti,
257         String JavaDoc copies[],
258         String JavaDoc subj,
259         String JavaDoc mailContent,
260         ResourceBundle msgBundle) {
261         //super(subj);
262
super((JFrame) null, subj, true);
263
264         this.plugin = plugin;
265         this.msgBundle = msgBundle;
266         this.profile = profile;
267
268         init();
269
270         destTextField.setText(desti);
271
272         if (copies.length > 0) {
273             StringBuffer JavaDoc cc = new StringBuffer JavaDoc(copies[0]);
274
275             for (int i = 1; i < copies.length; i++)
276                 cc.append(", " + copies[i]);
277
278             ccTextField.setText(cc.toString());
279         }
280
281         subject.setText(subj);
282
283         content.setText(
284             msgBundle.getString("SendMessage.originalMsgLabel") + "\n");
285         content.append(mailContent);
286
287         int nb = content.getLineCount();
288
289         try {
290             for (int i = 0; i < nb; i++) {
291                 int x = content.getLineStartOffset(i);
292                 content.insert("> ", x);
293             }
294         } catch (Exception JavaDoc e) {
295             e.printStackTrace();
296         }
297
298         content.append("\n");
299
300         if (profile.getSignature() != null
301             && profile.getSignature().compareTo("") != 0)
302             insertTextFile(profile.getSignature());
303
304         content.setCaretPosition(0);
305
306         setSize(455, 655);
307         pack();
308         setVisible(true);
309     }
310
311     /** Does the init stuff */
312     private void init() {
313         listener = new SendMessageListener();
314
315         menuBar = new JMenuBar();
316
317         fileMenu = new JMenu(msgBundle.getString("common.fileMenu"));
318         fileMenu.setMnemonic('F');
319
320         newMenuItem =
321             fileMenu.add(msgBundle.getString("common.new"));
322         newMenuItem.setAccelerator(
323             KeyStroke.getKeyStroke('N', Event.CTRL_MASK));
324         newMenuItem.setMnemonic('N');
325         newMenuItem.addActionListener(listener);
326
327         sendMenuItem =
328             fileMenu.add(msgBundle.getString("SendMessage.sendMenuItemLabel"));
329         sendMenuItem.addActionListener(listener);
330
331         fileMenu.addSeparator();
332
333         printMenuItem =
334             fileMenu.add(msgBundle.getString("common.print"));
335         printMenuItem.setEnabled(false);
336         printMenuItem.addActionListener(listener);
337
338         fileMenu.addSeparator();
339
340         closeMenuItem =
341             fileMenu.add(msgBundle.getString("SendMessage.closeMenuItemLabel"));
342         closeMenuItem.setMnemonic('F');
343         closeMenuItem.addActionListener(listener);
344
345         fileMenu = menuBar.add(fileMenu);
346
347         viewMenu = new JMenu(msgBundle.getString("SendMessage.viewMenuLabel"));
348         viewMenu.setMnemonic('V');
349
350         ccMenuItem = new JCheckBoxMenuItem("CC", true);
351         ccMenuItem.addActionListener(listener);
352         viewMenu.add(ccMenuItem);
353
354         bccMenuItem = new JCheckBoxMenuItem("BCC", true);
355         bccMenuItem.addActionListener(listener);
356         viewMenu.add(bccMenuItem);
357
358         viewMenu = menuBar.add(viewMenu);
359
360         insertMenu =
361             new JMenu(msgBundle.getString("SendMessage.insertMenuLabel"));
362         insertMenu.setMnemonic('I');
363
364         attachMenuItem =
365             insertMenu.add(
366                 msgBundle.getString("SendMessage.attachMenuItemLabel"));
367         attachMenuItem.addActionListener(listener);
368
369         textMenuItem =
370             insertMenu.add(
371                 msgBundle.getString("SendMessage.textMenuItemLabel"));
372         textMenuItem.addActionListener(listener);
373
374         insertMenu = menuBar.add(insertMenu);
375
376         messageMenu =
377             new JMenu(msgBundle.getString("common.mail"));
378         messageMenu.setMnemonic('M');
379
380         sendMsgMenuItem =
381             messageMenu.add(
382                 msgBundle.getString("SendMessage.sendMenuItemLabel"));
383         sendMsgMenuItem.setAccelerator(
384             KeyStroke.getKeyStroke('S', Event.CTRL_MASK));
385         sendMsgMenuItem.setMnemonic('E');
386         sendMsgMenuItem.addActionListener(listener);
387         messageMenu = menuBar.add(messageMenu);
388
389         setJMenuBar(menuBar);
390
391         panel = new JPanel();
392         panel.setLayout(new BorderLayout());
393
394         toolBar = new JToolBar();
395         toolBar.setFloatable(true);
396
397         send = new JButton(plugin.getImageIcon("send.png"));
398         send.setToolTipText(msgBundle.getString("SendMessage.sendLabel"));
399         send.addActionListener(listener);
400         toolBar.add(send);
401
402         toolBar.addSeparator();
403
404         attachment =
405             new JButton(plugin.getImageIcon("attach.png"));
406         attachment.setToolTipText(
407             msgBundle.getString("SendMessage.attachmentLabel"));
408         attachment.addActionListener(listener);
409         toolBar.add(attachment);
410
411         panel.add("North", toolBar);
412
413         fields = new JPanel();
414
415         GridBagLayout gridBag = new GridBagLayout();
416         GridBagConstraints constraints = new GridBagConstraints();
417
418         fields.setLayout(gridBag);
419
420         Common.buildConstraints(constraints, 0, 0, 1, 1, 30, 10);
421         dest = new JButton("To : ");
422         dest.addActionListener(listener);
423         gridBag.setConstraints(dest, constraints);
424         fields.add(dest);
425
426         constraints.fill = GridBagConstraints.HORIZONTAL;
427         Common.buildConstraints(constraints, 1, 0, 1, 1, 70, 0);
428         destTextField = new JTextField(30);
429         gridBag.setConstraints(destTextField, constraints);
430         fields.add(destTextField);
431
432         constraints.fill = GridBagConstraints.NONE;
433         Common.buildConstraints(constraints, 0, 1, 1, 1, 0, 10);
434         cc = new JButton("Cc : ");
435         cc.addActionListener(listener);
436         gridBag.setConstraints(cc, constraints);
437         fields.add(cc);
438
439         constraints.fill = GridBagConstraints.HORIZONTAL;
440         Common.buildConstraints(constraints, 1, 1, 1, 1, 0, 0);
441         ccTextField = new JTextField(30);
442         gridBag.setConstraints(ccTextField, constraints);
443         fields.add(ccTextField);
444
445         constraints.fill = GridBagConstraints.NONE;
446         Common.buildConstraints(constraints, 0, 2, 1, 1, 0, 10);
447         bcc = new JButton("Bcc : ");
448         bcc.addActionListener(listener);
449         gridBag.setConstraints(bcc, constraints);
450         fields.add(bcc);
451
452         constraints.fill = GridBagConstraints.HORIZONTAL;
453         Common.buildConstraints(constraints, 1, 2, 1, 1, 0, 0);
454         bccTextField = new JTextField(30);
455         gridBag.setConstraints(bccTextField, constraints);
456         fields.add(bccTextField);
457
458         constraints.fill = GridBagConstraints.NONE;
459         Common.buildConstraints(constraints, 0, 3, 1, 1, 0, 10);
460         subject = new JLabel(msgBundle.getString("SendMessage.subjectLabel"));
461         gridBag.setConstraints(subject, constraints);
462         fields.add(subject);
463
464         constraints.fill = GridBagConstraints.HORIZONTAL;
465         Common.buildConstraints(constraints, 1, 3, 1, 1, 0, 0);
466         subjectField = new JTextField(30);
467         gridBag.setConstraints(subjectField, constraints);
468         fields.add(subjectField);
469
470         constraints.fill = GridBagConstraints.NONE;
471         Common.buildConstraints(constraints, 0, 4, 1, 1, 0, 10);
472         attach = new JLabel(msgBundle.getString("SendMessage.attachLabel"));
473         attach.setEnabled(false);
474         gridBag.setConstraints(attach, constraints);
475         fields.add(attach);
476
477         constraints.fill = GridBagConstraints.HORIZONTAL;
478         Common.buildConstraints(constraints, 1, 4, 1, 1, 0, 0);
479         fileNames = new Vector();
480         files = new JList(fileNames);
481         files.setEnabled(false);
482         attachPane =
483             new JScrollPane(
484                 files,
485                 JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
486                 JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
487
488         attachPane.setPreferredSize(new Dimension(200, 20));
489         gridBag.setConstraints(attachPane, constraints);
490         attachPane.setEnabled(false);
491         fields.add(attachPane);
492
493         constraints.fill = GridBagConstraints.NONE;
494         Common.buildConstraints(constraints, 0, 5, 2, 1, 0, 50);
495         content = new JTextArea(20, 30);
496         content.setLineWrap(true);
497         contentPane = new JScrollPane(content);
498         gridBag.setConstraints(contentPane, constraints);
499         fields.add(contentPane);
500
501         panel.add("Center", fields);
502
503         setContentPane(panel);
504
505         sentMail = false;
506     }
507
508     private void insertTextFile(String JavaDoc name) {
509         try {
510             int pos = content.getCaretPosition();
511
512             BufferedReader in = new BufferedReader(new FileReader(name));
513
514             StringBuffer JavaDoc text = new StringBuffer JavaDoc();
515             String JavaDoc line = new String JavaDoc();
516
517             while ((line = in.readLine()) != null)
518                 text.append(line + "\n");
519
520             in.close();
521
522             content.insert(text.toString(), pos);
523         } catch (Exception JavaDoc ex) {
524             ex.printStackTrace();
525         }
526     }
527
528     protected boolean sentMail() {
529         return (sentMail);
530     }
531
532     /** Listener for this class */
533     private final class SendMessageListener
534         implements ActionListener {
535
536         /**
537          * This method is invoked when an event is triggered
538          *
539          * @param e
540          * event
541          */

542         public final void actionPerformed(ActionEvent e) {
543             AbstractButton b = (AbstractButton) e.getSource();
544
545             if (b == newMenuItem) {
546                 SendMessageDialog sm = new SendMessageDialog(plugin, profile, msgBundle);
547                 sm.dispose();
548             } else if (b == closeMenuItem)
549                 //dispose();
550
hide();
551
552             else if (b == ccMenuItem) {
553                 if (!ccMenuItem.getState() == true) {
554                     ccMenuItem.setSelected(false);
555                     cc.setVisible(false);
556                     ccTextField.setVisible(false);
557                 } else {
558                     ccMenuItem.setSelected(true);
559                     cc.setVisible(true);
560                     ccTextField.setVisible(true);
561                 }
562             } else if (b == bccMenuItem) {
563                 if (!bccMenuItem.getState()) {
564                     bccMenuItem.setSelected(false);
565                     bcc.setVisible(false);
566                     bccTextField.setVisible(false);
567                 } else {
568                     bccMenuItem.setSelected(true);
569                     bcc.setVisible(true);
570                     bccTextField.setVisible(true);
571                 }
572             } else if (b == textMenuItem) {
573                 JFileChooser fc = new JFileChooser();
574
575                 int val = fc.showOpenDialog(null);
576
577                 if (val == JFileChooser.APPROVE_OPTION) {
578                     File file = fc.getSelectedFile();
579
580                     if (file != null) {
581                         insertTextFile(file.getPath());
582
583                         /*
584                          * try { int pos = content.getCaretPosition();
585                          *
586                          * BufferedReader in = new BufferedReader(new
587                          * FileReader(file));
588                          *
589                          * StringBuffer text = new StringBuffer(); String line =
590                          * new String();
591                          *
592                          * while((line = in.readLine()) != null)
593                          * text.append(line + "\n");
594                          *
595                          * in.close();
596                          *
597                          * content.insert(text.toString(), pos); }
598                          *
599                          * catch(Exception ex) { ex.printStackTrace(); }
600                          */

601                     }
602                 }
603             } else if (
604                 b == sendMenuItem || b == send || b == sendMsgMenuItem) {
605                 StringTokenizer str =
606                     new StringTokenizer(destTextField.getText(), ",");
607                 String JavaDoc dest[] = new String JavaDoc[str.countTokens()];
608                 int index = 0;
609
610                 while (str.hasMoreTokens())
611                     dest[index++] = str.nextToken();
612
613                 str = new StringTokenizer(ccTextField.getText(), ",");
614                 String JavaDoc cc[] = new String JavaDoc[str.countTokens()];
615
616                 index = 0;
617
618                 while (str.hasMoreTokens())
619                     cc[index++] = str.nextToken();
620
621                 str = new StringTokenizer(bccTextField.getText(), ",");
622                 String JavaDoc bcc[] = new String JavaDoc[str.countTokens()];
623
624                 index = 0;
625
626                 while (str.hasMoreTokens())
627                     bcc[index++] = str.nextToken();
628
629                 if (MailClient
630                     .sendMsg(
631                         dest,
632                         cc,
633                         bcc,
634                         subjectField.getText(),
635                         content.getText(),
636                         fileNames,
637                         profile)) {
638                     JOptionPane.showMessageDialog(
639                         null,
640                         msgBundle.getString("SendMessage.msgSentLabel"),
641                         "SendMessage",
642                         JOptionPane.INFORMATION_MESSAGE);
643                     sentMail = true;
644
645                     //dispose();
646
hide();
647                 }
648             } else if (b == attachment || b == attachMenuItem) {
649                 JFileChooser fc = new JFileChooser();
650
651                 int val = fc.showOpenDialog(null);
652
653                 if (val == JFileChooser.APPROVE_OPTION) {
654                     attachPane.setMinimumSize(new Dimension(200, 40));
655
656                     File file = fc.getSelectedFile();
657
658                     if (file != null) {
659                         String JavaDoc name = file.getPath();
660                         fileNames.add(name);
661                         files.setListData(fileNames);
662                         attach.setEnabled(true);
663                         attachPane.setEnabled(true);
664                         files.setEnabled(true);
665                     }
666                 }
667             }
668         }
669     }
670 }
671
Popular Tags