package com.onaro.sanscreen.client.view.tabular; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.ResourceBundle; import org.apache.commons.lang3.StringUtils; import com.onaro.commons.swing.table.OnaroColumn; import com.onaro.commons.swing.table.OnaroTableUtils; import com.onaro.commons.util.IndexMap; import com.onaro.sanscreen.client.view.init.ConfigurableTableModelInitInfo; import com.onaro.sanscreen.client.view.init.ConfigurableTableModelInitInfo.ColumnInitInfo; import com.onaro.util.IllegalInitException; public class ConfigurableTableModel extends AbstractTabularDataTableModel { private static final long serialVersionUID = 3L; /** * The default sorting column number or -1 stands for no default sorting. */ protected int defaultSortingColumn = -1; /** * The default sorting order. */ protected boolean defaultAscendingSorting; /** * The default object type. If not null, it specifies the type of all the rows in the model. */ private String selectionType; /** * The name of the column that tells the type of each row. May be null. */ private String objectTypeColumn; /** * The name of the column which its values uniquely identify the whole row. */ private String identifierColumn; /** * keeps the index of the column that contains the flag. this column when filtered, is used to show only some of the * rows based on a query */ private int flaggedColumnIndex = -1; /** * The name of the table using this model. */ private String tableName; private TabularDataModel tabularDataModel; private IndexMap userDataMap; /** * Keeps track of the derived columns that are on the table, to be reset. */ private Collection derivedColumns = new ArrayList(1); protected final PropertyChangeSupport propertyChangeSupport; public ConfigurableTableModel(String tableName, ConfigurableTableModelInitInfo configurableTableModelInitInfo, ResourceBundle resources) throws IllegalInitException { if (StringUtils.isBlank(tableName)) throw new IllegalArgumentException("tableName"); //$NON-NLS-1$ this.tableName = tableName; /** * The name of the column in the data model that tells the type of each row. This column may not be included in * the table view. May be null. */ objectTypeColumn = configurableTableModelInitInfo.getObjectTypeColumn(); flaggedColumnIndex = configurableTableModelInitInfo.getFlaggedColumnIndex(); defaultSortingColumn = configurableTableModelInitInfo.getDefaultSortingColumn(); defaultAscendingSorting = configurableTableModelInitInfo.isDefaultAscendingSorting(); identifierColumn = configurableTableModelInitInfo.getIdentifierColumn(); List columnInitInfos = configurableTableModelInitInfo.getColumnInit(); for (ConfigurableTableModelInitInfo.ColumnInitInfo columnInitInfo : columnInitInfos) { ITabularColumn column = ColumnFactory.createColumn(tableName, columnInitInfo, resources); @SuppressWarnings("unchecked") OnaroColumn abstractColumn = (OnaroColumn) column; if (column instanceof DerivedNumberColumn) { derivedColumns.add((DerivedNumberColumn)column); } addColumn(abstractColumn); } propertyChangeSupport = new PropertyChangeSupport(this); } /** * Sets the data model for this table and notify all the columns of the new mode. * * @param newTabularDataModel * the data model */ public final void setTabularDataModel(TabularDataModel newTabularDataModel) { TabularDataModel oldTabularDataModel = this.tabularDataModel; List newRows = new ArrayList(newTabularDataModel != null ? newTabularDataModel.getRowCount() : 0); if (newTabularDataModel != null) { final int rowCount = newTabularDataModel.getRowCount(); for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { Row newRow = new MyRow(rowIndex); newRows.add(newRow); } } // Create a new user data map for the rows userDataMap = new IndexMap(newRows.size()); this.tabularDataModel = newTabularDataModel; this.setRows(newRows); propertyChangeSupport.firePropertyChange(PROP_TABULAR_DATA_MODEL, oldTabularDataModel, newTabularDataModel); // fireTableDataChanged(); } public final TabularDataModel getTabularDataModel() { return tabularDataModel; } /* * (non-Javadoc) * * @see com.onaro.sanscreen.client.view.tabular.GroupingDirectorTableModel#getFlaggedColumnIndex() */ public int getFlaggedColumnIndex() { return flaggedColumnIndex; } /* * (non-Javadoc) * * @see com.onaro.sanscreen.client.view.tabular.GroupingDirectorTableModel#getDefaultSortingColumn() */ public int getDefaultSortingColumn() { return defaultSortingColumn; } /* * (non-Javadoc) * * @see com.onaro.sanscreen.client.view.tabular.GroupingDirectorTableModel#isDefaultAscendingSorting() */ public boolean isDefaultAscendingSorting() { return defaultAscendingSorting; } /* * (non-Javadoc) * * @see com.onaro.sanscreen.client.view.tabular.GroupingDirectorTableModel#getSelectionType() */ public String getSelectionType() { return selectionType; } /* * (non-Javadoc) * * @see com.onaro.sanscreen.client.view.tabular.GroupingDirectorTableModel#getObjType(int) */ public String getObjectType(int row) { if (objectTypeColumn == null) { return selectionType; } TabularDataModel tabularDataModel = getTabularDataModel(); if (tabularDataModel == null) return selectionType; String objectType = (String) tabularDataModel.getValueAt(row, objectTypeColumn); return objectType; } /* * (non-Javadoc) * * @see com.onaro.sanscreen.client.view.tabular.GroupingDirectorTableModel#getIdentifierColumn() */ public String getIdentifierColumn() { return identifierColumn; } public Object getValueByColumnId(int rowIndex, String columnId) { int columnIndex = OnaroTableUtils.findColumnIndexByColumnId(this, columnId); return getValueAt(rowIndex, columnIndex); } public Object getValueByColumnName(int rowIndex, String columnName) { int columnIndex = this.findColumn(columnName); return getValueAt(rowIndex, columnIndex); } public String getObjectTypeColumn() { return objectTypeColumn; } /* * (non-Javadoc) * * @see com.onaro.sanscreen.client.view.tabular.GroupingDirectorTableModel#getTableName() */ public String getTableName() { return tableName; } @Override public final Object getRowId(int rowIndex) { String identifierColumn = getIdentifierColumn(); Object rowId; if (StringUtils.isNotBlank(identifierColumn)) { Row row = this.getRows().get(rowIndex); rowId = row.getTabularDataModelValue(identifierColumn); } else { rowId = super.getRowId(rowIndex); } return rowId; } class MyRow extends AbstractRow { public MyRow(int rowIndex) { super(rowIndex); } public TabularDataModel getTabularDataModel() { return ConfigurableTableModel.this.getTabularDataModel(); } @Override protected Object getUserData(String key, int rowIndex) { return userDataMap.get(key, rowIndex); } @Override protected Object setUserData(String key, Object value, int rowIndex) { return userDataMap.put(key, rowIndex, value); } } public final void addPropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(listener); } public final void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { propertyChangeSupport.addPropertyChangeListener(propertyName, listener); } public final void removePropertyChangeListener(PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(listener); } public final void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { propertyChangeSupport.removePropertyChangeListener(propertyName, listener); } }