|
1
|
|
/*
|
|
2
|
|
* Angkor Web Framework
|
|
3
|
|
*
|
|
4
|
|
* Distributable under LGPL license.
|
|
5
|
|
* See terms of license at gnu.org.
|
|
6
|
|
*/
|
|
7
|
|
|
|
8
|
|
package com.tirsen.angkor.beans;
|
|
9
|
|
|
|
10
|
|
import com.tirsen.angkor.table.AbstractTableModel;
|
|
11
|
|
import com.tirsen.angkor.widget.ValueModel;
|
|
12
|
|
|
|
13
|
|
import java.beans.BeanInfo;
|
|
14
|
|
import java.beans.IntrospectionException;
|
|
15
|
|
import java.beans.Introspector;
|
|
16
|
|
import java.beans.PropertyDescriptor;
|
|
17
|
|
import java.io.Serializable;
|
|
18
|
|
import java.util.ArrayList;
|
|
19
|
|
import java.util.Arrays;
|
|
20
|
|
import java.util.Collection;
|
|
21
|
|
import java.util.Iterator;
|
|
22
|
|
import java.util.List;
|
|
23
|
|
|
|
24
|
|
public class JavaBeanTableModel extends AbstractTableModel
|
|
25
|
|
{
|
|
26
|
|
private Class rowClass;
|
|
27
|
|
private List rows;
|
|
28
|
|
private List columns;
|
|
29
|
|
|
|
30
|
0
|
public JavaBeanTableModel()
|
|
31
|
|
{
|
|
32
|
|
}
|
|
33
|
|
|
|
34
|
0
|
public JavaBeanTableModel(Class rowClass)
|
|
35
|
|
{
|
|
36
|
0
|
this.rowClass = rowClass;
|
|
37
|
|
}
|
|
38
|
|
|
|
39
|
|
public interface TableColumnModel extends Serializable
|
|
40
|
|
{
|
|
41
|
|
String getName();
|
|
42
|
|
|
|
43
|
|
ValueModel getValue(Object row);
|
|
44
|
|
}
|
|
45
|
|
|
|
46
|
|
private static class PropertyTableColumnModel implements TableColumnModel
|
|
47
|
|
{
|
|
48
|
|
private String name;
|
|
49
|
|
private transient PropertyDescriptor property;
|
|
50
|
|
|
|
51
|
0
|
public PropertyTableColumnModel(PropertyDescriptor property)
|
|
52
|
|
{
|
|
53
|
0
|
this.property = property;
|
|
54
|
|
}
|
|
55
|
|
|
|
56
|
0
|
public PropertyTableColumnModel(String name, PropertyDescriptor property)
|
|
57
|
|
{
|
|
58
|
0
|
this.name = name;
|
|
59
|
0
|
this.property = property;
|
|
60
|
|
}
|
|
61
|
|
|
|
62
|
0
|
public String getName()
|
|
63
|
|
{
|
|
64
|
0
|
return name == null ? property.getDisplayName() : name;
|
|
65
|
|
}
|
|
66
|
|
|
|
67
|
0
|
public ValueModel getValue(Object row)
|
|
68
|
|
{
|
|
69
|
0
|
return new PropertyValueModel(row, property);
|
|
70
|
|
}
|
|
71
|
|
}
|
|
72
|
|
|
|
73
|
|
/**
|
|
74
|
|
* Resets the columns.
|
|
75
|
|
*/
|
|
76
|
0
|
public void resetColumns()
|
|
77
|
|
{
|
|
78
|
0
|
columns = null;
|
|
79
|
|
}
|
|
80
|
|
|
|
81
|
|
/**
|
|
82
|
|
* If the columns are not already created creates columns based on all the properties in the beans.
|
|
83
|
|
*/
|
|
84
|
0
|
private void createDefaultColumns()
|
|
85
|
|
{
|
|
86
|
0
|
if (columns == null)
|
|
87
|
|
{
|
|
88
|
0
|
try
|
|
89
|
|
{
|
|
90
|
0
|
PropertyDescriptor[] properties = getProperties();
|
|
91
|
0
|
columns = new ArrayList(properties.length);
|
|
92
|
0
|
for (int i = 0; i < properties.length; i++)
|
|
93
|
|
{
|
|
94
|
0
|
PropertyDescriptor property = properties[i];
|
|
95
|
0
|
addColumn(new PropertyTableColumnModel(property));
|
|
96
|
|
}
|
|
97
|
|
}
|
|
98
|
|
catch (IntrospectionException e)
|
|
99
|
|
{
|
|
100
|
0
|
throw new RuntimeException(e.getMessage());
|
|
101
|
|
}
|
|
102
|
|
}
|
|
103
|
|
}
|
|
104
|
|
|
|
105
|
0
|
private PropertyDescriptor[] getProperties() throws IntrospectionException
|
|
106
|
|
{
|
|
107
|
0
|
Class beanClass = getRowClass();
|
|
108
|
0
|
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);
|
|
109
|
0
|
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
|
|
110
|
0
|
return properties;
|
|
111
|
|
}
|
|
112
|
|
|
|
113
|
0
|
private Class getRowClass()
|
|
114
|
|
{
|
|
115
|
0
|
Class cls = rowClass;
|
|
116
|
0
|
if (cls == null && getRowCount() > 0) cls = getRow(0).getClass();
|
|
117
|
0
|
if (cls == null) cls = getRows().getClass().getComponentType();
|
|
118
|
0
|
if (cls == null) throw new IllegalStateException("Could not determine class of objects used for rows.");
|
|
119
|
0
|
return cls;
|
|
120
|
|
}
|
|
121
|
|
|
|
122
|
0
|
public void addColumn(TableColumnModel column)
|
|
123
|
|
{
|
|
124
|
0
|
columns.add(column);
|
|
125
|
|
}
|
|
126
|
|
|
|
127
|
0
|
public PropertyDescriptor findProperty(String property)
|
|
128
|
|
{
|
|
129
|
0
|
try
|
|
130
|
|
{
|
|
131
|
0
|
PropertyDescriptor[] properties = getProperties();
|
|
132
|
0
|
for (int i = 0; i < properties.length; i++)
|
|
133
|
|
{
|
|
134
|
0
|
PropertyDescriptor propertyDesc = properties[i];
|
|
135
|
0
|
if (propertyDesc.getName().equals(property)) return propertyDesc;
|
|
136
|
|
}
|
|
137
|
0
|
throw new IntrospectionException("No such property " + property);
|
|
138
|
|
}
|
|
139
|
|
catch (IntrospectionException e)
|
|
140
|
|
{
|
|
141
|
0
|
throw new RuntimeException(e.getMessage());
|
|
142
|
|
}
|
|
143
|
|
}
|
|
144
|
|
|
|
145
|
0
|
public void addColumn(String name, String property)
|
|
146
|
|
{
|
|
147
|
0
|
if (columns == null) columns = new ArrayList(1);
|
|
148
|
0
|
columns.add(new PropertyTableColumnModel(name, findProperty(property)));
|
|
149
|
|
}
|
|
150
|
|
|
|
151
|
0
|
public String getColumnName(int column)
|
|
152
|
|
{
|
|
153
|
0
|
return getColumn(column).getName();
|
|
154
|
|
}
|
|
155
|
|
|
|
156
|
0
|
public int getColumnCount()
|
|
157
|
|
{
|
|
158
|
0
|
createDefaultColumns();
|
|
159
|
0
|
return columns.size();
|
|
160
|
|
}
|
|
161
|
|
|
|
162
|
0
|
public ValueModel getValueAt(int row, int column)
|
|
163
|
|
{
|
|
164
|
0
|
return (getColumn(column)).getValue(getRow(row));
|
|
165
|
|
}
|
|
166
|
|
|
|
167
|
0
|
private TableColumnModel getColumn(int column)
|
|
168
|
|
{
|
|
169
|
0
|
createDefaultColumns();
|
|
170
|
0
|
return (TableColumnModel) columns.get(column);
|
|
171
|
|
}
|
|
172
|
|
|
|
173
|
|
/**
|
|
174
|
|
* Gets the value object for a specified row, also checks wheather row is within range specified by
|
|
175
|
|
* {@link #setRange(int,int)}.
|
|
176
|
|
* @throws IllegalArgumentException if <code>row</code> parameter is outside specified range.
|
|
177
|
|
*/
|
|
178
|
0
|
public Object getRow(int row)
|
|
179
|
|
{
|
|
180
|
0
|
if (row < getStart() || row > getEnd())
|
|
181
|
|
{
|
|
182
|
0
|
throw new IllegalArgumentException("Tried to access row " + row +
|
|
183
|
|
" outside range (" + getStart() + ", " + getEnd() + ").");
|
|
184
|
|
}
|
|
185
|
0
|
return getRows().get(row);
|
|
186
|
|
}
|
|
187
|
|
|
|
188
|
0
|
private List getRows()
|
|
189
|
|
{
|
|
190
|
0
|
if (rows == null) throw new IllegalStateException("Tried to access model before initializing rows.");
|
|
191
|
0
|
return rows;
|
|
192
|
|
}
|
|
193
|
|
|
|
194
|
0
|
public int getRowCount()
|
|
195
|
|
{
|
|
196
|
0
|
int rowCount = super.getRowCount();
|
|
197
|
0
|
if (rowCount == -1)
|
|
198
|
|
{
|
|
199
|
0
|
if (rows == null)
|
|
200
|
0
|
rowCount = 0;
|
|
201
|
|
else
|
|
202
|
0
|
rowCount = getRows().size();
|
|
203
|
|
}
|
|
204
|
0
|
return rowCount;
|
|
205
|
|
}
|
|
206
|
|
|
|
207
|
0
|
public void setRow(int row, Object object)
|
|
208
|
|
{
|
|
209
|
0
|
ensureCapable(row);
|
|
210
|
0
|
rows.set(row, object);
|
|
211
|
|
}
|
|
212
|
|
|
|
213
|
|
/**
|
|
214
|
|
* Sets a segment of rows starting at specified startRow.
|
|
215
|
|
*/
|
|
216
|
0
|
public void setRows(int startRow, Collection objects)
|
|
217
|
|
{
|
|
218
|
0
|
ensureCapable(startRow + objects.size());
|
|
219
|
0
|
int row = startRow;
|
|
220
|
0
|
for (Iterator iterator = objects.iterator(); iterator.hasNext();)
|
|
221
|
|
{
|
|
222
|
0
|
Object o = (Object) iterator.next();
|
|
223
|
0
|
rows.set(row++, o);
|
|
224
|
|
}
|
|
225
|
|
}
|
|
226
|
|
|
|
227
|
0
|
private void ensureCapable(int row)
|
|
228
|
|
{
|
|
229
|
0
|
if (rows == null) rows = new ArrayList(row);
|
|
230
|
0
|
int size = rows.size();
|
|
231
|
0
|
for (int i = size - row; i < size + 1; i++)
|
|
232
|
|
{
|
|
233
|
0
|
rows.add(null);
|
|
234
|
|
}
|
|
235
|
|
}
|
|
236
|
|
|
|
237
|
0
|
public void setRows(Object[] rows)
|
|
238
|
|
{
|
|
239
|
0
|
if (rows != null)
|
|
240
|
0
|
this.rows = Arrays.asList(rows);
|
|
241
|
|
else
|
|
242
|
0
|
this.rows = null;
|
|
243
|
|
}
|
|
244
|
|
|
|
245
|
0
|
public void setRows(List rows)
|
|
246
|
|
{
|
|
247
|
0
|
this.rows = new ArrayList(rows);
|
|
248
|
|
}
|
|
249
|
|
|
|
250
|
0
|
public void setRows(Iterator iterator)
|
|
251
|
|
{
|
|
252
|
0
|
rows = new ArrayList();
|
|
253
|
0
|
while (iterator.hasNext())
|
|
254
|
|
{
|
|
255
|
0
|
Object o = (Object) iterator.next();
|
|
256
|
0
|
rows.add(o);
|
|
257
|
|
}
|
|
258
|
|
}
|
|
259
|
|
|
|
260
|
0
|
public void empty()
|
|
261
|
|
{
|
|
262
|
0
|
super.empty();
|
|
263
|
0
|
rows = null;
|
|
264
|
|
}
|
|
265
|
|
}
|
|
266
|
|
|