Skip to content

Commit

Permalink
Source tab entry duplication (#3360)
Browse files Browse the repository at this point in the history
* Refactor entry change logic

* Reuse currentEntry in SourceTab

* Remove unused import

* Add changelog entry

* Remove update that has no effect

* Remove unused import
  • Loading branch information
lenhard authored Oct 27, 2017
1 parent 3d4b510 commit 6bb21d4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
- We improved font rendering of the Entry Editor for Linux based systems [#3295](https://github.com/JabRef/jabref/issues/3295)
- We fixed an issue where JabRef would freeze when trying to replace the original entry after a merge with new information from identifiers like DOI/ISBN etc. [3294](https://github.com/JabRef/jabref/issues/3294)
- We fixed an issue where JabRef would not show the translated content at some points, although there existed a translation
- We fixed an issue where editing in the source tab would override content of other entries [#3352](https://github.com/JabRef/jabref/issues/3352#issue-268580818)
### Removed


Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/entryeditor/EntryEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ private List<EntryEditorTab> createTabs() {
tabs.add(new RelatedArticlesTab(Globals.prefs));

// Source tab
sourceTab = new SourceTab(panel, movingToDifferentEntry);
sourceTab = new SourceTab(panel);
tabs.add(sourceTab);
return tabs;
}
Expand Down
47 changes: 18 additions & 29 deletions src/main/java/org/jabref/gui/entryeditor/SourceTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import javax.swing.undo.UndoManager;

import javafx.beans.property.BooleanProperty;
import javafx.scene.Node;
import javafx.scene.control.Tooltip;

Expand All @@ -20,7 +19,6 @@
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableChangeType;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.DefaultTaskExecutor;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.InvalidFieldValueException;
import org.jabref.logic.bibtex.LatexFieldFormatter;
Expand All @@ -44,13 +42,11 @@ public class SourceTab extends EntryEditorTab {
private final BibDatabaseMode mode;
private final BasePanel panel;
private CodeArea codeArea;
private BooleanProperty movingToDifferentEntry;
private UndoManager undoManager;

public SourceTab(BasePanel panel, BooleanProperty movingToDifferentEntry) {
public SourceTab(BasePanel panel) {
this.mode = panel.getBibDatabaseContext().getMode();
this.panel = panel;
this.movingToDifferentEntry = movingToDifferentEntry;
this.setText(Localization.lang("%0 source", mode.getFormattedName()));
this.setTooltip(new Tooltip(Localization.lang("Show/edit %0 source", mode.getFormattedName())));
this.setGraphic(IconTheme.JabRefIcon.SOURCE.getGraphicNode());
Expand Down Expand Up @@ -80,27 +76,20 @@ public void updateSourcePane(BibEntry entry) {
}
}

private Node createSourceEditor(BibEntry entry, BibDatabaseMode mode) {
private Node createSourceEditor(BibDatabaseMode mode) {
codeArea = new CodeArea();
codeArea.setWrapText(true);
codeArea.lookup(".styled-text-area").setStyle(
"-fx-font-size: " + Globals.prefs.getFontSizeFX() + "pt;");
// store source if new tab is selected (if this one is not focused anymore)
EasyBind.subscribe(codeArea.focusedProperty(), focused -> {
if (!focused) {
storeSource(entry);
}
});

// store source if new entry is selected in the maintable and the source tab is focused
EasyBind.subscribe(movingToDifferentEntry, newEntrySelected -> {
if (newEntrySelected && codeArea.focusedProperty().get()) {
DefaultTaskExecutor.runInJavaFXThread(() -> storeSource(entry));
storeSource();
}
});

try {
String srcString = getSourceString(entry, mode);
String srcString = getSourceString(this.currentEntry, mode);
codeArea.appendText(srcString);
} catch (IOException ex) {
codeArea.appendText(ex.getMessage() + "\n\n" +
Expand All @@ -126,10 +115,10 @@ public boolean shouldShow(BibEntry entry) {

@Override
protected void bindToEntry(BibEntry entry) {
this.setContent(createSourceEditor(entry, mode));
this.setContent(createSourceEditor(mode));
}

private void storeSource(BibEntry entry) {
private void storeSource() {
if (codeArea.getText().isEmpty()) {
return;
}
Expand Down Expand Up @@ -157,42 +146,42 @@ private void storeSource(BibEntry entry) {
String newKey = newEntry.getCiteKeyOptional().orElse(null);

if (newKey != null) {
entry.setCiteKey(newKey);
currentEntry.setCiteKey(newKey);
} else {
entry.clearCiteKey();
currentEntry.clearCiteKey();
}

// First, remove fields that the user has removed.
for (Map.Entry<String, String> field : entry.getFieldMap().entrySet()) {
for (Map.Entry<String, String> field : currentEntry.getFieldMap().entrySet()) {
String fieldName = field.getKey();
String fieldValue = field.getValue();

if (InternalBibtexFields.isDisplayableField(fieldName) && !newEntry.hasField(fieldName)) {
compound.addEdit(
new UndoableFieldChange(entry, fieldName, fieldValue, null));
entry.clearField(fieldName);
new UndoableFieldChange(currentEntry, fieldName, fieldValue, null));
currentEntry.clearField(fieldName);
}
}

// Then set all fields that have been set by the user.
for (Map.Entry<String, String> field : newEntry.getFieldMap().entrySet()) {
String fieldName = field.getKey();
String oldValue = entry.getField(fieldName).orElse(null);
String oldValue = currentEntry.getField(fieldName).orElse(null);
String newValue = field.getValue();
if (!Objects.equals(oldValue, newValue)) {
// Test if the field is legally set.
new LatexFieldFormatter(Globals.prefs.getLatexFieldFormatterPreferences())
.format(newValue, fieldName);

compound.addEdit(new UndoableFieldChange(entry, fieldName, oldValue, newValue));
entry.setField(fieldName, newValue);
compound.addEdit(new UndoableFieldChange(currentEntry, fieldName, oldValue, newValue));
currentEntry.setField(fieldName, newValue);
}
}

// See if the user has changed the entry type:
if (!Objects.equals(newEntry.getType(), entry.getType())) {
compound.addEdit(new UndoableChangeType(entry, entry.getType(), newEntry.getType()));
entry.setType(newEntry.getType());
if (!Objects.equals(newEntry.getType(), currentEntry.getType())) {
compound.addEdit(new UndoableChangeType(currentEntry, currentEntry.getType(), newEntry.getType()));
currentEntry.setType(newEntry.getType());
}
compound.end();
undoManager.addEdit(compound);
Expand All @@ -214,7 +203,7 @@ private void storeSource(BibEntry entry) {
if (!keepEditing) {
// Revert
try {
codeArea.replaceText(0, codeArea.getText().length(), getSourceString(entry, mode));
codeArea.replaceText(0, codeArea.getText().length(), getSourceString(this.currentEntry, mode));
} catch (IOException e) {
LOGGER.debug("Incorrect source", e);
}
Expand Down

0 comments on commit 6bb21d4

Please sign in to comment.