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 @@ -42,6 +42,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
- We fixed an issue where JabRef showed an "Executor has been shut down" error on Windows after resuming from suspend. [#17144](https://github.com/JabRef/jabref/pull/17144)
- We fixed the BibTeX source dialog background in the Citation Relations tab for dark themes. [#13599](https://github.com/JabRef/jabref/issues/13599)
- We fixed unreadable linked-file paths and indistinguishable selection backgrounds in the entry editor when using dark mode. [#17364](https://github.com/JabRef/jabref/issues/17364)
- 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)
- We fixed an issue where Backspace could not be registered as a keyboard shortcut. [#17241](https://github.com/JabRef/jabref/issues/17241)
- We re-enabled the [Grobid](https://github.com/grobidOrg/grobid) citation fetcher again. It is now reachable via https. [#16668](https://github.com/JabRef/jabref/issues/16668)
- We fixed an issue where the AI chat lost its scroll position when switching back to an entry. [#17172](https://github.com/JabRef/jabref/pull/17172)
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 @@ -159,7 +159,7 @@

#### View - FXML

The view consists a FXML file `MyDialog.fxml` which defines the structure and the layout of the UI. Moreover, the FXML file may be accompanied by a style file that should have the same name as the FXML file but with a `css` ending, e.g., `MyDialog.css`. It is recommended to use a graphical design tools like [SceneBuilder](http://gluonhq.com/labs/scene-builder/) to edit the FXML file. The tool [Scenic View](https://github.com/JonathanGiles/scenic-view) is very helpful in debugging styling issues.

Check failure on line 162 in docs/code-howtos/javafx.md

View workflow job for this annotation

GitHub Actions / Spellling

TextLint [write-good]

"very" is a weasel word | 162:460

Node ids exist for the walkthrough. A walkthrough step finds the control it highlights by id, so:

Expand All @@ -174,8 +174,8 @@

What remains in JabRef is `jabgui/src/main/resources/org/jabref/gui/theme/internal/jabref-base.css`, loaded with every theme. It holds the utility classes for padding, gaps, alignment, font size and color (`padding-12`, `gap-8`, `align-center-left`, `h3`, `text-accent`) and the structure of the shared controls and views: borders, radii, paddings and layout of buttons, tabs, the entry editor, the main table and so on, all expressed in `-color-*` tokens so that a theme only has to pick colors. The drivers, from <https://github.com/JabRef/jabref/issues/16042>, <https://github.com/JabRef/jabref/issues/16787> and <https://github.com/JabRef/jabref/issues/15721>:

* Do not introduce a CSS class. Every class is a lookup for the next reader, and one used by a single view is usually a padding or a font size a utility class already offers. When the utilities cannot express what a view needs, one class named after the view (`welcome-main-container`) is the trade-off between few classes and a UI that still looks right; a second step on the spacing scale is not.

Check failure on line 177 in docs/code-howtos/javafx.md

View workflow job for this annotation

GitHub Actions / Spellling

TextLint [write-good]

"few" is a weasel word | 177:312
* The utility classes form one fixed scale in `em`, so all views share the same few distances and grow with the user's font size. Do not add `padding-10` because one dialog looked better with it.

Check failure on line 178 in docs/code-howtos/javafx.md

View workflow job for this annotation

GitHub Actions / Spellling

TextLint [write-good]

"few" is a weasel word | 178:81
* The spacing between the children of a `VBox`, `HBox` or `GridPane` goes into the constructor: `new VBox(12)`, `new GridPane(24, 24)`.
* No inline styles: no `setStyle(..)`, no `styleProperty()` binding, no `-fx-*` string in Java. An inline style beats every stylesheet, so a theme could not change it.
* Colors only through the `-color-*` tokens, never as literals, so every theme keeps working.
Expand Down Expand Up @@ -217,7 +217,7 @@

## JavaFX Dialogs

All dialogs should be displayed to the user via `DialogService` interface methods. `DialogService` provides methods to display various dialogs (including custom ones) to the user. It also ensures the displayed dialog opens on the correct window via `initOwner()` (for cases where the user has multiple screens). The following code snippet demonstrates how a custom dialog is displayed to the user:

Check failure on line 220 in docs/code-howtos/javafx.md

View workflow job for this annotation

GitHub Actions / Spellling

TextLint [write-good]

"various" is a weasel word | 220:128

```java
dialogService.showCustomDialog(new DocumentViewerView());
Expand All @@ -234,6 +234,11 @@
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