001: package org.osbl.client.wings.form;
002:
003: import org.conform.BeanMeta;
004: import org.conform.PropertyMeta;
005: import org.conform.wings.ComponentFactory;
006: import org.osbl.client.wings.form.editor.ListTableModel;
007: import org.wingx.table.*;
008:
009: import java.util.*;
010: import java.lang.reflect.Method;
011:
012: public class GenericObjectTableModel extends XTableModel implements
013: ObjectTableModel, ListTableModel {
014: private BeanMeta beanMeta;
015: private XTableColumnModel columnModel = new XDefaultTableColumnModel();
016: protected List rows = new ArrayList();
017: List<Method> getters;
018: List<Method> setters;
019:
020: public GenericObjectTableModel(BeanMeta beanMeta,
021: String... propertyNames) {
022: this .beanMeta = (BeanMeta) beanMeta.clone();
023: getters = new ArrayList<Method>(propertyNames.length);
024: setters = new ArrayList<Method>(propertyNames.length);
025:
026: List<PropertyMeta> properties = new LinkedList<PropertyMeta>();
027: for (int i = 0; i < propertyNames.length; i++) {
028: String propertyName = propertyNames[i];
029: PropertyMeta propertyMeta = this .beanMeta
030: .getProperty(propertyName);
031: properties.add(propertyMeta);
032: columnModel.addColumn(ComponentFactory.INSTANCE
033: .createTableColumn(this .beanMeta, propertyName, i));
034:
035: try {
036: boolean isBoolean = boolean.class.equals(propertyMeta
037: .getType());
038: Method getter = beanMeta.getType().getMethod(
039: (isBoolean ? "is" : "get")
040: + Character.toUpperCase(propertyName
041: .charAt(0))
042: + propertyName.substring(1));
043: getters.add(getter);
044:
045: if (propertyMeta.isWritable()) {
046: Method setter = beanMeta.getType().getMethod(
047: "set"
048: + Character
049: .toUpperCase(propertyName
050: .charAt(0))
051: + propertyName.substring(1),
052: getter.getReturnType());
053: setters.add(setter);
054: } else
055: setters.add(null);
056: } catch (NoSuchMethodException e) {
057: throw new RuntimeException(e);
058: }
059: }
060: this .beanMeta.setProperties(properties
061: .toArray(new PropertyMeta[properties.size()]));
062: }
063:
064: public BeanMeta getBeanMeta() {
065: return beanMeta;
066: }
067:
068: public XTableColumnModel getColumnModel() {
069: return columnModel;
070: }
071:
072: public void setList(List rows) {
073: if (this .rows != rows) {
074: this .rows = rows != null ? rows : new ArrayList();
075: refresh();
076: }
077: }
078:
079: public List getList() {
080: return rows;
081: }
082:
083: public int getRowCount() {
084: return rows.size();
085: }
086:
087: public int getColumnCount() {
088: return columnModel.getColumnCount();
089: }
090:
091: public Object getRow(int rowIndex) {
092: return rows.get(rowIndex);
093: }
094:
095: private Method idGetter;
096:
097: public Object getRowId(int rowIndex) {
098: try {
099: if (idGetter == null)
100: idGetter = beanMeta.getType().getMethod("getId");
101:
102: return idGetter.invoke(rows.get(rowIndex));
103: } catch (Exception e) {
104: throw new RuntimeException(e);
105: }
106: }
107:
108: public Object getValueAt(int rowIndex, int columnIndex) {
109: try {
110: Object row = getRow(rowIndex);
111: Method getter = getters.get(columnIndex);
112: return getter.invoke(row);
113: } catch (Exception e) {
114: throw new RuntimeException(e);
115: }
116: }
117:
118: public void setValueAt(Object value, int rowIndex, int columnIndex) {
119: try {
120: Object object = getRow(rowIndex);
121: Method setter = setters.get(columnIndex);
122: setter.invoke(object, value);
123: fireTableCellUpdated(rowIndex, columnIndex);
124: } catch (Exception e) {
125: throw new RuntimeException(e);
126: }
127: }
128:
129: public String getColumnName(int column) {
130: return "" + columnModel.getColumn(column).getHeaderValue();
131: }
132:
133: public Class<?> getColumnClass(int columnIndex) {
134: return beanMeta.getProperties()[columnIndex].getType();
135: }
136:
137: public void refresh() {
138: fireTableDataChanged();
139: }
140: }
|