package org.jabref.gui;

import java.util.Optional;

import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.TextArea;

import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.undo.UndoablePreambleChange;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabase;

class PreambleEditor extends SimpleCommand {

    private final TextArea editor = new TextArea();

    private final JabRefFrame frame;

    public PreambleEditor(JabRefFrame frame) {
        this.frame = frame;
    }

    @Override
    public void execute() {
        BasePanel panel = frame.getCurrentBasePanel();
        BibDatabase database = frame.getCurrentBasePanel().getDatabase();

        DialogPane pane = new DialogPane();

        editor.setText(frame.getCurrentBasePanel()
                .getDatabase()
                .getPreamble()
                .orElse(""));
        pane.setContent(editor);

        Optional<ButtonType> pressedButton = frame.getDialogService().showCustomDialogAndWait(Localization.lang("Edit Preamble"), pane, ButtonType.APPLY, ButtonType.CANCEL);

        if (pressedButton.isPresent() && pressedButton.get().equals(ButtonType.APPLY)) {
            String toSet = editor.getText();

            // We check if the field has changed, since we don't want to mark the
            // base as changed unless we have a real change.
            if (!database.getPreamble().orElse("").equals(toSet)) {

                panel.getUndoManager().addEdit(
                        new UndoablePreambleChange(database, database.getPreamble().orElse(null), toSet));
                database.setPreamble(toSet);

                panel.markBaseChanged();
            }
        }

    }

}

@@ -850,24 +850,36 @@
             
+            
 .getDatabase
@@ -893,24 +893,36 @@
             
+      
+      
 .getPreamble
@@ -920,24 +920,36 @@
 tPreamble()%0A
+            
             
@@ -1283,21 +1283,27 @@
  String 
-toSet
+newPreamble
  = edito
@@ -1517,21 +1517,27 @@
 .equals(
-toSet
+newPreamble
 )) %7B%0A%0A  
@@ -1680,21 +1680,27 @@
 (null), 
-toSet
+newPreamble
 ));%0A    
@@ -1736,13 +1736,19 @@
 ble(
-toSet
+newPreamble
 );%0A%0A

Fix formatting