Skip to content

Commit

Permalink
Post change notifications on JavaFX
Browse files Browse the repository at this point in the history
Fixes #4817. The problem is that changes in other threads produce a "Not on FX application thread" exception. This is fixed by adding a wrapper around the list of entries. The wrapper only posts changes in the correct thread, without making a copy of the underlying list - so performance shouldn't be affected.
  • Loading branch information
tobiasdiez committed Apr 8, 2019
1 parent bb7b721 commit d55afba
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void output(String s) {

private void setupActions() {
SaveDatabaseAction saveAction = new SaveDatabaseAction(this, Globals.prefs);
CleanupAction cleanUpAction = new CleanupAction(this, Globals.prefs);
CleanupAction cleanUpAction = new CleanupAction(this, Globals.prefs, Globals.TASK_EXECUTOR);

actions.put(Actions.UNDO, undoAction);
actions.put(Actions.REDO, redoAction);
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/org/jabref/gui/actions/CleanupAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.jabref.gui.cleanup.CleanupDialog;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.cleanup.CleanupPreset;
import org.jabref.logic.cleanup.CleanupWorker;
import org.jabref.logic.l10n.Localization;
Expand All @@ -20,15 +22,17 @@ public class CleanupAction implements BaseAction {

private final BasePanel panel;
private final DialogService dialogService;
private final TaskExecutor taskExecutor;

private boolean isCanceled;
private int modifiedEntriesCount;
private final JabRefPreferences preferences;

public CleanupAction(BasePanel panel, JabRefPreferences preferences) {
public CleanupAction(BasePanel panel, JabRefPreferences preferences, TaskExecutor taskExecutor) {
this.panel = panel;
this.preferences = preferences;
this.dialogService = panel.frame().getDialogService();
this.taskExecutor = taskExecutor;
}

@Override
Expand Down Expand Up @@ -58,8 +62,9 @@ public void action() {

preferences.setCleanupPreset(chosenPreset.get());

this.cleanup(chosenPreset.get());
this.showResults();
BackgroundTask.wrap(() -> cleanup(chosenPreset.get()))
.onSuccess(result -> showResults())
.executeWith(taskExecutor);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jabref.gui.keyboard.KeyBindingRepository;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableInsertEntry;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.CustomLocalDragboard;
import org.jabref.gui.util.ViewModelTableRowFactory;
import org.jabref.logic.l10n.Localization;
Expand Down Expand Up @@ -108,7 +109,7 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,
}
this.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

this.setItems(model.getEntriesFilteredAndSorted());
this.setItems(BindingsHelper.forUI(model.getEntriesFilteredAndSorted()));
// Enable sorting
model.getEntriesFilteredAndSorted().comparatorProperty().bind(this.comparatorProperty());

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/jabref/gui/util/BindingsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ protected String computeValue() {
};
}

/**
* Returns a wrapper around the given list that posts changes on the JavaFX thread.
*/
public static <T> ObservableList<T> forUI(ObservableList<T> list) {
return new UiThreadList<>(list);
}


private static class BidirectionalBinding<A, B> {

private final ObservableValue<A> propertyA;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/jabref/gui/util/UiThreadList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jabref.gui.util;

import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.TransformationList;

class UiThreadList<T> extends TransformationList<T, T> {
public UiThreadList(ObservableList<? extends T> source) {
super(source);
}

@Override
protected void sourceChanged(ListChangeListener.Change<? extends T> change) {
Platform.runLater(() -> fireChange(change));
}

@Override
public int getSourceIndex(int index) {
return index;
}

@Override
public T get(int index) {
return getSource().get(index);
}

@Override
public int size() {
return getSource().size();
}
}

0 comments on commit d55afba

Please sign in to comment.