Skip to content

Commit

Permalink
Added new SimplePagingListView control.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemmermann committed Dec 4, 2024
1 parent 0e6215a commit 4d21706
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<String> 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();
}
}
67 changes: 67 additions & 0 deletions gemsfx/src/main/java/com/dlsc/gemsfx/SimplePagingListView.java
Original file line number Diff line number Diff line change
@@ -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 <T> the type of items to show in the list view
*/
public class SimplePagingListView<T> extends PagingListView<T> {

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<T> items = new SimpleListProperty<>(this, "items", FXCollections.observableArrayList());

public final ObservableList<T> getItems() {
return items.get();
}

public final ListProperty<T> itemsProperty() {
return items;
}

public final void setItems(ObservableList<T> items) {
this.items.set(items);
}
}

0 comments on commit 4d21706

Please sign in to comment.