Skip to content

Commit 46fd96b

Browse files
calixtusGena928tobiasdiez
authored
Fix Group hit counter calculation preferences (#6554)
Co-authored-by: Gennadiy Stakhovskiy <GennadiyStakhovskiy@gmail.com> Co-authored-by: Tobias Diez <tobiasdiez@gmx.de>
1 parent 2b51a85 commit 46fd96b

File tree

4 files changed

+28
-22
lines changed

4 files changed

+28
-22
lines changed

src/main/java/org/jabref/gui/groups/GroupNodeViewModel.java

+18-13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.jabref.model.groups.GroupEntryChanger;
3636
import org.jabref.model.groups.GroupTreeNode;
3737
import org.jabref.model.strings.StringUtil;
38+
import org.jabref.preferences.PreferencesService;
3839

3940
import com.google.common.base.Enums;
4041
import com.tobiasdiez.easybind.EasyBind;
@@ -57,13 +58,15 @@ public class GroupNodeViewModel {
5758
private final TaskExecutor taskExecutor;
5859
private final CustomLocalDragboard localDragBoard;
5960
private final ObservableList<BibEntry> entriesList;
61+
private final PreferencesService preferencesService;
6062

61-
public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager stateManager, TaskExecutor taskExecutor, GroupTreeNode groupNode, CustomLocalDragboard localDragBoard) {
63+
public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager stateManager, TaskExecutor taskExecutor, GroupTreeNode groupNode, CustomLocalDragboard localDragBoard, PreferencesService preferencesService) {
6264
this.databaseContext = Objects.requireNonNull(databaseContext);
6365
this.taskExecutor = Objects.requireNonNull(taskExecutor);
6466
this.stateManager = Objects.requireNonNull(stateManager);
6567
this.groupNode = Objects.requireNonNull(groupNode);
6668
this.localDragBoard = Objects.requireNonNull(localDragBoard);
69+
this.preferencesService = preferencesService;
6770

6871
displayName = new LatexToUnicodeFormatter().format(groupNode.getName());
6972
isRoot = groupNode.isRoot();
@@ -95,16 +98,16 @@ public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager state
9598
allSelectedEntriesMatched = selectedEntriesMatchStatus.isEmptyBinding().not().and(selectedEntriesMatchStatus.allMatch(matched -> matched));
9699
}
97100

98-
public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager stateManager, TaskExecutor taskExecutor, AbstractGroup group, CustomLocalDragboard localDragboard) {
99-
this(databaseContext, stateManager, taskExecutor, new GroupTreeNode(group), localDragboard);
101+
public GroupNodeViewModel(BibDatabaseContext databaseContext, StateManager stateManager, TaskExecutor taskExecutor, AbstractGroup group, CustomLocalDragboard localDragboard, PreferencesService preferencesService) {
102+
this(databaseContext, stateManager, taskExecutor, new GroupTreeNode(group), localDragboard, preferencesService);
100103
}
101104

102-
static GroupNodeViewModel getAllEntriesGroup(BibDatabaseContext newDatabase, StateManager stateManager, TaskExecutor taskExecutor, CustomLocalDragboard localDragBoard) {
103-
return new GroupNodeViewModel(newDatabase, stateManager, taskExecutor, DefaultGroupsFactory.getAllEntriesGroup(), localDragBoard);
105+
static GroupNodeViewModel getAllEntriesGroup(BibDatabaseContext newDatabase, StateManager stateManager, TaskExecutor taskExecutor, CustomLocalDragboard localDragBoard, PreferencesService preferencesService) {
106+
return new GroupNodeViewModel(newDatabase, stateManager, taskExecutor, DefaultGroupsFactory.getAllEntriesGroup(), localDragBoard, preferencesService);
104107
}
105108

106109
private GroupNodeViewModel toViewModel(GroupTreeNode child) {
107-
return new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, child, localDragBoard);
110+
return new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, child, localDragBoard, preferencesService);
108111
}
109112

