forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
78 additions
and
35 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
72 changes: 72 additions & 0 deletions
72
src/main/java/org/jabref/gui/fieldeditors/WriteMetadataToPdfCommand.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 |
---|---|---|
@@ -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); | ||
} | ||
} |