From fd5264ba80a236c787bd3d4f68788c7cc7b8353c Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Mon, 31 Jul 2023 22:05:47 +0200 Subject: [PATCH] Introduced withEventFilter method --- .../org/jabref/gui/groups/GroupTreeView.java | 4 +-- .../util/ViewModelTreeTableRowFactory.java | 28 +++++++++---------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupTreeView.java b/src/main/java/org/jabref/gui/groups/GroupTreeView.java index fc236c43bf6..e34fefb9de4 100644 --- a/src/main/java/org/jabref/gui/groups/GroupTreeView.java +++ b/src/main/java/org/jabref/gui/groups/GroupTreeView.java @@ -224,13 +224,13 @@ private void initialize() { new ViewModelTreeTableRowFactory() .withContextMenu(this::createContextMenuForGroup) - .withOnMousePressedEvent((row, event) -> { + .withEventFilter(MouseEvent.MOUSE_PRESSED, (row, event) -> { if (event.getTarget() instanceof StackPane pane) { if (pane.getStyleClass().contains("arrow") || pane.getStyleClass().contains("tree-disclosure-node")) { event.consume(); } } - }, true) + }) .withCustomInitializer(row -> { // Remove disclosure node since we display custom version in separate column // Simply setting to null is not enough since it would be replaced by the default node on every change diff --git a/src/main/java/org/jabref/gui/util/ViewModelTreeTableRowFactory.java b/src/main/java/org/jabref/gui/util/ViewModelTreeTableRowFactory.java index 03b4f4651b5..acb4ee65fd0 100644 --- a/src/main/java/org/jabref/gui/util/ViewModelTreeTableRowFactory.java +++ b/src/main/java/org/jabref/gui/util/ViewModelTreeTableRowFactory.java @@ -10,6 +10,8 @@ import javafx.beans.value.ObservableValue; import javafx.css.PseudoClass; +import javafx.event.Event; +import javafx.event.EventType; import javafx.geometry.Bounds; import javafx.geometry.Point2D; import javafx.scene.control.ContextMenu; @@ -27,11 +29,6 @@ public class ViewModelTreeTableRowFactory implements Callback, TreeTableRow> { private BiConsumer onMouseClickedEvent; - - // True if event capture should be at capture phase via an event filter, otherwise use default Node method to setup - // event handler (bubbling phase) - private boolean onMousePressedEventCapturePhase; - private BiConsumer onMousePressedEvent; private Consumer> toCustomInitializer; private Function contextMenuFactory; @@ -41,6 +38,7 @@ public class ViewModelTreeTableRowFactory implements Callback, S, ? super DragEvent> toOnDragExited; private TriConsumer, S, ? super DragEvent> toOnDragOver; private TriConsumer, S, ? super MouseDragEvent> toOnMouseDragEntered; + private final Map, BiConsumer> eventFilters = new HashMap<>(); private final Map, ObservableValue>> pseudoClasses = new HashMap<>(); public ViewModelTreeTableRowFactory withOnMouseClickedEvent(BiConsumer event) { @@ -49,12 +47,7 @@ public ViewModelTreeTableRowFactory withOnMouseClickedEvent(BiConsumer withOnMousePressedEvent(BiConsumer event) { - return withOnMousePressedEvent(event, false); - } - - public ViewModelTreeTableRowFactory withOnMousePressedEvent(BiConsumer event, boolean capturePhase) { this.onMousePressedEvent = event; - this.onMousePressedEventCapturePhase = capturePhase; return this; } @@ -124,6 +117,11 @@ public ViewModelTreeTableRowFactory withPseudoClass(PseudoClass pseudoClass, return this; } + public ViewModelTreeTableRowFactory withEventFilter(EventType event, BiConsumer toCondition) { + this.eventFilters.putIfAbsent(event, toCondition); + return this; + } + public void install(TreeTableView table) { table.setRowFactory(this); } @@ -175,11 +173,7 @@ protected void updateItem(S row, boolean empty) { } if (onMousePressedEvent != null) { - if (onMousePressedEventCapturePhase) { - addEventFilter(MouseEvent.MOUSE_PRESSED, event -> onMousePressedEvent.accept(getItem(), event)); - } else { - setOnMousePressed(event -> onMousePressedEvent.accept(getItem(), event)); - } + setOnMousePressed(event -> onMousePressedEvent.accept(getItem(), event)); } if (toCustomInitializer != null) { @@ -206,6 +200,10 @@ protected void updateItem(S row, boolean empty) { setOnMouseDragEntered(event -> toOnMouseDragEntered.accept(this, getItem(), event)); } + for (Map.Entry, BiConsumer> eventFilter : eventFilters.entrySet()) { + addEventFilter(eventFilter.getKey(), event -> eventFilter.getValue().accept(getItem(), event)); + } + for (Map.Entry, ObservableValue>> pseudoClassWithCondition : pseudoClasses.entrySet()) { ObservableValue condition = pseudoClassWithCondition.getValue().call(this); subscriptions.add(BindingsHelper.includePseudoClassWhen(