|
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.List;
|
|
20
|
|
|
|
21
|
|
public abstract class BeanTableModel extends AbstractTableModel
|
|
22
|
|
{
|
|
23
|
|
private Class rowClass;
|
|
24
|
|
private List columns;
|
|
25
|
|
|
|
26
|
6
|
public BeanTableModel()
|
|
27
|
|
{
|
|
28
|
|
}
|
|
29
|
|
|
|
30
|
0
|
public BeanTableModel(Class rowClass)
|
|
31
|
|
{
|
|
32
|
0
|
this.rowClass = rowClass;
|
|
33
|
|
}
|
|
34
|
|
|
|
35
|
|
public interface TableColumnModel extends Serializable
|
|
36
|
|
{
|
|
37
|
|
String getName();
|
|
38
|
|
|
|
39
|
|
ValueModel getValue(Object row);
|
|
40
|
|
}
|
|
41
|
|
|
|
42
|
|
private static class PropertyTableColumnModel implements TableColumnModel
|
|
43
|
|
{
|
|
44
|
|
private String name;
|
|
45
|
|
private PropertyDescriptor property;
|
|
46
|
|
|
|
47
|
0
|
public PropertyTableColumnModel(PropertyDescriptor property)
|
|
48
|
|
{
|
|
49
|
0
|
this.property = property;
|
|
50
|
|
}
|
|
51
|
|
|
|
52
|
11
|
public PropertyTableColumnModel(String name, PropertyDescriptor property)
|
|
53
|
|
{
|
|
54
|
11
|
this.name = name;
|
|
55
|
11
|
this.property = property;
|
|
56
|
|
}
|
|
57
|
|
|
|
58
|
0
|
public String getName()
|
|
59
|
|
{
|
|
60
|
0
|
return name == null ? property.getDisplayName() : name;
|
|
61
|
|
}
|
|
62
|
|
|
|
63
|
16
|
public ValueModel getValue(Object row)
|
|
64
|
|
{
|
|
65
|
16
|
return new PropertyValueModel(row, property);
|
|
66
|
|
}
|
|
67
|
|
}
|
|
68
|
|
|
|
69
|
|
/**
|
|
70
|
|
* Resets the columns.
|
|
71
|
|
*/
|
|
72
|
0
|
public void resetColumns()
|
|
73
|
|
{
|
|
74
|
0
|
columns = null;
|
|
75
|
|
}
|
|
76
|
|
|
|
77
|
|
/**
|
|
78
|
|
* If the columns are not already created creates columns based on all the properties in the beans.
|
|
79
|
|
*/
|
|
80
|
16
|
private void createDefaultColumns()
|
|
81
|
|
{
|
|
82
|
16
|
if (columns == null)
|
|
83
|
|
{
|
|
84
|
0
|
try
|
|
85
|
|
{
|
|
86
|
0
|
PropertyDescriptor[] properties = getProperties();
|
|
87
|
0
|
columns = new ArrayList(properties.length);
|
|
88
|
0
|
for (int i = 0; i < properties.length; i++)
|
|
89
|
|
{
|
|
90
|
0
|
PropertyDescriptor property = properties[i];
|
|
91
|
0
|
addColumn(new PropertyTableColumnModel(property));
|
|
92
|
|
}
|
|
93
|
|
}
|
|
94
|
|
catch (IntrospectionException e)
|
|
95
|
|
{
|
|
96
|
0
|
throw new RuntimeException(e.getMessage());
|
|
97
|
|
}
|
|
98
|
|
}
|
|
99
|
|
}
|
|
100
|
|
|
|
101
|
11
|
private PropertyDescriptor[] getProperties() throws IntrospectionException
|
|
102
|
|
{
|
|
103
|
11
|
Class beanClass = getRowClass();
|
|
104
|
11
|
BeanInfo beanInfo = Introspector.getBeanInfo(beanClass, Object.class);
|
|
105
|
11
|
PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
|
|
106
|
11
|
return properties;
|
|
107
|
|
}
|
|
108
|
|
|
|
109
|
0
|
protected Class getRowClass()
|
|
110
|
|
{
|
|
111
|
0
|
Class cls = rowClass;
|
|
112
|
0
|
if (cls == null && getRowCount() > 0) cls = getRow(0).getClass();
|
|
113
|
0
|
if (cls == null) cls = getRows().getClass().getComponentType();
|
|
114
|
0
|
if (cls == null) throw new IllegalStateException("Could not determine class of objects used for rows.");
|
|
115
|
0
|
return cls;
|
|
116
|
|
}
|
|
117
|
|
|
|
118
|
0
|
public void addColumn(TableColumnModel column)
|
|
119
|
|
{
|
|
120
|
0
|
columns.add(column);
|
|
121
|
|
}
|
|
122
|
|
|
|
123
|
11
|
public PropertyDescriptor findProperty(String property)
|
|
124
|
|
{
|
|
125
|
11
|
try
|
|
126
|
|
{
|
|
127
|
11
|
PropertyDescriptor[] properties = getProperties();
|
|
128
|
82
|
for (int i = 0; i < properties.length; i++)
|
|
129
|
|
{
|
|
130
|
82
|
PropertyDescriptor propertyDesc = properties[i];
|
|
131
|
11
|
if (propertyDesc.getName().equals(property)) return propertyDesc;
|
|
132
|
|
}
|
|
133
|
0
|
throw new IntrospectionException("No such property " + property);
|
|
134
|
|
}
|
|
135
|
|
catch (IntrospectionException e)
|
|
136
|
|
{
|
|
137
|
0
|
throw new RuntimeException(e.getMessage());
|
|
138
|
|
}
|
|
139
|
|
}
|
|
140
|
|
|
|
141
|
11
|
public void addColumn(String name, String property)
|
|
142
|
|
{
|
|
143
|
6
|
if (columns == null) columns = new ArrayList(1);
|
|
144
|
11
|
columns.add(new PropertyTableColumnModel(name, findProperty(property)));
|
|
145
|
|
}
|
|
146
|
|
|
|
147
|
0
|
public String getColumnName(int column)
|
|
148
|
|
{
|
|
149
|
0
|
return getColumn(column).getName();
|
|
150
|
|
}
|
|
151
|
|
|
|
152
|
0
|
public int getColumnCount()
|
|
153
|
|
{
|
|
154
|
0
|
createDefaultColumns();
|
|
155
|
0
|
return columns.size();
|
|
156
|
|
}
|
|
157
|
|
|
|
158
|
16
|
public ValueModel getValueAt(int row, int column)
|
|
159
|
|
{
|
|
160
|
16
|
return (getColumn(column)).getValue(getRow(row));
|
|
161
|
|
}
|
|
162
|
|
|
|
163
|
16
|
private TableColumnModel getColumn(int column)
|
|
164
|
|
{
|
|
165
|
16
|
createDefaultColumns();
|
|
166
|
16
|
return (TableColumnModel) columns.get(column);
|
|
167
|
|
}
|
|
168
|
|
|
|
169
|
|
/**
|
|
170
|
|
* Gets the value object for a specified row, also checks wheather row is within range specified by
|
|
171
|
|
* {@link #setRange(int,int)}.
|
|
172
|
|
* @throws IllegalArgumentException if <code>row</code> parameter is outside specified range.
|
|
173
|
|
*/
|
|
174
|
16
|
public Object getRow(int row)
|
|
175
|
|
{
|
|
176
|
16
|
if (row < getStart() || row > getEnd())
|
|
177
|
|
{
|
|
178
|
0
|
throw new IllegalArgumentException("Tried to access row " + row +
|
|
179
|
|
" outside range (" + getStart() + ", " + getEnd() + ").");
|
|
180
|
|
}
|
|
181
|
16
|
return getRows().get(row);
|
|
182
|
|
}
|
|
183
|
|
|
|
184
|
|
protected abstract List getRows();
|
|
185
|
|
}
|
|
186
|
|
|