Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected Stream<String> getExcludedPatterns() {
return Stream.concat(super.getExcludedPatterns(), Stream.of(
"com\\.vaadin\\.flow\\.component\\.contextmenu\\.osgi\\..*",
"com\\.vaadin\\.flow\\.component\\.grid\\.GridColumnOrderHelper.*",
"com\\.vaadin\\.flow\\.component\\.grid\\.GridSelectionSignalHelper.*",
"com\\.vaadin\\.flow\\.component\\.grid\\.osgi\\..*"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
import com.vaadin.flow.data.selection.SelectionEvent;
import com.vaadin.flow.data.selection.SelectionListener;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.SignalBinding;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.shared.Registration;
import com.vaadin.flow.signals.BindingActiveException;
import com.vaadin.flow.signals.Signal;

import tools.jackson.databind.node.ObjectNode;

Expand All @@ -60,6 +64,7 @@ public abstract class AbstractGridMultiSelectionModel<T>
private final Map<Object, T> selected;
private final GridSelectionColumn selectionColumn;
private SelectAllCheckboxVisibility selectAllCheckBoxVisibility;
private Registration selectionBindingCleanup;

/**
* Constructor for passing a reference of the grid to this implementation.
Expand Down Expand Up @@ -101,6 +106,10 @@ private void insertSelectionColumn(Grid<T> grid,
@Override
protected void remove() {
super.remove();
if (selectionBindingCleanup != null) {
selectionBindingCleanup.remove();
selectionBindingCleanup = null;
}
deselectAll();
if (selectionColumn.getParent().map(getGrid()::equals).orElse(false)) {
getGrid().getElement().removeChild(selectionColumn.getElement());
Expand Down Expand Up @@ -294,6 +303,18 @@ public Element getElement() {
public Set<T> getSelectedItems() {
return AbstractGridMultiSelectionModel.this.getSelectedItems();
}

@Override
public SignalBinding<Set<T>> bindValue(Signal<Set<T>> valueSignal,
SerializableConsumer<Set<T>> writeCallback) {
if (selectionBindingCleanup != null) {
throw new BindingActiveException();
}
var result = GridSelectionSignalHelper.bindValue(getGrid(),
this, valueSignal, writeCallback);
selectionBindingCleanup = result.cleanup();
return result.signalBinding();
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
import com.vaadin.flow.data.selection.SingleSelectionEvent;
import com.vaadin.flow.data.selection.SingleSelectionListener;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.dom.SignalBinding;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.shared.Registration;
import com.vaadin.flow.signals.BindingActiveException;
import com.vaadin.flow.signals.Signal;

import tools.jackson.databind.node.ObjectNode;

Expand All @@ -46,6 +50,7 @@ public abstract class AbstractGridSingleSelectionModel<T> extends

private T selectedItem;
private boolean deselectAllowed = true;
private Registration selectionBindingCleanup;

/**
* Constructor for passing a reference of the grid to this implementation.
Expand Down Expand Up @@ -143,6 +148,18 @@ public Registration addValueChangeListener(
public Element getElement() {
return getGrid().getElement();
}

@Override
public SignalBinding<T> bindValue(Signal<T> valueSignal,
SerializableConsumer<T> writeCallback) {
if (selectionBindingCleanup != null) {
throw new BindingActiveException();
}
var result = GridSelectionSignalHelper.bindValue(getGrid(),
this, valueSignal, writeCallback);
selectionBindingCleanup = result.cleanup();
return result.signalBinding();
}
};
}

Expand Down Expand Up @@ -176,6 +193,10 @@ public void generateData(T item, ObjectNode jsonObject) {
@Override
protected void remove() {
super.remove();
if (selectionBindingCleanup != null) {
selectionBindingCleanup.remove();
selectionBindingCleanup = null;
}
deselectAll();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Copyright 2000-2026 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.grid;

import java.io.Serializable;
import java.util.Objects;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.HasValue;
import com.vaadin.flow.component.HasValueAndElement;
import com.vaadin.flow.dom.BindingContext;
import com.vaadin.flow.dom.SignalBinding;
import com.vaadin.flow.function.SerializableConsumer;
import com.vaadin.flow.shared.Registration;
import com.vaadin.flow.signals.Signal;

/**
* Shared helper for binding signals to grid selection model values.
* <p>
* For internal use only. May be renamed or removed in a future release.
*/
class GridSelectionSignalHelper {

/**
* Result of binding a signal to a selection model value.
*
* @param signalBinding
* the signal binding for registering change callbacks
* @param cleanup
* a registration to remove the effect and listener when the
* selection model is removed
* @param <V>
* the value type
*/
record BindResult<V>(SignalBinding<V> signalBinding,
Registration cleanup) implements Serializable {
}

/**
* Binds a signal to a {@link HasValueAndElement} target, keeping the value
* synchronized while the owner component is attached.
*
* @param owner
* the component that owns the effect lifecycle
* @param target
* the HasValueAndElement to bind to
* @param valueSignal
* the signal to bind
* @param writeCallback
* the callback to propagate value changes back, or {@code null}
* for a read-only binding
* @param <E>
* the value change event type
* @param <V>
* the value type
* @return the binding result containing the signal binding and a cleanup
* registration
*/
@SuppressWarnings("unchecked")
static <E extends HasValue.ValueChangeEvent<V>, V> BindResult<V> bindValue(
Component owner, HasValueAndElement<E, V> target,
Signal<V> valueSignal, SerializableConsumer<V> writeCallback) {
Objects.requireNonNull(valueSignal, "Signal cannot be null");

SignalBinding<V> binding = new SignalBinding<>();
boolean[] fromSignal = { false };
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests still pass if I remove this.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, the signal's own equality check prevented an actual infinite loop, so existing tests didn't cover the guard. Added now tests that explicitly count calls.

V[] previousValue = (V[]) new Object[] { valueSignal.peek() };

Registration effectReg = Signal.effect(owner, ctx -> {
V newValue = valueSignal.get();
V oldValue = previousValue[0];
try {
fromSignal[0] = true;
target.setValue(newValue);
} finally {
fromSignal[0] = false;
}
if (ctx.isInitialRun() || binding.hasCallbacks()) {
var bindingContext = new BindingContext<>(ctx.isInitialRun(),
ctx.isBackgroundChange(), oldValue, newValue,
target.getElement());
binding.setInitialContext(bindingContext);
if (binding.hasCallbacks()) {
binding.fireOnChange(bindingContext);
}
}
previousValue[0] = newValue;
});

Registration listenerReg = target.addValueChangeListener(event -> {
if (!fromSignal[0] && writeCallback != null) {
writeCallback.accept(target.getValue());
}
});

Registration cleanup = () -> {
effectReg.remove();
listenerReg.remove();
};

return new BindResult<>(binding, cleanup);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protected Stream<String> getExcludedPatterns() {
"com\\.vaadin\\.flow\\.component\\.treegrid\\.it\\..*",
"com\\.vaadin\\.flow\\.component\\.datepicker\\..*",
"com\\.vaadin\\.flow\\.component\\.grid\\.GridColumnOrderHelper.*",
"com\\.vaadin\\.flow\\.component\\.grid\\.GridSelectionSignalHelper.*",
"com\\.vaadin\\.flow\\.spring\\..*"));
}

Expand Down
Loading