From d7ebc544c4cbbfde8303d4f3bbba28a475a23487 Mon Sep 17 00:00:00 2001 From: William Yao Date: Sat, 29 Oct 2022 00:18:07 +0800 Subject: [PATCH] Fix #9281 by update selectedGroups --- .../jabref/gui/groups/GroupTreeViewModel.java | 68 +++++++++++-------- 1 file changed, 39 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java b/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java index 535f763e939..0ebdd7b7dc8 100644 --- a/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java @@ -399,41 +399,45 @@ public void removeSubgroups(GroupNodeViewModel group) { public void removeGroupKeepSubgroups(GroupNodeViewModel group) { boolean confirmed; - if (selectedGroups.contains(group)) { - if (selectedGroups.size() <= 1) { + //check whether the group is in selectedGroups, if not, we clear all selected group and add the current group into it by default + if (!selectedGroups.contains(group)) { + selectedGroups.clear(); + selectedGroups.add(group); + } - confirmed = dialogService.showConfirmationDialogAndWait( - Localization.lang("Remove group"), - Localization.lang("Remove group \"%0\" and keep its subgroups?", group.getDisplayName()), - Localization.lang("Remove")); - } else { - confirmed = dialogService.showConfirmationDialogAndWait( - Localization.lang("Remove groups"), - Localization.lang("Remove all selected groups and keep their subgroups?"), - Localization.lang("Remove all")); - } + if (selectedGroups.size() <= 1) { - if (confirmed) { - // TODO: Add undo - // final UndoableAddOrRemoveGroup undo = new UndoableAddOrRemoveGroup(groupsRoot, node, UndoableAddOrRemoveGroup.REMOVE_NODE_KEEP_CHILDREN); - // panel.getUndoManager().addEdit(undo); + confirmed = dialogService.showConfirmationDialogAndWait( + Localization.lang("Remove group"), + Localization.lang("Remove group \"%0\" and keep its subgroups?", group.getDisplayName()), + Localization.lang("Remove")); + } else { + confirmed = dialogService.showConfirmationDialogAndWait( + Localization.lang("Remove groups"), + Localization.lang("Remove all selected groups and keep their subgroups?"), + Localization.lang("Remove all")); + } + + if (confirmed) { + // TODO: Add undo + // final UndoableAddOrRemoveGroup undo = new UndoableAddOrRemoveGroup(groupsRoot, node, UndoableAddOrRemoveGroup.REMOVE_NODE_KEEP_CHILDREN); + // panel.getUndoManager().addEdit(undo); - List selectedGroupNodes = new ArrayList<>(selectedGroups); - selectedGroupNodes.forEach(eachNode -> { - GroupTreeNode groupNode = eachNode.getGroupNode(); + List selectedGroupNodes = new ArrayList<>(selectedGroups); + selectedGroupNodes.forEach(eachNode -> { + GroupTreeNode groupNode = eachNode.getGroupNode(); - groupNode.getParent() - .ifPresent(parent -> groupNode.moveAllChildrenTo(parent, parent.getIndexOfChild(groupNode).get())); - groupNode.removeFromParent(); - }); + groupNode.getParent() + .ifPresent(parent -> groupNode.moveAllChildrenTo(parent, parent.getIndexOfChild(groupNode).get())); + groupNode.removeFromParent(); + }); - if (selectedGroupNodes.size() > 1) { - dialogService.notify(Localization.lang("Removed all selected groups.")); - } else { - dialogService.notify(Localization.lang("Removed group \"%0\".", group.getDisplayName())); - } - writeGroupChangesToMetaData(); + if (selectedGroupNodes.size() > 1) { + dialogService.notify(Localization.lang("Removed all selected groups.")); + } else { + dialogService.notify(Localization.lang("Removed group \"%0\".", group.getDisplayName())); } + writeGroupChangesToMetaData(); } } @@ -443,6 +447,12 @@ public void removeGroupKeepSubgroups(GroupNodeViewModel group) { */ public void removeGroupAndSubgroups(GroupNodeViewModel group) { boolean confirmed; + //check whether the group is in selectedGroups, if not, we clear all selected group and add the current group into it by default + if (!selectedGroups.contains(group)) { + selectedGroups.clear(); + selectedGroups.add(group); + } + if (selectedGroups.size() <= 1) { confirmed = dialogService.showConfirmationDialogAndWait( Localization.lang("Remove group and subgroups"),