diff --git a/src/main/java/org/jabref/gui/filelist/AttachFileAction.java b/src/main/java/org/jabref/gui/filelist/AttachFileAction.java index ff9a1ae3bf7..fc186923784 100644 --- a/src/main/java/org/jabref/gui/filelist/AttachFileAction.java +++ b/src/main/java/org/jabref/gui/filelist/AttachFileAction.java @@ -14,6 +14,7 @@ import org.jabref.gui.util.FileDialogConfiguration; import org.jabref.logic.l10n.Localization; import org.jabref.model.FieldChange; +import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; import org.jabref.preferences.PreferencesService; @@ -36,38 +37,43 @@ public AttachFileAction(BasePanel panel, DialogService dialogService, StateManag @Override public void execute() { + if (stateManager.getActiveDatabase().isEmpty()) { + dialogService.notify(Localization.lang("This operation requires exactly an open library.")); + return; + } + if (stateManager.getSelectedEntries().size() != 1) { dialogService.notify(Localization.lang("This operation requires exactly one item to be selected.")); return; } - stateManager.getActiveDatabase().ifPresent(databaseContext -> { - BibEntry entry = stateManager.getSelectedEntries().get(0); + BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get(); + + BibEntry entry = stateManager.getSelectedEntries().get(0); - Path workingDirectory = databaseContext.getFirstExistingFileDir(preferencesService.getFilePreferences()) - .orElse(preferencesService.getWorkingDir()); + Path workingDirectory = databaseContext.getFirstExistingFileDir(preferencesService.getFilePreferences()) + .orElse(preferencesService.getWorkingDir()); - FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() - .withInitialDirectory(workingDirectory) - .build(); + FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() + .withInitialDirectory(workingDirectory) + .build(); - dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> { - LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile(newFile, - databaseContext.getFileDirectoriesAsPaths(preferencesService.getFilePreferences()), - ExternalFileTypes.getInstance()); + dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> { + LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile(newFile, + databaseContext.getFileDirectoriesAsPaths(preferencesService.getFilePreferences()), + ExternalFileTypes.getInstance()); - LinkedFileEditDialogView dialog = new LinkedFileEditDialogView(linkedFile); + LinkedFileEditDialogView dialog = new LinkedFileEditDialogView(linkedFile); - dialog.showAndWait() - .ifPresent(editedLinkedFile -> { - Optional fieldChange = entry.addFile(editedLinkedFile); - fieldChange.ifPresent(change -> { - UndoableFieldChange ce = new UndoableFieldChange(change); - panel.getUndoManager().addEdit(ce); - panel.markBaseChanged(); - }); + dialog.showAndWait() + .ifPresent(editedLinkedFile -> { + Optional fieldChange = entry.addFile(editedLinkedFile); + fieldChange.ifPresent(change -> { + UndoableFieldChange ce = new UndoableFieldChange(change); + panel.getUndoManager().addEdit(ce); + panel.markBaseChanged(); }); - }); + }); }); } }