Skip to content

Commit

Permalink
Disable Write XMP Button in General tab of Entry-Editor when action i…
Browse files Browse the repository at this point in the history
…s in progress (#8728)

* fixes #8691
Disable the button before starting the 'write' task then re-enables the button after the task is complete

* Update CHANGELOG.md

* fixes #8691
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

* fixes #8691
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

* Update WriteMetadataToPdfCommand.java

Resolving merge conflict

* fixes #8691
Disable the button before starting the 'write' task then re-enables the button after the task is complete
Resolve checkstyle issues

* fixes #8691
Disable the button before starting the 'write' task then re-enables the button after the task is complete
Resolve checkstyle issues

Co-authored-by: Mohamed El-Morsy <[email protected]>
  • Loading branch information
melmorsy and Mohamed El-Morsy authored May 30, 2022
1 parent 5c26570 commit 1100526
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 2 additions & 29 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,31 +413,8 @@ public void edit() {
});
}

public void writeMetadataToPdf() {
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}
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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}
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);
}
}

0 comments on commit 1100526

Please sign in to comment.