1 import java.awt.*; 2 import java.awt.event.*; 3 import java.awt.image.ImageObserver ; 4 import java.io.Serializable ; 5 import java.util.*; 6 import javax.swing.*; 7 import javax.accessibility.Accessible ; 8 import org.jivesoftware.smack.*; 9 10 11 public final class JNewJabberDialog extends JDialog implements Accessible , ImageObserver , MenuContainer, RootPaneContainer, Serializable , WindowConstants{ 12 private final static int GAP=10; 13 14 final private JLabel infoLbl=new JLabel(); 15 16 final private JPanel fieldPanel=new JPanel(new SpringLayout()); 17 final private JLabel usernameLbl=new JLabel(Lang.gs("jabber_username")); 18 final private JTextField username=new JTextField(20); 19 final private JLabel passwordLbl=new JLabel(Lang.gs("jabber_password")); 20 final private JPasswordField password=new JPasswordField(15); 21 22 private JLabel[] fieldLbls; 23 private JTextField[] fields; 24 25 final private JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.TRAILING,GAP,GAP)); 26 final private JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL); 27 final private JButton registerBtn=new JButton(Lang.gs("register"),Icons.REGISTER); 28 29 final private BtnClick click=new BtnClick(); 31 private boolean failure=false; private boolean created=false; private XMPPConnection connection=null; private String Server; 35 private int port=5223; 36 private Frame Owner; 37 private boolean SSL; 38 private String [] Attributes=new String [0]; private JWaitDialog waitDialog; 40 41 48 public JNewJabberDialog(final Frame owner, final String server,String username, String pass,boolean ssl){ 49 super(owner,Lang.gs("jabber_new"),true); waitDialog=new JWaitDialog(this,"talk with server"); 51 Owner=owner; 53 SSL=ssl; 54 Server=getBareServerName(server); 55 if(SSL){ 56 port=getPort(server,5223); 57 } 58 else{ 59 port=getPort(server,5222); 60 } 61 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 62 Container cp=getContentPane(); 63 cp.setLayout(new BorderLayout()); 64 cp.add(infoLbl,BorderLayout.NORTH); 66 cancelBtn.setMnemonic(Lang.s2k("cancel_mn")); 68 registerBtn.setMnemonic(Lang.s2k("register_mn")); 69 cancelBtn.addActionListener(click); 70 registerBtn.addActionListener(click); 71 btnPanel.add(cancelBtn); 72 btnPanel.add(registerBtn); 73 cp.add(btnPanel,BorderLayout.SOUTH); 74 getRootPane().setDefaultButton(registerBtn); 75 this.username.setText(username); 77 this.password.setText(pass); 78 fieldPanel.add(usernameLbl); 79 fieldPanel.add(this.username); 80 fieldPanel.add(passwordLbl); 81 fieldPanel.add(this.password); 82 try{ 84 if(SSL){ 85 connection=new SSLXMPPConnection(Server,port); 86 } 87 else{ 88 connection=new XMPPConnection(Server,port); 89 } 90 } 91 catch(final XMPPException xe){ 92 failure=true; 93 SwingUtilities.invokeLater(new Runnable (){ 94 public void run(){ 95 GUI.showError(owner,"jabber_new","could not connect",null,xe); 96 }}); 97 return; 98 } 99 AccountManager am=connection.getAccountManager(); 101 if(!am.supportsAccountCreation()){ 102 failure=true; 103 SwingUtilities.invokeLater(new Runnable (){ 104 public void run(){ 105 GUI.showError(owner,"jabber_new","no new accounts",server); 106 }}); 107 return; 108 } 109 String info=am.getAccountInstructions(); 111 if(info!=null){ 112 infoLbl.setText("<html><table width='300' alighn='center'><tr><td><p>"+info+"</p></td></tr></table></html>"); 113 } 114 Iterator aa=am.getAccountAttributes(); 116 Vector tmp=new Vector(5,5); 117 String t; 118 while(aa.hasNext()){ 119 t=(String ) aa.next(); 120 tmp.add(t); } 122 Attributes=(String [])tmp.toArray(Attributes); 124 String labelstext[]=GUI.translate(Attributes); 126 fieldLbls=new JLabel[Attributes.length]; 128 fields=new JTextField[Attributes.length]; 129 for(int i=0;i<Attributes.length;i++){ 130 fieldLbls[i]=new JLabel(labelstext[i]); 131 fields[i]=new JTextField(20); 132 fieldPanel.add(fieldLbls[i]); 133 fieldPanel.add(fields[i]); 134 } 135 SpringUtilities.makeCompactGrid(fieldPanel, 136 2+Attributes.length, 2, GAP,GAP,GAP,GAP); cp.add(fieldPanel,BorderLayout.CENTER); 139 pack(); 140 getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); getRootPane().getActionMap().put("help",new HelpAction(this,"new.html#jabber")); 143 144 setLocationRelativeTo(owner); 145 } 146 147 private void doRegisterNewJabberAccount(){ 148 waitDialog.show(); 149 (new CreateAccountThread()).start(); 150 } 151 152 private void accountCreated(final XMPPException xe){ 153 SwingUtilities.invokeLater(new Runnable (){ 155 public void run(){ 156 waitDialog.dispose(); 157 if(xe!=null){ GUI.showError(null,"jabber_new","could not create account",Server,xe); 159 return; 160 } 161 created=true; 162 GUI.showInfo(null,"jabber_new","account created",username.getText()+"@"+Server); 163 dispose(); 164 } 165 }); 166 } 167 168 172 public static final int getPort(String hostname, int defaultport){ 173 if(hostname==null){return defaultport;} 174 int i=hostname.lastIndexOf(':'); 175 if(i==0 || i==(hostname.length()-1)){ 176 return defaultport; 177 } 178 int port=0; 179 try{ 180 port=Integer.parseInt(hostname.substring(i+1)); 181 } 182 catch(NumberFormatException nfe){ 183 return defaultport; 184 } 185 if(port>0){return port;} 186 else{return defaultport;} 187 } 188 189 194 public static final String getBareServerName(String hostname){ 195 if(hostname==null){return null;} 196 int i=hostname.lastIndexOf(':'); 197 if(i==-1){return hostname;} 198 return hostname.substring(0,i); 199 } 200 201 202 public boolean created(){ 203 return created; 204 } 205 206 public String getUsername(){ 207 return username.getText(); 208 } 209 210 public String getPassword(){ 211 return new String (password.getPassword()); 212 } 213 214 final class BtnClick implements ActionListener{ 215 public void actionPerformed(ActionEvent ae){ 216 String b=ae.getActionCommand(); 218 if(b.equals(cancelBtn.getText())){ 220 dispose(); 221 return; 222 } 223 if (b.equals(registerBtn.getText())){ 224 doRegisterNewJabberAccount(); 225 return; 226 } 227 } 228 } 229 230 public void show(){ 231 if(failure){ 232 dispose(); 233 return; 234 } 235 setLocationRelativeTo(Owner); 236 super.show(); 237 } 238 239 public void dispose(){ 240 if(connection!=null){ 241 if(connection.isConnected()){ 242 connection.close(); 243 } 244 } 245 super.dispose(); 246 } 247 248 final class CreateAccountThread extends Thread { 249 250 public CreateAccountThread(){ 251 super(); 252 setDaemon(true); 253 } 254 255 public void run(){ 256 try{ 257 AccountManager am=connection.getAccountManager(); 258 if(Attributes.length==0){ 259 am.createAccount(username.getText(),(new String (password.getPassword()))); 260 } 261 else{ 262 Hashtable ht=new Hashtable(Attributes.length); 264 for(int i=0;i<Attributes.length;i++){ 265 ht.put(Attributes[i],fields[i].getText()); 266 } 267 am.createAccount(username.getText(),(new String (password.getPassword())),ht); 268 } 269 accountCreated(null); 270 } 271 catch(XMPPException e){ 272 accountCreated(e); 273 } 274 275 } 276 } 277 } | Popular Tags |