1 import java.awt.*; 2 import java.awt.event.*; 3 import java.awt.image.ImageObserver ; 4 import java.io.Serializable ; 5 import javax.swing.*; 6 import javax.accessibility.Accessible ; 7 import org.jivesoftware.smack.*; 8 import org.jivesoftware.smack.filter.*; 9 import org.jivesoftware.smack.packet.*; 10 import org.jivesoftware.smackx.ServiceDiscoveryManager; 11 12 13 public final class ConnectionDialog extends JDialog implements Accessible , ImageObserver , MenuContainer, RootPaneContainer, Serializable , SwingConstants,WindowConstants{ 14 15 private final int GAP=10; 16 private boolean result=false; 18 final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP)); 19 final JButton connectBtn=new JButton(Lang.gs("connect"),Icons.LOGIN); 20 final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL); 21 22 final Box fieldsBox=new Box(BoxLayout.Y_AXIS); 23 final JPanel fieldsPanel=new JPanel(new SpringLayout()); 24 25 final JLabel jabberUsernameLbl=new JLabel(Lang.gs("jabber_username")); 26 final JTextField jabberUsername=new JTextField(15); 27 final JLabel jabberServerLbl=new JLabel(Lang.gs("jabber_server")); 28 final JTextField jabberServer=new JTextField(15); 29 final JLabel jabberPasswordLbl=new JLabel(Lang.gs("jabber_password")); 30 final JPasswordField jabberPassword=new JPasswordField(15); 31 final JLabel jabberResourceLbl=new JLabel(Lang.gs("resource")); 32 final JTextField jabberResource=new JTextField("here"); 33 final JLabel jabberSSLLbl=new JLabel(Lang.gs("jabber_ssl")); 34 final JCheckBox jabberSSL=new JCheckBox("",true); 35 36 37 final ConnectionDialog frame=this; 38 JWaitDialog wait; 39 final BtnClick click=new BtnClick(); 40 41 boolean showing=false; 42 43 public ConnectionDialog(){ 44 super(WhisperIM.MainWindow,Lang.gs("connect"),true); 45 wait=new JWaitDialog(frame,"talk with server"); 46 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); 47 Container cp=getContentPane(); 48 cp.setLayout(new BorderLayout(GAP,GAP)); 49 50 connectBtn.setMnemonic(Lang.s2k("connect_mn")); 51 connectBtn.setToolTipText(Lang.gs("connect_tt")); 52 connectBtn.addActionListener(click); 53 cancelBtn.addActionListener(click); 54 btnPanel.add(connectBtn); 55 btnPanel.add(cancelBtn); 56 cp.add(btnPanel,BorderLayout.SOUTH); 57 58 GUI.connect(jabberUsernameLbl,jabberUsername,"jabber_username_mn","jabber_username_tt"); 59 GUI.connect(jabberServerLbl,jabberServer,"jabber_server_mn","jabber_server_tt"); 60 GUI.connect(jabberPasswordLbl,jabberPassword,"jabber_password_mn","jabber_password_tt"); 61 GUI.connect(jabberResourceLbl,jabberResource,"jabber_resource_mn","jabber_resource_tt"); 62 GUI.connect(jabberSSLLbl,jabberSSL,null,"jabber_ssl_tt"); 63 jabberSSLLbl.setIcon(Icons.PADLOCK); 64 fieldsPanel.add(jabberUsernameLbl); 65 fieldsPanel.add(jabberUsername); 66 fieldsPanel.add(jabberServerLbl); 67 fieldsPanel.add(jabberServer); 68 fieldsPanel.add(jabberPasswordLbl); 69 fieldsPanel.add(jabberPassword); 70 fieldsPanel.add(jabberResourceLbl); 71 fieldsPanel.add(jabberResource); 72 fieldsPanel.add(jabberSSLLbl); 73 fieldsPanel.add(jabberSSL); 74 jabberUsername.setEditable(false); 75 jabberServer.setEditable(false); 76 if(WhisperIM.Config.getProperty("jabberLockToDefaultResource").equals("true")){ 78 jabberResource.setEnabled(false); 79 } 80 if(WhisperIM.Config.getProperty("jabberAllowNonSSL").equals("false")){ 81 jabberSSL.setEnabled(false); 82 } 83 84 SpringUtilities.makeCompactGrid(fieldsPanel,5,2,GAP,GAP,GAP,GAP); 85 fieldsBox.add(fieldsPanel); 86 fieldsBox.add(Box.createGlue()); 87 cp.add(fieldsBox,BorderLayout.NORTH); 88 89 connectBtn.setDefaultCapable(true); 90 getRootPane().setDefaultButton(connectBtn); 91 92 jabberUsername.setText(WhisperIM.accountSettings.JabberUserName); 93 jabberServer.setText(WhisperIM.accountSettings.JabberServer); 94 jabberResource.setText(WhisperIM.accountSettings.JabberResource); 95 if(WhisperIM.accountSettings.JabberRememberPassword){ 96 jabberPassword.setText(WhisperIM.accountSettings.JabberPassword); 97 } 98 jabberSSL.setSelected(WhisperIM.accountSettings.JabberSSL); 99 100 pack(); 101 addKeyListener(new KeyAction()); 102 addMouseListener(new MouseAction()); 103 } 104 105 public void setLang(){ 106 setTitle(Lang.gs("connect")); 107 connectBtn.setText(Lang.gs("connect")); 108 connectBtn.setMnemonic(Lang.s2k("connect_mn")); 109 connectBtn.setToolTipText(Lang.gs("connect_tt")); 110 cancelBtn.setText(Lang.gs("cancel")); 111 jabberUsernameLbl.setText(Lang.gs("jabber_username")); 112 jabberServerLbl.setText(Lang.gs("jabber_server")); 113 jabberPasswordLbl.setText(Lang.gs("jabber_password")); 114 jabberResourceLbl.setText(Lang.gs("resource")); 115 jabberSSLLbl.setText(Lang.gs("jabber_ssl")); 116 GUI.connect(jabberUsernameLbl,jabberUsername,"jabber_username_mn","jabber_username_tt"); 117 GUI.connect(jabberServerLbl,jabberServer,"jabber_server_mn","jabber_server_tt"); 118 GUI.connect(jabberPasswordLbl,jabberPassword,"jabber_password_mn","jabber_password_tt"); 119 GUI.connect(jabberResourceLbl,jabberResource,"jabber_resource_mn","jabber_resource_tt"); 120 GUI.connect(jabberSSLLbl,jabberSSL,null,"jabber_ssl_tt"); 121 } 122 123 public void connect(){ 124 jabberResource.setText(WhisperIM.accountSettings.JabberResource); 125 if(WhisperIM.accountSettings.JabberRememberPassword){ 126 jabberPassword.setText(WhisperIM.accountSettings.JabberPassword); 127 } 128 jabberSSL.setSelected(WhisperIM.accountSettings.JabberSSL); 129 result=false; setLocationRelativeTo(WhisperIM.MainWindow); 131 getRootPane().setDefaultButton(connectBtn); 132 show(); 133 } 134 135 public boolean getResult(){ 136 return result; 137 } 138 139 public void dispose(){ 140 wait.dispose(); 141 super.dispose(); 142 } 143 144 public void show(){ 145 showing=true; 146 super.show(); 147 } 148 149 public void doConnect(){ 150 if(jabberUsername.getText().equals("")){ 151 if(!showing){ 152 show(); 153 } 154 GUI.showInfo(frame,"connect","enter a user name"); 155 jabberUsername.requestFocus(); 156 return; 157 } 158 if(jabberServer.getText().equals("")){ 159 if(!showing){ 160 show(); 161 } 162 GUI.showInfo(frame,"connect","you must enter servername"); 163 jabberServer.requestFocus(); 164 return; 165 } 166 if(jabberPassword.getPassword().length==0){ 167 if(!showing){ 168 show(); 169 } 170 GUI.showInfo(frame,"connect","you must enter password"); 171 jabberPassword.requestFocus(); 172 return; 173 } 174 175 wait.setLocationRelativeTo(this); 176 wait.show(); 177 178 String server=jabberServer.getText(); 179 String Server=JNewJabberDialog.getBareServerName(server); 180 int port=5223; 181 if(jabberSSL.isSelected()){ 182 port=JNewJabberDialog.getPort(server,5223); 183 } 184 else{ 185 port=JNewJabberDialog.getPort(server,5222); 186 } 187 188 String resource=jabberResource.getText(); 189 if(resource.equals("")){ 190 resource="whisper"; 191 } 192 setEnabled(false); 193 (new ConnectThread(Server,port,jabberSSL.isSelected(),resource)).start(); 194 } 195 196 final class BtnClick implements ActionListener{ 197 public void actionPerformed(ActionEvent ae){ 198 String b=ae.getActionCommand(); 200 if(b.equals(cancelBtn.getText())){ 202 dispose(); 203 return; 204 } 205 if(b.equals(connectBtn.getText())){ 206 doConnect(); 207 } 208 } 209 } 210 211 final class ConnectThread extends Thread { 212 private boolean jabberSSLisSelected; 213 private int port; 214 private String Server; 215 private String resource; 216 217 public ConnectThread(String Server, int port, boolean jabberSSLisSelected,String resource){ 218 super("Connection Thread in ConnectionDialog.class"); 219 setPriority(Thread.MIN_PRIORITY); 220 setDaemon(true); 221 this.Server=Server; 222 this.port=port; 223 this.jabberSSLisSelected=jabberSSLisSelected; 224 this.resource=resource; 225 } 226 227 public void run(){ 228 try{ 229 if(jabberSSLisSelected){ 230 WhisperIM.MainWindow.Conn=new SSLXMPPConnection(Server,port); 231 } 232 else{ 233 WhisperIM.MainWindow.Conn=new XMPPConnection(Server,port); 234 } 235 WhisperIM.MainWindow.Conn.addConnectionListener(new ConnListener()); 237 WhisperIM.MainWindow.SDM=new ServiceDiscoveryManager(WhisperIM.MainWindow.Conn); 239 ServiceDiscoveryManager.getInstanceFor(WhisperIM.MainWindow.Conn).setIdentityName("Whisper "+WhisperIM.VERSION); 240 WhisperIM.MainWindow.Conn.addPacketListener( new MessagePacketListener(), new OrFilter(new MessageTypeFilter(Message.Type.NORMAL),new MessageTypeFilter(Message.Type.ERROR))); 242 WhisperIM.MainWindow.Conn.addPacketListener(new ChatPacketListener(), new MessageTypeFilter(Message.Type.CHAT)); 244 WhisperIM.MainWindow.Conn.addPacketListener(new SubscriptionRequestPacketListener(), new PacketTypeFilter(Presence.class)); 246 WhisperIM.MainWindow.Conn.addPacketListener(new VersionIQListener(),new PacketTypeFilter(VersionIQ.class)); 248 WhisperIM.MainWindow.Conn.login(jabberUsername.getText(),new String (jabberPassword.getPassword()), resource); 250 } 251 catch(final XMPPException xe){ 252 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 253 public void run(){ 254 wait.dispose(); 255 if(!showing){ 256 show(); 257 } 258 else{ 259 toFront(); 260 } 261 GUI.showError(frame,"connect","connection error",null,xe); 262 } 263 }); 264 return; 265 } 266 catch(final Exception e){ 267 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 268 public void run(){ 269 wait.dispose(); 270 if(!showing){ 271 show(); 272 } 273 else{ 274 frame.toFront(); 275 } 276 GUI.showError(frame,"connect","connection error",e.getMessage()); 277 } 278 }); 279 return; 280 } 281 result=true; 282 javax.swing.SwingUtilities.invokeLater(new Runnable (){ 283 public void run(){ 284 wait.dispose(); 285 dispose(); 286 } 287 }); 288 } 289 } 290 } | Popular Tags |