package org.jabref.gui.fieldeditors;

import java.util.Collection;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

import org.jabref.gui.AbstractViewModel;
import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.model.entry.BibEntry;

import org.controlsfx.control.textfield.AutoCompletionBinding;

public class AbstractEditorViewModel extends AbstractViewModel {
    protected final String fieldName;
    protected StringProperty text = new SimpleStringProperty("");
    protected BibEntry entry;
    private final AutoCompleteSuggestionProvider<?> suggestionProvider;

    public AbstractEditorViewModel(String fieldName, AutoCompleteSuggestionProvider<?> suggestionProvider) {
        this.fieldName = fieldName;
        this.suggestionProvider = suggestionProvider;
    }

    public StringProperty textProperty() {
        return text;
    }

    public void bindToEntry(BibEntry entry) {
        this.entry = entry;
        BindingsHelper.bindBidirectional(
                this.textProperty(),
                entry.getFieldBinding(fieldName),
                newValue -> {
                    if (newValue != null) {
                        entry.setField(fieldName, newValue);
                    }
                });
    }

    public Collection<?> complete(AutoCompletionBinding.ISuggestionRequest request) {
        return suggestionProvider.call(request);
    }
}

@@ -61,16 +61,59 @@
 ction;%0A%0A
+import javafx.beans.binding.ObjectBinding;%0A
 import j
@@ -155,16 +155,16 @@
 operty;%0A
-
 import j
@@ -728,16 +728,64 @@
 rovider;
+%0A    private ObjectBinding%3CString%3E fieldBinding;
 %0A%0A    pu
@@ -1125,24 +1125,171 @@
 ry = entry;%0A
+%0A        // We need to keep a reference to the binding since it otherwise gets discarded%0A        fieldBinding = entry.getFieldBinding(fieldName);%0A%0A
         Bind
@@ -1371,26 +1371,17 @@
         
-entry.getF
+f
 ieldBind
@@ -1383,27 +1383,16 @@
 dBinding
-(fieldName)
 ,%0A      

Fix #2946: external changes are now correctly shown in the entry editor