KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > gui > windows > ChannelListDialog


1 package rero.gui.windows;
2
3 import javax.swing.*;
4 import javax.swing.event.*;
5 import javax.swing.table.*;
6
7 import java.awt.*;
8 import java.awt.event.*;
9
10 import java.util.*;
11 import rero.util.*;
12
13 import contrib.javapro.*; // sorted JTable code...
14

15 import rero.ircfw.interfaces.*;
16
17 import rero.config.*;
18 import rero.client.*;
19
20 import rero.gui.*;
21 import rero.gui.windows.*;
22 import rero.gui.toolkit.*;
23
24 import text.*;
25
26 public class ChannelListDialog extends GeneralListDialog implements ActionListener
27 {
28    protected JTextField search;
29    protected JLabel label;
30    protected JSortTable table;
31
32    public void actionPerformed(ActionEvent ev)
33    {
34       ((ChannelTableModel)model).search(search.getText());
35    }
36
37    public void init() // overwriting the init in the other one...
38
{
39       capabilities.addTemporaryListener((ChannelTableModel)model);
40    }
41
42    public void processMouseEvent (MouseEvent ev, int row)
43    {
44       ChannelTableModel temp = (ChannelTableModel)model;
45       SessionManager.getGlobalCapabilities().getActiveSession().executeCommand("/JOIN " + ((LChannel)temp.getData().get(row)).getChannel());
46    }
47
48    public ChannelListDialog()
49    {
50       super("Channel List", "list", new ChannelTableModel());
51
52       JPanel top = new JPanel();
53       top.setLayout(new BorderLayout());
54       top.setOpaque(true);
55
56       JPanel righttop = new JPanel();
57       righttop.setLayout(new FlowLayout(FlowLayout.RIGHT));
58       righttop.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
59
60       search = new JTextField();
61       search.setColumns(12);
62       search.addActionListener(this);
63
64       JButton doit = new JButton("Search");
65       doit.addActionListener(this);
66
67       righttop.add(search);
68       righttop.add(doit);
69
70       top.add(righttop, BorderLayout.EAST);
71
72       label = new JLabel("Waiting for the storm...");
73
74       ((ChannelTableModel)model).setLabel(label);
75
76       top.add(label, BorderLayout.CENTER);
77       
78       add(top, BorderLayout.NORTH);
79    }
80
81    private static class LChannel
82    {
83       private String JavaDoc name, topic;
84       private int users;
85
86       public LChannel(String JavaDoc _name, int _users, String JavaDoc _topic)
87       {
88          name = _name;
89          users = _users;
90          topic = _topic;
91
92          if (topic.equals(":"))
93          {
94              topic = "";
95          }
96       }
97
98       public String JavaDoc getChannel()
99       {
100          return name;
101       }
102
103       public String JavaDoc getTopic()
104       {
105          return topic;
106       }
107
108       private AttributedString topicattrs, nameattrs, userattrs;
109
110       public AttributedString getTopicAttributed()
111       {
112          if (topicattrs != null)
113            return topicattrs;
114
115          topicattrs = AttributedString.CreateAttributedString(topic);
116          topicattrs.assignWidths();
117
118          return topicattrs;
119       }
120
121       public AttributedString getChannelAttributed()
122       {
123          if (nameattrs != null)
124            return nameattrs;
125
126          nameattrs = AttributedString.CreateAttributedString(name);
127          nameattrs.assignWidths();
128
129          return nameattrs;
130       }
131
132       public AttributedString getUsersAttributed()
133       {
134          if (userattrs != null)
135            return userattrs;
136
137          userattrs = AttributedString.CreateAttributedString(users + "");
138          userattrs.assignWidths();
139
140          return userattrs;
141       }
142
143       public int getUsers()
144       {
145          return users;
146       }
147
148       public boolean matches(String JavaDoc criteria)
149       {
150          if (criteria.indexOf('*') > -1 || criteria.indexOf('?') > -1)
151             return StringUtils.iswm(criteria.toUpperCase(), (getChannel() + " " + StringUtils.strip(getTopic())).toUpperCase());
152
153          return (getChannel() + " " + StringUtils.strip(getTopic())).toUpperCase().indexOf(criteria.toUpperCase()) > -1;
154       }
155    }
156  
157    private static class ChannelComparator implements Comparator
158    {
159       private int criteria;
160       private boolean reverse;
161
162       public ChannelComparator(int _criteria, boolean _reverse)
163       {
164          criteria = _criteria;
165          reverse = _reverse;
166       }
167
168       public int compare(Object JavaDoc one, Object JavaDoc two)
169       {
170          LChannel a = (LChannel)one;
171          LChannel b = (LChannel)two;
172
173          if (reverse)
174          {
175             LChannel c = b;
176             b = a;
177             a = c; // swap the values we're comparing if we're sorting in ascending mode...
178
}
179
180          switch (criteria)
181          {
182             case 0:
183               return a.getChannel().compareTo(b.getChannel());
184
185             case 1:
186               if (a.getUsers() < b.getUsers())
187                  return 1;
188
189               if (a.getUsers() > b.getUsers())
190                  return -1;
191
192               return 0;
193
194             case 2:
195               return a.getTopic().compareTo(b.getTopic());
196          }
197
198          return 0;
199       }
200    }
201  
202    private static class ChannelTableModel extends GeneralListModel implements ChatListener, FrameworkConstants
203    {
204       protected ArrayList filter = null;
205       protected ArrayList channels;
206       protected int x = 0;
207       protected JLabel label;
208
209       public HashMap getEventHashMap (int row)
210       {
211          LChannel channel = (LChannel)getData().get(row);
212          return ClientUtils.getEventHashMap(channel.getChannel(), channel.getUsers() + " " + channel.getTopic());
213       }
214
215       public void setLabel(JLabel l) { label = l; }
216
217       public boolean isChatEvent(String JavaDoc id, HashMap data)
218       {
219          return (id.equals("321") || id.equals("322") || id.equals("323"));
220       }
221
222       public void search(String JavaDoc criteria)
223       {
224          if (criteria.equals(""))
225          {
226             filter = null;
227             fireTableDataChanged();
228             return;
229          }
230
231          filter = new ArrayList();
232          Iterator i = channels.iterator();
233          while (i.hasNext())
234          {
235             LChannel temp = (LChannel)i.next();
236             if (temp.matches(criteria))
237             {
238                filter.add(temp);
239             }
240          }
241
242          fireTableDataChanged();
243       }
244  
245       private ArrayList getData()
246       {
247          if (filter != null)
248             return filter;
249
250          return channels;
251       }
252
253       public int fireChatEvent(HashMap data)
254       {
255          String JavaDoc event = (String JavaDoc)data.get($EVENT$);
256
257          if (event.equals("321"))
258          {
259             label.setText("... Listing Channels ...");
260
261             channels.clear();
262             filter = null;
263
264             fireTableDataChanged();
265          }
266
267          if (event.equals("322"))
268          {
269             TokenizedString text = new TokenizedString(data.get($PARMS$).toString());
270             text.tokenize(" ");
271
272             channels.add(new LChannel(text.getToken(0), Integer.parseInt(text.getToken(1)), text.getTokenFrom(2)));
273
274             if ((x % 1000) == 0)
275             {
276                fireTableDataChanged();
277             }
278
279             if (x > 10000 && x < 11000)
280             {
281                label.setText("Go grab a beer, this is gonna be awhile...");
282             }
283             else if (x > 20000 && x < 21000)
284             {
285                label.setText("I bet we're not even halfway there yet");
286             }
287             else if (x > 25000 && x < 26000)
288             {
289                label.setText("So what do you do for a living?");
290             }
291             else if (x > 26000 && x < 26500)
292             {
293                label.setText("Really, does that make you gay?");
294             }
295             else if (x > 26500 && x < 27500)
296             {
297                label.setText("Don't worry, I don't judge");
298             }
299             else if (x > 27500 && x < 28500)
300             {
301                label.setText("I'm just an irc client... or am I?");
302             }
303             else if (x > 35000 && x < 36000)
304             {
305                label.setText("I love you. You love me. We're a happy...");
306             }
307             else if (x > 36000 && x < 36500)
308             {
309                label.setText("... family?");
310             }
311             else if (x > 43000 && x < 44000)
312             {
313                label.setText("How many freaking channels are on this network?");
314             }
315             else if ((x % 175) == 0)
316             {
317                label.setText(".x. Listing Channels .x.: " + channels.size());
318             }
319             else if ((x % 175) == 43)
320             {
321                label.setText("..x Listing Channels ..x: " + channels.size());
322             }
323             else if ((x % 175) == 81)
324             {
325                label.setText(".x. Listing Channels .x.: " + channels.size());
326             }
327             else if ((x % 175) == 127)
328             {
329                label.setText("x.. Listing Channels x..: " + channels.size());
330             }
331
332             x++;
333          }
334
335          if (event.equals("323"))
336          {
337             label.setText("Channel /list complete: " + channels.size() + " channels");
338
339             x = 0;
340             fireTableDataChanged();
341             return REMOVE_LISTENER | EVENT_DONE;
342          }
343
344
345          return EVENT_HALT;
346       }
347
348       public ChannelTableModel()
349       {
350          channels = new ArrayList(10000);
351       }
352
353       public void sortColumn(int col, boolean ascending)
354       {
355          Collections.sort(getData(), new ChannelComparator(col, ascending));
356          fireTableDataChanged();
357       }
358
359       public int getRowCount()
360       {
361          return getData().size();
362       }
363
364       public int getColumnCount()
365       {
366          return 3;
367       }
368
369       public int getColumnWidth(int col)
370       {
371          if (col == 0)
372              return 150;
373
374          if (col == 1)
375              return 75;
376
377          return -1;
378       }
379
380       public String JavaDoc getColumnName(int col)
381       {
382          switch (col)
383          {
384             case 0:
385               return "#Channel";
386
387             case 1:
388               return "Users";
389
390             case 2:
391               return "Current Topic";
392          }
393
394          return "Unknown";
395       }
396
397       public Object JavaDoc getValueAt(int row, int col)
398       {
399          if (row >= getData().size()) return null;
400
401          switch (col)
402          {
403             case 0:
404               return ((LChannel)getData().get(row)).getChannelAttributed();
405
406             case 1:
407               return ((LChannel)getData().get(row)).getUsersAttributed();
408
409             case 2:
410               return ((LChannel)getData().get(row)).getTopicAttributed();
411          }
412
413          return "Unknown";
414       }
415    }
416
417    public String JavaDoc getWindowType()
418    {
419       return "ListDialog";
420    }
421 }
422
Popular Tags