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 javax.swing.table.AbstractTableModel; |
18 | |
19 | /** |
20 | * @author Jean-Francois Poilpret |
21 | */ |
22 | public class BeanTableModel<T> |
23 | extends AbstractTableModel |
24 | implements DataListModelListener |
25 | { |
26 | public BeanTableModel(DataListModel<T> listModel, DataModel<T> dataModel) |
27 | { |
28 | setModel(listModel); |
29 | _dataModel = dataModel; |
30 | } |
31 | |
32 | public DataListModel<T> getModel() |
33 | { |
34 | return _listModel; |
35 | } |
36 | |
37 | public void setModel(DataListModel<T> listModel) |
38 | { |
39 | if (_listModel != null) |
40 | { |
41 | _listModel.removeDataListModelListener(this); |
42 | } |
43 | _listModel = listModel; |
44 | _listModel.addDataListModelListener(this); |
45 | modelReset(); |
46 | } |
47 | |
48 | public int getRowCount() |
49 | { |
50 | return _listModel.getRowCount(); |
51 | } |
52 | |
53 | public int getColumnCount() |
54 | { |
55 | return _dataModel.getColumnCount(); |
56 | } |
57 | |
58 | @Override public String getColumnName(int column) |
59 | { |
60 | return _dataModel.getColumnName(column); |
61 | } |
62 | |
63 | @Override public Class<?> getColumnClass(int column) |
64 | { |
65 | return _dataModel.getColumnClass(column); |
66 | } |
67 | |
68 | public Object getValueAt(int row, int column) |
69 | { |
70 | return _dataModel.getColumnValue(_listModel.getRow(row), column); |
71 | } |
72 | |
73 | @Override public void setValueAt(Object value, int row, int column) |
74 | { |
75 | _dataModel.setColumnValue(_listModel.getRow(row), column, value); |
76 | fireTableCellUpdated(row, column); |
77 | } |
78 | |
79 | @Override public boolean isCellEditable(int row, int column) |
80 | { |
81 | return _dataModel.isColumnEditable(_listModel.getRow(row), column); |
82 | } |
83 | |
84 | public void modelReset() |
85 | { |
86 | fireTableDataChanged(); |
87 | } |
88 | |
89 | public void rowsAdded(int begin, int end) |
90 | { |
91 | fireTableRowsInserted(begin, end); |
92 | } |
93 | |
94 | public void rowsRemoved(int begin, int end) |
95 | { |
96 | fireTableRowsDeleted(begin, end); |
97 | } |
98 | |
99 | public void rowsModified(int begin, int end) |
100 | { |
101 | fireTableRowsUpdated(begin, end); |
102 | } |
103 | |
104 | public void rowModified(int source, int destination) |
105 | { |
106 | fireTableRowsDeleted(source, source); |
107 | fireTableRowsInserted(destination, destination); |
108 | } |
109 | |
110 | private DataListModel<T> _listModel = null; |
111 | private final DataModel<T> _dataModel; |
112 | } |