Skip to content

Commit

Permalink
Merge pull request #5731 from Ka0o0/fix-2868-keep-focus-on-proper-group
Browse files Browse the repository at this point in the history
Fix #2868 - keep source groups selected after drag and drop
  • Loading branch information
Siedlerchr authored Dec 12, 2019
2 parents c3b7ee7 + 70c3c40 commit 3235780
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- 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 an issue where renaming referenced files for bib entries with long titles was not possible. [#5603](https://github.com/JabRef/jabref/issues/5603)
- 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
31 changes: 29 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,16 @@ 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 (TreeItem<GroupNodeViewModel> child : root.getChildren()) {
node = getTreeItemByValue(child, value);
if (node.isPresent()) {
break;
}
}

return node;
}

private ContextMenu createContextMenuForGroup(GroupNodeViewModel group) {
Expand Down

0 comments on commit 3235780

Please sign in to comment.