Skip to content

Commit 4c7dc13

Browse files
committed
updated bindContentFiltered to filter on Tab using passed predicate
1 parent 21e82e4 commit 4c7dc13

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

jabgui/src/main/java/org/jabref/gui/frame/JabRefFrame.java

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,15 @@ private void initKeyBindings() {
373373
}
374374

375375
private void initBindings() {
376-
// This variable cannot be inlined, since otherwise the list created by EasyBind is being garbage collected
377-
openDatabaseList = EasyBind.map(tabbedPane.getTabs(), tab -> {
378-
if (tab instanceof LibraryTab libraryTab) {
379-
return libraryTab.getBibDatabaseContext();
380-
} else {
381-
return null;
382-
}
383-
});
376+
// FIXME: See https://github.com/JabRef/jabref-koppor/pull/713 - workaround in place until issue is resolved.
377+
// Original code used FilteredList and EasyBind to filter and map tabs directly:
378+
// FilteredList<Tab> filteredTabs = new FilteredList<>(tabbedPane.getTabs());
379+
// filteredTabs.setPredicate(LibraryTab.class::isInstance);
380+
// openDatabaseList = EasyBind.map(filteredTabs, tab -> ((LibraryTab) tab).getBibDatabaseContext());
381+
// EasyBind.bindContent(stateManager.getOpenDatabases(), openDatabaseList);
382+
// Once JabRef#713 is fixed, remove this comment and the bindContentFiltered() method, and restore the original code
384383

385-
// call compromised until further notice, See https://github.com/JabRef/jabref-koppor/pull/713 for details.
386-
// EasyBind.bindContent(stateManager.getOpenDatabases(), new FilteredList<>(openDatabaseList));
387-
bindContentFiltered(openDatabaseList, stateManager.getOpenDatabases(), Objects::nonNull);
384+
bindContentFiltered(tabbedPane.getTabs(), stateManager.getOpenDatabases(), LibraryTab.class::isInstance);
388385

389386
// the binding for stateManager.activeDatabaseProperty() is at org.jabref.gui.LibraryTab.onDatabaseLoadingSucceed
390387

@@ -458,21 +455,30 @@ private void initBindings() {
458455
EasyBind.subscribe(preferences.getWorkspacePreferences().hideTabBarProperty(), _ -> updateTabBarVisible());
459456
}
460457

461-
private static <T> void bindContentFiltered(ObservableList<T> source, ObservableList<T> target, java.util.function.Predicate<T> filter) {
458+
private static void bindContentFiltered(ObservableList<Tab> source, ObservableList<BibDatabaseContext> target, java.util.function.Predicate<Tab> filter) {
459+
java.util.function.Function<Tab, BibDatabaseContext> tabToContext = tab -> ((LibraryTab) tab).getBibDatabaseContext();
462460
// Initial sync
463-
target.setAll(source.stream().filter(filter).toList());
461+
target.setAll(source.stream()
462+
.filter(filter)
463+
.map(tabToContext)
464+
.toList());
464465

465-
source.addListener((ListChangeListener<T>) c -> {
466+
source.addListener((ListChangeListener<Tab>) c -> {
466467
while (c.next()) {
467468
if (c.wasPermutated()) {
468469
// We need a fresh copy as permutation is much harder to mirror
469-
List<T> reordered = source.stream().filter(filter).toList();
470+
List<BibDatabaseContext> reordered = source.stream()
471+
.filter(filter)
472+
.map(tabToContext)
473+
.toList();
470474
target.setAll(reordered);
471475
}
472476

473477
if (c.wasRemoved()) {
474-
for (T removed : c.getRemoved()) {
475-
target.remove(removed);
478+
for (Tab removed : c.getRemoved()) {
479+
if (filter.test(removed)) {
480+
target.remove(tabToContext.apply(removed));
481+
}
476482
}
477483
}
478484

@@ -482,15 +488,15 @@ private static <T> void bindContentFiltered(ObservableList<T> source, Observable
482488

483489
// We need to add at the correct place - therefore, we need to find out the correct position
484490
for (int i = 0; i < sourceIndex; i++) {
485-
T element = source.get(i);
486-
if (filter.test(element)) {
491+
Tab tab = source.get(i);
492+
if (filter.test(tab)) {
487493
targetIndex++;
488494
}
489495
}
490496

491-
for (T added : c.getAddedSubList()) {
497+
for (Tab added : c.getAddedSubList()) {
492498
if (filter.test(added)) {
493-
target.add(targetIndex++, added);
499+
target.add(targetIndex++, tabToContext.apply(added));
494500
}
495501
}
496502
}

0 commit comments

Comments
 (0)