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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where "Regenerate (custom)" in the AI summary ignored the chosen summarization algorithm. [#17216](https://github.com/JabRef/jabref/issues/17216)
- We fixed AI summary regeneration restoring an outdated summary after a new generation started. [#17236](https://github.com/JabRef/jabref/pull/17236)
- We fixed an issue where back navigation selected a deleted entry instead of skipping it. [#17353](https://github.com/JabRef/jabref/issues/17353)
- We fixed the linked file tooltip showing the old file name after renaming or moving the file. [#9277](https://github.com/JabRef/jabref/issues/9277)

## [6.0-beta.1] - 2026-09-21

Expand Down
5 changes: 5 additions & 0 deletions docs/code-howtos/javafx.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ DialogService dialogService = Injector.instantiateModelOrService(DialogService.c
JabRef makes heavy use of Properties and Bindings. These are wrappers around Observables. A good explanation on the concept can be found here:
[JavaFX Bindings and Properties](https://web.archive.org/web/20240921154452/https://edencoding.com/javafx-properties-and-binding-a-complete-guide/)

For observable list-cell tooltip text, use `ViewModelListCellFactory.withTooltipText` with a property or lazy observable.
Each cell creates one tooltip and binds its text once through the cell's item property using `flatMap`.
The binding follows the current item and detaches its listeners from the previous item or when the cell becomes empty.
Use `withStringTooltip` for static text and `withTooltip` for custom tooltips whose resources are managed by the caller.

## Features missing in JavaFX

* bidirectional binding in FXML, see [official feature request](https://bugs.openjdk.java.net/browse/JDK-8090665)
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ private void initialize() {
);

new ViewModelListCellFactory<LinkedFileViewModel>()
.withStringTooltip(LinkedFileViewModel::getDescriptionAndLink)
.withTooltipText(linkedFile -> linkedFile.linkProperty().flatMap(_ ->
linkedFile.descriptionProperty().map(_ -> linkedFile.getDescriptionAndLink())))
.withPseudoClass(PseudoClass.getPseudoClass("auto-found"), LinkedFileViewModel::isAutomaticallyFoundProperty)
.withGraphic(this::createFileDisplay)
.withOnMouseClickedEvent(this::handleItemMouseClick)
Expand Down
104 changes: 64 additions & 40 deletions jabgui/src/main/java/org/jabref/gui/util/ViewModelListCellFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,40 +26,43 @@

import com.tobiasdiez.easybind.Subscription;
import de.saxsys.mvvmfx.utils.validation.ValidationStatus;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

/// Constructs a [ListCell] based on the view model of the row and a bunch of specified converter methods.
///
/// @param <T> cell value
@NullMarked
public class ViewModelListCellFactory<T> implements Callback<ListView<T>, ListCell<T>> {

private static final PseudoClass INVALID_PSEUDO_CLASS = PseudoClass.getPseudoClass("invalid");

private Callback<T, String> toText;
private Callback<T, Node> toGraphic;
private Callback<T, Tooltip> toTooltip;
private BiConsumer<T, ? super MouseEvent> toOnMouseClickedEvent;
private Supplier<List<String>> styleClassSupplier;
private Callback<T, ContextMenu> toContextMenu;
private BiConsumer<T, ? super MouseEvent> toOnDragDetected;
private BiConsumer<T, ? super DragEvent> toOnDragDropped;
private BiConsumer<T, ? super DragEvent> toOnDragEntered;
private BiConsumer<T, ? super DragEvent> toOnDragExited;
private BiConsumer<T, ? super DragEvent> toOnDragOver;
private BiConsumer<T, ? super DragEvent> toOnDragDone;
private @Nullable Callback<T, String> toText;
private @Nullable Callback<T, @Nullable Node> toGraphic;
private Callback<ListCell<T>, Callback<T, @Nullable Tooltip>> tooltipFactory = _ -> _ -> null;
private @Nullable BiConsumer<T, ? super MouseEvent> toOnMouseClickedEvent;
private @Nullable Supplier<List<String>> styleClassSupplier;
private @Nullable Callback<T, @Nullable ContextMenu> toContextMenu;
private @Nullable BiConsumer<T, ? super MouseEvent> toOnDragDetected;
private @Nullable BiConsumer<T, ? super DragEvent> toOnDragDropped;
private @Nullable BiConsumer<T, ? super DragEvent> toOnDragEntered;
private @Nullable BiConsumer<T, ? super DragEvent> toOnDragExited;
private @Nullable BiConsumer<T, ? super DragEvent> toOnDragOver;
private @Nullable BiConsumer<T, ? super DragEvent> toOnDragDone;
private final Map<PseudoClass, Callback<T, ObservableValue<Boolean>>> pseudoClasses = new HashMap<>();
private Callback<T, ValidationStatus> validationStatusProperty;
private @Nullable Callback<T, ValidationStatus> validationStatusProperty;

public ViewModelListCellFactory<T> withText(Callback<T, String> toText) {
this.toText = toText;
return this;
}

public ViewModelListCellFactory<T> withGraphic(Callback<T, Node> toGraphic) {
public ViewModelListCellFactory<T> withGraphic(Callback<T, @Nullable Node> toGraphic) {
this.toGraphic = toGraphic;
return this;
}

public ViewModelListCellFactory<T> withIcon(Callback<T, JabRefIcon> toIcon) {
public ViewModelListCellFactory<T> withIcon(Callback<T, @Nullable JabRefIcon> toIcon) {
this.toGraphic = viewModel -> {
JabRefIcon icon = toIcon.call(viewModel);
if (icon != null) {
Expand All @@ -75,23 +78,38 @@ public ViewModelListCellFactory<T> withIcon(Callback<T, JabRefIcon> toIcon, Call
return this;
}

public ViewModelListCellFactory<T> withStringTooltip(Callback<T, String> toStringTooltip) {
this.toTooltip = viewModel -> {
public ViewModelListCellFactory<T> withStringTooltip(Callback<T, @Nullable String> toStringTooltip) {
return withTooltip(viewModel -> {
String tooltipText = toStringTooltip.call(viewModel);
if (StringUtil.isNotBlank(tooltipText)) {
return new Tooltip(tooltipText);
}
return null;
};
});
}

public ViewModelListCellFactory<T> withTooltip(Callback<T, @Nullable Tooltip> toTooltip) {
this.tooltipFactory = _ -> toTooltip;
return this;
}

public ViewModelListCellFactory<T> withTooltip(Callback<T, Tooltip> toTooltip) {
this.toTooltip = toTooltip;
/// Creates one tooltip per cell and binds its text to the current item's observable value.
///
/// The callback is called when the cell receives an item. It must return a property or a lazy
/// observable that releases its listeners when the cell changes items or becomes empty.
///
/// @param toTooltipText provides observable tooltip text for an item
/// @return this factory
public ViewModelListCellFactory<T> withTooltipText(Callback<T, ObservableValue<String>> toTooltipText) {
this.tooltipFactory = cell -> {
Tooltip tooltip = new Tooltip();
tooltip.textProperty().bind(cell.itemProperty().flatMap(toTooltipText::call).when(cell.emptyProperty().not()));
return _ -> tooltip;
};
return this;
}

public ViewModelListCellFactory<T> withContextMenu(Callback<T, ContextMenu> toContextMenu) {
public ViewModelListCellFactory<T> withContextMenu(Callback<T, @Nullable ContextMenu> toContextMenu) {
this.toContextMenu = toContextMenu;
return this;
}
Expand Down Expand Up @@ -160,12 +178,13 @@ public void install(ListView<T> listView) {
}

@Override
public ListCell<T> call(ListView<T> param) {
public ListCell<T> call(@Nullable ListView<T> param) {
ListCell<T> listCell = new ListCell<>() {
final List<Subscription> subscriptions = new ArrayList<>();
private final Callback<T, @Nullable Tooltip> toTooltip = tooltipFactory.call(this);

@Override
protected void updateItem(T item, boolean empty) {
protected void updateItem(@Nullable T item, boolean empty) {
super.updateItem(item, empty);

// Remove previous subscriptions
Expand All @@ -186,22 +205,23 @@ protected void updateItem(T item, boolean empty) {
if (toGraphic != null) {
setGraphic(toGraphic.call(viewModel));
}
if (toOnMouseClickedEvent != null) {
setOnMouseClicked(event -> toOnMouseClickedEvent.accept(viewModel, event));
}
if (toTooltip != null) {
setTooltip(toTooltip.call(viewModel));
BiConsumer<T, ? super MouseEvent> onMouseClickedEvent = toOnMouseClickedEvent;
if (onMouseClickedEvent != null) {
setOnMouseClicked(event -> onMouseClickedEvent.accept(viewModel, event));
}
setTooltip(toTooltip.call(viewModel));
if (toContextMenu != null) {
setContextMenu(toContextMenu.call(viewModel));
}
if (toOnDragDetected != null) {
setOnDragDetected(event -> toOnDragDetected.accept(viewModel, event));
BiConsumer<T, ? super MouseEvent> onDragDetected = toOnDragDetected;
if (onDragDetected != null) {
setOnDragDetected(event -> onDragDetected.accept(viewModel, event));
}
if (toOnDragDropped != null) {
BiConsumer<T, ? super DragEvent> onDragDropped = toOnDragDropped;
if (onDragDropped != null) {
setOnDragDropped(event -> {
// For reordering we need to accept the event so that the local handlers get it
toOnDragDropped.accept(viewModel, event);
onDragDropped.accept(viewModel, event);
Node parent = getParent();
if (parent != null) {
// The parent is the box for dropping from external files; that should be used as target (and not this cell)
Expand All @@ -212,16 +232,19 @@ protected void updateItem(T item, boolean empty) {
event.consume();
});
}
if (toOnDragEntered != null) {
setOnDragEntered(event -> toOnDragEntered.accept(viewModel, event));
BiConsumer<T, ? super DragEvent> onDragEntered = toOnDragEntered;
if (onDragEntered != null) {
setOnDragEntered(event -> onDragEntered.accept(viewModel, event));
}
if (toOnDragExited != null) {
setOnDragExited(event -> toOnDragExited.accept(viewModel, event));
BiConsumer<T, ? super DragEvent> onDragExited = toOnDragExited;
if (onDragExited != null) {
setOnDragExited(event -> onDragExited.accept(viewModel, event));
}
if (toOnDragOver != null) {
BiConsumer<T, ? super DragEvent> onDragOver = toOnDragOver;
if (onDragOver != null) {
setOnDragOver(event -> {
// For reordering we need to accept the event so that the local handlers get it
toOnDragOver.accept(viewModel, event);
onDragOver.accept(viewModel, event);
Node parent = getParent();
if (parent != null) {
DragEvent forwarded = event.copyFor(parent, parent);
Expand All @@ -230,8 +253,9 @@ protected void updateItem(T item, boolean empty) {
event.consume();
});
}
if (toOnDragDone != null) {
setOnDragDone(event -> toOnDragDone.accept(viewModel, event));
BiConsumer<T, ? super DragEvent> onDragDone = toOnDragDone;
if (onDragDone != null) {
setOnDragDone(event -> onDragDone.accept(viewModel, event));
}
for (Map.Entry<PseudoClass, Callback<T, ObservableValue<Boolean>>> pseudoClassWithCondition : pseudoClasses.entrySet()) {
ObservableValue<Boolean> condition = pseudoClassWithCondition.getValue().call(viewModel);
Expand Down
Loading
Loading