com.github.bordertech.wcomponents.addons.table.TableBeanModel Maven / Gradle / Ivy
The newest version!
package com.github.bordertech.wcomponents.addons.table;
import com.github.bordertech.wcomponents.AbstractBeanBoundTableModel;
import com.github.bordertech.wcomponents.WTable;
import com.github.bordertech.wcomponents.WTableColumn;
import com.github.bordertech.wcomponents.addons.table.edit.RowActionable;
import com.github.bordertech.wcomponents.addons.table.edit.RowMode;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Bean bound table model with column definitions.
*
* @param the bean class used by this model
* @param the table column definition
* @author Jonathan Austin
*/
public class TableBeanModel> extends AbstractBeanBoundTableModel {
/**
* Indicates whether rows are globally selectable.
*/
private boolean selectable;
/**
* Columns for the table.
*/
private final List columns;
/**
* The column used for row actions.
*/
private final RowActionable actionColumn;
/**
* @param columns the columns for this table
*/
public TableBeanModel(final List columns) {
this.columns = columns;
RowActionable actionCol = null;
for (U column : columns) {
if (column.getRenderer() instanceof RowActionable) {
actionCol = (RowActionable) column.getRenderer();
break;
}
}
this.actionColumn = actionCol;
}
/**
* @return the column used for row actions
*/
public final RowActionable getActionColumn() {
return actionColumn;
}
@Override
public Object getValueAt(final List row, final int col) {
// Get the bean for the row
T bean = getRowBean(row);
// Get the column
TableColumn, T> column = getColumns().get(col);
// Return the value for the column
return column.getValue(bean);
}
@Override
public void setValueAt(final Object value, final List row, final int col) {
// Get the bean for the row
T bean = getRowBean(row);
// Get the column
TableColumn column = getColumns().get(col);
// Update the value
column.setValue(bean, value);
}
@Override
public int getRowCount() {
return getBeanList().size();
}
@Override
public int getChildCount(final List row) {
return 0;
}
@Override
public boolean isCellEditable(final List row, final int col) {
TableColumn column = getColumn(col);
return column.isEditable() && isRowEditable(getRowKey(row));
}
@Override
public boolean isSortable(final int col) {
TableColumn column = getColumn(col);
return column.getComparator() != null;
}
@Override
public Object getRowKey(final List row) {
T bean = getRowBean(row);
return getBeanKey(bean);
}
@Override
public int[] sort(final int col, final boolean ascending) {
TableColumn column = getColumn(col);
Comparator