Skip to content

Commit

Permalink
Fixes in PagingListView.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Dec 12, 2024
1 parent 40d2c86 commit beb9ba0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ public class PagingListViewApp extends Application {

private final BooleanProperty simulateNoData = new SimpleBooleanProperty(false);

private final IntegerProperty count = new SimpleIntegerProperty(205);
private final IntegerProperty count = new SimpleIntegerProperty(12);

@Override
public void start(Stage stage) {
PagingListView<String> pagingListView = new PagingListView<>();
pagingListView.setPrefWidth(600);
pagingListView.totalItemCountProperty().bind(Bindings.createIntegerBinding(() -> simulateNoData.get() ? 0 : count.get(), simulateNoData, count));
pagingListView.setPageSize(10);
pagingListView.setPageSize(5);
pagingListView.setLoader(loadRequest -> {
if (simulateDelayProperty.get()) {
try {
Expand Down
58 changes: 30 additions & 28 deletions gemsfx/src/main/java/com/dlsc/gemsfx/skins/PagingControlsSkin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dlsc.gemsfx.skins;

import com.dlsc.gemsfx.PagingControlBase;
import com.dlsc.gemsfx.PagingControls;
import com.dlsc.gemsfx.Spacer;
import javafx.beans.InvalidationListener;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class PagingControlsSkin extends SkinBase<PagingControls> {
private final IntegerProperty startPage = new SimpleIntegerProperty();

private final HBox pageButtonsBox = new HBox();
private final BooleanBinding moreItemsThanMinimumAvailablePageSize;

private Button lastPageButton;
private Button nextButton;
Expand All @@ -45,6 +47,18 @@ public class PagingControlsSkin extends SkinBase<PagingControls> {
public PagingControlsSkin(PagingControls view) {
super(view);

/*
* We do not want to see the page size selector if the page sizes shown inside the selector are all bigger
* than the total amount of items.
*/
moreItemsThanMinimumAvailablePageSize = Bindings.createBooleanBinding(() -> {
int smallestAvailablePageSize = view.getAvailablePageSizes().stream()
.min(Integer::compareTo)
.orElse(1);
int totalItemCount = view.getTotalItemCount();
return totalItemCount > smallestAvailablePageSize;
}, view.totalItemCountProperty(), view.availablePageSizesProperty());

createStaticElements();

pageButtonsBox.visibleProperty().bind(view.pageCountProperty().greaterThan(1));
Expand Down Expand Up @@ -78,6 +92,21 @@ public PagingControlsSkin(PagingControls view) {
});

updateView();

BooleanBinding neededBinding = moreItemsThanMinimumAvailablePageSize.and(Bindings.createBooleanBinding(() -> {
if (view.getPageCount() > 1) {
return true;
}
if (view.getMessageLabelStrategy().equals(PagingControlBase.MessageLabelStrategy.ALWAYS_SHOW)) {
return true;
}
return false;
}, view.pageCountProperty(), view.availablePageSizesProperty(), view.messageLabelStrategyProperty()));

neededBinding.addListener(it -> {
view.getProperties().remove("controls.needed");
view.getProperties().put("controls.needed", neededBinding.get());
});
}

private void createStaticElements() {
Expand All @@ -102,21 +131,9 @@ private void createStaticElements() {
pageSizeSelectorLabel.visibleProperty().bind(pageSizeSelectorLabel.textProperty().isNotEmpty());
pageSizeSelectorLabel.managedProperty().bind(pageSizeSelectorLabel.textProperty().isNotEmpty());

/*
* We do not want to see the page size selector if the page sizes shown inside the selector are all bigger
* than the total amount of items.
*/
BooleanBinding moreItemsThanMinimumPageSize = Bindings.createBooleanBinding(() -> {
int smallestAvailablePageSize = view.getAvailablePageSizes().stream()
.min(Integer::compareTo)
.orElse(1);
int totalItemCount = view.getTotalItemCount();
return totalItemCount > smallestAvailablePageSize;
}, view.totalItemCountProperty(), view.availablePageSizesProperty());

pageSizeSelectorContainer = new HBox(pageSizeSelectorLabel, pageSizeSelector);
pageSizeSelectorContainer.getStyleClass().add("page-size-container");
pageSizeSelectorContainer.visibleProperty().bind(view.showPageSizeSelectorProperty().and(view.totalItemCountProperty().greaterThan(0)).and(moreItemsThanMinimumPageSize));
pageSizeSelectorContainer.visibleProperty().bind(view.showPageSizeSelectorProperty().and(view.totalItemCountProperty().greaterThan(0)).and(moreItemsThanMinimumAvailablePageSize));
pageSizeSelectorContainer.managedProperty().bind(pageSizeSelectorContainer.visibleProperty());

messageLabel = new Label();
Expand Down Expand Up @@ -187,21 +204,6 @@ private void createStaticElements() {
lastPageButton.disableProperty().bind(view.pageProperty().add(view.getMaxPageIndicatorsCount()).lessThan(view.getPageCount()).not());
lastPageButton.visibleProperty().bind(view.firstLastPageDisplayModeProperty().isEqualTo(PagingControls.FirstLastPageDisplayMode.SHOW_ARROW_BUTTONS).and(view.pageCountProperty().greaterThan(1)));
lastPageButton.setOnMouseClicked(evt -> view.setPage(view.getPageCount() - 1));

BooleanProperty neededBinding = new SimpleBooleanProperty();
neededBinding.bind(pageSizeSelectorContainer.visibleProperty()
.or(pageButtonsBox.visibleProperty())
.or(firstPageButton.visibleProperty())
.or(previousButton.visibleProperty())
.or(nextButton.visibleProperty())
.or(lastPageButton.visibleProperty())
.or(messageLabel.visibleProperty())
);

neededBinding.addListener(it -> {
view.getProperties().remove("controls.needed");
view.getProperties().put("controls.needed", neededBinding.get());
});
}

private Node wrapIcon(Region region) {
Expand Down

0 comments on commit beb9ba0

Please sign in to comment.