From 4ff68b1e853dc6f3c6c26c53ee60e4f9e2344a95 Mon Sep 17 00:00:00 2001 From: Kai Takac Date: Wed, 11 Dec 2019 00:00:12 +0100 Subject: [PATCH] Fix #2868 - keep source groups selected after drag and drop --- CHANGELOG.md | 1 + .../org/jabref/gui/groups/GroupTreeView.java | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e62873f5eedf..2e296b6fad7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -71,6 +71,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We fixed an issue where the side pane was not remembering its position. [#5615](https://github.com/JabRef/jabref/issues/5615) - We fixed an issue where JabRef could not interact with [Oracle XE](https://www.oracle.com/de/database/technologies/appdev/xe.html) in the [shared SQL database setup](https://docs.jabref.org/collaborative-work/sqldatabase). - We fixed an issue where the toolbar icons were hidden on smaller screens. +- We fixed a bug where the selection of groups was lost after drag and drop.[#2868](https://github.com/JabRef/jabref/issues/2868) ### Removed diff --git a/src/main/java/org/jabref/gui/groups/GroupTreeView.java b/src/main/java/org/jabref/gui/groups/GroupTreeView.java index ddfbf0434e13..d6cf0f29f0bc 100644 --- a/src/main/java/org/jabref/gui/groups/GroupTreeView.java +++ b/src/main/java/org/jabref/gui/groups/GroupTreeView.java @@ -4,6 +4,7 @@ import java.lang.reflect.Method; import java.time.Duration; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -232,14 +233,18 @@ public void initialize() { if (dragboard.hasContent(DragAndDropDataFormats.GROUP)) { List pathToSources = (List) dragboard.getContent(DragAndDropDataFormats.GROUP); + List changedGroups = new LinkedList<>(); for (String pathToSource : pathToSources) { Optional source = viewModel.rootGroupProperty().get() .getChildByPath(pathToSource); if (source.isPresent()) { source.get().draggedOn(row.getItem(), ControlHelper.getDroppingMouseLocation(row, event)); + changedGroups.add(source.get()); success = true; } } + groupTree.getSelectionModel().clearSelection(); + changedGroups.forEach(value -> selectNode(value, true)); } if (localDragboard.hasBibEntries()) { @@ -268,8 +273,21 @@ private void updateSelection(List> newSelectedGroup } private void selectNode(GroupNodeViewModel value) { + selectNode(value, false); + } + + private void selectNode(GroupNodeViewModel value, boolean expandParents) { getTreeItemByValue(value) - .ifPresent(treeItem -> groupTree.getSelectionModel().select(treeItem)); + .ifPresent(treeItem -> { + if (expandParents) { + TreeItem parent = treeItem.getParent(); + while (parent != null) { + parent.setExpanded(true); + parent = parent.getParent(); + } + } + groupTree.getSelectionModel().select(treeItem); + }); } private Optional> getTreeItemByValue(GroupNodeViewModel value) { @@ -281,7 +299,13 @@ private Optional> getTreeItemByValue(TreeItem getTreeItemByValue(child, value).isPresent()).findFirst(); + + Optional> node = Optional.empty(); + for (int i = 0; i < root.getChildren().size() && node.isEmpty(); i++) { + node = getTreeItemByValue(root.getChildren().get(i), value); + } + + return node; } private ContextMenu createContextMenuForGroup(GroupNodeViewModel group) {