Skip to content

Commit

Permalink
Fix #9281 by update selectedGroups
Browse files Browse the repository at this point in the history
  • Loading branch information
William-Yao0993 committed Oct 28, 2022
1 parent 610dc63 commit d7ebc54
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupNodeViewModel> selectedGroupNodes = new ArrayList<>(selectedGroups);
selectedGroupNodes.forEach(eachNode -> {
GroupTreeNode groupNode = eachNode.getGroupNode();
List<GroupNodeViewModel> 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();
}

}
Expand All @@ -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"),
Expand Down

3 comments on commit d7ebc54

@Hus7128
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, could you outline your logic for solving this problem? I believe this will help with code reading.

@William-Yao0993
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi sibo

The logic is to add the input group into selectedGroup and take the remove action.

Regards
William

@Hus7128
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi William

That is helpful to me. Thanks.

Regards
Sibo

Please sign in to comment.