KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JNewJabberDialog


1 import java.awt.*;
2 import java.awt.event.*;
3 import java.awt.image.ImageObserver JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.util.*;
6 import javax.swing.*;
7 import javax.accessibility.Accessible JavaDoc;
8 import org.jivesoftware.smack.*;
9
10 /** New jabber account dialog.*/
11 public final class JNewJabberDialog extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable JavaDoc, 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(); //Button click action listener
30

31     private boolean failure=false; //if fail to connect or server not support new accounts
32
private boolean created=false; //if the account was created
33
private XMPPConnection connection=null; //connection
34
private String JavaDoc Server;
35     private int port=5223;
36     private Frame Owner;
37     private boolean SSL;
38     private String JavaDoc[] Attributes=new String JavaDoc[0]; //List of account creation attributes
39
private JWaitDialog waitDialog;
40         
41     /** Creates a new modal JNewJabberDialog.
42     * The constuctor may take a while to execute, as it will attempt connect to the sever.
43     * @param owner The frame that owns the dialog.
44     * @param server The name of the server to create the new account at.
45     * @param username Desired username of new account.
46     * @param pass The password for the new account
47     * @param ssl If SSL is to be used.*/

48     public JNewJabberDialog(final Frame owner, final String JavaDoc server,String JavaDoc username, String JavaDoc pass,boolean ssl){
49         super(owner,Lang.gs("jabber_new"),true); //create dialog
50
waitDialog=new JWaitDialog(this,"talk with server");
51         // copy parameter values
52
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         // instructions
65
cp.add(infoLbl,BorderLayout.NORTH);
66         //button panel
67
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         //field panel
76
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         // now connect to server
83
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 JavaDoc(){
94                 public void run(){
95                     GUI.showError(owner,"jabber_new","could not connect",null,xe);
96                 }});
97             return;
98         }
99         // get account manager
100
AccountManager am=connection.getAccountManager();
101         if(!am.supportsAccountCreation()){
102             failure=true;
103             SwingUtilities.invokeLater(new Runnable JavaDoc(){
104                 public void run(){
105                     GUI.showError(owner,"jabber_new","no new accounts",server);
106                 }});
107             return;
108         }
109         // get any instructions
110
String JavaDoc 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         // get required attributes
115
Iterator aa=am.getAccountAttributes();
116         Vector tmp=new Vector(5,5);
117         String JavaDoc t;
118         while(aa.hasNext()){
119             t=(String JavaDoc) aa.next();
120             tmp.add(t); //create vector of attributes
121
}
122         // covert to string array
123
Attributes=(String JavaDoc[])tmp.toArray(Attributes);
124         // try to translate
125
String JavaDoc labelstext[]=GUI.translate(Attributes);
126         // create labels and text fields, setting the label text according to the translation
127
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, //rows, cols
137
GAP,GAP,GAP,GAP);//xPad, yPad
138
cp.add(fieldPanel,BorderLayout.CENTER);
139         pack();
140         //help key
141
getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(WhisperIM.HELP_KEY,0),"help"); //put in help key for action help
142
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         // This method is called outside the EDT
154
SwingUtilities.invokeLater(new Runnable JavaDoc(){
155                 public void run(){
156                     waitDialog.dispose();
157                     if(xe!=null){ // there was an error
158
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     /** Extracts the port number from a server name.
169     * eg somehost.net:8894 will return 8894. defaultport is returned
170     * if no valid port number is present.
171     * @see #getBareServerName(String hostname, int port)*/

172     public static final int getPort(String JavaDoc 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 JavaDoc nfe){
183             return defaultport;
184         }
185         if(port>0){return port;}
186         else{return defaultport;}
187     }
188     
189     /** Returns just the server name, removing port specification.
190     * eg somehost.net:8894 will return somehost.
191     * If :port is not present, the method will return hostname.
192     * @param hostname The name of the host
193     *@see #getPort(String hostname, int defaultport)*/

194     public static final String JavaDoc getBareServerName(String JavaDoc 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     /** Returns true if the account was successfully created.*/
202     public boolean created(){
203         return created;
204     }
205     
206     public String JavaDoc getUsername(){
207         return username.getText();
208     }
209     
210     public String JavaDoc getPassword(){
211         return new String JavaDoc(password.getPassword());
212     }
213     
214     final class BtnClick implements ActionListener{
215         public void actionPerformed(ActionEvent ae){
216             //get button
217
String JavaDoc b=ae.getActionCommand();
218             // call method depending on button clicked
219
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 JavaDoc{
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 JavaDoc(password.getPassword())));
260                 }
261                 else{
262                     // create map of attributes
263
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 JavaDoc(password.getPassword())),ht);
268                 }
269                 accountCreated(null);
270             }
271             catch(XMPPException e){
272                 accountCreated(e);
273             }
274             
275         }
276     }
277 }
Popular Tags