Expose coherent DataGrid selection snapshots for view bindings
This commit is contained in:
@@ -94,6 +94,8 @@ public final class DataGrid<K, T> extends VBox implements AutoCloseable {
|
||||
}
|
||||
|
||||
private final DataGridDeclaration<K, T> declaration = new DataGridDeclaration<>(this);
|
||||
private final ReadOnlyObjectWrapper<GridSelection<K, T>> selection =
|
||||
new ReadOnlyObjectWrapper<>(GridSelection.empty());
|
||||
private final ReadOnlyObjectWrapper<K> selectedKey = new ReadOnlyObjectWrapper<>();
|
||||
private final ReadOnlyObjectWrapper<T> selectedElement = new ReadOnlyObjectWrapper<>();
|
||||
private final ReadOnlyListWrapper<K> selectedKeys = new ReadOnlyListWrapper<>(
|
||||
@@ -292,6 +294,7 @@ public final class DataGrid<K, T> extends VBox implements AutoCloseable {
|
||||
renderer.enableColumnSeparators();
|
||||
selectedKey.bind(renderer.selectedKeyProperty());
|
||||
selectedElement.bind(renderer.selectedElementProperty());
|
||||
selection.bind(renderer.selectionProperty());
|
||||
coordinator =
|
||||
new GridCoordinator<>(
|
||||
source, Platform::runLater, this::changed, result -> publishRows(result));
|
||||
@@ -395,6 +398,18 @@ public final class DataGrid<K, T> extends VBox implements AutoCloseable {
|
||||
|
||||
public ReadOnlyListProperty<T> selectedElementsProperty() { return selectedElements.getReadOnlyProperty(); }
|
||||
|
||||
/**
|
||||
* Observes selection as one snapshot from the current state, including before the first load.
|
||||
* The caller owns the adapter and closes it when the observing ViewModel stops using it.
|
||||
*
|
||||
* @return an independently closeable selection adapter for this grid
|
||||
*/
|
||||
public DataGridSelection<K, T> observeSelection() {
|
||||
requireFx();
|
||||
if (closed) throw new IllegalStateException("Grid is closed");
|
||||
return new DataGridSelection<>(this, selection.getReadOnlyProperty());
|
||||
}
|
||||
|
||||
public void selectKeys(Collection<? extends K> keys) {
|
||||
check();
|
||||
renderer.selectKeys(keys);
|
||||
@@ -1339,6 +1354,7 @@ public final class DataGrid<K, T> extends VBox implements AutoCloseable {
|
||||
if (renderer != null) renderer.close();
|
||||
selectedKey.unbind();
|
||||
selectedElement.unbind();
|
||||
selection.unbind();
|
||||
selectedKeys.unbind();
|
||||
selectedElements.unbind();
|
||||
selectedKeys.set(FXCollections.emptyObservableList());
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import java.util.Collection;
|
||||
import javafx.application.Platform;
|
||||
import javafx.beans.property.ReadOnlyObjectProperty;
|
||||
import javafx.beans.property.ReadOnlyObjectWrapper;
|
||||
|
||||
/**
|
||||
* An observable selection for a ViewModel to read, with explicit methods for selection changes.
|
||||
* Create it through {@link DataGrid#observeSelection()} and use it on the JavaFX application thread.
|
||||
* Closing the adapter detaches it without changing the grid's selection or closing the grid.
|
||||
*
|
||||
* @param <K> row key type
|
||||
* @param <T> row model type
|
||||
*/
|
||||
public final class DataGridSelection<K, T> implements AutoCloseable {
|
||||
private final ReadOnlyObjectWrapper<GridSelection<K, T>> selection = new ReadOnlyObjectWrapper<>();
|
||||
private DataGrid<K, T> grid;
|
||||
|
||||
DataGridSelection(DataGrid<K, T> grid, ReadOnlyObjectProperty<GridSelection<K, T>> source) {
|
||||
this.grid = grid;
|
||||
selection.bind(source);
|
||||
}
|
||||
|
||||
/** Returns one read-only observable containing the selected keys and available elements. */
|
||||
public ReadOnlyObjectProperty<GridSelection<K, T>> selectionProperty() {
|
||||
return selection.getReadOnlyProperty();
|
||||
}
|
||||
|
||||
/** Returns the current snapshot, or the last snapshot when the adapter is closed. */
|
||||
public GridSelection<K, T> getSelection() {
|
||||
return selection.get();
|
||||
}
|
||||
|
||||
/** Replaces the selection with one key, or clears it for a null key. */
|
||||
public void select(K key) {
|
||||
requireOpen().select(key);
|
||||
}
|
||||
|
||||
/** Restores the selection by key, respecting the grid's current selection mode. */
|
||||
public void selectKeys(Collection<? extends K> keys) {
|
||||
requireOpen().selectKeys(keys);
|
||||
}
|
||||
|
||||
private DataGrid<K, T> requireOpen() {
|
||||
if (grid == null) throw new IllegalStateException("Selection adapter is closed");
|
||||
return grid;
|
||||
}
|
||||
|
||||
/** Detaches the adapter and preserves its last published snapshot. */
|
||||
@Override
|
||||
public void close() {
|
||||
if (!Platform.isFxApplicationThread())
|
||||
throw new IllegalStateException("Use the JavaFX application thread");
|
||||
selection.unbind();
|
||||
grid = null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package de.neitzel.fx.view;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* One published selection of a grid. Keys retain their selection order and can include keys outside
|
||||
* the current page. Elements follow the grid's display order and contain only available selected
|
||||
* rows. The lead element is null when the lead key has no available row.
|
||||
*
|
||||
* @param selectedKeys selected keys, including retained off-page keys
|
||||
* @param selectedElements selected models of visible and interactive rows
|
||||
* @param leadKey lead key, or null for an empty selection
|
||||
* @param leadElement lead model, or null when unavailable
|
||||
* @param <K> row key type
|
||||
* @param <T> row model type
|
||||
*/
|
||||
public record GridSelection<K, T>(
|
||||
List<K> selectedKeys, List<T> selectedElements, K leadKey, T leadElement) {
|
||||
|
||||
/** Copies both lists so a published selection remains stable as the grid changes. */
|
||||
public GridSelection {
|
||||
selectedKeys = List.copyOf(selectedKeys);
|
||||
selectedElements = List.copyOf(selectedElements);
|
||||
}
|
||||
|
||||
static <K, T> GridSelection<K, T> empty() {
|
||||
return new GridSelection<>(List.of(), List.of(), null, null);
|
||||
}
|
||||
|
||||
/** A refreshed row can replace an equal model with a different instance. */
|
||||
boolean sameSelection(GridSelection<K, T> previous) {
|
||||
if (!selectedKeys.equals(previous.selectedKeys) || !Objects.equals(leadKey, previous.leadKey)
|
||||
|| leadElement != previous.leadElement
|
||||
|| selectedElements.size() != previous.selectedElements.size()) {
|
||||
return false;
|
||||
}
|
||||
for (int index = 0; index < selectedElements.size(); index++) {
|
||||
if (selectedElements.get(index) != previous.selectedElements.get(index)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,8 @@ public final class TableResults<K, T> implements AutoCloseable {
|
||||
private List<String> order;
|
||||
private final Set<String> hiddenColumns = new HashSet<>();
|
||||
private List<Entry<K, T>> entries = List.of();
|
||||
private final ReadOnlyObjectWrapper<GridSelection<K, T>> selection =
|
||||
new ReadOnlyObjectWrapper<>(GridSelection.empty());
|
||||
private final ReadOnlyObjectWrapper<K> selectedKey = new ReadOnlyObjectWrapper<>();
|
||||
private final ReadOnlyObjectWrapper<T> selectedElement = new ReadOnlyObjectWrapper<>();
|
||||
private final ObservableList<K> selectedKeys = FXCollections.observableArrayList();
|
||||
@@ -131,6 +133,10 @@ public final class TableResults<K, T> implements AutoCloseable {
|
||||
/** Only selected models currently visible and interactive are actionable. */
|
||||
public ReadOnlyListProperty<T> selectedElementsProperty() { return elementsView.getReadOnlyProperty(); }
|
||||
|
||||
ReadOnlyObjectProperty<GridSelection<K, T>> selectionProperty() {
|
||||
return selection.getReadOnlyProperty();
|
||||
}
|
||||
|
||||
public RowSelectionMode getSelectionMode() { return selectionMode; }
|
||||
|
||||
public void setSelectionMode(RowSelectionMode mode) {
|
||||
@@ -623,6 +629,10 @@ public final class TableResults<K, T> implements AutoCloseable {
|
||||
}
|
||||
selectedElement.set(rowsInteractive ? loaded : null);
|
||||
selectedElements.setAll(actionable);
|
||||
|
||||
GridSelection<K, T> snapshot = new GridSelection<>(
|
||||
selectedKeys, selectedElements, selectedKey.get(), selectedElement.get());
|
||||
if (!snapshot.sameSelection(selection.get())) selection.set(snapshot);
|
||||
}
|
||||
|
||||
public void setOnLayoutCommitted(Runnable handler) {
|
||||
@@ -980,6 +990,7 @@ public final class TableResults<K, T> implements AutoCloseable {
|
||||
selectedElement.set(null);
|
||||
selectedKeys.clear();
|
||||
selectedElements.clear();
|
||||
selection.set(GridSelection.empty());
|
||||
anchor = null;
|
||||
captured = GroupProjection.capture(List.of(), List.of());
|
||||
groupTree = List.of();
|
||||
|
||||
Reference in New Issue
Block a user