Skip to content

Commit

Permalink
fixes JabRef#8691
Browse files Browse the repository at this point in the history
Disable the button before starting the 'write' task then re-enables the button after the task is complete
Create WriteMetadataToPdfCommand, bind its 'executable' property to the button's 'disable' property
  • Loading branch information
Mohamed El-Morsy committed May 23, 2022
1 parent a854a5c commit 915f86d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 35 deletions.
33 changes: 2 additions & 31 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -417,33 +413,8 @@ public void edit() {
});
}

public void writeMetadataToPdf(Runnable preWrite, Runnable postWrite) {
BackgroundTask<Void> writeTask = BackgroundTask.wrap(() -> {
Optional<Path> 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}
XmpUtilWriter.writeXmp(file.get(), entry, databaseContext.getDatabase(), preferences.getXmpPreferences());

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(preWrite)
.onFinished(postWrite);
taskExecutor.execute(writeTask);
public WriteMetadataToPdfCommand createWriteMetadataToPdfCommand() {
return new WriteMetadataToPdfCommand(linkedFile, databaseContext, preferences, dialogService, entry, LOGGER, taskExecutor);
}

public void download() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.List;
import java.util.Optional;

import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.ObservableList;
Expand Down Expand Up @@ -183,9 +182,10 @@ private Node createFileDisplay(LinkedFileViewModel linkedFile) {
writeMetadataToPdf.setTooltip(new Tooltip(Localization.lang("Write BibTeXEntry metadata to PDF.")));
writeMetadataToPdf.visibleProperty().bind(linkedFile.isOfflinePdfProperty());
writeMetadataToPdf.getStyleClass().setAll("icon-button");
Runnable disableButtonBeforeWriteStarts = () -> Platform.runLater(() -> writeMetadataToPdf.setDisable(true));
Runnable enableButtonAfterWriteCompletes = () -> Platform.runLater(() -> writeMetadataToPdf.setDisable(false));
writeMetadataToPdf.setOnAction(event -> linkedFile.writeMetadataToPdf(disableButtonBeforeWriteStarts, enableButtonAfterWriteCompletes));

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.")));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.jabref.gui.fieldeditors;

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;

import javax.xml.transform.TransformerException;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;

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<Void> writeTask = BackgroundTask.wrap(() -> {
Optional<Path> 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 {
Thread.sleep(3000);
// Similar code can be found at {@link org.jabref.gui.exporter.WriteMetadataToPdfAction.writeMetadataToFile}
XmpUtilWriter.writeXmp(file.get(), entry, databaseContext.getDatabase(), preferences.getXmpPreferences());

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);
}
}

0 comments on commit 915f86d

Please sign in to comment.