1 | // Copyright 2004-2007 Jean-Francois Poilpret |
2 | // |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | // you may not use this file except in compliance with the License. |
5 | // You may obtain a copy of the License at |
6 | // |
7 | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | // |
9 | // Unless required by applicable law or agreed to in writing, software |
10 | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | // See the License for the specific language governing permissions and |
13 | // limitations under the License. |
14 | |
15 | package net.sourceforge.hivegui.table; |
16 | |
17 | import org.apache.hivemind.util.PropertyUtils; |
18 | |
19 | /** |
20 | * @author Jean-Francois Poilpret |
21 | */ |
22 | public class BeanDataModel<T> implements DataModel<T> |
23 | { |
24 | public BeanDataModel(ColumnContribution[] columns) |
25 | { |
26 | _columns = columns; |
27 | } |
28 | |
29 | public int getColumnCount() |
30 | { |
31 | return _columns.length; |
32 | } |
33 | |
34 | public String getColumnName(int column) |
35 | { |
36 | return _columns[column].getProperty(); |
37 | } |
38 | |
39 | public Class getColumnClass(int column) |
40 | { |
41 | return _columns[column].getType(); |
42 | } |
43 | |
44 | public Object getColumnValue(Object item, int column) |
45 | { |
46 | return PropertyUtils.read(item, _columns[column].getProperty()); |
47 | } |
48 | |
49 | public void setColumnValue(Object item, int column, Object value) |
50 | { |
51 | PropertyUtils.write(item, _columns[column].getProperty(), value); |
52 | } |
53 | |
54 | // Can be overridden if more complex behavior is needed |
55 | public boolean isColumnEditable(Object item, int column) |
56 | { |
57 | return _columns[column].isEditable(); |
58 | } |
59 | |
60 | final private ColumnContribution[] _columns; |
61 | } |