I tried an early implementation but didn't worked out too good.
Anyway most of the code is translated to java but not quite functional, still many TODO's in it.
If someone would like to improve it, here is the
GridPager.java
Anyway most of the code is translated to java but not quite functional, still many TODO's in it.
If someone would like to improve it, here is the
GridPager.java
Code:
public class GridPager extends VLayout { private class ButtonLabel extends Label { private int pageNum; public String getTitle() { if (GridPager.this.pageNum == pageNum) {return pageNum + "";} else {return "<u>" + this.pageNum + "</u>";} } public int getPageNum() {return pageNum;} public void setPageNum(int pageNum) {this.pageNum = pageNum;} } private int pageSize = 50; private int numPages = 4; private boolean showTotal = true; private int footerHeight = 20; private final ListGrid grid; private HLayout footer; private Label totalLabel; private int pageNum; private ToolStrip pagerControls; private boolean _scrollingGrid; private int pagerButtonWidth = 20; private int groupStart; public GridPager(ListGrid listGrid) { this.grid = listGrid; grid.setCellHeight(20); grid.setHeaderHeight(22); // Allow the listGrid enough space for 1 "page" of data int gridHeight = (grid.getCellHeight() * pageSize) + grid.getHeaderHeight(); setHeight(gridHeight + footerHeight); // Observe dataChanged on the grid - if the total number of rows changes // we may need // to rework our pagination grid.addDataArrivedHandler(new DataArrivedHandler() { public void onDataArrived(DataArrivedEvent event) {goToPage(pageNum, true);} }); // grid shows up at the top of the VLayout addMember(this.grid); // create the footer - HLayout to contain controls / totals information makeFooter(); addMember(footer); // create the total label if appropriate if (showTotal) { makeTotalLabel(); footer.addMember(this.totalLabel); } // have the paging controls be right-aligned in the footer LayoutSpacer layoutSpacer = new LayoutSpacer(); layoutSpacer.setWidth("*"); footer.addMember(layoutSpacer); // create the pager controls pagerControls = makePagerControls(); footer.addMember(pagerControls); // always start at page 1 goToPage(1, true); } private Label makeTotalLabel() { totalLabel = new Label() { public String getTitle() {return getTotalLabelTitle();}; }; // TODO workaround for line below // totalLabel.setProperty("autoDraw", false); totalLabel.setWrap(false); totalLabel.setAlign(Alignment.LEFT); return totalLabel; } private String getTotalLabelTitle() { int totalRows = grid.getTotalRows(); return "Total Rows:" + totalRows; } private HLayout makeFooter() { // Footer: an HLayout containing the 'total' label and the paging // controls footer = new HLayout(); // TODO workaround for line below // footer.setProperty("autoDraw", false); footer.setHeight(footerHeight); // footer.setOverflow(Overflow.HIDDEN); footer.setMembersMargin(10); footer.setLayoutLeftMargin(5); footer.setLayoutRightMargin(5); return footer; } private ToolStrip makePagerControls() { ArrayList<Label> buttons = new ArrayList<Label>(); Label label = new Label(); // TODO workaround for line below // label.setProperty("autoDraw", false); label.setWidth(this.pagerButtonWidth); label.setContents("<<"); label.setAlign(Alignment.CENTER); label.setCursor(Cursor.HAND); label.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { previousPages(); } }); buttons.add(label); for (int i = 0; i < numPages; i++) { final ButtonLabel tmpLabel = new ButtonLabel(); tmpLabel.setAlign(Alignment.CENTER); // TODO workaround for line below // tmpLabel.setProperty("autoDraw", false); tmpLabel.setCursor(Cursor.HAND); tmpLabel.setWidth(this.pagerButtonWidth); tmpLabel.setPageNum(i + 1); tmpLabel.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { goToPage(tmpLabel.getPageNum(), true); } }); buttons.add(tmpLabel); } Label label2 = new Label(); // TODO workaround for line below // label2.setProperty("autoDraw", false); label2.setContents(">>"); label2.setAlign(Alignment.CENTER); label2.setWidth(this.pagerButtonWidth); label2.setCursor(Cursor.HAND); label2.addClickHandler(new ClickHandler() { public void onClick(ClickEvent event) { nextPages(); } }); buttons.add(label2); ToolStrip toolStrip = new ToolStrip(); toolStrip.setWidth(1); toolStrip.setOverflow(Overflow.VISIBLE); toolStrip.setStyleName("normal"); toolStrip.setMembers(buttons.toArray(new Label[] {})); // TODO workaround for line below // toolStrip.setProperty("autoDraw", false); return toolStrip; } private void goToPage(int pageNum, boolean forceRefresh) { // clamp to the end of the possible set of pages int pages = getTotalPages(); if (pageNum > pages) pageNum = pages; if (pageNum < 1) pageNum = 1; if (!forceRefresh && this.pageNum == pageNum) return; this.pageNum = pageNum; _scrollingGrid = true; // TODO pass line below to java // grid.scrollRecordIntoView((pageNum -1) * this.pageSize, false); _scrollingGrid = false; // update the buttons updatePagerControls(); } public void draw() { super.draw(); // TODO: pass code below to java // // On an external scroll of the grid, ensure we update our pageNum // info // var body = this.grid.body; // body.addMethods({ // scrolled : new Function (this.getID() + // ".listGridScrolled(this.getScrollTop())") // }); } private void listGridScrolled(boolean scrollTop) { if (this._scrollingGrid) return; // TODO pass line below to java and uncoment last code // int rowNum = scrollTop / grid.getRowHeight(); // each page spans from start (pageNum-1 * pageSize) to start + pageSize int start = (pageNum - 1) * pageSize; int end = start + pageSize; // TODO uncoment when we have rowNum // if (rowNum > end || rowNum < start) { // pageNum = (int) Math.ceil(rowNum / pageSize); // updatePagerControls(); // } } private int getTotalPages() { int total = this.grid.getTotalRows(); int pages = (int) Math.ceil(total / pageSize); // never return zero pages if (pages == 0) pages = 1; return pages; } // given a page number, returns the first page in the "group" of pages we // will show in // the navigation controls // We're showing N pages in the pager controls, so we'll always be starting // with // a multiple of N private int getGroupStart(int pageNum) { int groupIndex = (int) (Math.ceil(pageNum / numPages) - 1); int groupStart = 1 + (groupIndex * numPages); // If you're in the "last" group - shift the start if necessary so we // always // show numPages links (unless that would give us a negative value, of // course) groupStart = Math.max(1, Math.min(groupStart, (this.getTotalPages() - (numPages - 1)))); return groupStart; } private void updatePagerControls() { Canvas[] controls = this.pagerControls.getMembers(); int total = this.getTotalPages(); // if we're already showing a group that contains the page we moved to, // don't shift // groups - confusing UI // [This can happen at the end of pages where we have overlapping // groups] int groupStart; if (this.groupStart <= pageNum && ((this.groupStart + numPages - 1) >= pageNum)) { groupStart = this.groupStart; } else { groupStart = getGroupStart(pageNum); } this.groupStart = groupStart; int currentPage = this.groupStart; for (int i = 1; i < controls.length - 1; i++) { ((ButtonLabel) controls[i]).setPageNum(currentPage); if (currentPage > total) controls[i].hide(); else if (!controls[i].isVisible()) controls[i].show(); if (currentPage == pageNum) controls[i].setCursor(Cursor.DEFAULT); else controls[i].setCursor(Cursor.HAND); currentPage += 1; } // show / hide the prev next buttons if appropriate if (groupStart == 1) controls[0].hide(); else controls[0].show(); if (groupStart + numPages > total) controls[controls.length - 1].hide(); else controls[controls.length - 1].show(); if (this.footer.isDrawn()) this.footer.markForRedraw(); } private void previousPages() { goToPage(groupStart - 1, true); } private void nextPages() { goToPage(groupStart + numPages, true); } /** * pageSize - how many records to show per 'page' of data * * @return */ public int getPageSize() { return pageSize; } /** * pageSize - how many records to show per 'page' of data * * @param pageSize */ public void setPageSize(int pageSize) { this.pageSize = pageSize; } /** * numPages - how many page navigation buttons to show at one time * * @return */ public int getNumPages() { return numPages; } /** * numPages - how many page navigation buttons to show at one time * * @param numPages */ public void setNumPages(int numPages) { this.numPages = numPages; } /** * showTotal - should the total number of rows be displayed in the footer * * @return */ public boolean getShowTotal() { return showTotal; } /** * showTotal - should the total number of rows be displayed in the footer * * @param showTotal */ public void setShowTotal(boolean showTotal) { this.showTotal = showTotal; } /** * Footer - will contain total label and page navigation controls * * @return */ public int getFooterHeight() { return footerHeight; } /** * Footer - will contain total label and page navigation controls * * @param footerHeight */ public void setFooterHeight(int footerHeight) { this.footerHeight = footerHeight; } }
Comment