110113
public List<FieldChange> addEntriesToGroup(List<BibEntry> entries) {
@@ -251,13 +254,15 @@ private void updateMatchedEntries() {
251254
// We calculate the new hit value
252255
// We could be more intelligent and try to figure out the new number of hits based on the entry change
253256
// for example, a previously matched entry gets removed -> hits = hits - 1
254-
BackgroundTask
255-
.wrap(() -> groupNode.findMatches(databaseContext.getDatabase()))
256-
.onSuccess(entries -> {
257-
matchedEntries.clear();
258-
matchedEntries.addAll(entries);
259-
})
260-
.executeWith(taskExecutor);
257+
if (preferencesService.getDisplayGroupCount()) {
258+
BackgroundTask
259+
.wrap(() -> groupNode.findMatches(databaseContext.getDatabase()))
260+
.onSuccess(entries -> {
261+
matchedEntries.clear();
262+
matchedEntries.addAll(entries);
263+
})
264+
.executeWith(taskExecutor);
265+
}
261266
}
262267

263268
public GroupTreeNode addSubgroup(AbstractGroup subgroup) {

src/main/java/org/jabref/gui/groups/GroupTreeViewModel.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,13 @@ private void onActiveDatabaseChanged(Optional<BibDatabaseContext> newDatabase) {
125125
GroupNodeViewModel newRoot = newDatabase
126126
.map(BibDatabaseContext::getMetaData)
127127
.flatMap(MetaData::getGroups)
128-
.map(root -> new GroupNodeViewModel(newDatabase.get(), stateManager, taskExecutor, root, localDragboard))
129-
.orElse(GroupNodeViewModel.getAllEntriesGroup(newDatabase.get(), stateManager, taskExecutor, localDragboard));
128+
.map(root -> new GroupNodeViewModel(newDatabase.get(), stateManager, taskExecutor, root, localDragboard, preferences))
129+
.orElse(GroupNodeViewModel.getAllEntriesGroup(newDatabase.get(), stateManager, taskExecutor, localDragboard, preferences));
130130

131131
rootGroup.setValue(newRoot);
132132
selectedGroups.setAll(
133133
stateManager.getSelectedGroup(newDatabase.get()).stream()
134-
.map(selectedGroup -> new GroupNodeViewModel(newDatabase.get(), stateManager, taskExecutor, selectedGroup, localDragboard))
134+
.map(selectedGroup -> new GroupNodeViewModel(newDatabase.get(), stateManager, taskExecutor, selectedGroup, localDragboard, preferences))
135135
.collect(Collectors.toList()));
136136
} else {
137137
rootGroup.setValue(null);

src/test/java/org/jabref/gui/groups/GroupNodeViewModelTest.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.jabref.model.groups.GroupHierarchyType;
2020
import org.jabref.model.groups.GroupTreeNode;
2121
import org.jabref.model.groups.WordKeywordGroup;
22+
import org.jabref.preferences.PreferencesService;
2223

2324
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
@@ -159,18 +160,18 @@ void entriesAreAddedCorrectly() {
159160
BibEntry entry = new BibEntry();
160161
databaseContext.getDatabase().insertEntry(entry);
161162

162-
GroupNodeViewModel model = new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard());
163+
GroupNodeViewModel model = new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard(), mock(PreferencesService.class));
163164
model.addEntriesToGroup(databaseContext.getEntries());
164165

165166
assertEquals(databaseContext.getEntries(), model.getGroupNode().getEntriesInGroup(databaseContext.getEntries()));
166167
assertEquals(groupName, entry.getField(StandardField.GROUPS).get());
167168
}
168169

169170
private GroupNodeViewModel getViewModelForGroup(AbstractGroup group) {
170-
return new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard());
171+
return new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard(), mock(PreferencesService.class));
171172
}
172173

173174
private GroupNodeViewModel getViewModelForGroup(GroupTreeNode group) {
174-
return new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard());
175+
return new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard(), mock(PreferencesService.class));
175176
}
176177
}

src/test/java/org/jabref/gui/groups/GroupTreeViewModelTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void setUp() throws Exception {
4040
@Test
4141
void rootGroupIsAllEntriesByDefault() throws Exception {
4242
AllEntriesGroup allEntriesGroup = new AllEntriesGroup("All entries");
43-
assertEquals(new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, allEntriesGroup, new CustomLocalDragboard()), groupTree.rootGroupProperty().getValue());
43+
assertEquals(new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, allEntriesGroup, new CustomLocalDragboard(), mock(PreferencesService.class)), groupTree.rootGroupProperty().getValue());
4444
}
4545

4646
@Test
@@ -49,7 +49,7 @@ void explicitGroupsAreRemovedFromEntriesOnDelete() {
4949
BibEntry entry = new BibEntry();
5050
databaseContext.getDatabase().insertEntry(entry);
5151

52-
GroupNodeViewModel model = new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard());
52+
GroupNodeViewModel model = new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard(), mock(PreferencesService.class));
5353
model.addEntriesToGroup(databaseContext.getEntries());
5454
groupTree.removeGroupsAndSubGroupsFromEntries(model);
5555

@@ -63,7 +63,7 @@ void keywordGroupsAreNotRemovedFromEntriesOnDelete() {
6363
BibEntry entry = new BibEntry();
6464
databaseContext.getDatabase().insertEntry(entry);
6565

66-
GroupNodeViewModel model = new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard());
66+
GroupNodeViewModel model = new GroupNodeViewModel(databaseContext, stateManager, taskExecutor, group, new CustomLocalDragboard(), mock(PreferencesService.class));
6767
model.addEntriesToGroup(databaseContext.getEntries());
6868
groupTree.removeGroupsAndSubGroupsFromEntries(model);
6969

0 commit comments

Comments
 (0)