diff --git a/CHANGELOG.md b/CHANGELOG.md index 6b5c81386b42..2a9773105e09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/docs/code-howtos/javafx.md b/docs/code-howtos/javafx.md index d179805bb105..858ee3dbfbda 100644 --- a/docs/code-howtos/javafx.md +++ b/docs/code-howtos/javafx.md @@ -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) diff --git a/jabgui/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java b/jabgui/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java index f3e7f3ba1fef..3faafcec0bc1 100644 --- a/jabgui/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java +++ b/jabgui/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java @@ -163,7 +163,8 @@ private void initialize() { ); new ViewModelListCellFactory() - .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) diff --git a/jabgui/src/main/java/org/jabref/gui/util/ViewModelListCellFactory.java b/jabgui/src/main/java/org/jabref/gui/util/ViewModelListCellFactory.java index 65f71ec27b39..5ad00b34fdcf 100644 --- a/jabgui/src/main/java/org/jabref/gui/util/ViewModelListCellFactory.java +++ b/jabgui/src/main/java/org/jabref/gui/util/ViewModelListCellFactory.java @@ -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 cell value +@NullMarked public class ViewModelListCellFactory implements Callback, ListCell> { private static final PseudoClass INVALID_PSEUDO_CLASS = PseudoClass.getPseudoClass("invalid"); - private Callback toText; - private Callback toGraphic; - private Callback toTooltip; - private BiConsumer toOnMouseClickedEvent; - private Supplier> styleClassSupplier; - private Callback toContextMenu; - private BiConsumer toOnDragDetected; - private BiConsumer toOnDragDropped; - private BiConsumer toOnDragEntered; - private BiConsumer toOnDragExited; - private BiConsumer toOnDragOver; - private BiConsumer toOnDragDone; + private @Nullable Callback toText; + private @Nullable Callback toGraphic; + private Callback, Callback> tooltipFactory = _ -> _ -> null; + private @Nullable BiConsumer toOnMouseClickedEvent; + private @Nullable Supplier> styleClassSupplier; + private @Nullable Callback toContextMenu; + private @Nullable BiConsumer toOnDragDetected; + private @Nullable BiConsumer toOnDragDropped; + private @Nullable BiConsumer toOnDragEntered; + private @Nullable BiConsumer toOnDragExited; + private @Nullable BiConsumer toOnDragOver; + private @Nullable BiConsumer toOnDragDone; private final Map>> pseudoClasses = new HashMap<>(); - private Callback validationStatusProperty; + private @Nullable Callback validationStatusProperty; public ViewModelListCellFactory withText(Callback toText) { this.toText = toText; return this; } - public ViewModelListCellFactory withGraphic(Callback toGraphic) { + public ViewModelListCellFactory withGraphic(Callback toGraphic) { this.toGraphic = toGraphic; return this; } - public ViewModelListCellFactory withIcon(Callback toIcon) { + public ViewModelListCellFactory withIcon(Callback toIcon) { this.toGraphic = viewModel -> { JabRefIcon icon = toIcon.call(viewModel); if (icon != null) { @@ -75,23 +78,38 @@ public ViewModelListCellFactory withIcon(Callback toIcon, Call return this; } - public ViewModelListCellFactory withStringTooltip(Callback toStringTooltip) { - this.toTooltip = viewModel -> { + public ViewModelListCellFactory withStringTooltip(Callback toStringTooltip) { + return withTooltip(viewModel -> { String tooltipText = toStringTooltip.call(viewModel); if (StringUtil.isNotBlank(tooltipText)) { return new Tooltip(tooltipText); } return null; - }; + }); + } + + public ViewModelListCellFactory withTooltip(Callback toTooltip) { + this.tooltipFactory = _ -> toTooltip; return this; } - public ViewModelListCellFactory withTooltip(Callback 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 withTooltipText(Callback> 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 withContextMenu(Callback toContextMenu) { + public ViewModelListCellFactory withContextMenu(Callback toContextMenu) { this.toContextMenu = toContextMenu; return this; } @@ -160,12 +178,13 @@ public void install(ListView listView) { } @Override - public ListCell call(ListView param) { + public ListCell call(@Nullable ListView param) { ListCell listCell = new ListCell<>() { final List subscriptions = new ArrayList<>(); + private final Callback 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 @@ -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 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 onDragDetected = toOnDragDetected; + if (onDragDetected != null) { + setOnDragDetected(event -> onDragDetected.accept(viewModel, event)); } - if (toOnDragDropped != null) { + BiConsumer 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) @@ -212,16 +232,19 @@ protected void updateItem(T item, boolean empty) { event.consume(); }); } - if (toOnDragEntered != null) { - setOnDragEntered(event -> toOnDragEntered.accept(viewModel, event)); + BiConsumer onDragEntered = toOnDragEntered; + if (onDragEntered != null) { + setOnDragEntered(event -> onDragEntered.accept(viewModel, event)); } - if (toOnDragExited != null) { - setOnDragExited(event -> toOnDragExited.accept(viewModel, event)); + BiConsumer onDragExited = toOnDragExited; + if (onDragExited != null) { + setOnDragExited(event -> onDragExited.accept(viewModel, event)); } - if (toOnDragOver != null) { + BiConsumer 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); @@ -230,8 +253,9 @@ protected void updateItem(T item, boolean empty) { event.consume(); }); } - if (toOnDragDone != null) { - setOnDragDone(event -> toOnDragDone.accept(viewModel, event)); + BiConsumer onDragDone = toOnDragDone; + if (onDragDone != null) { + setOnDragDone(event -> onDragDone.accept(viewModel, event)); } for (Map.Entry>> pseudoClassWithCondition : pseudoClasses.entrySet()) { ObservableValue condition = pseudoClassWithCondition.getValue().call(viewModel); diff --git a/jabgui/src/test/java/org/jabref/gui/util/ViewModelListCellFactoryTest.java b/jabgui/src/test/java/org/jabref/gui/util/ViewModelListCellFactoryTest.java new file mode 100644 index 000000000000..bf63588b5ef0 --- /dev/null +++ b/jabgui/src/test/java/org/jabref/gui/util/ViewModelListCellFactoryTest.java @@ -0,0 +1,174 @@ +package org.jabref.gui.util; + +import java.util.Optional; + +import javafx.beans.InvalidationListener; +import javafx.beans.property.ReadOnlyBooleanWrapper; +import javafx.beans.property.SimpleStringProperty; +import javafx.beans.property.StringProperty; +import javafx.scene.control.ListCell; +import javafx.scene.control.ListView; +import javafx.scene.control.Tooltip; + +import org.jabref.gui.testutils.JavaFxTest; + +import de.saxsys.mvvmfx.utils.validation.ValidationMessage; +import de.saxsys.mvvmfx.utils.validation.ValidationStatus; +import org.jspecify.annotations.NullMarked; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import org.mockito.ArgumentCaptor; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@NullMarked +class ViewModelListCellFactoryTest extends JavaFxTest { + + @Test + void defaultAndCallbackTooltipPathsHandleMissingText() { + interact(() -> { + ListView listView = new ListView<>(); + listView.getItems().setAll("file.pdf", "", "unknown"); + + ListCell defaultCell = new ViewModelListCellFactory().call(listView); + defaultCell.updateListView(listView); + defaultCell.updateIndex(0); + assertNull(defaultCell.getTooltip()); + + ListCell stringCell = new ViewModelListCellFactory() + .withStringTooltip(item -> "unknown".equals(item) ? null : item) + .call(listView); + stringCell.updateListView(listView); + stringCell.updateIndex(0); + assertEquals("file.pdf", stringCell.getTooltip().getText()); + stringCell.updateIndex(1); + assertNull(stringCell.getTooltip()); + stringCell.updateIndex(2); + assertNull(stringCell.getTooltip()); + + ListCell customCell = new ViewModelListCellFactory() + .withStringTooltip(item -> item) + .withTooltip(item -> item.isEmpty() ? null : new Tooltip("Custom " + item)) + .call(listView); + customCell.updateListView(listView); + customCell.updateIndex(0); + assertEquals("Custom file.pdf", customCell.getTooltip().getText()); + customCell.updateIndex(1); + assertNull(customCell.getTooltip()); + }); + } + + @Test + void tooltipTracksLinkAndDescriptionChanges() { + interact(() -> { + StringProperty link = spy(new SimpleStringProperty("original.pdf")); + StringProperty description = spy(new SimpleStringProperty("Attachment")); + ListView listView = new ListView<>(); + listView.getItems().add(link); + ListCell cell = new ViewModelListCellFactory() + .withTooltipText(item -> item.flatMap(path -> description.map(text -> text + " (" + path + ")"))) + .call(listView); + cell.updateListView(listView); + cell.updateIndex(0); + + assertEquals("Attachment (original.pdf)", cell.getTooltip().getText()); + + link.set("renamed.pdf"); + assertEquals("Attachment (renamed.pdf)", cell.getTooltip().getText()); + + link.set("directory/renamed.pdf"); + assertEquals("Attachment (directory/renamed.pdf)", cell.getTooltip().getText()); + + description.set("Updated attachment"); + assertEquals("Updated attachment (directory/renamed.pdf)", cell.getTooltip().getText()); + + cell.updateIndex(-1); + assertAllListenersRemoved(link); + assertAllListenersRemoved(description); + }); + } + + @ParameterizedTest + @ValueSource(ints = {-1, 1}) + void detachesTooltipListenersWhenCellIsClearedOrReused(int nextIndex) { + interact(() -> { + StringProperty first = spy(new SimpleStringProperty("first.pdf")); + StringProperty second = new SimpleStringProperty("second.pdf"); + ListView listView = new ListView<>(); + listView.getItems().setAll(first, second); + ListCell cell = new ViewModelListCellFactory() + .withTooltipText(item -> item) + .call(listView); + cell.updateListView(listView); + cell.updateIndex(0); + Tooltip oldTooltip = cell.getTooltip(); + assertEquals("first.pdf", oldTooltip.getText()); + + ArgumentCaptor listener = ArgumentCaptor.forClass(InvalidationListener.class); + verify(first).addListener(listener.capture()); + + cell.updateIndex(nextIndex); + + verify(first).removeListener(listener.getValue()); + first.set("stale.pdf"); + if (nextIndex == -1) { + assertNull(cell.getTooltip()); + } else { + assertSame(oldTooltip, cell.getTooltip()); + assertEquals("second.pdf", cell.getTooltip().getText()); + second.set("updated.pdf"); + assertEquals("updated.pdf", cell.getTooltip().getText()); + } + + cell.updateIndex(-1); + verify(first).removeListener(listener.getValue()); + + cell.updateIndex(0); + assertSame(oldTooltip, cell.getTooltip()); + assertEquals("stale.pdf", cell.getTooltip().getText()); + + cell.updateIndex(-1); + assertAllListenersRemoved(first); + }); + } + + @Test + void validationTooltipOverridesBoundTextWithoutRetainingOldItemListeners() { + interact(() -> { + StringProperty link = spy(new SimpleStringProperty("file.pdf")); + ValidationStatus validationStatus = mock(ValidationStatus.class); + when(validationStatus.getHighestMessage()).thenReturn(Optional.of(ValidationMessage.warning("Missing file"))); + when(validationStatus.validProperty()).thenReturn(new ReadOnlyBooleanWrapper(false).getReadOnlyProperty()); + + ListView listView = new ListView<>(); + listView.getItems().add(link); + ListCell cell = new ViewModelListCellFactory() + .withTooltipText(item -> item) + .withValidation(_ -> validationStatus) + .call(listView); + cell.updateListView(listView); + cell.updateIndex(0); + assertEquals("Missing file", cell.getTooltip().getText()); + + cell.updateIndex(-1); + assertNull(cell.getTooltip()); + assertAllListenersRemoved(link); + }); + } + + private static void assertAllListenersRemoved(StringProperty property) { + ArgumentCaptor added = ArgumentCaptor.forClass(InvalidationListener.class); + ArgumentCaptor removed = ArgumentCaptor.forClass(InvalidationListener.class); + verify(property, atLeastOnce()).addListener(added.capture()); + verify(property, atLeastOnce()).removeListener(removed.capture()); + assertEquals(added.getAllValues(), removed.getAllValues()); + } +}