-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JavaFX thread exception when fetching new infos (#4354)
* Fix JavaFX thread exception when fetching new infos Fixes by reworking the SwingWorker using JavaFX BackgroundTasks. * Fix checkstyle * Improve code * fix checkstyle
- Loading branch information
1 parent
d8f6c5c
commit 5dca3b6
Showing
7 changed files
with
82 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 0 additions & 64 deletions
64
src/main/java/org/jabref/gui/mergeentries/EntryFetchAndMergeWorker.java
This file was deleted.
Oops, something went wrong.
91 changes: 66 additions & 25 deletions
91
src/main/java/org/jabref/gui/mergeentries/FetchAndMergeEntry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,93 @@ | ||
package org.jabref.gui.mergeentries; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.jabref.JabRefGUI; | ||
import org.jabref.Globals; | ||
import org.jabref.gui.BasePanel; | ||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.util.BackgroundTask; | ||
import org.jabref.gui.util.TaskExecutor; | ||
import org.jabref.logic.importer.EntryBasedFetcher; | ||
import org.jabref.logic.importer.IdBasedFetcher; | ||
import org.jabref.logic.importer.WebFetchers; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.FieldName; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Class for fetching and merging information based on a specific field | ||
* | ||
* Class for fetching and merging bibliographic information | ||
*/ | ||
public class FetchAndMergeEntry { | ||
|
||
// A list of all field which are supported | ||
public static List<String> SUPPORTED_FIELDS = Arrays.asList(FieldName.DOI, FieldName.EPRINT, FieldName.ISBN); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(FetchAndMergeEntry.class); | ||
private final BasePanel panel; | ||
private final DialogService dialogService; | ||
private final TaskExecutor taskExecutor; | ||
|
||
public FetchAndMergeEntry(BasePanel panel, TaskExecutor taskExecutor) { | ||
this.dialogService = panel.frame().getDialogService(); | ||
this.panel = panel; | ||
this.taskExecutor = taskExecutor; | ||
} | ||
|
||
/** | ||
* Convenience constructor for a single field | ||
* | ||
* @param entry - BibEntry to fetch information for | ||
* @param panel - current BasePanel | ||
* @param field - field to get information from | ||
*/ | ||
public FetchAndMergeEntry(BibEntry entry, BasePanel panel, String field) { | ||
this(entry, panel, Arrays.asList(field)); | ||
public void fetchAndMerge(BibEntry entry) { | ||
fetchAndMerge(entry, SUPPORTED_FIELDS); | ||
} | ||
|
||
public FetchAndMergeEntry(BibEntry entry, String field) { | ||
this(entry, JabRefGUI.getMainFrame().getCurrentBasePanel(), field); | ||
public void fetchAndMerge(BibEntry entry, String field) { | ||
fetchAndMerge(entry, Collections.singletonList(field)); | ||
} | ||
|
||
/** | ||
* Default constructor | ||
* | ||
* @param entry - BibEntry to fetch information for | ||
* @param panel - current BasePanel | ||
* @param fields - List of fields to get information from, one at a time in given order | ||
*/ | ||
public FetchAndMergeEntry(BibEntry entry, BasePanel panel, List<String> fields) { | ||
public void fetchAndMerge(BibEntry entry, List<String> fields) { | ||
for (String field : fields) { | ||
if (entry.hasField(field)) { | ||
new FetchAndMergeWorker(panel, entry, field).execute(); | ||
Optional<String> fieldContent = entry.getField(field); | ||
if (fieldContent.isPresent()) { | ||
Optional<IdBasedFetcher> fetcher = WebFetchers.getIdBasedFetcherForField(field, Globals.prefs.getImportFormatPreferences()); | ||
if (fetcher.isPresent()) { | ||
BackgroundTask.wrap(() -> fetcher.get().performSearchById(fieldContent.get())) | ||
.onSuccess(fetchedEntry -> { | ||
String type = FieldName.getDisplayName(field); | ||
if (fetchedEntry.isPresent()) { | ||
MergeFetchedEntryDialog dialog = new MergeFetchedEntryDialog(panel, entry, fetchedEntry.get(), type); | ||
dialog.setVisible(true); | ||
} else { | ||
panel.frame().setStatus(Localization.lang("Cannot get info based on given %0: %1", type, fieldContent.get())); | ||
} | ||
}) | ||
.onFailure(exception -> { | ||
LOGGER.error("Error while fetching bibliographic information", exception); | ||
dialogService.showErrorDialogAndWait(exception); | ||
}) | ||
.executeWith(Globals.TASK_EXECUTOR); | ||
} | ||
} else { | ||
panel.frame().setStatus(Localization.lang("No %0 found", FieldName.getDisplayName(field))); | ||
dialogService.notify(Localization.lang("No %0 found", FieldName.getDisplayName(field))); | ||
} | ||
} | ||
} | ||
|
||
public void fetchAndMerge(BibEntry entry, EntryBasedFetcher fetcher) { | ||
BackgroundTask.wrap(() -> fetcher.performSearch(entry).stream().findFirst()) | ||
.onSuccess(fetchedEntry -> { | ||
if (fetchedEntry.isPresent()) { | ||
MergeFetchedEntryDialog dialog = new MergeFetchedEntryDialog(panel, entry, fetchedEntry.get(), fetcher.getName()); | ||
dialog.setVisible(true); | ||
} else { | ||
dialogService.notify(Localization.lang("Could not find any bibliographic information.")); | ||
} | ||
}) | ||
.onFailure(exception -> { | ||
LOGGER.error("Error while fetching entry with " + fetcher.getName(), exception); | ||
dialogService.showErrorDialogAndWait(Localization.lang("Error while fetching from %0", fetcher.getName()), exception); | ||
}) | ||
.executeWith(taskExecutor); | ||
} | ||
} |
76 changes: 0 additions & 76 deletions
76
src/main/java/org/jabref/gui/mergeentries/FetchAndMergeWorker.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters