-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new SimplePagingListView control.
- Loading branch information
1 parent
0e6215a
commit 4d21706
Showing
2 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
gemsfx-demo/src/main/java/com/dlsc/gemsfx/demo/SimplePagingListViewApp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
gemsfx/src/main/java/com/dlsc/gemsfx/SimplePagingListView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |