Skip to content

Commit

Permalink
New control PagingListView.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Dec 3, 2024
1 parent 0616f71 commit 12b470d
Show file tree
Hide file tree
Showing 8 changed files with 838 additions and 304 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dlsc.gemsfx.demo;

import com.dlsc.gemsfx.PagingControlBase.MessageLabelStrategy;
import com.dlsc.gemsfx.PagingControls;
import com.dlsc.gemsfx.PagingControls.MessageLabelStrategy;
import com.dlsc.gemsfx.Spacer;
import fr.brouillard.oss.cssfx.CSSFX;
import javafx.application.Application;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.dlsc.gemsfx.demo;

import com.dlsc.gemsfx.EnhancedLabel;
import com.dlsc.gemsfx.PagingListView;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.scenicview.ScenicView;

import java.util.ArrayList;
import java.util.List;

public class PagingListViewApp extends Application {

@Override
public void start(Stage stage) {
PagingListView<String> pagingListView = new PagingListView<>();
pagingListView.setPrefWidth(400);
pagingListView.setTotalItemCount(30);
pagingListView.setPageSize(10);
pagingListView.setLoader(lv -> {
List<String> data = new ArrayList<>();
int offset = lv.getPage() * lv.getPageSize();
for (int i = 0; i < lv.getPageSize(); i++) {
data.add("Item " + (offset + i));
}
return data;
});

Button scenicView = new Button("Scenic View");
scenicView.setOnAction(evt -> ScenicView.show(scenicView.getScene()));

VBox box = new VBox(20, pagingListView, scenicView);
box.setPadding(new Insets(20));

Scene scene = new Scene(box);
stage.setTitle("Paging List View");
stage.setScene(scene);
stage.sizeToScene();
stage.centerOnScreen();
stage.show();
}

public static void main(String[] args) {
launch();
}
}
309 changes: 309 additions & 0 deletions gemsfx/src/main/java/com/dlsc/gemsfx/PagingControlBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,309 @@
package com.dlsc.gemsfx;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.ReadOnlyIntegerProperty;
import javafx.beans.property.ReadOnlyIntegerWrapper;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.scene.Node;
import javafx.scene.control.Control;
import javafx.util.Callback;

