/** * @param currentPage the currentPage to set. Zero based. */ public void setCurrentPage(final int currentPage) { if (currentPage < 0) { throw new IllegalArgumentException("Page number must be greater than or equal to zero."); } WTableComponentModel model = getOrCreateComponentModel(); model.currentPage = Math.min(model.getMaxPage(), currentPage); // Notify the table model that the page has changed TableModel dataModel = getTableModel(); if (dataModel instanceof ScrollableTableModel) { if (!isPaginated()) { throw new IllegalStateException( "Table with no pagination tried to set the current page on a ScrollableTableModel."); } int startIndex = getCurrentPage() * getRowsPerPage(); int endIndex = Math.min(startIndex + getRowsPerPage(), dataModel.getRowCount()) - 1; ((ScrollableTableModel) dataModel).setCurrentRows(startIndex, endIndex); } }
/** * {@inheritDoc} */ @Override public Object getBean(final BeanProviderBound beanProviderBound) { TableModel dataModel = table.getTableModel(); int rowCount = dataModel.getRowCount(); if (rowCount == 0) { return Collections.emptyList(); } int startIndex = 0; int endIndex = rowCount - 1; if (PaginationMode.DYNAMIC == table.getPaginationMode() && table.isPaginated()) { int rowsPerPage = table.getRowsPerPage(); int currentPage = table.getCurrentPage(); // Only render the rows on the current page // If total row count has changed, calc the new last page startIndex = Math. min(currentPage * rowsPerPage, rowCount - (rowCount % rowsPerPage)); endIndex = Math.min(startIndex + rowsPerPage, rowCount) - 1; } if (endIndex < startIndex) { // No data return Collections.EMPTY_LIST; } return table.getRowIds(startIndex, endIndex, false); } }
/** * Method to call when the model data has changed. For example, when a row has been added or removed. * <p> * Handles resorting the data (if table sorted) and making sure the table pagination is still correct. * </p> */ public void handleDataChanged() { // Apply sort (if required) if (isSorted()) { sort(getSortColumnIndex(), isSortAscending()); } setCurrentPage(getCurrentPage()); }
/** * Test getCurrentPage - when set beyond maxPage set back to it. */ @Test public void testGetCurrentPageTooHigh() { final int currentPage = 42; WTable table = new WTable(); table.setLocked(true); setActiveContext(createUIContext()); Assert.assertEquals("current page should be 0", 0, table.getCurrentPage()); table.setCurrentPage(currentPage); Assert.assertEquals("current page should still be 0", 0, table.getCurrentPage()); }
int currentPage = getCurrentPage();
/** * Test getCurrentPage - set back to maxPage. */ @Test public void testGetCurrentPage() { final int currentPage = 12; WTable table = new WTable(); table.setLocked(true); setActiveContext(createUIContext()); table.setCurrentPage(currentPage); Assert.assertEquals("Too high currentPage should be set back to maxPage", 0, table. getCurrentPage()); resetContext(); Assert.assertEquals("Incorrect default page", 0, table.getCurrentPage()); }
String rowsStr = request.getParameter(getId() + ".rows"); int prevPage = getCurrentPage(); int prevRows = getRowsPerPage();
@Test public void testCurrentPageLogicWithRowsOptions() { WTable table = new WTable(); table.setTableModel(createModel(new String[100][1])); table.setPaginationMode(PaginationMode.CLIENT); table.setRowsPerPageOptions(Arrays.asList(0, 5)); // Rows option of 5 table.setRowsPerPage(5); table.setCurrentPage(2); Assert.assertEquals("Current page should be 2 for a table with rows option of 5", 2, table. getCurrentPage()); // Rows option of 0 table.setRowsPerPage(0); table.setCurrentPage(2); Assert.assertEquals("Current page should alway be 0 for a table with rows option of 0", 0, table.getCurrentPage()); }
@Test public void testCurrentPageLogicWithNoPagination() { WTable table = new WTable(); table.setPaginationMode(PaginationMode.NONE); table.setTableModel(createModel(new String[100][1])); table.setRowsPerPage(10); table.setCurrentPage(50); Assert.assertEquals("Current page should alway be zero for a table with no pagination", 0, table.getCurrentPage()); }
@Test public void testCurrentPageLogic() { WTable table = new WTable(); table.setPaginationMode(PaginationMode.CLIENT); table.setTableModel(createModel(new String[100][1])); table.setRowsPerPage(10); // Page is greater than number of pages table.setCurrentPage(90); Assert.assertEquals("Current page should be equal to the max page of 9", 9, table. getCurrentPage()); }
/** * Sets the table model which provides the row/column data. * * @param tableModel the table model. */ public void setTableModel(final TableModel tableModel) { getOrCreateComponentModel().tableModel = tableModel; getOrCreateComponentModel().rowIndexMapping = null; setSelectedRows(null); setExpandedRows(null); clearPrevExpandedRows(); clearPrevRenderedRows(); if (tableModel instanceof BeanBoundTableModel) { ((BeanBoundTableModel) tableModel). setBeanProvider(new BeanBoundTableModelBeanProvider(this)); ((BeanBoundTableModel) tableModel).setBeanProperty("."); } if (tableModel instanceof ScrollableTableModel) { if (!isPaginated()) { throw new IllegalStateException( "Set a ScrollableTableModel on a table that is not paginated."); } int startIndex = getCurrentPage() * getRowsPerPage(); int endIndex = startIndex + getRowsPerPage() - 1; ((ScrollableTableModel) tableModel).setCurrentRows(startIndex, endIndex); } // Flush the repeater's row contexts and scratch maps repeater.reset(); }
@Test public void testHandlePaginationRequest() { WTable table = new WTable(); table.setPaginationMode(WTable.PaginationMode.DYNAMIC); table.setTableModel(createModel(new String[100][1])); table.setRowsPerPage(10); table.setLocked(true); setActiveContext(createUIContext()); MockRequest request = new MockRequest(); request.setParameter(table.getId() + "-h", "x"); request.setParameter(table.getId() + ".page", "5"); table.handleRequest(request); Assert.assertEquals("Incorrect page number after handleRequest", 5, table.getCurrentPage()); resetContext(); Assert.assertEquals("Incorrect default page number after handleRequest", 0, table. getCurrentPage()); }
@Test public void testDoPaintPaginatedDynamic() throws IOException, SAXException, XpathException { WTable component = new WTable(); component.addColumn(new WTableColumn(COL1_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL2_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL3_HEADING_TEST, WTextField.class)); TableModel tableModel = createTableModel(); component.setTableModel(tableModel); component.setVisible(true); component.setPaginationMode(PaginationMode.DYNAMIC); setActiveContext(createUIContext()); assertXpathEvaluatesTo("dynamic", "//ui:table/ui:pagination/@mode", component); assertXpathEvaluatesTo((new Integer(component.getCurrentPage())).toString(), "//ui:table/ui:pagination/@currentPage", component); assertXpathEvaluatesTo((new Integer(component.getRowsPerPage())).toString(), "//ui:table/ui:pagination/@rowsPerPage", component); assertXpathEvaluatesTo((new Integer(tableModel.getRowCount())).toString(), "//ui:table/ui:pagination/@rows", component); }
@Test public void testDoPaintWithRowsPerPageOptions() throws IOException, SAXException, XpathException { WTable component = new WTable(); component.addColumn(new WTableColumn(COL1_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL2_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL3_HEADING_TEST, WTextField.class)); TableModel tableModel = createTableModel(); component.setTableModel(tableModel); component.setVisible(true); component.setPaginationMode(PaginationMode.DYNAMIC); component.setRowsPerPageOptions(Arrays.asList(10, 20, 30)); setActiveContext(createUIContext()); assertXpathEvaluatesTo("dynamic", "//ui:table/ui:pagination/@mode", component); assertXpathEvaluatesTo((new Integer(component.getCurrentPage())).toString(), "//ui:table/ui:pagination/@currentPage", component); assertXpathEvaluatesTo((new Integer(component.getRowsPerPage())).toString(), "//ui:table/ui:pagination/@rowsPerPage", component); assertXpathEvaluatesTo((new Integer(tableModel.getRowCount())).toString(), "//ui:table/ui:pagination/@rows", component); assertXpathEvaluatesTo("10", "//ui:table/ui:pagination/ui:rowsselect/ui:option[1]/@value", component); assertXpathEvaluatesTo("20", "//ui:table/ui:pagination/ui:rowsselect/ui:option[2]/@value", component); assertXpathEvaluatesTo("30", "//ui:table/ui:pagination/ui:rowsselect/ui:option[3]/@value", component); }
@Test public void testDoPaintPaginatedClient() throws IOException, SAXException, XpathException { WTable component = new WTable(); component.addColumn(new WTableColumn(COL1_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL2_HEADING_TEST, WTextField.class)); component.addColumn(new WTableColumn(COL3_HEADING_TEST, WTextField.class)); TableModel tableModel = createTableModel(); component.setTableModel(tableModel); component.setVisible(true); component.setPaginationMode(PaginationMode.CLIENT); setActiveContext(createUIContext()); assertXpathEvaluatesTo("client", "//ui:table/ui:pagination/@mode", component); assertXpathEvaluatesTo((new Integer(component.getCurrentPage())).toString(), "//ui:table/ui:pagination/@currentPage", component); assertXpathEvaluatesTo((new Integer(component.getRowsPerPage())).toString(), "//ui:table/ui:pagination/@rowsPerPage", component); assertXpathEvaluatesTo((new Integer(tableModel.getRowCount())).toString(), "//ui:table/ui:pagination/@rows", component); assertXpathNotExists("//ui:table/ui:pagination/@controls", component); component.setPaginationLocation(PaginationLocation.BOTTOM); assertXpathEvaluatesTo("bottom", "//ui:table/ui:pagination/@controls", component); }
getRowsPerPage()); Assert.assertEquals("Incorrect page number after handleRequest with change of rows", 2, table.getCurrentPage()); getRowsPerPage()); Assert.assertEquals("Incorrect page number after handleRequest and no change", 2, table. getCurrentPage()); getRowsPerPage()); Assert.assertEquals("Incorrect page number after handleRequest with change", 4, table. getCurrentPage()); Assert.assertEquals( "Incorrect page number after handleRequest with change to zero rows option", 0, table.getCurrentPage()); getCurrentPage()); Assert.assertEquals("Incorrect default rows per page after handleRequest", 20, table. getRowsPerPage());
xml.appendOptionalAttribute("rowsPerPage", table.getRowsPerPage() > 0, table. getRowsPerPage()); xml.appendAttribute("currentPage", table.getCurrentPage());