-
Notifications
You must be signed in to change notification settings - Fork 0
/
DownloadsTableModel.java
72 lines (60 loc) · 2 KB
/
DownloadsTableModel.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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package downloadmanager;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JProgressBar;
import javax.swing.table.AbstractTableModel;
/**
*
* @author Vijay
*/
public class DownloadsTableModel extends AbstractTableModel implements Observer{
private static final String[] columnNames={"URL","Size","Progress","Status"};
private static final Class[] columnClasses={String.class,String.class,JProgressBar.class,String.class};
private ArrayList<Download> downloadList=new ArrayList<Download>();
@Override
public int getRowCount() {
return downloadList.size();
}
@Override
public int getColumnCount() {
return columnNames.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Download dld=downloadList.get(rowIndex);
switch(columnIndex){
case 0:return dld.getUrl();//URL
case 1: return (dld.getSize()==-1)?"":Integer.toString(dld.getSize());
case 2: return dld.getProgress();
case 3: return Download.STATUSES[dld.getStatus()];
}
return "";
}
@Override
public void update(Observable o, Object arg) {
int index=downloadList.indexOf(o);
fireTableRowsUpdated(index, index);
}
public void addDownload(Download download){
download.addObserver(this);
downloadList.add(download);
fireTableRowsInserted(getRowCount()-1,getRowCount()-1);
}
public Download getDownload(int row){
return downloadList.get(row);
}
public void clearDownload(int row){
downloadList.remove(row);
fireTableRowsDeleted(row,row);
}
@Override
public String getColumnName(int col){
return columnNames[col];
}
}