Skip to content

Commit

Permalink
Refactored AttachFileAction
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Feb 18, 2020
1 parent b71360b commit 18a3439
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/main/java/org/jabref/gui/filelist/AttachFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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> fieldChange = entry.addFile(editedLinkedFile);
fieldChange.ifPresent(change -> {
UndoableFieldChange ce = new UndoableFieldChange(change);
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
});
dialog.showAndWait()
.ifPresent(editedLinkedFile -> {
Optional<FieldChange> fieldChange = entry.addFile(editedLinkedFile);
fieldChange.ifPresent(change -> {
UndoableFieldChange ce = new UndoableFieldChange(change);
panel.getUndoManager().addEdit(ce);
panel.markBaseChanged();
});
});
});
});
}
}

0 comments on commit 18a3439

Please sign in to comment.