1 19 package org.lucane.applications.sendmail; 20 21 import java.awt.Dimension ; 22 import java.awt.GridLayout ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.KeyEvent ; 26 import java.awt.event.KeyListener ; 27 28 import javax.swing.JButton ; 29 import javax.swing.JLabel ; 30 import javax.swing.JTextField ; 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 , KeyListener 40 { 41 private ConnectInfo service; 42 private ManagedWindow window; 43 private JTextField txtSmtpHost; 44 private JButton 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 (1, 3)); 68 69 this.txtSmtpHost = new JTextField (getSmtpHost()); 70 txtSmtpHost.addKeyListener(this); 71 window.getContentPane().add(new JLabel (tr("lbl.smtpHost"))); 72 window.getContentPane().add(txtSmtpHost); 73 74 this.btnApply = new JButton (tr("btn.apply"), Client.getImageIcon("ok.png")); 75 btnApply.addActionListener(this); 76 window.getContentPane().add(btnApply); 77 78 window.setPreferredSize(new Dimension (400, 50)); 79 window.show(); 80 } 81 82 public void actionPerformed(ActionEvent 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 host) 95 { 96 String 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 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 getSmtpHost() 116 { 117 String ack = null; 118 String 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 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 ke) {} 136 public void keyTyped(KeyEvent ke) {} 137 public void keyPressed(KeyEvent ke) 138 { 139 if(ke.getKeyCode() == KeyEvent.VK_ENTER) 140 btnApply.doClick(); 141 } 142 } 143 | Popular Tags |