Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ListTableModel |
|
| 0.0;0 |
1 | package us.daveread.basicquery.util; | |
2 | ||
3 | import java.util.List; | |
4 | import java.util.ArrayList; | |
5 | import javax.swing.table.AbstractTableModel; | |
6 | ||
7 | /** | |
8 | * Ordered model for a table | |
9 | * | |
10 | * @author David Read | |
11 | * | |
12 | * @param <T> Data type housed in the model | |
13 | * | |
14 | */ | |
15 | public class ListTableModel<T> extends AbstractTableModel { | |
16 | /** | |
17 | * Serial UID | |
18 | */ | |
19 | private static final long serialVersionUID = 15744098355129546L; | |
20 | ||
21 | /** | |
22 | * Column names | |
23 | */ | |
24 | private List<String> columnNames; | |
25 | ||
26 | /** | |
27 | * Row data | |
28 | */ | |
29 | private List<List<T>> rowData; | |
30 | ||
31 | /** | |
32 | * Setup the instance with empty column and data lists | |
33 | */ | |
34 | 9 | public ListTableModel() { |
35 | 9 | rowData = new ArrayList<List<T>>(); |
36 | 9 | columnNames = new ArrayList<String>(); |
37 | 9 | } |
38 | ||
39 | /** | |
40 | * Set the list of column names for the model | |
41 | * | |
42 | * @param cols | |
43 | * The list of column names | |
44 | */ | |
45 | public void setColumnIdentifiers(List<String> cols) { | |
46 | 1 | columnNames = cols; |
47 | 1 | } |
48 | ||
49 | /** | |
50 | * Add a column name to the model | |
51 | * | |
52 | * @param colName | |
53 | * The column name | |
54 | */ | |
55 | public void addColumn(String colName) { | |
56 | 20 | columnNames.add(colName); |
57 | 20 | } |
58 | ||
59 | /** | |
60 | * Get the column name for the requested column | |
61 | * | |
62 | * @param col | |
63 | * The column number | |
64 | * | |
65 | * @return The column name | |
66 | */ | |
67 | public String getColumnName(int col) { | |
68 | 6 | return columnNames.get(col); |
69 | } | |
70 | ||
71 | /** | |
72 | * Add a data row to the model and notify the table that its model has changed | |
73 | * | |
74 | * @see #addRowFast(List) | |
75 | * @param row | |
76 | * The data row (a list of objects, one per columns) | |
77 | */ | |
78 | public void addRow(List<T> row) { | |
79 | 19 | rowData.add(row); |
80 | 19 | fireTableRowsInserted(rowData.size() - 1, rowData.size() - 1); |
81 | 19 | } |
82 | ||
83 | /** | |
84 | * Add a data row to the model witout notifying the table that the model has | |
85 | * changed | |
86 | * | |
87 | * @see #updateCompleted() | |
88 | * | |
89 | * @param row | |
90 | * The data row (a list of objects, one per columns | |
91 | */ | |
92 | public void addRowFast(List<T> row) { | |
93 | 10 | rowData.add(row); |
94 | 10 | } |
95 | ||
96 | /** | |
97 | * Notify the table that the model has changed. | |
98 | * | |
99 | * @see #addRowFast(List) | |
100 | */ | |
101 | public void updateCompleted() { | |
102 | 1 | fireTableRowsInserted(0, rowData.size() - 1); |
103 | 1 | } |
104 | ||
105 | /** | |
106 | * Get the number of columns in the model (based on number of column names) | |
107 | * | |
108 | * @return The number of columns in the model | |
109 | */ | |
110 | public int getColumnCount() { | |
111 | 3 | return columnNames.size(); |
112 | } | |
113 | ||
114 | /** | |
115 | * Get the number of data rows in the model | |
116 | * | |
117 | * @return The number of data rows in the model | |
118 | */ | |
119 | public int getRowCount() { | |
120 | 3 | return rowData.size(); |
121 | } | |
122 | ||
123 | /** | |
124 | * Get the value for a row and column | |
125 | * | |
126 | * @param row | |
127 | * The row number | |
128 | * @param col | |
129 | * The column number | |
130 | * | |
131 | * @return The object at that row and column location | |
132 | */ | |
133 | public T getValueAt(int row, int col) { | |
134 | 2 | return rowData.get(row).get(col); |
135 | } | |
136 | } |