-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathListTableModel.java
More file actions
302 lines (263 loc) · 8.13 KB
/
ListTableModel.java
File metadata and controls
302 lines (263 loc) · 8.13 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import java.util.*;
import java.sql.*;
public class ListTableModel extends RowTableModel<List>
{
/**
* Initialize the List with null values. This will set the size of the
* List and will prevent an IndexOutOfBoundsException when trying to
* access an Element in the List.
*
* @param size the number of Elements to add to the List
*
*/
protected static List<String> newList(int size)
{
ArrayList<String> list = new ArrayList<String>(size);
for (int i = 0; i < size; i++)
{
list.add( null );
}
return list;
}
/**
* Constructs an empty <code>ListTableModel</code> with default
* column names for the specified number of <code>columns</code>.
*
* @param columns the number of columns the table holds
*/
public ListTableModel(int columns)
{
super( newList(columns) );
setRowClass( List.class );
}
/**
* Constructs an empty <code>ListTableModel</code> with customized
* column names. The number of columns is determined bye the
* number of items in the <code>columnNames</code> List.
*
* @param columnNames <code>List</code> containing the names
* of the new columns
*/
public ListTableModel(List<String> columnNames)
{
super(columnNames);
setRowClass( List.class );
}
/**
* Constructs a <code>ListTableModel</code> with the specified number
* of rows. Default column names will be used for the specified number
* of <code>columns</code>.
*
* @param rows the number of initially empty rows to create
* @param columns the number of columns the table holds
*/
public ListTableModel(int rows, int columns)
{
super( newList(columns) );
setRowClass( List.class );
List<List> data = new ArrayList<List>(rows);
for (int i = 0; i < rows; i++)
data.add( new ArrayList(columns) );
insertRows(0, data);
}
/**
* Constructs a <code>ListTableModel</code> with initial data and
* customized column names.
*
* Each item in the <code>modelData</code> List must also be a List Object
* containing items for each column of the row.
*
* Each column's name will be taken from the <code>columnNames</code>
* List and the number of colums is determined by thenumber of items
* in the <code>columnNames</code> List.
*
* @param modelData the data of the table
* @param columnNames <code>List</code> containing the names
* of the new columns
*/
public ListTableModel(List<List> modelData, List<String> columnNames)
{
super(modelData, columnNames);
setRowClass( List.class );
}
//
// Implement the TableModel interface
//
/**
* Returns an attribute value for the cell at <code>row</code>
* and <code>column</code>.
*
* @param row the row whose value is to be queried
* @param column the column whose value is to be queried
* @return the value Object at the specified cell
* @exception IndexOutOfBoundsException
* if an invalid row or column was given
*/
public Object getValueAt(int row, int column)
{
List rowData = getRow( row );
return rowData.get( column );
}
/**
* Sets the object value for the cell at <code>column</code> and
* <code>row</code>. <code>value</code> is the new value. This method
* will generate a <code>tableChanged</code> notification.
*
* @param value the new value; this can be null
* @param row the row whose value is to be changed
* @param column the column whose value is to be changed
* @exception IndexOutOfBoundsException if an invalid row or
* column was given
*/
@SuppressWarnings("unchecked")
public void setValueAt(Object value, int row, int column)
{
List rowData = getRow( row );
rowData.set(column, value);
fireTableCellUpdated(row, column);
}
/**
* Insert a row of data at the <code>row</code> location in the model.
* Notification of the row being added will be generated.
*
* @param row row in the model where the data will be inserted
* @param rowData data of the row being added
*/
@Override
public void insertRow(int row, List rowData)
{
justifyRow( rowData );
super.insertRow(row, rowData);
}
/**
* Insert multiple rows of data at the <code>row</code> location in the model.
* Notification of the row being added will be generated.
*
* @param row row in the model where the data will be inserted
* @param rowList each item in the list is a separate row of data
*/
@Override
public void insertRows(int row, List<List> rowList)
{
for (List rowData: rowList)
{
justifyRow( rowData );
}
super.insertRows(row, rowList);
}
/*
* Make sure each List row contains the required number of columns.
*/
@SuppressWarnings("unchecked")
private void justifyRow(List rowData)
{
for (int i = rowData.size(); i < getColumnCount(); i++)
{
rowData.add( null );
}
}
/**
* Adds a row of data to the end of the model.
* Notification of the row being added will be generated.
*
* @param rowData data of the row being added
*/
public void addRow(Object[] rowData)
{
insertRow(getRowCount(), rowData);
}
/**
* Insert a row of data at the <code>row</code> location in the model.
* Notification of the row being added will be generated.
*
* @param row row in the model where the data will be inserted
* @param rowData data of the row being added
*/
public void insertRow(int row, Object[] rowData)
{
insertRow(row, copyToList(rowData));
}
/**
* Insert multiple rows of data at the <code>row</code> location in the model.
* Notification of the row being added will be generated.
*
* @param row row in the model where the data will be inserted
* @param rowArray each item in the Array is a separate row of data
*/
public void insertRows(int row, Object[][] rowArray)
{
List<List> data = new ArrayList<List>(rowArray.length);
for (int i = 0; i < rowArray.length; i++)
{
data.add( copyToList(rowArray[i]) );
}
insertRows(row, data);
}
/*
* Copy the information in the Array to a List so a List can be added
* to the model
*/
private List copyToList(Object[] rowData)
{
List<Object> row = new ArrayList<Object>(rowData.length);
for (int i = 0; i < rowData.length; i++)
{
row.add( rowData[i] );
}
return row;
}
/**
* Create a ListTableModel given a specific ResultSet.
*
* The column names and class type will be retrieved from the
* ResultSetMetaData. The data is retrieved from each row found in the
* ResultSet. The class of
*
* @param resultSet ResultSet containing results of a database query
* @return a newly created ListTableModel
* @exception SQLException when an SQL error is encountered
*/
public static ListTableModel createModelFromResultSet(ResultSet resultSet)
throws SQLException
{
ResultSetMetaData metaData = resultSet.getMetaData();
int columns = metaData.getColumnCount();
// Create empty model using the column names
ArrayList<String> columnNames = new ArrayList<String>();
for (int i = 1; i <= columns; i++)
{
String columnName = metaData.getColumnName(i);
String columnLabel = metaData.getColumnLabel(i);
if (columnLabel.equals(columnName))
columnNames.add( formatColumnName(columnName) );
else
columnNames.add( columnLabel );
}
ListTableModel model = new ListTableModel( columnNames );
model.setModelEditable( false );
// Assign the class of each column
for (int i = 1; i <= columns; i++)
{
try
{
String className = metaData.getColumnClassName( i );
model.setColumnClass(i - 1, Class.forName(className));
}
catch ( Exception exception ) {}
}
// Get row data
ArrayList<List> data = new ArrayList<List>();
while (resultSet.next())
{
ArrayList<Object>row = new ArrayList<Object>(columns);
for (int i = 1; i <= columns; i++)
{
Object o = resultSet.getObject(i);
row.add( o );
}
data.add( row );
}
model.insertRows(0, data);
return model;
}
}