001package org.gwtbootstrap3.client.ui; 002 003/* 004 * #%L 005 * GwtBootstrap3 006 * %% 007 * Copyright (C) 2013 - 2014 GwtBootstrap3 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import org.gwtbootstrap3.client.ui.base.HasPaginationSize; 024import org.gwtbootstrap3.client.ui.base.HasResponsiveness; 025import org.gwtbootstrap3.client.ui.base.helper.StyleHelper; 026import org.gwtbootstrap3.client.ui.constants.IconType; 027import org.gwtbootstrap3.client.ui.constants.PaginationSize; 028import org.gwtbootstrap3.client.ui.constants.Styles; 029import org.gwtbootstrap3.client.ui.html.UnorderedList; 030 031import com.google.gwt.event.dom.client.ClickEvent; 032import com.google.gwt.event.dom.client.ClickHandler; 033import com.google.gwt.user.cellview.client.SimplePager; 034 035/** 036 * Support for Bootstrap pagination (http://getbootstrap.com/components/#pagination) 037 * 038 * @author Joshua Godi 039 */ 040public class Pagination extends UnorderedList implements HasResponsiveness, HasPaginationSize { 041 042 public Pagination() { 043 setStyleName(Styles.PAGINATION); 044 } 045 046 public Pagination(final PaginationSize paginationSize) { 047 this(); 048 setPaginationSize(paginationSize); 049 } 050 051 @Override 052 public void setPaginationSize(final PaginationSize paginationSize) { 053 StyleHelper.addUniqueEnumStyleName(this, PaginationSize.class, paginationSize); 054 } 055 056 @Override 057 public PaginationSize getPaginationSize() { 058 return PaginationSize.fromStyleName(getStyleName()); 059 } 060 061 public AnchorListItem addPreviousLink() { 062 final AnchorListItem listItem = new AnchorListItem(); 063 listItem.setIcon(IconType.ANGLE_DOUBLE_LEFT); 064 insert(listItem, 0); 065 return listItem; 066 } 067 068 public AnchorListItem addNextLink() { 069 final AnchorListItem listItem = new AnchorListItem(); 070 listItem.setIcon(IconType.ANGLE_DOUBLE_RIGHT); 071 add(listItem); 072 return listItem; 073 } 074 075 /** 076 * This will help to rebuild the Pagination based on the data inside the SimplePager passed in. 077 * <p/> 078 * Make sure to all this after adding/remove data from any of the grid to ensure that this stays 079 * current with the SimplePager. 080 * <p/> 081 * ex. 082 * dataProvider.getList().addAll(newData); 083 * pagination.rebuild(mySimplePager); 084 * 085 * @param pager the SimplePager of the CellTable/DataGrid 086 */ 087 public void rebuild(final SimplePager pager) { 088 clear(); 089 090 if (pager.getPageCount() == 0) { 091 return; 092 } 093 094 final AnchorListItem prev = addPreviousLink(); 095 prev.addClickHandler(new ClickHandler() { 096 @Override 097 public void onClick(final ClickEvent event) { 098 pager.previousPage(); 099 updatePaginationState(pager); 100 } 101 }); 102 prev.setEnabled(pager.hasPreviousPage()); 103 104 for (int i = 0; i < pager.getPageCount(); i++) { 105 final int display = i + 1; 106 final AnchorListItem page = new AnchorListItem(String.valueOf(display)); 107 page.addClickHandler(new ClickHandler() { 108 @Override 109 public void onClick(final ClickEvent event) { 110 pager.setPage(display - 1); 111 updatePaginationState(pager); 112 } 113 }); 114 115 if (i == pager.getPage()) { 116 page.setActive(true); 117 } 118 119 add(page); 120 } 121 122 final AnchorListItem next = addNextLink(); 123 next.addClickHandler(new ClickHandler() { 124 @Override 125 public void onClick(final ClickEvent event) { 126 pager.nextPage(); 127 updatePaginationState(pager); 128 } 129 }); 130 next.setEnabled(pager.hasNextPage()); 131 } 132 133 /** 134 * This updates the current active page, and the enabled state 135 * of the previous and next buttons in the Pagination based 136 * on the state of the given SimplePager. 137 * @param pager the SimplePager of the CellTable/DataGrid 138 */ 139 private void updatePaginationState(final SimplePager pager) { 140 141 for (int i = 0; i < getWidgetCount(); i++) { 142 if (i == 0) { //previous button 143 ((AnchorListItem)getWidget(i)).setEnabled(pager.hasPreviousPage()); 144 } 145 else if (i == getWidgetCount() - 1) { //next button 146 ((AnchorListItem)getWidget(i)).setEnabled(pager.hasNextPage()); 147 } 148 else { 149 int index = i - 1; 150 if (index == pager.getPage()) { 151 ((AnchorListItem)getWidget(i)).setActive(true); 152 } 153 else { 154 ((AnchorListItem)getWidget(i)).setActive(false); 155 } 156 } 157 } 158 } 159}