-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDownloadTableModel.java
98 lines (80 loc) · 3.19 KB
/
DownloadTableModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
// This class manages the download table's data.
class DownloadsTableModel extends AbstractTableModel
implements Observer {
// These are the names for the table's columns.
private static final String[] columnNames = {"URL", "Size in MB", "Progress", "Speed in KB/s",
"Avg Speed in KB/s", "Elapsed Time", "Remaing Time" ,"Status"};
// These are the classes for each column's values.
private static final Class[] columnClasses = {String.class, String.class,
JProgressBar.class, String.class, String.class, String.class, String.class, String.class};
// The table's list of downloads.
private ArrayList<Download> downloadList = new ArrayList<Download>();
// Add a new download to the table.
public void addDownload(Download download) {
// Register to be notified when the download changes.
download.addObserver(this);
downloadList.add(download);
// Fire table row insertion notification to table.
fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
}
// Get a download for the specified row.
public Download getDownload(int row) {
return downloadList.get(row);
}
// Remove a download from the list.
public void clearDownload(int row) {
downloadList.remove(row);
// Fire table row deletion notification to table.
fireTableRowsDeleted(row, row);
}
// Get table's column count.
public int getColumnCount() {
return columnNames.length;
}
// Get a column's name.
public String getColumnName(int col) {
return columnNames[col];
}
// Get a column's class.
public Class getColumnClass(int col) {
return columnClasses[col];
}
// Get table's row count.
public int getRowCount() {
return downloadList.size();
}
// Get value for a specific row and column combination.
public Object getValueAt(int row, int col) {
Download download = downloadList.get(row);
switch (col) {
case 0: // URL
return download.getUrl();
case 1: // Size
long size = download.getSize();
return (size == -1) ? "" : Float.toString((float)size/1048576);
case 2: // Progress
return new Float(download.getProgress());
case 3: //Speed
return download.getSpeed();
case 4: //Avg Speed
return download.getAvgSpeed();
case 5: //Elapsed Time
return download.getElapsedTime();
case 6: //Remaining Time
return download.getRemainingTime();
case 7: // Status
return Download.STATUSES[download.getStatus()];
}
return "";
}
/* Update is called when a Download notifies its
observers of any changes */
public void update(Observable o, Object arg) {
int index = downloadList.indexOf(o);
// Fire table row update notification to table.
fireTableRowsUpdated(index, index);
}
}