Skip to content

Commit

Permalink
Fix JabRef#2868 - keep source groups selected after drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Ka0o0 committed Dec 10, 2019
1 parent 8674e70 commit 4ff68b1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 26 additions & 2 deletions src/main/java/org/jabref/gui/groups/GroupTreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -232,14 +233,18 @@ public void initialize() {

if (dragboard.hasContent(DragAndDropDataFormats.GROUP)) {
List<String> pathToSources = (List<String>) dragboard.getContent(DragAndDropDataFormats.GROUP);
List<GroupNodeViewModel> changedGroups = new LinkedList<>();
for (String pathToSource : pathToSources) {
Optional<GroupNodeViewModel> 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()) {
Expand Down Expand Up @@ -268,8 +273,21 @@ private void updateSelection(List<TreeItem<GroupNodeViewModel>> 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<GroupNodeViewModel> parent = treeItem.getParent();
while (parent != null) {
parent.setExpanded(true);
parent = parent.getParent();
}
}
groupTree.getSelectionModel().select(treeItem);
});
}

private Optional<TreeItem<GroupNodeViewModel>> getTreeItemByValue(GroupNodeViewModel value) {
Expand All @@ -281,7 +299,13 @@ private Optional<TreeItem<GroupNodeViewModel>> getTreeItemByValue(TreeItem<Group
if (root.getValue().equals(value)) {
return Optional.of(root);
}
return root.getChildren().stream().filter(child -> getTreeItemByValue(child, value).isPresent()).findFirst();

Optional<TreeItem<GroupNodeViewModel>> 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) {
Expand Down

0 comments on commit 4ff68b1

Please sign in to comment.