001: package org.conform;
002:
003: import org.conform.util.ListView;
004: import org.apache.commons.logging.LogFactory;
005:
006: import javax.swing.event.TableModelListener;
007: import java.util.*;
008:
009: public abstract class AbstractTableData extends AbstractData implements
010: TableData {
011: private static org.apache.commons.logging.Log LOG = LogFactory
012: .getLog(Data.class);
013:
014: protected BeanMeta beanMeta;
015: protected List columns = new ArrayList();
016: private Collection collection = new ArrayList();
017: private List rows = (List) collection;
018:
019: public AbstractTableData() {
020: }
021:
022: public AbstractTableData(BeanMeta bean) {
023: setBeanMeta(bean);
024: }
025:
026: public BeanMeta getBeanMeta() {
027: return beanMeta;
028: }
029:
030: public Meta getMeta() {
031: return beanMeta;
032: }
033:
034: public void setBeanMeta(BeanMeta beanMeta) {
035: this .beanMeta = beanMeta;
036: if (beanMeta == null)
037: throw new IllegalArgumentException(
038: "null beanMeta not allowed");
039: columns.clear();
040: initBean(beanMeta);
041: }
042:
043: public void revalidate() {
044: }
045:
046: public Object validate(Object value) throws ValidationException {
047: return value;
048: }
049:
050: protected void initBean(BeanMeta bean) {
051: PropertyMeta[] properties = bean.getProperties();
052: for (int i = 0; i < properties.length; i++) {
053: PropertyMeta property = properties[i];
054: initPropertyMeta(property);
055: }
056: }
057:
058: protected void initPropertyMeta(PropertyMeta property) {
059: if (!property.isReadable())
060: return;
061:
062: String label = property.getLabel();
063:
064: Column column = new Column();
065: column.property = property;
066: column.label = label;
067: columns.add(column);
068: }
069:
070: public void setValue(Object collection) {
071: this .collection = (Collection) collection;
072: if (collection instanceof List)
073: setRows((List) collection);
074: else
075: setRows(new ListView((Collection) collection));
076: LOG.trace("*** " + System.identityHashCode(this )
077: + " set collection " + System.identityHashCode(rows));
078: }
079:
080: public Object getValue() {
081: LOG.trace("*** " + System.identityHashCode(this )
082: + " get collection " + System.identityHashCode(rows));
083: return collection;
084: }
085:
086: public void initialize() {
087: }
088:
089: public List getRows() {
090: return rows;
091: }
092:
093: public void setRows(List rows) {
094: this .rows = rows;
095: }
096:
097: public int getRowCount() {
098: return getRows() != null ? getRows().size() : 0;
099: }
100:
101: public Object newRow() {
102: getRows().add(null);
103: return null;
104: }
105:
106: public void addRow(Object object) {
107: getRows().add(object);
108: }
109:
110: public Object removeRow(int index) {
111: return getRows().remove(index);
112: }
113:
114: public void clear() {
115: collection = Collections.EMPTY_LIST;
116: }
117:
118: public void flush() {
119: }
120:
121: public void update() {
122: }
123:
124: public int getColumnCount() {
125: return columns.size();
126: }
127:
128: public boolean isCellEditable(int rowIndex, int columnIndex) {
129: Column column = (Column) columns.get(columnIndex);
130: return column.property.isWritable();
131: }
132:
133: public Class getColumnClass(int columnIndex) {
134: Column column = (Column) columns.get(columnIndex);
135: return column.property.getType();
136: }
137:
138: public Object getValueAt(int rowIndex, int columnIndex) {
139: return getCellData(rowIndex, columnIndex).getValue();
140: }
141:
142: public void setValueAt(Object value, int rowIndex, int columnIndex) {
143: getCellData(rowIndex, columnIndex).setValue(value);
144: }
145:
146: public String getColumnName(int columnIndex) {
147: Column column = (Column) columns.get(columnIndex);
148: return column.label;
149: }
150:
151: public void addTableModelListener(TableModelListener l) {
152: }
153:
154: public void removeTableModelListener(TableModelListener l) {
155: }
156:
157: public PropertyData getCellData(int rowIndex, int columnIndex) {
158: AbstractTableData.Column column = (AbstractTableData.Column) columns
159: .get(columnIndex);
160: return getRowData(rowIndex).getPropertyData(column.property);
161: }
162:
163: public PropertyData getCellData(int rowIndex, String propertyName) {
164: return getRowData(rowIndex).getPropertyData(
165: beanMeta.getProperty(propertyName));
166: }
167:
168: protected static class Column {
169: public PropertyMeta property;
170: public String label;
171: }
172: }
|