Skip to content

Commit

Permalink
Introduced withEventFilter method
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Jul 31, 2023
1 parent aa701c4 commit fd5264b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/groups/GroupTreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,13 @@ private void initialize() {

new ViewModelTreeTableRowFactory<GroupNodeViewModel>()
.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
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/jabref/gui/util/ViewModelTreeTableRowFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,11 +29,6 @@

public class ViewModelTreeTableRowFactory<S> implements Callback<TreeTableView<S>, TreeTableRow<S>> {
private BiConsumer<S, ? super MouseEvent> 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<S, ? super MouseEvent> onMousePressedEvent;
private Consumer<TreeTableRow<S>> toCustomInitializer;
private Function<S, ContextMenu> contextMenuFactory;
Expand All @@ -41,6 +38,7 @@ public class ViewModelTreeTableRowFactory<S> implements Callback<TreeTableView<S
private TriConsumer<TreeTableRow<S>, S, ? super DragEvent> toOnDragExited;
private TriConsumer<TreeTableRow<S>, S, ? super DragEvent> toOnDragOver;
private TriConsumer<TreeTableRow<S>, S, ? super MouseDragEvent> toOnMouseDragEntered;
private final Map<EventType<?>, BiConsumer<S, ? super Event>> eventFilters = new HashMap<>();
private final Map<PseudoClass, Callback<TreeTableRow<S>, ObservableValue<Boolean>>> pseudoClasses = new HashMap<>();

public ViewModelTreeTableRowFactory<S> withOnMouseClickedEvent(BiConsumer<S, ? super MouseEvent> event) {
Expand All @@ -49,12 +47,7 @@ public ViewModelTreeTableRowFactory<S> withOnMouseClickedEvent(BiConsumer<S, ? s
}

public ViewModelTreeTableRowFactory<S> withOnMousePressedEvent(BiConsumer<S, ? super MouseEvent> event) {
return withOnMousePressedEvent(event, false);
}

public ViewModelTreeTableRowFactory<S> withOnMousePressedEvent(BiConsumer<S, ? super MouseEvent> event, boolean capturePhase) {
this.onMousePressedEvent = event;
this.onMousePressedEventCapturePhase = capturePhase;
return this;
}

Expand Down Expand Up @@ -124,6 +117,11 @@ public ViewModelTreeTableRowFactory<S> withPseudoClass(PseudoClass pseudoClass,
return this;
}

public ViewModelTreeTableRowFactory<S> withEventFilter(EventType<?> event, BiConsumer<S, ? super Event> toCondition) {
this.eventFilters.putIfAbsent(event, toCondition);
return this;
}

public void install(TreeTableView<S> table) {
table.setRowFactory(this);
}
Expand Down Expand Up @@ -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) {
Expand All @@ -206,6 +200,10 @@ protected void updateItem(S row, boolean empty) {
setOnMouseDragEntered(event -> toOnMouseDragEntered.accept(this, getItem(), event));
}

for (Map.Entry<EventType<?>, BiConsumer<S, ? super Event>> eventFilter : eventFilters.entrySet()) {
addEventFilter(eventFilter.getKey(), event -> eventFilter.getValue().accept(getItem(), event));
}

for (Map.Entry<PseudoClass, Callback<TreeTableRow<S>, ObservableValue<Boolean>>> pseudoClassWithCondition : pseudoClasses.entrySet()) {
ObservableValue<Boolean> condition = pseudoClassWithCondition.getValue().call(this);
subscriptions.add(BindingsHelper.includePseudoClassWhen(
Expand Down

0 comments on commit fd5264b

Please sign in to comment.