Skip to content

Commit

Permalink
Make Group(Node)TreeViewModel more OO (#9978)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor authored Jun 7, 2023
1 parent dc0d10c commit 7d64663
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 193 deletions.
139 changes: 139 additions & 0 deletions src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
18 changes: 9 additions & 9 deletions src/main/java/org/jabref/gui/groups/GroupTreeView.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,14 @@ private void handleOnDragDropped(TreeTableRow<GroupNodeViewModel> 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<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() && 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;
Expand Down Expand Up @@ -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)),
Expand Down Expand Up @@ -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;
}));
Expand Down
159 changes: 0 additions & 159 deletions src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
}
Loading

0 comments on commit 7d64663

Please sign in to comment.