diff --git a/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SimplePagingListViewApp.java b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SimplePagingListViewApp.java new file mode 100644 index 00000000..f918b63d --- /dev/null +++ b/gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SimplePagingListViewApp.java @@ -0,0 +1,42 @@ +package com.dlsc.gemsfx.demo; + +import com.dlsc.gemsfx.SimplePagingListView; +import javafx.application.Application; +import javafx.geometry.Insets; +import javafx.scene.Scene; +import javafx.scene.control.Button; +import javafx.scene.layout.VBox; +import javafx.stage.Stage; +import org.scenicview.ScenicView; + +public class SimplePagingListViewApp extends Application { + + @Override + public void start(Stage stage) { + SimplePagingListView pagingListView = new SimplePagingListView<>(); + pagingListView.setPrefWidth(400); + for (int i = 0; i < 200; i++) { + pagingListView.getItems().add("Item " + (i + 1)); + } + + Button scenicView = new Button("Scenic View"); + scenicView.setOnAction(evt -> ScenicView.show(scenicView.getScene())); + + VBox box = new VBox(20, pagingListView, new PagingControlsSettingsView(pagingListView), scenicView); + box.setPadding(new Insets(20)); + + Scene scene = new Scene(box); + + scene.focusOwnerProperty().addListener(it -> System.out.println(scene.getFocusOwner())); + + stage.setTitle("Simple Paging List View"); + stage.setScene(scene); + stage.sizeToScene(); + stage.centerOnScreen(); + stage.show(); + } + + public static void main(String[] args) { + launch(); + } +} diff --git a/gemsfx/src/main/java/com/dlsc/gemsfx/SimplePagingListView.java b/gemsfx/src/main/java/com/dlsc/gemsfx/SimplePagingListView.java new file mode 100644 index 00000000..e2c83f65 --- /dev/null +++ b/gemsfx/src/main/java/com/dlsc/gemsfx/SimplePagingListView.java @@ -0,0 +1,67 @@ +package com.dlsc.gemsfx; + +import javafx.beans.Observable; +import javafx.beans.property.ListProperty; +import javafx.beans.property.SimpleListProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + +/** + * A simple version of the paging list view that is completely based on a list of tiems, just like a normal + * list view would be. + * + * @param the type of items to show in the list view + */ +public class SimplePagingListView extends PagingListView { + + private boolean internal; + + /** + * Constructs a new list view and sets a loader that uses the data list. + */ + public SimplePagingListView() { + setLoader(lv -> { + int pageSize = getPageSize(); + int index = getPage() * pageSize; + return getItems().subList(index, Math.min(index + pageSize, getItems().size() - 1)); + }); + + loaderProperty().addListener(it -> { + throw new UnsupportedOperationException("a custom loader can not be used for this list view"); + }); + + totalItemCountProperty().addListener(it -> { + if (!internal) { + throw new UnsupportedOperationException("the total item count can not be explicitly changed for this list view"); + + } + }); + + items.addListener((Observable it) -> { + ObservableList list = getItems(); + if (list != null) { + internal = true; + try { + setTotalItemCount(list.size()); + setPage(Math.min(getTotalItemCount() / getPageSize(), getPage())); + } finally { + internal = false; + } + } + }); + } + + private final ListProperty items = new SimpleListProperty<>(this, "items", FXCollections.observableArrayList()); + + public final ObservableList getItems() { + return items.get(); + } + + public final ListProperty itemsProperty() { + return items; + } + + public final void setItems(ObservableList items) { + this.items.set(items); + } +}