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

Source tab entry duplication #3360

Merged
merged 6 commits into from
Oct 27, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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
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
46 changes: 18 additions & 28 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 Down Expand Up @@ -44,13 +43,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 +77,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 +116,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 +147,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 +204,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