diff --git a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java index ff9cf822a82..bd113bf249e 100644 --- a/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java @@ -32,9 +32,17 @@ import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.groups.AbstractGroup; +import org.jabref.model.groups.AllEntriesGroup; import org.jabref.model.groups.AutomaticGroup; +import org.jabref.model.groups.AutomaticKeywordGroup; +import org.jabref.model.groups.AutomaticPersonsGroup; +import org.jabref.model.groups.ExplicitGroup; import org.jabref.model.groups.GroupEntryChanger; import org.jabref.model.groups.GroupTreeNode; +import org.jabref.model.groups.KeywordGroup; +import org.jabref.model.groups.LastNameGroup; +import org.jabref.model.groups.RegexKeywordGroup; +import org.jabref.model.groups.SearchGroup; import org.jabref.model.groups.TexGroup; import org.jabref.model.strings.StringUtil; import org.jabref.preferences.PreferencesService; @@ -368,4 +376,135 @@ public void draggedOn(GroupNodeViewModel target, DroppingMouseLocation mouseLoca private int getPositionInParent() { return groupNode.getPositionInParent(); } + + public boolean hasSubgroups() { + return !getChildren().isEmpty(); + } + + public boolean canAddEntriesIn() { + AbstractGroup group = groupNode.getGroup(); + if (group instanceof AllEntriesGroup) { + return false; + } else if (group instanceof ExplicitGroup) { + return true; + } else if (group instanceof LastNameGroup || group instanceof RegexKeywordGroup) { + return groupNode.getParent() + .map(parent -> parent.getGroup()) + .map(groupParent -> groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup) + .orElse(false); + } else if (group instanceof KeywordGroup) { + // also covers WordKeywordGroup + return true; + } else if (group instanceof SearchGroup) { + return false; + } else if (group instanceof AutomaticKeywordGroup) { + return false; + } else if (group instanceof AutomaticPersonsGroup) { + return false; + } else if (group instanceof TexGroup) { + return false; + } else { + throw new UnsupportedOperationException("canAddEntriesIn method not yet implemented in group: " + group.getClass().getName()); + } + } + + public boolean canBeDragged() { + AbstractGroup group = groupNode.getGroup(); + if (group instanceof AllEntriesGroup) { + return false; + } else if (group instanceof ExplicitGroup) { + return true; + } else if (group instanceof KeywordGroup) { + // KeywordGroup is parent of LastNameGroup, RegexKeywordGroup and WordKeywordGroup + return groupNode.getParent() + .map(parent -> parent.getGroup()) + .map(groupParent -> !(groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup)) + .orElse(false); + } else if (group instanceof SearchGroup) { + return true; + } else if (group instanceof AutomaticKeywordGroup) { + return true; + } else if (group instanceof AutomaticPersonsGroup) { + return true; + } else if (group instanceof TexGroup) { + return true; + } else { + throw new UnsupportedOperationException("canBeDragged method not yet implemented in group: " + group.getClass().getName()); + } + } + + public boolean canAddGroupsIn() { + AbstractGroup group = groupNode.getGroup(); + if (group instanceof AllEntriesGroup) { + return true; + } else if (group instanceof ExplicitGroup) { + return true; + } else if (group instanceof KeywordGroup) { + // KeywordGroup is parent of LastNameGroup, RegexKeywordGroup and WordKeywordGroup + return groupNode.getParent() + .map(parent -> parent.getGroup()) + .map(groupParent -> !(groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup)) + .orElse(false); + } else if (group instanceof SearchGroup) { + return true; + } else if (group instanceof AutomaticKeywordGroup) { + return false; + } else if (group instanceof AutomaticPersonsGroup) { + return false; + } else if (group instanceof TexGroup) { + return true; + } else { + throw new UnsupportedOperationException("canAddGroupsIn method not yet implemented in group: " + group.getClass().getName()); + } + } + + public boolean canRemove() { + AbstractGroup group = groupNode.getGroup(); + if (group instanceof AllEntriesGroup) { + return false; + } else if (group instanceof ExplicitGroup) { + return true; + } else if (group instanceof KeywordGroup) { + // KeywordGroup is parent of LastNameGroup, RegexKeywordGroup and WordKeywordGroup + return groupNode.getParent() + .map(parent -> parent.getGroup()) + .map(groupParent -> !(groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup)) + .orElse(false); + } else if (group instanceof SearchGroup) { + return true; + } else if (group instanceof AutomaticKeywordGroup) { + return true; + } else if (group instanceof AutomaticPersonsGroup) { + return true; + } else if (group instanceof TexGroup) { + return true; + } else { + throw new UnsupportedOperationException("canRemove method not yet implemented in group: " + group.getClass().getName()); + } + } + + public boolean isEditable() { + AbstractGroup group = groupNode.getGroup(); + if (group instanceof AllEntriesGroup) { + return false; + } else if (group instanceof ExplicitGroup) { + return true; + } else if (group instanceof KeywordGroup) { + // KeywordGroup is parent of LastNameGroup, RegexKeywordGroup and WordKeywordGroup + return groupNode.getParent() + .map(parent -> parent.getGroup()) + .map(groupParent -> !(groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup)) + .orElse(false); + } else if (group instanceof SearchGroup) { + return true; + } else if (group instanceof AutomaticKeywordGroup) { + return true; + } else if (group instanceof AutomaticPersonsGroup) { + return true; + } else if (group instanceof TexGroup) { + return true; + } else { + throw new UnsupportedOperationException("isEditable method not yet implemented in group: " + group.getClass().getName()); + } + } } diff --git a/src/main/java/org/jabref/gui/groups/GroupTreeView.java b/src/main/java/org/jabref/gui/groups/GroupTreeView.java index d2ccb279c56..99a251dc91a 100644 --- a/src/main/java/org/jabref/gui/groups/GroupTreeView.java +++ b/src/main/java/org/jabref/gui/groups/GroupTreeView.java @@ -340,14 +340,14 @@ private void handleOnDragDropped(TreeTableRow row, GroupNode Dragboard dragboard = event.getDragboard(); boolean success = false; - if (dragboard.hasContent(DragAndDropDataFormats.GROUP) && viewModel.canAddGroupsIn(row.getItem())) { + if (dragboard.hasContent(DragAndDropDataFormats.GROUP) && row.getItem().canAddGroupsIn()) { 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() && viewModel.canBeDragged(source.get())) { + if (source.isPresent() && source.get().canBeDragged()) { source.get().draggedOn(row.getItem(), ControlHelper.getDroppingMouseLocation(row, event)); changedGroups.add(source.get()); success = true; @@ -477,7 +477,7 @@ private ContextMenu createContextMenuForGroup(GroupNodeViewModel group) { ActionFactory factory = new ActionFactory(Globals.getKeyPrefs()); MenuItem removeGroup; - if (viewModel.hasSubgroups(group) && viewModel.canAddGroupsIn(group) && !group.isRoot()) { + if (group.hasSubgroups() && group.canAddGroupsIn() && !group.isRoot()) { removeGroup = new Menu(Localization.lang("Remove group"), null, factory.createMenuItem(StandardActions.GROUP_REMOVE_KEEP_SUBGROUPS, new GroupTreeView.ContextAction(StandardActions.GROUP_REMOVE_KEEP_SUBGROUPS, group)), @@ -568,20 +568,20 @@ public ContextAction(StandardActions command, GroupNodeViewModel group) { this.executable.bind(BindingsHelper.constantOf( switch (command) { case GROUP_EDIT -> - viewModel.isEditable(group); + group.isEditable(); case GROUP_REMOVE, GROUP_REMOVE_WITH_SUBGROUPS, GROUP_REMOVE_KEEP_SUBGROUPS -> - viewModel.isEditable(group) && viewModel.canRemove(group); + group.isEditable() && group.canRemove(); case GROUP_SUBGROUP_ADD -> - viewModel.isEditable(group) && viewModel.canAddGroupsIn(group) + group.isEditable() && group.canAddGroupsIn() || group.isRoot(); case GROUP_SUBGROUP_REMOVE -> - viewModel.isEditable(group) && viewModel.hasSubgroups(group) && viewModel.canRemove(group) + group.isEditable() && group.hasSubgroups() && group.canRemove() || group.isRoot(); case GROUP_SUBGROUP_SORT -> - viewModel.isEditable(group) && viewModel.hasSubgroups(group) && viewModel.canAddGroupsIn(group) + group.isEditable() && group.hasSubgroups() && group.canAddEntriesIn() || group.isRoot(); case GROUP_ENTRIES_ADD, GROUP_ENTRIES_REMOVE -> - viewModel.canAddEntriesIn(group); + group.canAddEntriesIn(); default -> true; })); diff --git a/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java b/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java index fd217c575b8..4c9e8118590 100644 --- a/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java +++ b/src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java @@ -30,13 +30,10 @@ import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.groups.AbstractGroup; -import org.jabref.model.groups.AllEntriesGroup; import org.jabref.model.groups.AutomaticKeywordGroup; import org.jabref.model.groups.AutomaticPersonsGroup; import org.jabref.model.groups.ExplicitGroup; import org.jabref.model.groups.GroupTreeNode; -import org.jabref.model.groups.KeywordGroup; -import org.jabref.model.groups.LastNameGroup; import org.jabref.model.groups.RegexKeywordGroup; import org.jabref.model.groups.SearchGroup; import org.jabref.model.groups.TexGroup; @@ -572,160 +569,4 @@ public void removeSelectedEntries(GroupNodeViewModel group) { public void sortAlphabeticallyRecursive(GroupTreeNode group) { group.sortChildren(compAlphabetIgnoreCase, true); } - - public boolean canBeDragged(GroupNodeViewModel groupnode) { - AbstractGroup group = groupnode.getGroupNode().getGroup(); - if (group instanceof AllEntriesGroup) { - return false; - } else if (group instanceof ExplicitGroup) { - return true; - } else if (group instanceof LastNameGroup || group instanceof KeywordGroup || group instanceof RegexKeywordGroup) { - if (groupnode.getParent().isPresent()) { - AbstractGroup groupParent = groupnode.getParent().get().getGroup(); - if (groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup) { - return false; - } else { - return true; - } - } else { - return false; - } - } else if (group instanceof SearchGroup) { - return true; - } else if (group instanceof AutomaticKeywordGroup) { - return true; - } else if (group instanceof AutomaticPersonsGroup) { - return true; - } else if (group instanceof TexGroup) { - return true; - } else { - throw new UnsupportedOperationException("canBeDragged method not yet implemented in group: " + group.getClass().getName()); - } - } - - public boolean canAddGroupsIn(GroupNodeViewModel groupnode) { - AbstractGroup group = groupnode.getGroupNode().getGroup(); - if (group instanceof AllEntriesGroup) { - return true; - } else if (group instanceof ExplicitGroup) { - return true; - } else if (group instanceof LastNameGroup || group instanceof KeywordGroup || group instanceof RegexKeywordGroup) { - if (groupnode.getParent().isPresent()) { - AbstractGroup groupParent = groupnode.getParent().get().getGroup(); - if (groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup) { - return false; - } else { - return true; - } - } else { - return false; - } - } else if (group instanceof SearchGroup) { - return true; - } else if (group instanceof AutomaticKeywordGroup) { - return false; - } else if (group instanceof AutomaticPersonsGroup) { - return false; - } else if (group instanceof TexGroup) { - return true; - } else { - throw new UnsupportedOperationException("canAddGroupsIn method not yet implemented in group: " + group.getClass().getName()); - } - } - - public boolean canRemove(GroupNodeViewModel groupNode) { - AbstractGroup group = groupNode.getGroupNode().getGroup(); - if (group instanceof AllEntriesGroup) { - return false; - } else if (group instanceof ExplicitGroup) { - return true; - } else if (group instanceof LastNameGroup || group instanceof KeywordGroup || group instanceof RegexKeywordGroup) { - if (groupNode.getParent().isPresent()) { - AbstractGroup groupParent = groupNode.getParent().get().getGroup(); - if (groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup) { - return false; - } else { - return true; - } - } else { - return false; - } - } else if (group instanceof SearchGroup) { - return true; - } else if (group instanceof AutomaticKeywordGroup) { - return true; - } else if (group instanceof AutomaticPersonsGroup) { - return true; - } else if (group instanceof TexGroup) { - return true; - } else { - throw new UnsupportedOperationException("canRemove method not yet implemented in group: " + group.getClass().getName()); - } - } - - public boolean hasSubgroups(GroupNodeViewModel groupnode) { - return groupnode.getChildren().size() > 0; - } - - public boolean canAddEntriesIn(GroupNodeViewModel groupnode) { - AbstractGroup group = groupnode.getGroupNode().getGroup(); - if (group instanceof AllEntriesGroup) { - return false; - } else if (group instanceof ExplicitGroup) { - return true; - } else if (group instanceof LastNameGroup || group instanceof RegexKeywordGroup) { - if (groupnode.getParent().isPresent()) { - AbstractGroup groupParent = groupnode.getParent().get().getGroup(); - if (groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup) { - return true; - } else { - return false; - } - } else { - return false; - } - } else if (group instanceof KeywordGroup) { - return true; - } else if (group instanceof SearchGroup) { - return false; - } else if (group instanceof AutomaticKeywordGroup) { - return false; - } else if (group instanceof AutomaticPersonsGroup) { - return false; - } else if (group instanceof TexGroup) { - return false; - } else { - throw new UnsupportedOperationException("canAddEntriesIn method not yet implemented in group: " + group.getClass().getName()); - } - } - - public boolean isEditable(GroupNodeViewModel groupnode) { - AbstractGroup group = groupnode.getGroupNode().getGroup(); - if (group instanceof AllEntriesGroup) { - return false; - } else if (group instanceof ExplicitGroup) { - return true; - } else if (group instanceof LastNameGroup || group instanceof KeywordGroup || group instanceof RegexKeywordGroup) { - if (groupnode.getParent().isPresent()) { - AbstractGroup groupParent = groupnode.getParent().get().getGroup(); - if (groupParent instanceof AutomaticKeywordGroup || groupParent instanceof AutomaticPersonsGroup) { - return false; - } else { - return true; - } - } else { - return false; - } - } else if (group instanceof SearchGroup) { - return true; - } else if (group instanceof AutomaticKeywordGroup) { - return true; - } else if (group instanceof AutomaticPersonsGroup) { - return true; - } else if (group instanceof TexGroup) { - return true; - } else { - throw new UnsupportedOperationException("isEditable method not yet implemented in group: " + group.getClass().getName()); - } - } } diff --git a/src/test/java/org/jabref/logic/database/DatabaseMergerTest.java b/src/test/java/org/jabref/logic/database/DatabaseMergerTest.java index 5797e805cbb..a7464dbaed5 100644 --- a/src/test/java/org/jabref/logic/database/DatabaseMergerTest.java +++ b/src/test/java/org/jabref/logic/database/DatabaseMergerTest.java @@ -10,7 +10,6 @@ import org.jabref.model.entry.BibtexString; import org.jabref.model.entry.field.StandardField; import org.jabref.model.entry.types.StandardEntryType; -import org.jabref.model.groups.AbstractGroup; import org.jabref.model.groups.AllEntriesGroup; import org.jabref.model.groups.ExplicitGroup; import org.jabref.model.groups.GroupHierarchyType; @@ -146,10 +145,10 @@ void mergeBibTexStringsWithSameNameAndContentAreIgnored() { void mergeMetaDataWithoutAllEntriesGroup() { MetaData target = new MetaData(); target.addContentSelector(new ContentSelector(StandardField.AUTHOR, List.of("Test Author"))); - GroupTreeNode targetRootGroup = new GroupTreeNode(new TestGroup("targetGroup", GroupHierarchyType.INDEPENDENT)); + GroupTreeNode targetRootGroup = new GroupTreeNode(new ExplicitGroup("targetGroup", GroupHierarchyType.INDEPENDENT, ',')); target.setGroups(targetRootGroup); MetaData other = new MetaData(); - GroupTreeNode otherRootGroup = new GroupTreeNode(new TestGroup("otherGroup", GroupHierarchyType.INCLUDING)); + GroupTreeNode otherRootGroup = new GroupTreeNode(new ExplicitGroup("otherGroup", GroupHierarchyType.INCLUDING, ',')); other.setGroups(otherRootGroup); other.addContentSelector(new ContentSelector(StandardField.TITLE, List.of("Test Title"))); List expectedContentSelectors = @@ -190,26 +189,4 @@ void mergeMetaDataWithAllEntriesGroup() { assertEquals(expectedImportedGroupNode, target.getGroups().get().getChildren().get(0)); assertEquals(expectedContentSelectors, target.getContentSelectorList()); } - - static class TestGroup extends AbstractGroup { - - protected TestGroup(String name, GroupHierarchyType context) { - super(name, context); - } - - @Override - public boolean contains(BibEntry entry) { - return false; - } - - @Override - public boolean isDynamic() { - return false; - } - - @Override - public AbstractGroup deepCopy() { - return null; - } - } }