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.plugin.opennap.gui;
21  
22  import java.util.ArrayList;
23  
24  import javax.swing.SwingUtilities;
25  
26  import org.xnap.XNap;
27  import org.xnap.gui.table.AbstractColumnTableModel;
28  import org.xnap.gui.table.Column;
29  import org.xnap.gui.table.NumberCellRenderer;
30  import org.xnap.gui.table.TimeCellRenderer;
31  import org.xnap.plugin.opennap.net.msg.MessageHandler;
32  import org.xnap.plugin.opennap.net.msg.MessageListener;
33  import org.xnap.plugin.opennap.net.msg.server.ServerMessage;
34  import org.xnap.plugin.opennap.net.msg.server.WhoisResponseMessage;
35  import org.xnap.plugin.opennap.user.OpenNapUser;
36  import org.xnap.plugin.opennap.util.OpenNapLinkType;
37  
38  public class OpenNapWhoisTableModel extends AbstractColumnTableModel 
39      implements MessageListener {
40  
41      //--- Constant(s) ---
42  
43      //--- Data field(s) ---
44  
45      private ArrayList rows = new ArrayList();
46  
47      //--- Constructor(s) ---
48  
49      public OpenNapWhoisTableModel() 
50      {
51  		Column columns[] = new Column[] {
52  			new Column("user", XNap.tr("User"), String.class),
53  			new Column("server", XNap.tr("Server"), String.class),
54  			new Column("host", XNap.tr("Host"), String.class),
55  			new Column("fileCount", XNap.tr("File Count"), Integer.class,
56  					   new NumberCellRenderer()),
57  			new Column("linkSpeed", XNap.tr("Link Speed"), String.class),
58  			new Column("level", XNap.tr("Level"), String.class),
59  			new Column("status", XNap.tr("Status"), String.class),
60  			new Column("downloads", XNap.tr("Downloads"), Integer.class,
61  					   new NumberCellRenderer()),
62  			new Column("uploads", XNap.tr("Uploads"), Integer.class,
63  					   new NumberCellRenderer()),
64  			new Column("client", XNap.tr("Client"), String.class),
65  			new Column("lastSeen", XNap.tr("Last Seen"), Long.class, 
66  					   new TimeCellRenderer()),
67  			new Column("online", XNap.tr("Online"), Long.class, 
68  					   new TimeCellRenderer()),
69  		};
70  		addColumns(columns);
71  
72  		MessageHandler.subscribe(WhoisResponseMessage.TYPE, this);
73      }
74  
75      //--- Method(s) ---
76  
77      public void add(OpenNapUser u)
78      {
79  		for (int i = 0; i < rows.size(); i++) {
80  			OpenNapUser user = (OpenNapUser)rows.get(i);
81  			if (u.getName().equals(user.getName())
82  				&& u.getServer().equals(user.getServer())) {
83  				// user already exists in table
84  				fireTableRowsUpdated(i, i);
85  				return;
86  			}
87  		}
88  
89  		rows.add(u);
90  		fireTableRowsInserted(rows.size() - 1, rows.size() - 1);
91      }
92  
93      public void clear()
94      {
95  		int i = rows.size() - 1;
96  		rows.clear();
97  		fireTableRowsDeleted(0, i);
98      }
99  
100     public OpenNapUser get(int i)
101     {
102 		return (OpenNapUser)rows.get(mapToIndex(i));
103     }
104     
105     public Object get(int i, int j) 
106     {
107         OpenNapUser u = (OpenNapUser)rows.get(i);
108 
109         switch (j) {
110 		case 0:
111 			return u.getName();
112 		case 1:
113 			return u.getServer().getHost() + ":" + u.getServer().getPort();
114 		case 2:
115 			String host = u.getHost();
116 			return (host != null) ? host + ":" + u.getPort() : null;
117 		case 3:
118 			return new Integer(u.getFileCount());
119 		case 4:
120 			return OpenNapLinkType.getType(u.getLinkSpeed()).toString();
121 		case 5:
122 			return u.getLevel();
123 		case 6:
124 			return u.getStatus();
125 		case 7:
126 			return new Integer(u.getDownloadCount());
127 		case 8:
128 			return new Integer(u.getUploadCount());
129 		case 9:
130 			return u.getClientInfo();
131 		case 10:
132 			long l = u.getLastSeen();
133 			return new Long((l > 0) ? l : -1);
134 		case 11:
135 			return new Integer(u.getConnectDuration());
136 		}
137 
138 		return null;
139     }
140 
141     public int getRowCount() 
142     {
143         return rows.size();
144     }
145 
146     public void messageReceived(ServerMessage msg)
147     {
148 		final WhoisResponseMessage m = (WhoisResponseMessage)msg;
149 		Runnable runner = new Runnable() 
150 			{
151 				public void run()
152 				{
153 					add(m.getServer().getUser(m.nick));
154 				}
155 			};
156 		SwingUtilities.invokeLater(runner);
157     }
158 
159     public void reload()
160     {
161 		fireTableDataChanged();
162     }
163 
164     public void remove(OpenNapUser u)
165     {
166 		int i = rows.indexOf(u);
167 		if (i != -1) {
168 			rows.remove(u);
169 			fireTableRowsDeleted(i, i);
170 		}
171     }
172 
173 }
174 
175