From beb9ba06be78c59d26694fb4f59ac6c9ff8eb9c0 Mon Sep 17 00:00:00 2001 From: Dirk Lemmermann Date: Thu, 12 Dec 2024 17:31:05 +0100 Subject: [PATCH] Fixes in PagingListView. --- .../dlsc/gemsfx/demo/PagingListViewApp.java | 4 +- .../dlsc/gemsfx/skins/PagingControlsSkin.java | 58 ++++++++++--------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/PagingListViewApp.java b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/PagingListViewApp.java index e27b6703..33fb9be2 100644 --- a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/PagingListViewApp.java +++ b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/PagingListViewApp.java @@ -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 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 { diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/PagingControlsSkin.java b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/PagingControlsSkin.java index f16e07c0..2d3f04c2 100644 --- a/gemsfx/src/main/java/com/dlsc/gemsfx/skins/PagingControlsSkin.java +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/skins/PagingControlsSkin.java @@ -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; @@ -32,6 +33,7 @@ public class PagingControlsSkin extends SkinBase { private final IntegerProperty startPage = new SimpleIntegerProperty(); private final HBox pageButtonsBox = new HBox(); + private final BooleanBinding moreItemsThanMinimumAvailablePageSize; private Button lastPageButton; private Button nextButton; @@ -45,6 +47,18 @@ public class PagingControlsSkin extends SkinBase { 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)); @@ -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() { @@ -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(); @@ -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) {