1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.plugin.limewire;
21
22 import java.text.NumberFormat;
23 import java.util.ArrayList;
24
25 import javax.swing.SwingUtilities;
26
27 import org.xnap.XNap;
28 import org.xnap.gui.table.AbstractColumnTableModel;
29 import org.xnap.gui.table.Column;
30 import org.xnap.gui.table.NumberCellRenderer;
31 import org.xnap.gui.table.TimeCellRenderer;
32 import org.xnap.gui.util.SwingSynchronizedTask;
33 import org.xnap.util.Scheduler;
34 import org.xnap.util.XNapTask;
35
36 import com.limegroup.gnutella.ManagedConnection;
37
38 /***
39 * Table model for {@link ManagedConnection} objects.
40 *
41 * FIX: the connections that have been initialized before the gui
42 * was visible are not displayed in the table
43 */
44 public class LimeWireConnectionTableModel extends AbstractColumnTableModel
45 {
46
47
48
49 public static int UPDATE_INTERVAL = 1 * 1000;
50
51
52
53 private ArrayList data = new ArrayList();
54 private XNapTask task;
55
56
57
58 public LimeWireConnectionTableModel()
59 {
60 Column columns[] = new Column[] {
61 new Column("host", XNap.tr("Host"), String.class),
62 new Column("status", XNap.tr("Status"), String.class),
63 new Column("msgCount", XNap.tr("Messages (I/O)"), String.class),
64 new Column("dropped", XNap.tr("Dropped (I/O)"), String.class),
65 new Column("bandwidth", XNap.tr("Bandwidth (I/O)"), String.class),
66 new Column("connectionType", XNap.tr("Connection Type"), String.class),
67 new Column("hosts", XNap.tr("Hosts"), Long.class, new NumberCellRenderer()),
68 new Column("protocol", XNap.tr("Protocol"), String.class),
69 new Column("vendor", XNap.tr("Vendor"), String.class),
70 new Column("uptime", XNap.tr("Uptime"), Integer.class,
71 new TimeCellRenderer()),
72 new Column("compressed", XNap.tr("Compressed (I/O)"),
73 String.class),
74 };
75 addColumns(columns);
76 }
77
78
79
80 public void add(final ManagedConnection c)
81 {
82 Runnable runner = new Runnable()
83 {
84 public void run()
85 {
86 data.add(c);
87 fireTableRowsInserted(data.size() - 1, data.size() - 1);
88 }
89 };
90 SwingUtilities.invokeLater(runner);
91 }
92
93 /***
94 * @param c
95 */
96 public void update(ManagedConnection c)
97 {
98 }
99
100 /***
101 * @param c
102 */
103 public void remove(final ManagedConnection c)
104 {
105 Runnable runner = new Runnable()
106 {
107 public void run()
108 {
109 int i = data.indexOf(c);
110 if (i != -1) {
111 data.remove(i);
112 fireTableRowsDeleted(i, i);
113 }
114 }
115 };
116 SwingUtilities.invokeLater(runner);
117 }
118
119 public int getRowCount()
120 {
121 return data.size();
122 }
123
124 public Object get(int i, int j)
125 {
126 ManagedConnection c = (ManagedConnection)data.get(i);
127 StringBuffer sb = new StringBuffer();
128
129 switch (j) {
130 case 0:
131 return (c.isInitialized())
132 ? c.getAddress() + ":" + c.getSocket().getPort()
133 : c.getAddress();
134 case 1:
135 return c.isOutgoing() ? XNap.tr("Outgoing") : XNap.tr("Incoming");
136 case 2:
137 sb.append(c.getNumMessagesReceived());
138 sb.append(" / ");
139 sb.append(c.getNumMessagesSent());
140 return sb.toString();
141 case 3:
142 sb.append(c.getNumReceivedMessagesDropped());
143 sb.append(" / ");
144 sb.append(c.getNumSentMessagesDropped());
145 return sb.toString();
146 case 4:
147 NumberFormat n = NumberFormat.getNumberInstance();
148 n.setMinimumFractionDigits(3);
149 n.setMaximumFractionDigits(3);
150 sb.append(n.format(c.getMeasuredDownstreamBandwidth()));
151 sb.append(" / ");
152 sb.append(n.format(c.getMeasuredUpstreamBandwidth()));
153 sb.append(" KB/s");
154 return sb.toString();
155 case 5:
156 return (c.isClientSupernodeConnection()
157 || c.isSupernodeSupernodeConnection())
158 ? XNap.tr("Supernode")
159 : c.isSupernodeClientConnection()
160 ? XNap.tr("Leaf")
161 : XNap.tr("Servant");
162 case 6:
163 return (c.isClientSupernodeConnection()
164 || c.isSupernodeSupernodeConnection())
165 ? new Long(c.getNumIntraUltrapeerConnections())
166 : new Long(-1);
167 case 7:
168 return c.getVersion();
169 case 8:
170 return c.getUserAgent();
171 case 9:
172 int length = (int)((System.currentTimeMillis()
173 - c.getConnectionTime()) / 1000);
174 return new Integer(length);
175 default:
176 return null;
177 }
178 }
179
180 public String getIO(long in, long out)
181 {
182 StringBuffer sb = new StringBuffer();
183 sb.append(in);
184 sb.append(" / ");
185 sb.append(out);
186 return sb.toString();
187 }
188
189 public void setConnected(boolean connected)
190 {
191 if (task != null) {
192 task.cancel();
193 task = null;
194 }
195
196 if (connected) {
197 task = new Updater();
198 Scheduler.run(0, UPDATE_INTERVAL, task);
199 }
200 }
201
202
203
204 private class Updater extends SwingSynchronizedTask
205 {
206
207 public void runSynchronized()
208 {
209 fireTableRowsUpdated(0, data.size() - 1);
210 }
211
212 }
213
214 }