Last active
March 22, 2016 08:17
-
-
Save smallufo/429908c4c2d956cf9ff7 to your computer and use it in GitHub Desktop.
slightly modified PaginationBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by smallufo on 2016-03-21. | |
*/ | |
package destiny.vaadin.components; | |
import com.vaadin.server.FontAwesome; | |
import com.vaadin.ui.Alignment; | |
import com.vaadin.ui.Button; | |
import com.vaadin.ui.Label; | |
import com.vaadin.ui.themes.ValoTheme; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.vaadin.viritin.button.MButton; | |
import org.vaadin.viritin.layouts.MHorizontalLayout; | |
import java.util.function.Supplier; | |
/** | |
* ref | |
* https://github.com/viritin/viritin/blob/master/src/test/java/org/vaadin/viritin/it/MTableWithTraditionalPaging.java | |
* change to 1-based paging | |
*/ | |
public class PaginationBar extends MHorizontalLayout { | |
private Logger logger = LoggerFactory.getLogger(getClass()); | |
private PagingListener listener; | |
private int currentPage = 1; | |
private Button first, previous , next, last; | |
private final Label status = new Label(); | |
/** total pages */ | |
private final Supplier<Integer> pagesSupplier; | |
private Button.ClickListener handler = new Button.ClickListener() { | |
@Override | |
public void buttonClick(Button.ClickEvent event) { | |
if (event.getButton() == first) { | |
currentPage = 1; | |
} | |
else if (event.getButton() == last) { | |
currentPage = pagesSupplier.get() ; | |
} | |
else if (event.getButton() == next) { | |
currentPage++; | |
} | |
else if (event.getButton() == previous) { | |
currentPage--; | |
} | |
updateState(); | |
listener.pageRequested(currentPage); | |
} | |
}; | |
private void updateState() { | |
final boolean hasPrev = currentPage > 1; | |
first.setEnabled(hasPrev); | |
previous.setEnabled(hasPrev); | |
final boolean hasNext = currentPage < pagesSupplier.get() ; | |
logger.debug("currentPage = {} , pages = {} , hasNext = {}", currentPage, pagesSupplier.get(), hasNext); | |
next.setEnabled(hasNext); | |
last.setEnabled(hasNext); | |
status.setValue(currentPage + "/" + pagesSupplier.get()); | |
} | |
public interface PagingListener { | |
/** 1-based */ | |
void pageRequested(int page); | |
} | |
public PaginationBar(PagingListener listener, int pageSize, Supplier<Long> countSupplier) { | |
this.listener = listener; | |
this.pagesSupplier = () -> { | |
long total = countSupplier.get(); | |
return (int) (total % pageSize == 0 ? total / pageSize : total / pageSize + 1); | |
}; | |
this.first = new MButton(FontAwesome.FAST_BACKWARD, handler).withStyleName(ValoTheme.BUTTON_BORDERLESS); | |
this.previous = new MButton(FontAwesome.BACKWARD, handler).withStyleName(ValoTheme.BUTTON_BORDERLESS); | |
this.next = new MButton(FontAwesome.FORWARD, handler).withStyleName(ValoTheme.BUTTON_BORDERLESS); | |
this.last = new MButton(FontAwesome.FAST_FORWARD, handler).withStyleName(ValoTheme.BUTTON_BORDERLESS); | |
updateState(); | |
addComponents(first, previous, status, next, last); | |
alignAll(Alignment.MIDDLE_CENTER); | |
withFullWidth(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment