1 15 package org.apache.tapestry.contrib.table.model.simple; 16 17 import java.util.Arrays ; 18 import java.util.Comparator ; 19 import java.util.Iterator ; 20 21 import org.apache.tapestry.contrib.table.model.CTableDataModelEvent; 22 import org.apache.tapestry.contrib.table.model.ITableColumn; 23 import org.apache.tapestry.contrib.table.model.ITableColumnModel; 24 import org.apache.tapestry.contrib.table.model.ITableDataModel; 25 import org.apache.tapestry.contrib.table.model.ITableDataModelListener; 26 import org.apache.tapestry.contrib.table.model.ITableSortingState; 27 import org.apache.tapestry.contrib.table.model.common.AbstractTableModel; 28 import org.apache.tapestry.contrib.table.model.common.ArrayIterator; 29 import org.apache.tapestry.contrib.table.model.common.ReverseComparator; 30 31 36 public class SimpleTableModel extends AbstractTableModel implements ITableDataModelListener 37 { 38 private static final long serialVersionUID = 1L; 39 40 private ITableDataModel m_objDataModel = null; 41 private Object [] m_arrRows = null; 42 private ITableColumnModel m_objColumnModel = null; 43 44 private SimpleTableSortingState m_objLastSortingState; 45 46 public SimpleTableModel(Object [] arrData, ITableColumn[] arrColumns) 47 { 48 this(new SimpleListTableDataModel(arrData), new SimpleTableColumnModel(arrColumns)); 49 } 50 51 public SimpleTableModel(Object [] arrData, ITableColumnModel objColumnModel) 52 { 53 this(new SimpleListTableDataModel(arrData), objColumnModel); 54 } 55 56 public SimpleTableModel(ITableDataModel objDataModel, ITableColumnModel objColumnModel) 57 { 58 this(objDataModel, objColumnModel, new SimpleTableState()); 59 } 60 61 public SimpleTableModel(ITableDataModel objDataModel, ITableColumnModel objColumnModel, SimpleTableState objState) 62 { 63 super(objState); 64 65 m_arrRows = null; 66 m_objColumnModel = objColumnModel; 67 m_objLastSortingState = new SimpleTableSortingState(); 68 69 setDataModel(objDataModel); 70 } 71 72 public ITableColumnModel getColumnModel() 73 { 74 return m_objColumnModel; 75 } 76 77 public Iterator getCurrentPageRows() 78 { 79 sortRows(); 80 81 int nPageSize = getPagingState().getPageSize(); 82 if (nPageSize <= 0) 83 return new ArrayIterator(m_arrRows); 84 85 int nCurrentPage = getPagingState().getCurrentPage(); 86 int nFrom = nCurrentPage * nPageSize; 87 int nTo = (nCurrentPage + 1) * nPageSize; 88 89 return new ArrayIterator(m_arrRows, nFrom, nTo); 90 } 91 92 public int getRowCount() 93 { 94 updateRows(); 95 return m_arrRows.length; 96 } 97 98 private void updateRows() 99 { 100 if (m_arrRows != null) 102 return; 103 104 m_objLastSortingState = new SimpleTableSortingState(); 106 107 int nRowCount = m_objDataModel.getRowCount(); 108 Object [] arrRows = new Object [nRowCount]; 109 110 int i = 0; 111 for (Iterator it = m_objDataModel.getRows(); it.hasNext();) 112 arrRows[i++] = it.next(); 113 114 m_arrRows = arrRows; 115 } 116 117 protected void sortRows() 118 { 119 updateRows(); 120 121 ITableSortingState objSortingState = getSortingState(); 122 123 String strSortColumn = objSortingState.getSortColumn(); 125 if (strSortColumn == null) 126 return; 127 128 boolean bSortOrder = objSortingState.getSortOrder(); 129 130 if (strSortColumn.equals(m_objLastSortingState.getSortColumn()) 132 && m_objLastSortingState.getSortOrder() == bSortOrder) 133 return; 134 135 ITableColumn objColumn = getColumnModel().getColumn(strSortColumn); 136 if (objColumn == null || !objColumn.getSortable()) 137 return; 138 139 Comparator objCmp = objColumn.getComparator(); 140 if (objCmp == null) 141 return; 142 143 if (bSortOrder == ITableSortingState.SORT_DESCENDING) 145 objCmp = new ReverseComparator(objCmp); 146 147 Arrays.sort(m_arrRows, objCmp); 148 149 m_objLastSortingState.setSortColumn(strSortColumn, bSortOrder); 150 } 151 152 public void tableDataChanged(CTableDataModelEvent objEvent) 153 { 154 m_arrRows = null; 155 } 156 157 161 public ITableDataModel getDataModel() 162 { 163 return m_objDataModel; 164 } 165 166 170 public void setDataModel(ITableDataModel dataModel) 171 { 172 if (m_objDataModel != null) 173 m_objDataModel.removeTableDataModelListener(this); 174 175 m_objDataModel = dataModel; 176 m_objDataModel.addTableDataModelListener(this); 177 178 m_arrRows = null; 179 } 180 181 } 182 | Popular Tags |