001: package org.osbl.identity.gui;
002:
003: import org.osbl.client.wings.form.ObjectTableModel;
004: import org.osbl.client.wings.form.StringEditableCellRenderer;
005: import org.osbl.persistence.*;
006: import org.conform.*;
007: import org.conform.hibernate.HibernateEnvironment;
008: import org.conform.wings.ComponentFactory;
009: import org.wingx.table.*;
010:
011: import java.util.*;
012:
013: public class SimpleQueryCommandTableModel extends XTableModel implements
014: ObjectTableModel {
015: private BeanMeta beanMeta;
016: private XTableColumnModel columnModel = new XDefaultTableColumnModel();
017: protected SimpleQueryCommand command;
018: DefaultTableData data;
019:
020: public SimpleQueryCommandTableModel(SimpleQueryCommand command,
021: BeanMeta beanMeta, String... propertyNames) {
022: command.setType(beanMeta.getType());
023:
024: this .command = command;
025: this .beanMeta = (BeanMeta) beanMeta.clone();
026:
027: for (int i = 0; i < propertyNames.length; i++) {
028: String propertyName = propertyNames[i];
029: columnModel.addColumn(ComponentFactory.INSTANCE
030: .createTableColumn(this .beanMeta, propertyName, i));
031: }
032: this .data = new DefaultTableData(beanMeta);
033: }
034:
035: public void setSortableColumns(String... sortables) {
036: for (XTableColumn tableColumn : (Collection<XTableColumn>) columnModel
037: .getColumns())
038: tableColumn.setSortable(false);
039:
040: for (String sortable : sortables)
041: columnModel.getColumn(sortable).setSortable(true);
042: }
043:
044: public void setFilterableColumns(String... filterables) {
045: for (XTableColumn tableColumn : (Collection<XTableColumn>) columnModel
046: .getColumns())
047: tableColumn.setFilterable(false);
048:
049: for (String sortable : filterables) {
050: XTableColumn tableColumn = columnModel.getColumn(sortable);
051: tableColumn.setFilterable(true);
052: tableColumn
053: .setFilterRenderer(new StringEditableCellRenderer());
054: }
055: }
056:
057: public SimpleQueryCommand getCommand() {
058: return command;
059: }
060:
061: public XTableColumnModel getColumnModel() {
062: return columnModel;
063: }
064:
065: public int getRowCount() {
066: return data.getRows().size();
067: }
068:
069: public int getColumnCount() {
070: return columnModel.getColumnCount();
071: }
072:
073: public Object getRowId(int rowIndex) {
074: return data.getCellData(rowIndex, "id").getValue();
075: }
076:
077: public Object getValueAt(int rowIndex, int columnIndex) {
078: return data.getCellData(rowIndex,
079: propertyNameForIndex(columnIndex)).getValue();
080: }
081:
082: public String getColumnName(int column) {
083: return "" + columnModel.getColumn(column).getHeaderValue();
084: }
085:
086: public Class<?> getColumnClass(int columnIndex) {
087: return beanMeta.getProperties()[columnIndex].getType();
088: }
089:
090: protected String propertyNameForIndex(int columnIndex) {
091: if (columnIndex >= columnModel.getColumnCount())
092: throw new RuntimeException("Index below column count '"
093: + columnIndex + "'");
094:
095: return (String) columnModel.getColumn(columnIndex)
096: .getIdentifier();
097: }
098:
099: private int indexForPropertyName(String propertyName) {
100: for (int i = 0; i < columnModel.getColumnCount(); i++)
101: if (propertyName.equals(columnModel.getColumn(i)
102: .getIdentifier()))
103: return i;
104:
105: throw new RuntimeException("Unknown property name '"
106: + propertyName + "'");
107: }
108:
109: public void refresh() {
110: filters();
111: sorts();
112:
113: try {
114: HibernateEnvironment.getInstance().beginTransaction();
115: data.setValue(command.execute());
116: } finally {
117: HibernateEnvironment.getInstance().endTransaction();
118: }
119: fireTableDataChanged();
120: }
121:
122: protected void sorts() {
123: command.clearSorts();
124: for (int i = 0; i < getColumnCount(); i++) {
125: int sort = getSort(i);
126: if (sort == SORT_NONE)
127: continue;
128:
129: command.addSort(propertyNameForIndex(i),
130: sort == SORT_ASCENDING ? Sort.ASCENDING
131: : Sort.DESCENDING);
132: }
133: }
134:
135: protected void filters() {
136: command.clearFilters();
137:
138: for (Filter filter : filters)
139: command.addFilter(filter.getProperty(), filter
140: .getOperator(), filter.getValue());
141:
142: for (int i = 0; i < getColumnCount(); i++) {
143: Object value = getFilter(i);
144: if (value == null)
145: continue;
146:
147: command.addFilter(propertyNameForIndex(i),
148: Operator.LIKE_IGNORE_CASE, "%" + value + "%");
149: }
150: }
151:
152: Set<Filter> filters = new HashSet<Filter>();
153:
154: public void addFilter(String property, Operator operator,
155: Object value) {
156: filters.add(new Filter(property, operator, value));
157: }
158:
159: public void removeFilter(String property, Operator operator) {
160: filters.remove(new Filter(property, operator, null));
161: }
162:
163: static class Filter {
164: String property;
165: Operator operator;
166: Object value;
167:
168: public Filter(String property, Operator operator, Object value) {
169: this .property = property;
170: this .operator = operator;
171: this .value = value;
172: }
173:
174: public String getProperty() {
175: return property;
176: }
177:
178: public void setProperty(String property) {
179: this .property = property;
180: }
181:
182: public Operator getOperator() {
183: return operator;
184: }
185:
186: public void setOperator(Operator operator) {
187: this .operator = operator;
188: }
189:
190: public Object getValue() {
191: return value;
192: }
193:
194: public void setValue(Object value) {
195: this .value = value;
196: }
197:
198: public boolean equals(Object o) {
199: if (this == o)
200: return true;
201: if (o == null || getClass() != o.getClass())
202: return false;
203:
204: Filter filter = (Filter) o;
205:
206: if (operator != filter.operator)
207: return false;
208: if (!property.equals(filter.property))
209: return false;
210:
211: return true;
212: }
213:
214: public int hashCode() {
215: int result;
216: result = property.hashCode();
217: result = 31 * result + operator.hashCode();
218: return result;
219: }
220: }
221: }
|