package org.jabref.gui.filelist;

import java.util.Optional;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.DialogService;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.FileDialogConfiguration;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.FieldChange;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.preferences.JabRefPreferences;

public class AttachFileAction extends SimpleCommand {

    private final BasePanel panel;
    private final DialogService dialogService;

    public AttachFileAction(BasePanel panel, DialogService dialogService) {
        this.panel = panel;
        this.dialogService = dialogService;
    }

    @Override
    public void execute() {
        if (panel.getSelectedEntries().size() != 1) {
            panel.output(Localization.lang("This operation requires exactly one item to be selected."));
            return;
        }
        BibEntry entry = panel.getSelectedEntries().get(0);
        FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder()
                                                                                               .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY))
                                                                                               .build();

        dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> {
            LinkedFile newLinkedFile = new LinkedFile("", newFile.toString(), "");

            LinkedFileEditDialogView dialog = new LinkedFileEditDialogView(newLinkedFile);

            Optional<LinkedFile> editedLinkeDfile = dialog.showAndWait();
            editedLinkeDfile.ifPresent(file -> {

                Optional<FieldChange> fieldChange = entry.addFile(file);
                fieldChange.ifPresent(change -> {
                    UndoableFieldChange ce = new UndoableFieldChange(change);
                    panel.getUndoManager().addEdit(ce);
                    panel.markBaseChanged();
                });
            });
        });
    }
}

@@ -1209,244 +1209,86 @@
     
-                                                                               .withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY))%0A                                                                               
+.withInitialDirectory(Globals.prefs.get(JabRefPreferences.WORKING_DIRECTORY))%0A
     
@@ -1590,48 +1590,8 @@
     
- Optional%3CLinkedFile%3E editedLinkeDfile =
  dia
@@ -1607,18 +1607,19 @@
 ndWait()
-;
 %0A
+  
         
@@ -1626,36 +1626,36 @@
     
-editedLinkeDfile.ifPresent(f
+    .ifPresent(editedLinkedF
 ile 
@@ -1659,17 +1659,22 @@
 le -%3E %7B%0A
-%0A
+      
         
@@ -1731,17 +1731,29 @@
 addFile(
-f
+editedLinkedF
 ile);%0A  
@@ -1766,16 +1766,22 @@
         
+   
+   
 fieldCha
@@ -1802,24 +1802,30 @@
 change -%3E %7B%0A
+      
             
@@ -1906,24 +1906,30 @@
             
+      
 panel.getUnd
@@ -1968,24 +1968,30 @@
             
+  
+    
 panel.markBa
@@ -2011,36 +2011,48 @@
                 
-%7D);%0A
+      %7D);%0A      
             %7D);%0A

Fix formatting