Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional check if values have changed in bindToEntry #6528

Merged
merged 5 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.jabref.gui.util.BindingsHelper;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.logic.integrity.ValueChecker;
import org.jabref.logic.util.OS;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.Field;

Expand All @@ -22,6 +23,7 @@
import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
import de.saxsys.mvvmfx.utils.validation.ValidationMessage;
import de.saxsys.mvvmfx.utils.validation.Validator;
import org.apache.commons.lang3.StringUtils;
import org.controlsfx.control.textfield.AutoCompletionBinding;

public class AbstractEditorViewModel extends AbstractViewModel {
Expand Down Expand Up @@ -63,10 +65,15 @@ public void bindToEntry(BibEntry entry) {
fieldBinding,
newValue -> {
if (newValue != null) {
String oldValue = entry.getField(field).orElse(null);
entry.setField(field, newValue);
UndoManager undoManager = JabRefGUI.getMainFrame().getUndoManager();
undoManager.addEdit(new UndoableFieldChange(entry, field, oldValue, newValue));
// Controlsfx uses hardcoded \n for multiline fields, but JabRef stores them in OS Newlines format
String oldValue = entry.getField(field).map(value -> value.replace(OS.NEWLINE, "\n")).orElse(null);
// Autosave and save action trigger the entry editor to reload the fields, so we have to
// check for changes here, otherwise the cursor position is annoyingly reset every few seconds
if (!(newValue.trim()).equals(StringUtils.trim(oldValue))) {
entry.setField(field, newValue);
UndoManager undoManager = JabRefGUI.getMainFrame().getUndoManager();
undoManager.addEdit(new UndoableFieldChange(entry, field, oldValue, newValue));
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Objects;
import java.util.Set;

import org.jabref.logic.formatter.bibtexfields.NormalizeNewlinesFormatter;
import org.jabref.model.FieldChange;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.event.EntriesEventSource;
Expand Down Expand Up @@ -56,6 +57,9 @@ private List<FieldChange> cleanupSingleField(Field fieldKey, BibEntry entry) {

// Run formatter
String newValue = formatter.format(oldValue);
if (formatter instanceof NormalizeNewlinesFormatter) {
newValue = oldValue;
}

if (oldValue.equals(newValue)) {
return Collections.emptyList();
Expand Down