KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > sendmail > SendMailPlugin


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.sendmail;
20
21 import java.awt.Dimension JavaDoc;
22 import java.awt.GridLayout JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.awt.event.KeyListener JavaDoc;
27
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JTextField JavaDoc;
31
32 import org.lucane.client.*;
33 import org.lucane.client.widgets.DialogBox;
34 import org.lucane.client.widgets.ManagedWindow;
35 import org.lucane.common.*;
36 import org.lucane.common.net.ObjectConnection;
37
38 public class SendMailPlugin extends StandalonePlugin
39 implements ActionListener JavaDoc, KeyListener JavaDoc
40 {
41     private ConnectInfo service;
42     private ManagedWindow window;
43     private JTextField JavaDoc txtSmtpHost;
44     private JButton JavaDoc btnApply;
45     
46     public SendMailPlugin()
47     {
48     }
49
50     public Plugin newInstance(ConnectInfo[] friends)
51     {
52         return new SendMailPlugin();
53     }
54
55     public void start()
56     {
57         this.service = Communicator.getInstance().getConnectInfo(getName());
58         
59         showWindow();
60     }
61     
62     private void showWindow()
63     {
64         this.window = new ManagedWindow(this, getTitle());
65         window.setName("window");
66         window.setExitPluginOnClose(true);
67         window.getContentPane().setLayout(new GridLayout JavaDoc(1, 3));
68         
69         this.txtSmtpHost = new JTextField JavaDoc(getSmtpHost());
70         txtSmtpHost.addKeyListener(this);
71         window.getContentPane().add(new JLabel JavaDoc(tr("lbl.smtpHost")));
72         window.getContentPane().add(txtSmtpHost);
73
74         this.btnApply = new JButton JavaDoc(tr("btn.apply"), Client.getImageIcon("ok.png"));
75         btnApply.addActionListener(this);
76         window.getContentPane().add(btnApply);
77         
78         window.setPreferredSize(new Dimension JavaDoc(400, 50));
79         window.show();
80     }
81     
82     public void actionPerformed(ActionEvent JavaDoc ae)
83     {
84         if(ae.getSource() == btnApply)
85         {
86             if(setSmtpHost(txtSmtpHost.getText()))
87             {
88                 window.dispose();
89                 this.exit();
90             }
91         }
92     }
93     
94     private boolean setSmtpHost(String JavaDoc host)
95     {
96         String JavaDoc ack = null;
97         try {
98             SendMailAction action = new SendMailAction(SendMailAction.SET_SMTPHOST, host);
99             ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
100             ack = oc.readString();
101             oc.close();
102         } catch(Exception JavaDoc e) {
103             ack = tr("err.unableToSetHost");
104         }
105         
106         if(!ack.equals("OK"))
107         {
108             DialogBox.error(ack);
109             return false;
110         }
111         
112         return true;
113     }
114
115     private String JavaDoc getSmtpHost()
116     {
117         String JavaDoc ack = null;
118         String JavaDoc host = null;
119         try {
120             SendMailAction action = new SendMailAction(SendMailAction.GET_SMTPHOST);
121             ObjectConnection oc = Communicator.getInstance().sendMessageTo(service, getName(), action);
122             ack = oc.readString();
123             host = oc.readString();
124             oc.close();
125         } catch(Exception JavaDoc e) {
126             ack = tr("err.unableToGetHost");
127         }
128         
129         if(!ack.equals("OK"))
130             DialogBox.error(ack);
131         
132         return host;
133     }
134
135     public void keyReleased(KeyEvent JavaDoc ke) {}
136     public void keyTyped(KeyEvent JavaDoc ke) {}
137     public void keyPressed(KeyEvent JavaDoc ke)
138     {
139         if(ke.getKeyCode() == KeyEvent.VK_ENTER)
140             btnApply.doClick();
141     }
142 }
143
Popular Tags