1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui.table;
21
22 import java.util.Iterator;
23
24 import org.xnap.XNap;
25 import org.xnap.gui.event.SwingListListener;
26 import org.xnap.gui.event.TransferManagerListener;
27 import org.xnap.gui.util.SwingSynchronizedCache;
28 import org.xnap.gui.util.SwingSynchronizedTask;
29 import org.xnap.peer.Peer;
30 import org.xnap.transfer.AbstractTransferManager;
31 import org.xnap.transfer.*;
32 import org.xnap.transfer.TransferContainer;
33 import org.xnap.transfer.Upload;
34 import org.xnap.util.Progress;
35 import org.xnap.util.Scheduler;
36
37 /***
38 * The model for the upload and download table.
39 *
40 * @see xnap.gui.TransferPanel
41 */
42 public class TransferTableModel extends DefaultColumnTreeTableModel
43 implements SwingListListener {
44
45
46
47 /***
48 * The table update interval.
49 */
50 public static final int UPDATE_INTERVAL = 1 * 1000;
51
52
53
54 private TransferManagerListener listener;
55 private boolean printActiveBold;
56
57
58
59 public TransferTableModel
60 (AbstractTransferManager queue, boolean printActiveBold)
61 {
62 this.printActiveBold = printActiveBold;
63
64 Column columns[] = new Column[] {
65 new Column("icon", XNap.tr("Filename"), TreeTableModel.class),
66 new Column("filename", XNap.tr("Filename"), TransferWrapper.class),
67 new Column("queuePos", XNap.tr("Queued At"), Integer.class,
68 new NumberCellRenderer(1)),
69 new Column("filesize", XNap.tr("Size"), Long.class,
70 new FilesizeCellRenderer()),
71 new Column("peer", XNap.tr("Peer"), String.class,
72 new StringCellRenderer()),
73 new Column("client", XNap.tr("Client"), String.class),
74 new Column("status", XNap.tr("Status"), String.class,
75 new StringCellRenderer()),
76 new Column("segments", XNap.tr("Segments"), Segment[].class,
77 new SegmentCellRenderer()),
78 new Column("progress", XNap.tr("Progress"), Progress.class,
79 new ProgressCellRenderer()),
80 new Column("timeLeft", XNap.tr("Time Left"), Integer.class,
81 new TimeCellRenderer()),
82 new Column("transferred", XNap.tr("Transfered"), Long.class,
83 new FilesizeCellRenderer()),
84 };
85 addColumns(columns);
86
87 SwingSynchronizedCache cache = new SwingSynchronizedCache(this);
88 queue.addListListener(cache);
89
90 Scheduler.run(0, UPDATE_INTERVAL, new Updater());
91 }
92
93
94
95 public void add(Transfer t)
96 {
97 if (t instanceof TransferContainer) {
98 TransferContainerTreeTableNode node
99 = new TransferContainerTreeTableNode(this, t);
100 add(node);
101 node.addExistingChildren();
102 }
103 else {
104 add(new TransferTreeTableNode(this, t));
105 }
106 }
107
108 /***
109 * Invoked when transfers have been added to the queue.
110 */
111 public void itemsAdded(Object[] items)
112 {
113 for (int i = 0; i < items.length; i++) {
114 add((Transfer)items[i]);
115 }
116 }
117
118 /***
119 * Invoked when transfers have been removed from the queue.
120 */
121 public void itemsRemoved(Object[] items)
122 {
123 for (int i = 0; i < items.length; i++) {
124 remove((Transfer)items[i]);
125 }
126 }
127
128 public Object getValueAt(Object node, int column)
129 {
130 if (node instanceof TreeTableNode) {
131 Transfer t = (Transfer)((TreeTableNode)node).getData();
132 Peer p = t.getPeer();
133
134 switch (column) {
135 case 0:
136 return (node instanceof LeafTreeTableNode)
137 ? (t instanceof Upload) ? t.getIcon() : null : t.getIcon();
138 case 1:
139 return new TransferWrapper(t);
140
141
142
143 case 2:
144 return new Integer(t.getQueuePosition());
145 case 3:
146 return new Long(t.getFilesize());
147 case 4:
148 return (p != null) ? p.getName() : "";
149 case 5:
150 return (p != null) ? p.getClientInfo() : "";
151 case 6:
152 return t.getStatus();
153 case 7:
154 return t.getSegments();
155 case 8:
156 Progress pr = new Progress
157 (t.getFilesize(), t.getTotalBytesTransferred());
158 pr.setRate((t.isRunning())
159 ? t.getCurrentRate()
160 : t.getAverageRate());
161 return pr;
162 case 9:
163 return new Integer(t.getRemainingTime());
164 case 10:
165 return new Long(t.getTotalBytesTransferred());
166 }
167 }
168
169 return null;
170 }
171
172 public void remove(Transfer t)
173 {
174 int index = indexOfByData(t);
175 if (index != -1) {
176 remove(index);
177 }
178 }
179
180 public void setListener(TransferManagerListener listener)
181 {
182 this.listener = listener;
183 }
184
185 private class Updater extends SwingSynchronizedTask
186 {
187
188 public void runSynchronized()
189 {
190 long rate = 0;
191 int running = 0;
192 for (Iterator it = iterator(); it.hasNext();) {
193 TreeTableNode node = (TreeTableNode)it.next();
194 Transfer t = (Transfer)node.getData();
195
196
197 if (t.isRunning()) {
198 nodeChanged(node);
199
200 long transferRate = t.getCurrentRate();
201 if (transferRate > 0) {
202 rate += transferRate;
203 }
204 running++;
205 }
206
207
208 for (int i = 0; i < node.getChildCount(); i++) {
209 TreeTableNode child = (TreeTableNode)node.getChildAt(i);
210 t = (Transfer)child.getData();
211
212 if (t.isRunning()) {
213 childChanged(node, i, child);
214 }
215 }
216 }
217
218 if (listener != null) {
219 listener.statsUpdated(running, rate);
220 }
221 }
222
223 }
224
225 }