diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fa6c48d1bb..a3776691921 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -101,6 +101,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve - We fixed an issue where no citationkey was generated on import, pasting a doi or an entry on the main table. [8406](https://github.com/JabRef/jabref/issues/8406), [koppor#553](https://github.com/koppor/jabref/issues/553) - We fixed an issue where accent search does not perform consistently. [#6815](https://github.com/JabRef/jabref/issues/6815) - We fixed an issue where the incorrect entry was selected when "New Article" is pressed while search filters are active. [#8674](https://github.com/JabRef/jabref/issues/8674) +- We fixed an issue where "Write BibTeXEntry metadata to PDF" button remains enabled while writing to PDF is in-progress. [#8691](https://github.com/JabRef/jabref/issues/8691) ### Removed diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index c2db24cd29f..db615c074aa 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -14,7 +14,6 @@ import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLSocketFactory; -import javax.xml.transform.TransformerException; import javafx.beans.Observable; import javafx.beans.property.BooleanProperty; @@ -28,7 +27,6 @@ import org.jabref.gui.AbstractViewModel; import org.jabref.gui.DialogService; -import org.jabref.gui.Globals; import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.gui.externalfiles.FileDownloadTask; import org.jabref.gui.externalfiletype.ExternalFileType; @@ -41,7 +39,6 @@ import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.ControlHelper; import org.jabref.gui.util.TaskExecutor; -import org.jabref.logic.exporter.EmbeddedBibFilePdfExporter; import org.jabref.logic.externalfiles.LinkedFileHandler; import org.jabref.logic.importer.Importer; import org.jabref.logic.importer.ParserResult; @@ -54,7 +51,6 @@ import org.jabref.logic.net.URLDownload; import org.jabref.logic.util.io.FileNameUniqueness; import org.jabref.logic.util.io.FileUtil; -import org.jabref.logic.xmp.XmpUtilWriter; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; @@ -417,31 +413,8 @@ public void edit() { }); } - public void writeMetadataToPdf() { - BackgroundTask writeTask = BackgroundTask.wrap(() -> { - Optional file = linkedFile.findIn(databaseContext, preferences.getFilePreferences()); - if (file.isEmpty()) { - dialogService.notify(Localization.lang("Failed to write metadata, file %1 not found.", file.map(Path::toString).orElse(""))); - } else { - synchronized (linkedFile) { - try { - // Similar code can be found at {@link org.jabref.gui.exporter.WriteMetadataToPdfAction.writeMetadataToFile} - new XmpUtilWriter(preferences.getXmpPreferences()).writeXmp(file.get(), entry, databaseContext.getDatabase()); - - EmbeddedBibFilePdfExporter embeddedBibExporter = new EmbeddedBibFilePdfExporter(databaseContext.getMode(), Globals.entryTypesManager, preferences.getFieldWriterPreferences()); - embeddedBibExporter.exportToFileByPath(databaseContext, databaseContext.getDatabase(), preferences.getFilePreferences(), file.get()); - - dialogService.notify(Localization.lang("Success! Finished writing metadata.")); - } catch (IOException | TransformerException ex) { - dialogService.notify(Localization.lang("Error while writing metadata. See the error log for details.")); - LOGGER.error("Error while writing metadata to {}", file.map(Path::toString).orElse(""), ex); - } - } - } - return null; - }); - - taskExecutor.execute(writeTask); + public WriteMetadataToPdfCommand createWriteMetadataToPdfCommand() { + return new WriteMetadataToPdfCommand(linkedFile, databaseContext, preferences, dialogService, entry, LOGGER, taskExecutor); } public void download() { diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java index b0efcf387cb..08a329d508d 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java @@ -180,9 +180,12 @@ private Node createFileDisplay(LinkedFileViewModel linkedFile) { Button writeMetadataToPdf = IconTheme.JabRefIcons.IMPORT.asButton(); writeMetadataToPdf.setTooltip(new Tooltip(Localization.lang("Write BibTeXEntry metadata to PDF."))); writeMetadataToPdf.visibleProperty().bind(linkedFile.isOfflinePdfProperty()); - writeMetadataToPdf.setOnAction(event -> linkedFile.writeMetadataToPdf()); writeMetadataToPdf.getStyleClass().setAll("icon-button"); + WriteMetadataToPdfCommand writeMetadataToPdfCommand = linkedFile.createWriteMetadataToPdfCommand(); + writeMetadataToPdf.disableProperty().bind(writeMetadataToPdfCommand.executableProperty().not()); + writeMetadataToPdf.setOnAction(event -> writeMetadataToPdfCommand.execute()); + Button parsePdfMetadata = IconTheme.JabRefIcons.FILE_SEARCH.asButton(); parsePdfMetadata.setTooltip(new Tooltip(Localization.lang("Parse Metadata from PDF."))); parsePdfMetadata.visibleProperty().bind(linkedFile.isOfflinePdfProperty()); diff --git a/src/main/java/org/jabref/gui/fieldeditors/WriteMetadataToPdfCommand.java b/src/main/java/org/jabref/gui/fieldeditors/WriteMetadataToPdfCommand.java new file mode 100644 index 00000000000..6f5ed16cca3 --- /dev/null +++ b/src/main/java/org/jabref/gui/fieldeditors/WriteMetadataToPdfCommand.java @@ -0,0 +1,72 @@ +package org.jabref.gui.fieldeditors; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Optional; + +import javax.xml.transform.TransformerException; + +import org.jabref.gui.DialogService; +import org.jabref.gui.Globals; +import org.jabref.gui.actions.SimpleCommand; +import org.jabref.gui.util.BackgroundTask; +import org.jabref.gui.util.TaskExecutor; +import org.jabref.logic.exporter.EmbeddedBibFilePdfExporter; +import org.jabref.logic.l10n.Localization; +import org.jabref.logic.xmp.XmpUtilWriter; +import org.jabref.model.database.BibDatabaseContext; +import org.jabref.model.entry.BibEntry; +import org.jabref.model.entry.LinkedFile; +import org.jabref.preferences.PreferencesService; + +import org.slf4j.Logger; + +public class WriteMetadataToPdfCommand extends SimpleCommand { + private final LinkedFile linkedFile; + private final BibDatabaseContext databaseContext; + private final PreferencesService preferences; + private final DialogService dialogService; + private final BibEntry entry; + private final Logger logger; + private final TaskExecutor taskExecutor; + + public WriteMetadataToPdfCommand(LinkedFile linkedFile, BibDatabaseContext databaseContext, PreferencesService preferences, DialogService dialogService, BibEntry entry, Logger logger, TaskExecutor taskExecutor) { + this.linkedFile = linkedFile; + this.databaseContext = databaseContext; + this.preferences = preferences; + this.dialogService = dialogService; + this.entry = entry; + this.logger = logger; + this.taskExecutor = taskExecutor; + } + + @Override + public void execute() { + BackgroundTask writeTask = BackgroundTask.wrap(() -> { + Optional file = linkedFile.findIn(databaseContext, preferences.getFilePreferences()); + if (file.isEmpty()) { + dialogService.notify(Localization.lang("Failed to write metadata, file %1 not found.", file.map(Path::toString).orElse(""))); + } else { + synchronized (linkedFile) { + try { + // Similar code can be found at {@link org.jabref.gui.exporter.WriteMetadataToPdfAction.writeMetadataToFile} + new XmpUtilWriter(preferences.getXmpPreferences()).writeXmp(file.get(), entry, databaseContext.getDatabase()); + + EmbeddedBibFilePdfExporter embeddedBibExporter = new EmbeddedBibFilePdfExporter(databaseContext.getMode(), Globals.entryTypesManager, preferences.getFieldWriterPreferences()); + embeddedBibExporter.exportToFileByPath(databaseContext, databaseContext.getDatabase(), preferences.getFilePreferences(), file.get()); + + dialogService.notify(Localization.lang("Success! Finished writing metadata.")); + } catch (IOException | TransformerException ex) { + dialogService.notify(Localization.lang("Error while writing metadata. See the error log for details.")); + logger.error("Error while writing metadata to {}", file.map(Path::toString).orElse(""), ex); + } + } + } + return null; + }); + writeTask + .onRunning(() -> setExecutable(false)) + .onFinished(() -> setExecutable(true)); + taskExecutor.execute(writeTask); + } +}