Skip to content

Commit

Permalink
Added new control PagingControls.java
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Sep 19, 2024
1 parent 97de3d8 commit f77c0bb
Show file tree
Hide file tree
Showing 4 changed files with 549 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.dlsc.gemsfx.demo;

import com.dlsc.gemsfx.PagingControls;
import com.dlsc.gemsfx.Spacer;
import fr.brouillard.oss.cssfx.CSSFX;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.List;

public class PagingControlsApp extends Application {

@Override
public void start(Stage stage) {
VBox vBox1 = createSection(10, 221, false);
VBox vBox2 = createSection(15, 45, false);
VBox vBox3 = createSection(20, 1000, true);
VBox vBox4 = createSection(5, 5, false);

VBox all = new VBox(20, vBox1, vBox2, vBox3, vBox4);

StackPane stackPane = new StackPane(all);
stackPane.setPadding(new Insets(50, 50, 50, 50));

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

CSSFX.start(scene);
}

private static VBox createSection(int pageSize, int itemCount, boolean showFirstLastButtons) {
PagingControls pagingControls = new PagingControls();

pagingControls.setTotalItemCount(itemCount);
pagingControls.setPageSize(pageSize);

pagingControls.setShowGotoFirstPageButton(showFirstLastButtons);
pagingControls.setShowGotoLastPageButton(showFirstLastButtons);

pagingControls.setStyle("-fx-border-color: black; -fx-padding: 20px");
pagingControls.setPrefWidth(800);

Label pageLabel = new Label();
pageLabel.textProperty().bind(Bindings.createStringBinding(() -> "Page Index: " + pagingControls.getPage(), pagingControls.pageProperty()));

Label pageCountLabel = new Label();
pageCountLabel.textProperty().bind(Bindings.createStringBinding(() -> "Page count: " + pagingControls.getPageCount(), pagingControls.pageCountProperty()));

CheckBox showItemCounter = new CheckBox("Show item counter");
showItemCounter.selectedProperty().bindBidirectional(pagingControls.showMessageLabelProperty());

CheckBox showGotoFirstPageButton = new CheckBox("First page button");
showGotoFirstPageButton.selectedProperty().bindBidirectional(pagingControls.showGotoFirstPageButtonProperty());

CheckBox showGotoLastPageButton = new CheckBox("Last page button");
showGotoLastPageButton.selectedProperty().bindBidirectional(pagingControls.showGotoLastPageButtonProperty());

CheckBox showMaxPage = new CheckBox("Show max page");
showMaxPage.selectedProperty().bindBidirectional(pagingControls.showMaxPageProperty());

ChoiceBox<Integer> maxPageIndicatorsBox = new ChoiceBox<>();
maxPageIndicatorsBox.getItems().setAll(List.of(1, 2, 5, 10));
maxPageIndicatorsBox.valueProperty().bindBidirectional(pagingControls.maxPageIndicatorsCountProperty().asObject());

HBox hbox = new HBox(20, pageLabel, pageCountLabel, new Spacer(), showGotoFirstPageButton, showGotoLastPageButton, showMaxPage, showItemCounter, new Label("# Indicators: "), maxPageIndicatorsBox);

VBox vBox = new VBox(10, pagingControls, hbox);
vBox.setMaxHeight(Region.USE_PREF_SIZE);

return vBox;
}

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

import com.dlsc.gemsfx.skins.PagingControlsSkin;
import javafx.beans.binding.Bindings;
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.scene.control.Label;
import javafx.scene.control.Skin;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.util.Callback;

import java.util.Objects;

public class PagingControls extends Control {

private static final String DEFAULT_STYLE_CLASS = "paging-view";
private static final String PAGE_BUTTON = "page-button";

private final IntegerProperty startPage = new SimpleIntegerProperty();

public PagingControls() {
getStyleClass().add(DEFAULT_STYLE_CLASS);

setMessageLabelProvider(view -> {
int startIndex = (view.getPage() * getPageSize()) + 1;
int endIndex = startIndex + getPageSize() - 1;

return "Showing items " + startIndex + " to " + endIndex + " of " + getTotalItemCount();
});

addEventHandler(KeyEvent.KEY_PRESSED, evt -> {
if (Objects.equals(evt.getCode(), KeyCode.RIGHT)) {
nextPage();
} else if (Objects.equals(evt.getCode(), KeyCode.LEFT)) {
previousPage();
} else if (Objects.equals(evt.getCode(), KeyCode.HOME)) {
firstPage();
} else if (Objects.equals(evt.getCode(), KeyCode.END)) {
lastPage();
}
});

pageCount.bind(Bindings.createIntegerBinding(() -> {
int count = getTotalItemCount() / getPageSize();
if (getTotalItemCount() % getPageSize() > 0) {
count++;
}
return count;
}, totalItemCountProperty(), pageSizeProperty()));
}

@Override
protected Skin<?> createDefaultSkin() {
return new PagingControlsSkin(this);
}

@Override
public String getUserAgentStylesheet() {
return Objects.requireNonNull(PagingControls.class.getResource("paging-view.css")).toExternalForm();
}

private final BooleanProperty showMaxPage = new SimpleBooleanProperty(this, "showMaxButton");

public final boolean isShowMaxPage() {
return showMaxPage.get();
}

public final BooleanProperty showMaxPageProperty() {
return showMaxPage;
}

public final void setShowMaxPage(boolean showMaxPage) {
this.showMaxPage.set(showMaxPage);
}

private final ObjectProperty<Node> maxPageDividerNode = new SimpleObjectProperty<>(this, "maxPageDividerNode", new Label("..."));

public final Node getMaxPageDividerNode() {
return maxPageDividerNode.get();
}

public final ObjectProperty<Node> maxPageDividerNodeProperty() {
return maxPageDividerNode;
}

public final void setMaxPageDividerNode(Node maxPageDividerNode) {
this.maxPageDividerNode.set(maxPageDividerNode);
}

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

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

public final ObjectProperty<Callback<PagingControls, String>> messageLabelProviderProperty() {
return messageLabelProvider;
}

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

public void firstPage() {
setPage(0);
}

public void lastPage() {
setPage(getPageCount() - 1);
}

public void nextPage() {
setPage(Math.min(getPageCount() - 1, getPage() + 1));
}

public void previousPage() {
setPage(Math.max(0, getPage() - 1));
}

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

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

public final IntegerProperty totalItemCountProperty() {
return totalItemCount;
}

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

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

public final boolean getShowMessageLabel() {
return showMessageLabel.get();
}

public final BooleanProperty showMessageLabelProperty() {
return showMessageLabel;
}

public final void setShowMessageLabel(boolean showMessageLabel) {
this.showMessageLabel.set(showMessageLabel);
}

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

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

public final ReadOnlyIntegerProperty pageCountProperty() {
return pageCount.getReadOnlyProperty();
}

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

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

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();
}

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();
}

public final IntegerProperty pageSizeProperty() {
return pageSize;
}

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

public void refresh() {
startPage.set(0);
}

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

public final boolean isShowGotoFirstPageButton() {
return showGotoFirstPageButton.get();
}

public final BooleanProperty showGotoFirstPageButtonProperty() {
return showGotoFirstPageButton;
}

public final void setShowGotoFirstPageButton(boolean showGotoFirstPageButton) {
this.showGotoFirstPageButton.set(showGotoFirstPageButton);
}

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

public final boolean isShowGotoLastPageButton() {
return showGotoLastPageButton.get();
}

public final BooleanProperty showGotoLastPageButtonProperty() {
return showGotoLastPageButton;
}

public final void setShowGotoLastPageButton(boolean showGotoLastPageButton) {
this.showGotoLastPageButton.set(showGotoLastPageButton);
}
}
Loading

0 comments on commit f77c0bb

Please sign in to comment.