View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.gui.table;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  
25  import org.xnap.XNap;
26  import org.xnap.chat.ChannelInfo;
27  import org.xnap.chat.ChatProvider;
28  
29  public class ChannelTableModel extends AbstractColumnTableModel
30  {
31      //--- Constant(s) ---
32  
33      //--- Data field(s) ---
34  
35      private ArrayList rows = new ArrayList();
36      private ChatProvider provider;
37  
38      //--- Constructor(s) ---
39  
40      public ChannelTableModel() 
41      {
42  	Column columns[] = new Column[] {
43  	    new Column("channel", XNap.tr("Channel"), String.class),
44  	    new Column("userCount", XNap.tr("Users"), Integer.class, 
45  		       new NumberCellRenderer()),
46  	    new Column("topic", XNap.tr("Topic"), String.class),
47  	};
48  	addColumns(columns);
49      }
50  
51      //--- Method(s) ---
52  
53      public void clear()
54      {
55  	rows.clear();
56  	fireTableDataChanged();
57      }
58  
59      public ChannelInfo get(int i)
60      {
61  	return (ChannelInfo)rows.get(mapToIndex(i));
62      }
63  
64      public int getRowCount()
65      {
66          return rows.size();
67      }
68      
69      public Object get(int row, int column) 
70      {
71          ChannelInfo i = (ChannelInfo)rows.get(row);
72  
73          switch (column) {
74  	case 0:
75  	    return i.getName();
76    	case 1:
77    	    return new Integer(i.getPeerCount());
78    	case 2:
79    	    return i.getTopic();
80  	default:
81  	    return null;
82          }
83      }
84  
85      public void set(ChatProvider provider)
86      {
87  	this.provider = provider;
88  
89  	ChannelInfo[] channels = provider.getChannels();
90  	if (channels != null) {
91  	    rows = new ArrayList(Arrays.asList(channels));
92  	    fireTableDataChanged();
93  	    resort();
94  	}
95  	else {
96  	    clear();
97  	}
98      }
99  
100 }