01: package org.osbl.client.wings.devtools.hibernate;
02:
03: import org.osbl.client.wings.form.ObjectTableModel;
04: import org.conform.BeanMeta;
05: import org.conform.DefaultTableData;
06: import org.conform.hibernate.HibernateEnvironment;
07: import org.conform.wings.ComponentFactory;
08: import org.osbl.persistence.hibernate.HibernateQueryCommand;
09: import org.wingx.table.*;
10:
11: public class HibernateQueryCommandTableModel extends XTableModel
12: implements ObjectTableModel {
13: private BeanMeta beanMeta;
14: private XTableColumnModel columnModel = new XDefaultTableColumnModel();
15: HibernateQueryCommand command;
16: DefaultTableData data;
17:
18: public HibernateQueryCommandTableModel(
19: HibernateQueryCommand command, BeanMeta beanMeta,
20: String[] propertyNames) {
21: this .command = command;
22: this .beanMeta = (BeanMeta) beanMeta.clone();
23:
24: for (int i = 0; i < propertyNames.length; i++) {
25: String propertyName = propertyNames[i];
26: columnModel.addColumn(ComponentFactory.INSTANCE
27: .createTableColumn(this .beanMeta, propertyName, i));
28: }
29: this .data = new DefaultTableData(beanMeta);
30: }
31:
32: public XTableColumnModel getColumnModel() {
33: return columnModel;
34: }
35:
36: public int getRowCount() {
37: return data.getRows().size();
38: }
39:
40: public int getColumnCount() {
41: return columnModel.getColumnCount();
42: }
43:
44: public Object getRowId(int rowIndex) {
45: return data.getCellData(rowIndex, "id").getValue();
46: }
47:
48: public Object getValueAt(int rowIndex, int columnIndex) {
49: return data.getCellData(rowIndex,
50: propertyNameForIndex(columnIndex)).getValue();
51: }
52:
53: public String getColumnName(int column) {
54: return "" + columnModel.getColumn(column).getHeaderValue();
55: }
56:
57: public Class<?> getColumnClass(int columnIndex) {
58: return beanMeta.getProperties()[columnIndex].getType();
59: }
60:
61: private String propertyNameForIndex(int columnIndex) {
62: if (columnIndex >= columnModel.getColumnCount())
63: throw new RuntimeException("Index below column count '"
64: + columnIndex + "'");
65:
66: return (String) columnModel.getColumn(columnIndex)
67: .getIdentifier();
68: }
69:
70: private int indexForPropertyName(String propertyName) {
71: for (int i = 0; i < columnModel.getColumnCount(); i++)
72: if (propertyName.equals(columnModel.getColumn(i)
73: .getIdentifier()))
74: return i;
75:
76: throw new RuntimeException("Unknown property name '"
77: + propertyName + "'");
78: }
79:
80: public void refresh() {
81: try {
82: HibernateEnvironment.getInstance().beginTransaction();
83: if (command.getType() != null)
84: data.setValue(command.execute());
85: } finally {
86: HibernateEnvironment.getInstance().endTransaction();
87: }
88: fireTableDataChanged();
89: }
90: }
|