KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Form


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
9 import org.jivesoftware.smack.*;
10 import org.jivesoftware.smack.filter.PacketIDFilter;
11 import org.jivesoftware.smack.packet.*;
12 import org.jivesoftware.smackx.*;
13 import org.jivesoftware.smackx.muc.*;
14
15 /** Dialog for Jabber/XMPP forms.*/
16 public final class Form extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable JavaDoc, SwingConstants,WindowConstants{
17     
18     public static final int MUC_REGISTRATION_FORM=0;
19     public static final int MUC_CONFIG_FORM=1;
20     public static final int GENERAL_FORM=2;
21     public static final int SEARCH_FORM=3;
22     public static final int REGISTRATION_FORM=4;
23     
24     private static final int GAP=10;
25     private static int TEXTFIELD_SIZE=12;
26     private org.jivesoftware.smackx.Form form;
27     private int type=GENERAL_FORM;
28     private MultiUserChat muc;
29     private Vector jids=new Vector(WhisperIM.MainWindow.Contacts.values());
30     private String JavaDoc host=null;
31     
32     /** Screen Size.*/
33     public final static Dimension d=Toolkit.getDefaultToolkit().getScreenSize();
34     /** Half screen size.*/
35     public final static Dimension d2=new Dimension((int)d.getWidth()/2,(int)d.getHeight()/2);
36     private Component c;
37     
38     final BorderLayout layout=new BorderLayout(GAP,GAP);
39     final Box box=new Box(BoxLayout.Y_AXIS);
40     final JPanel panel=new JPanel(new SpringLayout());
41     final JScrollPane sp=new JScrollPane(box);
42     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
43         final JButton cancelBtn=new JButton(Lang.gs("cancel"),Icons.CANCEL);
44         final JButton submitBtn=new JButton(Lang.gs("submit"),Icons.SUBMIT);
45     
46     final BtnClick click=new BtnClick();
47     final JWaitDialog fetchWait=new JWaitDialog(this,"fetching form");
48     final JWaitDialog submitWait=new JWaitDialog(this,"submitting form");
49     
50     final JLabel instructionsLbl=new JLabel();
51     
52     final Vector fields=new Vector(5);
53     final Vector components=new Vector(5);
54     
55     final Form thisDialog=this;
56     
57     private SearchResults searchResultDialog=null;
58     
59     boolean registrationResult=false;
60     
61     /** Registration form key value.
62     * If a registration form has a key element, we want to rememember it,
63     * but not show it as a field to the user.
64     * Only applies to REGISTRATION_FORM forms.*/

65     private String JavaDoc registrationKey=null;
66     
67     /** Creates a blank form.*/
68     private Form(){
69         super(WhisperIM.MainWindow,Lang.gs("form"),false);
70         try{ //DEBUG
71
Collections.sort(jids,new ContactComparator());
72         }
73         catch(Exception JavaDoc e){
74             e.printStackTrace();
75         }
76         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
77         Container cp=getContentPane();
78         cp.setLayout(layout);
79         
80         cp.add(instructionsLbl,BorderLayout.NORTH);
81         
82         box.add(panel);
83         if(WhisperIM.isMac){
84             sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
85             sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
86         }
87         else{
88             sp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
89             sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
90         }
91         cp.add(sp,BorderLayout.CENTER);
92         
93         cancelBtn.setMnemonic(Lang.s2k("cancel_mn"));
94         cancelBtn.addActionListener(click);
95         submitBtn.setMnemonic(Lang.s2k("submit_mn"));
96         submitBtn.addActionListener(click);
97         submitBtn.setDefaultCapable(true);
98         btnPanel.add(cancelBtn);
99         btnPanel.add(submitBtn);
100         cp.add(btnPanel,BorderLayout.SOUTH);
101         getRootPane().setDefaultButton(submitBtn);
102         
103         pack();
104         setSize(d2);
105         addKeyListener(new KeyAction());
106         addMouseListener(new MouseAction());
107     }
108     
109     /** Creates a form dialog for MUC registration or configuration.*/
110     public Form(int type, MultiUserChat muc){
111         this();
112         if(type!=MUC_CONFIG_FORM && type!=MUC_REGISTRATION_FORM){
113             throw new IllegalArgumentException JavaDoc("Incorrect type of "+type+" passed to Form constructor");
114         }
115         this.type=type;
116         this.muc=muc;
117     }
118     
119     /** Creates a form for searching or registration.*/
120     public Form(int type,String JavaDoc host){
121         this();
122         if(type!=SEARCH_FORM && type!=REGISTRATION_FORM){
123             throw new IllegalArgumentException JavaDoc("Incorrect type of "+type+" passed to Form constructor");
124         }
125         if(type==SEARCH_FORM){
126             submitBtn.setIcon(Icons.SEARCH);
127             setTitle(Lang.gs("DISCO_search")+" "+host);
128         }
129         if(type==REGISTRATION_FORM){
130             submitBtn.setIcon(Icons.REGISTER);
131             setTitle(Lang.gs("DISCO_register")+" "+host);
132         }
133         if(host==null){
134             throw new NullPointerException JavaDoc("Null host passed to Form constructor");
135         }
136         this.type=type;
137         this.host=host;
138     }
139     
140     /** Resizes the form after the fields have been added.
141     * Must be called in the EDT.*/

142     public void doResize(){
143         box.add(box.createGlue());
144         SpringUtilities.makeCompactGrid(panel,components.size(),2,GAP,GAP,GAP,GAP);
145         pack();
146         if(components.size()>0){
147             ((JComponent)(components.elementAt(0))).requestFocus();
148         }
149         Dimension size=getPreferredSize();
150         if(size.getWidth()<=d2.getWidth() && size.getHeight()<=d2.getHeight()){
151             setSize(size);
152             setLocationRelativeTo(c);
153             return;
154         }
155         if(size.getWidth()>d.getWidth()){
156             size.setSize(d.getWidth(),size.getHeight());
157         }
158         if(size.getHeight()>d2.getHeight()){
159             size.setSize(size.getWidth(),d2.getHeight());
160         }
161         setSize(size);
162         setLocationRelativeTo(c);
163     }
164     
165     /** Shows the dialog and retrieves the form information.
166     * @param c The component the dialog will postioned relative to.*/

167     public void show(Component c){
168         this.c=c;
169         setLocationRelativeTo(c);
170         super.show();
171         fetchWait.show();
172         if(type==MUC_CONFIG_FORM || type==MUC_REGISTRATION_FORM){
173             (new FetchFormThread()).start();
174             return;
175         }
176         if(type==SEARCH_FORM || type==REGISTRATION_FORM){
177             (new FetchIQFormThread()).start();
178         }
179     }
180     
181     /** Submits the form.*/
182     public void doSubmit(){
183         
184         setCursor(GUI.WAIT);
185         if(type==SEARCH_FORM || type==REGISTRATION_FORM){
186             submitWait.show();
187             final Vector answers=new Vector(components.size());
188             for(int i=0;i<components.size();i++){
189                 answers.add( ( (JTextField)components.elementAt(i)).getText());
190             }
191             if(type==SEARCH_FORM){
192                 (new SubmitSeachFormThread(answers)).start();
193             }
194             else{
195                 (new SubmitRegistrationFormThread(answers)).start();
196             }
197             return;
198         }
199         final org.jivesoftware.smackx.Form answerform=form.createAnswerForm();
200         FormField f=null;
201         String JavaDoc t=null;
202         int c=0; // component count
203
for(int i=0;i<fields.size();i++){
204             f=(FormField) fields.elementAt(i);
205             t=f.getType().trim().toLowerCase();
206             if(t.equals("text-single")){
207                 answerform.setAnswer(f.getVariable(),( (JTextField) components.elementAt(c)).getText());
208                 c++;
209             }
210             if(t.equals("boolean")){
211                 answerform.setAnswer(f.getVariable(),( (JCheckBox) components.elementAt(c)).isSelected());
212                 c++;
213             }
214             if(t.equals("fixed")){
215                 c++;
216             }
217             if(t.equals("hidden")){
218                 ArrayList l=new ArrayList();
219                 for (Iterator it=f.getValues(); it.hasNext();) {
220                     String JavaDoc v=(String JavaDoc) it.next();
221                     l.add(v);
222                 }
223                 answerform.setAnswer(f.getVariable(),l);
224             }
225             if(t.equals("text-multi")){
226                 answerform.setAnswer(f.getVariable(),( (JTextArea) components.elementAt(c)).getText());
227                 c++;
228             }
229             if(t.equals("text-private")){
230                 answerform.setAnswer(f.getVariable(),new String JavaDoc (( (JPasswordField) components.elementAt(c)).getPassword()));
231                 c++;
232             }
233             if(t.equals("list-single")){
234                 FormField.Option ffo=(FormField.Option)((JComboBox) components.elementAt(c)).getSelectedItem();
235                 ArrayList ffolist=new ArrayList();
236                 ffolist.add(ffo);
237                 answerform.setAnswer(f.getVariable(),ffolist);
238                 c++;
239             }
240             if(t.equals("jid-single")){
241                 answerform.setAnswer(f.getVariable(), ((ContactListItem) ((JComboBox) components.elementAt(c)).getSelectedItem()).getID());
242                 c++;
243             }
244             if(t.equals("list-multi")){
245                 Object JavaDoc selected[]=( (JList) components.elementAt(c) ).getSelectedValues();
246                 c++;
247                 if(selected!=null){
248                     ArrayList mlist=new ArrayList();
249                     for(int i2=0; i2<mlist.size();i2++){
250                         FormField.Option ffo=(FormField.Option)selected[i2];
251                         mlist.add(ffo);
252                     }
253                     answerform.setAnswer(f.getVariable(),mlist);
254                 }
255             }
256             if(t.equals("jid-multi")){
257                 Object JavaDoc selectedjids[]=( (JList) components.elementAt(c) ).getSelectedValues();
258                 c++;
259                 if(selectedjids!=null){
260                     ArrayList jidlist=new ArrayList();
261                     for(int i3=0; i3<jidlist.size();i3++){
262                         ContactListItem cli=(ContactListItem)selectedjids[i3];
263                         jidlist.add(cli.getID());
264                     }
265                     answerform.setAnswer(f.getVariable(),jidlist);
266                 }
267             }
268         }
269         
270         submitWait.show();
271         (new SubmitFormThread(answerform)).start();
272     }
273     
274     public boolean getRegistrationResult(){
275         return registrationResult;
276     }
277     
278     public static JLabel field2Label(FormField f){
279         final JLabel result=new JLabel();
280         String JavaDoc l=f.getLabel();
281         if(l==null || l.equals("")){
282             l=f.getVariable();
283         }
284         try{
285             result.setText(Lang.gs("form_"+l.trim().toLowerCase()));
286         }
287         catch(MissingResourceException e){
288             result.setText(l+":");
289         }
290         catch(Exception JavaDoc e){
291             return result;
292         }
293         if(f.isRequired()){
294             result.setText("<html><b>"+result.getText()+"</b></html>");
295         }
296         if(f.getDescription()!=null && !f.getDescription().equals("")){
297             result.setToolTipText(f.getDescription());
298         }
299         if(f.getType()!=null && f.getType().trim().toLowerCase().equals("fixed")){
300             String JavaDoc fixed="";
301             Iterator i=f.getValues();
302             while(i.hasNext()){
303                 fixed=fixed+( (String JavaDoc) i.next())+"<br>";
304             }
305             result.setText("<html><p>"+fixed+"</p></html>");
306         }
307         return result;
308     }
309     
310     public void showError(final Exception JavaDoc e, final JWaitDialog w){
311         SwingUtilities.invokeLater(new Runnable JavaDoc(){
312                 public void run(){
313                 w.dispose();
314                 GUI.showError(thisDialog,"form",null,host+"\r\n"+e.getMessage());
315                 if(w==fetchWait){
316                     dispose();
317                 }
318             }
319         });
320     }
321     
322     
323     
324     public void showError(final XMPPException xe, final JWaitDialog w){
325         SwingUtilities.invokeLater(new Runnable JavaDoc(){
326                 public void run(){
327                 w.dispose();
328                 GUI.showError(thisDialog,"form", null ,host,xe);
329                 if(w==fetchWait){
330                     dispose();
331                 }
332             }
333         });
334     }
335
336     final class BtnClick implements ActionListener{
337         public void actionPerformed(ActionEvent ae){
338             //get button
339
String JavaDoc b=ae.getActionCommand();
340             // call method depending on button clicked
341
if(b.equals(cancelBtn.getText())){
342                 dispose();
343                 return;
344             }
345             if(b.equals(submitBtn.getText())){
346                 doSubmit();
347                 return;
348             }
349         }
350     }
351     
352     final class FetchFormThread extends Thread JavaDoc{
353         
354         public FetchFormThread(){
355             super("Fetch Form Thread in Form.class");
356             setDaemon(true);
357             setPriority(Thread.MIN_PRIORITY);
358         }
359         
360         public void run(){
361             try{
362                 //get form
363
if(type==MUC_CONFIG_FORM){
364                     form=muc.getConfigurationForm();
365                 }
366                 if(type==MUC_REGISTRATION_FORM){
367                     form=muc.getRegistrationForm();
368                 }
369                 
370                 if(form==null){ // no form recieved
371
if(type==MUC_CONFIG_FORM){
372                         SwingUtilities.invokeLater(new Runnable JavaDoc(){
373                             public void run(){
374                                 fetchWait.dispose();
375                                 GUI.showError(thisDialog,"configure room","config not possible");
376                                 thisDialog.dispose();
377                             }
378                         });
379                     }
380                     if(type==MUC_REGISTRATION_FORM){
381                         SwingUtilities.invokeLater(new Runnable JavaDoc(){
382                             public void run(){
383                                 fetchWait.dispose();
384                                 GUI.showError(thisDialog,"register","registration not possible");
385                                 thisDialog.dispose();
386                             }
387                         });
388                     }
389                     return;
390                 }
391                 final Iterator f =form.getFields();
392                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
393                     public void run(){
394                         instructionsLbl.setText("<html><p>"+form.getInstructions()+"</p></html>");
395                         String JavaDoc t;
396                         Iterator vi;
397                         String JavaDoc v;
398                         thisDialog.setTitle(Lang.gs("form")+" - "+form.getTitle());
399                         while(f.hasNext()){
400                             FormField field=(FormField) f.next();
401                             fields.add(field);
402                             t=field.getType().trim().toLowerCase();
403                             if(!t.equals("hidden")){
404                                 panel.add(field2Label(field));
405                             }
406                             
407                             if(t.equals("fixed")){
408                                 final JLabel blank=new JLabel();
409                                 panel.add(blank);
410                                 components.add(blank);
411                             }
412                             
413                             vi=field.getValues();
414                             if(t.equals("text-single")){
415                                 v="";
416                                 if(vi.hasNext()){
417                                     v=(String JavaDoc) vi.next();
418                                 }
419                                 final JTextField tf=new JTextField(v,TEXTFIELD_SIZE);
420                                 components.add(tf);
421                                 panel.add(tf);
422                             }
423                             if(t.equals("text-multi")){
424                                 v="";
425                                 if(vi.hasNext()){
426                                     v=(String JavaDoc) vi.next();
427                                 }
428                                 final JTextArea ta=new JTextArea(v,3,TEXTFIELD_SIZE);
429                                 final JScrollPane sp=new JScrollPane(ta);
430                                 GUI.setScrollBars(sp);
431                                 ta.setLineWrap(true);
432                                 ta.setWrapStyleWord(true);
433                                 components.add(ta);
434                                 panel.add(sp);
435                             }
436                             if(t.equals("text-private")){
437                                 JPasswordField pf=new JPasswordField(TEXTFIELD_SIZE);
438                                 components.add(pf);
439                                 panel.add(pf);
440                             }
441                             if(t.equals("boolean")){
442                                 final JCheckBox chb=new JCheckBox("");
443                                 String JavaDoc b="";
444                                 if(vi.hasNext()){
445                                     b=((String JavaDoc) vi.next()).trim().toLowerCase();
446                                 }
447                                 if(b.equals("1") || b.equals("true") || b.equals("yes")){
448                                     chb.setSelected(true);
449                                 }
450                                 components.add(chb);
451                                 panel.add(chb);
452                             }
453                             if(t.equals("list-single") || t.equals("list-multi")){
454                                 final Vector o=new Vector(5);
455                                 final Iterator oi=field.getOptions();
456                                 while(oi.hasNext()){
457                                     o.add( (FormField.Option) oi.next());
458                                 }
459                                 if(t.equals("list-single")){
460                                     final JComboBox cb=new JComboBox(o);
461                                     cb.setRenderer(new ListRenderer());
462                                     components.add(cb);
463                                     panel.add(cb);
464                                 }
465                                 else{
466                                     final JList jlist=new JList(o);
467                                     jlist.setCellRenderer(new ListRenderer());
468                                     final JScrollPane listsp=new JScrollPane(jlist);
469                                     GUI.setScrollBars(listsp);
470                                     jlist.setVisibleRowCount(3);
471                                     jlist.setLayoutOrientation(JList.VERTICAL);
472                                     jlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
473                                     components.add(jlist);
474                                     panel.add(listsp);
475                                 }
476                             }
477                             if(t.equals("jid-single")){
478                                     final JComboBox jcb=new JComboBox(jids);
479                                     jcb.setRenderer(new ListRenderer());
480                                     components.add(jcb);
481                                     panel.add(jcb);
482                             }
483                             if(t.equals("jid-multi")){
484                                 final JList jidlist=new JList(jids);
485                                 jidlist.setCellRenderer(new ListRenderer());
486                                 final JScrollPane jidlistsp=new JScrollPane(jidlist);
487                                 GUI.setScrollBars(jidlistsp);
488                                 jidlist.setVisibleRowCount(3);
489                                 jidlist.setLayoutOrientation(JList.VERTICAL);
490                                 jidlist.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
491                                 components.add(jidlist);
492                                 panel.add(jidlistsp);
493                             }
494                             
495                         } //end while
496
doResize();
497                         fetchWait.dispose();
498                     } // end swing run
499
}); // end invokeLater
500
return;
501             }
502             catch(final XMPPException xe){
503                 int e=0;
504                 if(xe.getXMPPError()!=null){
505                     e=xe.getXMPPError().getCode();
506                 }
507                 if(e==403 || e==405){
508                     SwingUtilities.invokeLater(new Runnable JavaDoc(){
509                     public void run(){
510                         String JavaDoc t="form";
511                         if(type==MUC_REGISTRATION_FORM){
512                             t="register";
513                         }
514                         if(type==MUC_CONFIG_FORM){
515                             t="configure room";
516                         }
517                         fetchWait.dispose();
518                         GUI.showWarning(thisDialog,t,"forbidden");
519                         thisDialog.dispose();
520                     }});
521                     return;
522                 }
523                 showError(xe,fetchWait);
524                 return;
525             }
526             catch(final Exception JavaDoc e){
527                 System.err.println(e.getMessage());
528                 showError(e,fetchWait);
529             }
530         }
531     }
532     
533     final class SubmitFormThread extends Thread JavaDoc{
534         
535         org.jivesoftware.smackx.Form answers;
536         
537         public SubmitFormThread(org.jivesoftware.smackx.Form answerform){
538             super("Submit Form Thread in Form.class");
539             setDaemon(true);
540             setPriority(Thread.MIN_PRIORITY);
541             this.answers=answerform;
542         }
543         
544         public void run(){
545             try{
546                 if(type==MUC_CONFIG_FORM){
547                     muc.sendConfigurationForm(answers);
548                 }
549                 if(type==MUC_REGISTRATION_FORM){
550                     muc.sendRegistrationForm(answers);
551                 }
552             }
553             catch (final XMPPException xe){
554                 showError(xe,submitWait);
555                 return;
556             }
557             catch(final Exception JavaDoc e){
558                 System.err.println(e.getMessage());
559                 showError(e,submitWait);
560                 return;
561             }
562             SwingUtilities.invokeLater(new Runnable JavaDoc(){
563                 public void run(){
564                     submitWait.dispose();
565                     GUI.showInfo(thisDialog,"form","form submitted");
566                     thisDialog.dispose();
567                 }
568             });
569         }
570     }
571     
572     final class FetchIQFormThread extends Thread JavaDoc{
573         
574         public FetchIQFormThread(){
575             super("Fetch IQ Form Thread in Form.class");
576             setDaemon(true);
577             setPriority(Thread.MIN_PRIORITY);
578         }
579         
580         private String JavaDoc n2e(String JavaDoc s){
581             if(s==null){
582                 return "";
583             }
584             else{
585                 return s;
586             }
587         }
588         
589         public void run(){
590             PacketCollector collector=null;
591             try{
592                 final IQ iq;
593                 if(type==SEARCH_FORM){
594                     iq=new SearchIQ();
595                 }
596                 else{
597                     if(type!=REGISTRATION_FORM){
598                         throw new IllegalArgumentException JavaDoc("Bad form type of"+type);
599                     }
600                     iq=new Registration();
601                 }
602                 iq.setType(IQ.Type.GET);
603                 iq.setTo(host);
604                 iq.setFrom(WhisperIM.MainWindow.Conn.getUser());
605                 collector=WhisperIM.MainWindow.Conn.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
606                 
607                 WhisperIM.MainWindow.Conn.sendPacket(iq);
608                 SearchIQ searchResult=null;
609                 Registration regResult=null;
610                 if(type==SEARCH_FORM){
611                     searchResult=(SearchIQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
612                 }
613                 else{
614                     regResult=(Registration)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
615                 }
616                 
617                 collector.cancel();
618                 if((type==SEARCH_FORM && searchResult==null) || (type==REGISTRATION_FORM && regResult==null)){
619                     throw new Exception JavaDoc("No reply from server");
620                 }
621                 if((type==SEARCH_FORM && searchResult.getError()!=null) || (type==REGISTRATION_FORM && regResult.getError()!=null)){
622                     if(type==SEARCH_FORM){
623                         throw new XMPPException(searchResult.getError());
624                     }
625                     else{
626                         throw new XMPPException(regResult.getError());
627                     }
628                 }
629                 
630                 String JavaDoc name;
631                 final Vector fieldnames=new Vector(5);
632                 String JavaDoc labelname;
633                 final Vector fielddata=new Vector(5);
634                 
635                 if(type==SEARCH_FORM){
636                     Enumeration e=searchResult.getFields();
637                     while(e.hasMoreElements()){
638                         name=(String JavaDoc)e.nextElement();
639                         fields.add(name);
640                         fielddata.add("");
641                         try{
642                             labelname=Lang.gs("form_"+name.trim().toLowerCase());
643                         }
644                         catch(MissingResourceException mre){
645                             labelname=name+":";
646                         }
647                         fieldnames.add(labelname);
648                     }
649                 }
650                 else{
651                     //need to see if there's a password and or username in attributes
652
// as the Smack PacketReader pulls them out when creating the Registration packet object.
653
Map m=regResult.getAttributes();
654                     String JavaDoc xml=regResult.toXML().toLowerCase();
655                     if(xml.indexOf("<password/>")!=-1 || xml.indexOf("<password>")!=-1){
656                         if(!m.containsKey("password")){
657                             m.put("password",n2e(regResult.getPassword()));
658                         }
659                     }
660                     if(xml.indexOf("<username/>")!=-1 || xml.indexOf("<username>")!=-1){
661                         if(!m.containsKey("username")){
662                             m.put("username",n2e(regResult.getUsername()));
663                         }
664                     }
665                     // we dont't want to show a field for a <registered/> tag
666
if(m.containsKey("registered")){
667                         String JavaDoc tmp=(String JavaDoc)m.get("registered");
668                         if(tmp==null || tmp.equals("")){ //check teg is empty
669
m.remove("registered");
670                         }
671                     }
672                     // we don't want to to show a key element if one exists
673
if(m.containsKey("key")){
674                         registrationKey=n2e((String JavaDoc)m.get("key")); //rememember it for submitting the form
675
m.remove("key"); // don't show the field to the user
676
}
677                     Iterator i=regResult.getAttributes().keySet().iterator();
678                     while(i.hasNext()){
679                         name=(String JavaDoc) i.next();
680                         fields.add(name);
681                         fielddata.add(n2e( (String JavaDoc) m.get(name)));
682                         try{
683                             labelname=Lang.gs("form_"+name.trim().toLowerCase());
684                         }
685                         catch(MissingResourceException mre){
686                             labelname=name+":";
687                         }
688                         fieldnames.add(labelname);
689                     }
690                 }
691                 // create form.
692
String JavaDoc instructions="";
693                 if(type==SEARCH_FORM && searchResult.getInstructions()!=null){
694                         instructions=searchResult.getInstructions();
695                 }
696                 if(type==REGISTRATION_FORM && regResult.getInstructions()!=null){
697                         instructions=regResult.getInstructions();
698                 }
699                 final String JavaDoc finalinstructions=instructions;
700                 SwingUtilities.invokeLater(new Runnable JavaDoc(){
701                     public void run(){
702                         instructionsLbl.setText(finalinstructions);
703                         String JavaDoc labelname2;
704                         for (int i=0;i<fields.size();i++){
705                             labelname2=(String JavaDoc)fieldnames.elementAt(i);
706                             JTextField tf=new JTextField(TEXTFIELD_SIZE);
707                             tf.setText(n2e( (String JavaDoc) fielddata.elementAt(i)));
708                             components.add(tf);
709                             panel.add(new JLabel(labelname2));
710                             panel.add(tf);
711                         }
712                         doResize();
713                         fetchWait.dispose();
714                     }
715                 });
716             }
717             catch (final XMPPException xe){
718                 if(collector!=null){
719                     collector.cancel();
720                 }
721                 showError(xe,fetchWait);
722                 return;
723             }
724             catch(final Exception JavaDoc e){
725                 e.printStackTrace();
726                 if(collector!=null){
727                     collector.cancel();
728                 }
729                 System.err.println(e.getMessage());
730                 showError(e,fetchWait);
731                 return;
732             }
733         }
734     }
735     
736     final class SubmitSeachFormThread extends Thread JavaDoc{
737         
738         private Vector answers;
739         
740         public SubmitSeachFormThread(Vector answers){
741             super("Submit Search Form Thread in Form.class");
742             setDaemon(true);
743             setPriority(Thread.MIN_PRIORITY);
744             this.answers=answers;
745         }
746         
747         public void run(){
748             PacketCollector collector=null;
749             try{
750                 SearchIQ iq=new SearchIQ();
751                 for(int i=0;i<answers.size();i++){
752                     iq.addField((String JavaDoc)fields.elementAt(i));
753                     iq.setField((String JavaDoc)fields.elementAt(i),(String JavaDoc)answers.elementAt(i));
754                 }
755                 iq.setFrom(WhisperIM.MainWindow.Conn.getUser());
756                 iq.setTo(host);
757                 iq.setType(IQ.Type.SET);
758                 collector=WhisperIM.MainWindow.Conn.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
759                 WhisperIM.MainWindow.Conn.sendPacket(iq);
760                 final SearchIQ result=(SearchIQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
761                 collector.cancel();
762                 if(result==null){
763                     throw new Exception JavaDoc("No reply to search from server");
764                 }
765                 if(result.getError()!=null){
766                     throw new XMPPException(result.getError());
767                 }
768                 //System.out.println(result.toXML());
769
SwingUtilities.invokeLater(new Runnable JavaDoc(){
770                     public void run(){
771                         submitWait.dispose();
772                         if(result.getNumberOfItems()==0){
773                             GUI.showInfo(thisDialog,"search result","no search result");
774                             return;
775                         }
776                         if(searchResultDialog==null){
777                             searchResultDialog=new SearchResults();
778                         }
779                         searchResultDialog.setData(result);
780                         searchResultDialog.setTitle(Lang.gs("search result")+" : "+host);
781                         searchResultDialog.setLocationRelativeTo(thisDialog);
782                         searchResultDialog.show();
783                     }
784                 });
785             }
786             catch (final XMPPException xe){
787                 if(collector!=null){
788                     collector.cancel();
789                 }
790                 showError(xe,submitWait);
791                 return;
792             }
793             catch(final Exception JavaDoc e){
794                 //e.printStackTrace();
795
if(collector!=null){
796                     collector.cancel();
797                 }
798                 System.err.println(e.getMessage());
799                 showError(e,submitWait);
800                 return;
801             }
802         }
803     }
804     
805     final class SubmitRegistrationFormThread extends Thread JavaDoc{
806         
807         private Vector answers;
808         
809         public SubmitRegistrationFormThread(Vector answers){
810             super("Submit Registration Form Thread in Form.class");
811             setDaemon(true);
812             setPriority(Thread.MIN_PRIORITY);
813             this.answers=answers;
814         }
815         
816         public void run(){
817             PacketCollector collector=null;
818             try{
819                 Registration iq=new Registration();
820                 String JavaDoc f;
821                 Map m=iq.getAttributes();
822                 if(m==null){
823                     m=new HashMap();
824                 }
825                 for(int i=0;i<answers.size();i++){
826                     f=(String JavaDoc)fields.elementAt(i);
827                     if(f.equals("password") || f.equals("username")){
828                         if(f.equals("password")){
829                             iq.setPassword((String JavaDoc)answers.elementAt(i));
830                         }
831                         else{
832                             iq.setUsername((String JavaDoc)answers.elementAt(i));
833                         }
834                     }
835                     else{
836                         m.put(f,(String JavaDoc)answers.elementAt(i));
837                     }
838                 }
839                 if(registrationKey!=null){ //if there was a key element, put in the submission
840
m.put("key",registrationKey);
841                 }
842                 
843                 iq.setAttributes(m);
844                 iq.setFrom(WhisperIM.MainWindow.Conn.getUser());
845                 iq.setTo(host);
846                 iq.setType(IQ.Type.SET);
847                 collector=WhisperIM.MainWindow.Conn.createPacketCollector(new PacketIDFilter(iq.getPacketID()));
848                 WhisperIM.MainWindow.Conn.sendPacket(iq);
849                 final IQ result=(IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
850                 collector.cancel();
851                 collector=null;
852                 if(result==null){
853                     throw new Exception JavaDoc("No reply to registration from server");
854                 }
855                 if(result.getError()!=null){
856                     throw new XMPPException(result.getError());
857                 }
858                 //System.out.println(result.toXML());
859
SwingUtilities.invokeLater(new Runnable JavaDoc(){
860                     public void run(){
861                         submitWait.dispose();
862                         registrationResult=true;
863                         GUI.showInfo(thisDialog,"DISCO_register","registered");
864                         thisDialog.dispose();
865                     }
866                 });
867             }
868             catch (final XMPPException xe){
869                 if(collector!=null){
870                     collector.cancel();
871                 }
872                 showError(xe,submitWait);
873                 return;
874             }
875             catch(final Exception JavaDoc e){
876                 //e.printStackTrace();
877
if(collector!=null){
878                     collector.cancel();
879                 }
880                 System.err.println(e.getMessage());
881                 showError(e,submitWait);
882                 return;
883             }
884         }
885     }
886     
887     
888     final class ListRenderer extends JLabel implements ListCellRenderer {
889
890         public Component getListCellRendererComponent(JList list,Object JavaDoc value,int index,boolean isSelected,boolean cellHasFocus){
891             setText( ( (FormField.Option) value).getLabel());
892             if(isSelected){
893                 setBackground(list.getSelectionBackground());
894                 setForeground(list.getSelectionForeground());
895             }
896             else{
897                 setBackground(list.getBackground());
898                 setForeground(list.getForeground());
899             }
900             setEnabled(list.isEnabled());
901             setFont(list.getFont());
902             setOpaque(true);
903             return this;
904         }
905     }
906     
907 }
Popular Tags