public abstract class PagingControlBase extends Control {

private final BooleanProperty showPreviousNextPageButton = new SimpleBooleanProperty(this, "showPreviousNextButton", true);

public final boolean getShowPreviousNextPageButton() {
return showPreviousNextPageButton.get();
}

/**
* A flag used to determine whether the control will display arrow buttons to
* go to the next or the previous page.
*
* @return a boolean property to control the visibility of the previous / next buttons
*/
public final BooleanProperty showPreviousNextPageButtonProperty() {
return showPreviousNextPageButton;
}

public final void setShowPreviousNextPageButton(boolean showPreviousNextPageButton) {
this.showPreviousNextPageButton.set(showPreviousNextPageButton);
}

/**
* A list of possible strategies for showing / hiding the message label.
*
* @see #messageLabelStrategyProperty()
*/
public enum MessageLabelStrategy {

/**
* Always hide the message label.
*/
HIDE,

/**
* Show the message label when needed, usually when the total item count is larger than zero.
*/
SHOW_WHEN_NEEDED,

/**
* Always show the message label, even when there are no items in the controlled view.
*/
ALWAYS_SHOW
}

private final ObjectProperty<PagingControls.MessageLabelStrategy> messageLabelStrategy = new SimpleObjectProperty<>(this, "messageLabelStrategy", PagingControls.MessageLabelStrategy.SHOW_WHEN_NEEDED);

public final PagingControls.MessageLabelStrategy getMessageLabelStrategy() {
return messageLabelStrategy.get();
}

/**
* The message label strategy controls whether the message label will appear in
* certain situations, for example, when there are no items currently in the view
* that is being controlled by these pagination controls.
*
* @return the strategy used to show or hide the message label
*/
public final ObjectProperty<PagingControls.MessageLabelStrategy> messageLabelStrategyProperty() {
return messageLabelStrategy;
}

public final void setMessageLabelStrategy(PagingControls.MessageLabelStrategy messageLabelStrategy) {
this.messageLabelStrategy.set(messageLabelStrategy);
}

/**
* An enum listing the different ways the control will display or
* not display controls to quickly go to the first or the last page.
*/
public enum FirstLastPageDisplayMode {

/**
* Do not show controls for jumping to the first or last page.
*/
HIDE,

/**
* Show separate controls in front and after the page buttons to
* perform the jump.
*/
SHOW_ARROW_BUTTONS,

/**
* Show extra page buttons to perform the jump (1 ... 5 6 7 8 ... 20).
*/
SHOW_PAGE_BUTTONS
}

private final ObjectProperty<PagingControls.FirstLastPageDisplayMode> firstLastPageDisplayMode = new SimpleObjectProperty<>(this, "firstLastPageStrategy", PagingControls.FirstLastPageDisplayMode.SHOW_PAGE_BUTTONS);

public final PagingControls.FirstLastPageDisplayMode getFirstLastPageDisplayMode() {
return firstLastPageDisplayMode.get();
}

public final ObjectProperty<PagingControls.FirstLastPageDisplayMode> firstLastPageDisplayModeProperty() {
return firstLastPageDisplayMode;
}

public final void setFirstLastPageDisplayMode(PagingControls.FirstLastPageDisplayMode firstLastPageDisplayMode) {
this.firstLastPageDisplayMode.set(firstLastPageDisplayMode);
}

private final ObjectProperty<Node> firstPageDivider = new SimpleObjectProperty<>(this, "firstPageDivider");

public final Node getFirstPageDivider() {
return firstPageDivider.get();
}

/**
* Stores the node that will be placed between the regular page buttons and the page button
* that represents the "first" page. This is usually a label showing "...".
*
* @return a node for separating the "first page" button, usually a label showing "..."
*/
public final ObjectProperty<Node> firstPageDividerProperty() {
return firstPageDivider;
}

public final void setFirstPageDivider(Node firstPageDivider) {
this.firstPageDivider.set(firstPageDivider);
}

private final ObjectProperty<Node> lastPageDivider = new SimpleObjectProperty<>(this, "firstPageDivider");

public final Node getLastPageDivider() {
return lastPageDivider.get();
}

/**
* Stores the node that will be placed between the regular page buttons and the page button
* that represents the "last" page. This is usually a label showing "...".
*
* @return a node for separating the "last page" button, usually a label showing "..."
*/
public final ObjectProperty<Node> lastPageDividerProperty() {
return lastPageDivider;
}

public final void setLastPageDivider(Node lastPageDivider) {
this.lastPageDivider.set(lastPageDivider);
}

private final ObjectProperty<Callback<PagingControls, String>> messageLabelProvider = new SimpleObjectProperty<>(this, "messageLabelProvider");

public final Callback<PagingControls, String> getMessageLabelProvider() {
return messageLabelProvider.get();
}

/**
* A message label provider is used to customize the messages shown by the message label
* of this control, for example, "Showing items 11 to 20 of a total of 1000 items".
*
* @return the message label provider
*/
public final ObjectProperty<Callback<PagingControls, String>> messageLabelProviderProperty() {
return messageLabelProvider;
}

public final void setMessageLabelProvider(Callback<PagingControls, String> messageLabelProvider) {
this.messageLabelProvider.set(messageLabelProvider);
}

/**
* Sets the page index to zero.
*/
public void firstPage() {
setPage(0);
}

/**
* Sets the page index to the page count minus one.
*
* @see #pageProperty()
* @see #pageCountProperty()
*/
public void lastPage() {
setPage(getPageCount() - 1);
}

/**
* Increments the page index.
*
* @see #pageProperty()
*/
public void nextPage() {
setPage(Math.min(getPageCount() - 1, getPage() + 1));
}

/**
* Decrements the page index.
*
* @see #pageProperty()
*/
public void previousPage() {
setPage(Math.max(0, getPage() - 1));
}

private final IntegerProperty totalItemCount = new SimpleIntegerProperty(this, "totalItemCount");

public final int getTotalItemCount() {
return totalItemCount.get();
}

/**
* The total number of items (rows) displayed by the control that utilizes
* this pagination control for paging.
*
* @return the total number of items in the view
*/
public final IntegerProperty totalItemCountProperty() {
return totalItemCount;
}

public final void setTotalItemCount(int totalItemCount) {
this.totalItemCount.set(totalItemCount);
}

final ReadOnlyIntegerWrapper pageCount = new ReadOnlyIntegerWrapper(this, "pageCount");

public final int getPageCount() {
return pageCount.get();
}

/**
* A read-only property that stores the number of pages that are required for the given
* number of items and the given page size.
*
* @return a read-only integer property storing the number of required pages
* @see #totalItemCountProperty()
* @see #pageSizeProperty()
*/
public final ReadOnlyIntegerProperty pageCountProperty() {
return pageCount.getReadOnlyProperty();
}

private final IntegerProperty maxPageIndicatorsCount = new SimpleIntegerProperty(this, "maxPageIndicatorsCount", 5);

public final int getMaxPageIndicatorsCount() {
return maxPageIndicatorsCount.get();
}

/**
* The maximum number of page indicators / buttons that will be shown at any time
* by this control.
*
* @return the number of page buttons shown by the control
*/
public final IntegerProperty maxPageIndicatorsCountProperty() {
return maxPageIndicatorsCount;
}

public final void setMaxPageIndicatorsCount(int maxPageIndicatorsCount) {
this.maxPageIndicatorsCount.set(maxPageIndicatorsCount);
}

private final IntegerProperty page = new SimpleIntegerProperty(this, "page");

public final int getPage() {
return page.get();
}

/**
* The index of the currently showing page.
*
* @return the number of the currently showing page
*/
public final IntegerProperty pageProperty() {
return page;
}

public final void setPage(int page) {
this.page.set(page);
}

private final IntegerProperty pageSize = new SimpleIntegerProperty(this, "pageSize", 10);

public final int getPageSize() {
return pageSize.get();
}

/**
* The number of items shown per page of the control that is being controlled
* by the pagination control.
*
* @return the number of items per page
*/
public final IntegerProperty pageSizeProperty() {
return pageSize;
}

public final void setPageSize(int pageSize) {
this.pageSize.set(pageSize);
}
}
Loading

0 comments on commit 12b470d

Please sign in to comment.