KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SearchResults


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.swing.table.DefaultTableModel JavaDoc;
8 import javax.accessibility.Accessible JavaDoc;
9
10 import org.jivesoftware.smack.*;
11 import org.jivesoftware.smack.filter.PacketIDFilter;
12 import org.jivesoftware.smack.packet.*;
13 import org.jivesoftware.smackx.*;
14 import org.jivesoftware.smackx.muc.*;
15
16 /** Dialog for jabber search results.*/
17 public final class SearchResults extends JDialog implements Accessible JavaDoc, ImageObserver JavaDoc, MenuContainer, RootPaneContainer, Serializable JavaDoc, SwingConstants,WindowConstants{
18     
19     final static private int GAP=10;
20     final BorderLayout layout=new BorderLayout(GAP,GAP);
21     
22     final ReadOnlyTableModel tableModel=new ReadOnlyTableModel();
23     final JTable table=new JTable(tableModel);
24     final JScrollPane sp=new JScrollPane(table);
25     
26     final Vector columns=new Vector();
27         
28     
29     final JPanel btnPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT,GAP,GAP));
30         final JButton closeBtn=new JButton(Lang.gs("close"),Icons.CLOSE);
31     
32     final BtnClick click=new BtnClick();
33     
34     public SearchResults(){
35         super(WhisperIM.MainWindow,Lang.gs("search result"),false);
36         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
37         Container cp=getContentPane();
38         cp.setLayout(layout);
39         
40         table.setColumnSelectionAllowed(false);
41         table.setRowSelectionAllowed(false);
42         table.setCellSelectionEnabled(false);
43         cp.add(sp,BorderLayout.CENTER);
44         
45         closeBtn.setMnemonic(Lang.s2k("close_mn"));
46         closeBtn.addActionListener(click);
47         closeBtn.setDefaultCapable(true);
48         btnPanel.add(closeBtn);
49         cp.add(btnPanel,BorderLayout.SOUTH);
50         getRootPane().setDefaultButton(closeBtn);
51         setSize(Form.d2);
52         pack();
53     }
54     
55     public void setData(SearchIQ iq){
56         //reset the data first
57
tableModel.setColumnCount(0);
58         tableModel.setRowCount(0);
59         
60         final Hashtable columns=new Hashtable();
61         Enumeration e=iq.getJids();
62         DefaultPacketExtension dpe=null;
63         Iterator i=null;
64         String JavaDoc c=null;
65         while(e.hasMoreElements()){
66             dpe=iq.getItem((String JavaDoc)e.nextElement());
67             i=dpe.getNames() ;
68             while(i.hasNext()){
69                 c=(String JavaDoc)i.next();
70                 columns.put(c,c);
71             }
72         }
73         
74         e=iq.getJids();
75         Enumeration columnNames=columns.keys();
76         String JavaDoc name=null;
77         tableModel.addColumn("Jabber ID");
78         while(columnNames.hasMoreElements()){
79             name=(String JavaDoc)columnNames.nextElement();
80             try{
81                 name=Lang.gs("form_"+name.trim().toLowerCase());
82             }
83             catch(MissingResourceException mre){
84                 //do nothing
85
}
86             tableModel.addColumn(name);
87         }
88         String JavaDoc data="";
89         String JavaDoc jid="";
90         Vector row=null;
91         while(e.hasMoreElements()){
92             columnNames=columns.keys();
93             jid=(String JavaDoc)e.nextElement();
94             dpe=iq.getItem(jid);
95             row=new Vector();
96             row.add(jid);
97             while(columnNames.hasMoreElements()){
98                 try{
99                     data=dpe.getValue( (String JavaDoc)columnNames.nextElement());
100                 }
101                 catch(Exception JavaDoc ex){
102                     data="";
103                 }
104                 row.add(data);
105             }
106             tableModel.addRow(row);
107         }
108         columns.clear();
109         table.doLayout();
110         closeBtn.requestFocus();
111     }
112     
113     
114     final class BtnClick implements ActionListener{
115         public void actionPerformed(ActionEvent ae){
116             //get button
117
String JavaDoc b=ae.getActionCommand();
118             // call method depending on button clicked
119
if(b.equals(closeBtn.getText())){
120                 dispose();
121                 return;
122             }
123         }
124     }
125     
126     final class ReadOnlyTableModel extends DefaultTableModel JavaDoc{
127         
128         public ReadOnlyTableModel(){
129             super();
130         }
131         
132         public boolean isCellEditable(int row,int column){
133             return false;
134         }
135     }
136 }
Popular Tags