Skip to content

Commit 6bb21d4

Browse files
authored
Source tab entry duplication (#3360)
* Refactor entry change logic * Reuse currentEntry in SourceTab * Remove unused import * Add changelog entry * Remove update that has no effect * Remove unused import
1 parent 3d4b510 commit 6bb21d4

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `#
3535
- We improved font rendering of the Entry Editor for Linux based systems [#3295](https://github.com/JabRef/jabref/issues/3295)
3636
- 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)
3737
- We fixed an issue where JabRef would not show the translated content at some points, although there existed a translation
38+
- 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)
3839
### Removed
3940

4041

src/main/java/org/jabref/gui/entryeditor/EntryEditor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ private List<EntryEditorTab> createTabs() {
346346
tabs.add(new RelatedArticlesTab(Globals.prefs));
347347

348348
// Source tab
349-
sourceTab = new SourceTab(panel, movingToDifferentEntry);
349+
sourceTab = new SourceTab(panel);
350350
tabs.add(sourceTab);
351351
return tabs;
352352
}

src/main/java/org/jabref/gui/entryeditor/SourceTab.java

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import javax.swing.undo.UndoManager;
1010

11-
import javafx.beans.property.BooleanProperty;
1211
import javafx.scene.Node;
1312
import javafx.scene.control.Tooltip;
1413

@@ -20,7 +19,6 @@
2019
import org.jabref.gui.undo.NamedCompound;
2120
import org.jabref.gui.undo.UndoableChangeType;
2221
import org.jabref.gui.undo.UndoableFieldChange;
23-
import org.jabref.gui.util.DefaultTaskExecutor;
2422
import org.jabref.logic.bibtex.BibEntryWriter;
2523
import org.jabref.logic.bibtex.InvalidFieldValueException;
2624
import org.jabref.logic.bibtex.LatexFieldFormatter;
@@ -44,13 +42,11 @@ public class SourceTab extends EntryEditorTab {
4442
private final BibDatabaseMode mode;
4543
private final BasePanel panel;
4644
private CodeArea codeArea;
47-
private BooleanProperty movingToDifferentEntry;
4845
private UndoManager undoManager;
4946

50-
public SourceTab(BasePanel panel, BooleanProperty movingToDifferentEntry) {
47+
public SourceTab(BasePanel panel) {
5148
this.mode = panel.getBibDatabaseContext().getMode();
5249
this.panel = panel;
53-
this.movingToDifferentEntry = movingToDifferentEntry;
5450
this.setText(Localization.lang("%0 source", mode.getFormattedName()));
5551
this.setTooltip(new Tooltip(Localization.lang("Show/edit %0 source", mode.getFormattedName())));
5652
this.setGraphic(IconTheme.JabRefIcon.SOURCE.getGraphicNode());
@@ -80,27 +76,20 @@ public void updateSourcePane(BibEntry entry) {
8076
}
8177
}
8278

83-
private Node createSourceEditor(BibEntry entry, BibDatabaseMode mode) {
79+
private Node createSourceEditor(BibDatabaseMode mode) {
8480
codeArea = new CodeArea();
8581
codeArea.setWrapText(true);
8682
codeArea.lookup(".styled-text-area").setStyle(
8783
"-fx-font-size: " + Globals.prefs.getFontSizeFX() + "pt;");
8884
// store source if new tab is selected (if this one is not focused anymore)
8985
EasyBind.subscribe(codeArea.focusedProperty(), focused -> {
9086
if (!focused) {
91-
storeSource(entry);
92-
}
93-
});
94-
95-
// store source if new entry is selected in the maintable and the source tab is focused
96-
EasyBind.subscribe(movingToDifferentEntry, newEntrySelected -> {
97-
if (newEntrySelected && codeArea.focusedProperty().get()) {
98-
DefaultTaskExecutor.runInJavaFXThread(() -> storeSource(entry));
87+
storeSource();
9988
}
10089
});
10190

10291
try {
103-
String srcString = getSourceString(entry, mode);
92+
String srcString = getSourceString(this.currentEntry, mode);
10493
codeArea.appendText(srcString);
10594
} catch (IOException ex) {
10695
codeArea.appendText(ex.getMessage() + "\n\n" +
@@ -126,10 +115,10 @@ public boolean shouldShow(BibEntry entry) {
126115

127116
@Override
128117
protected void bindToEntry(BibEntry entry) {
129-
this.setContent(createSourceEditor(entry, mode));
118+
this.setContent(createSourceEditor(mode));
130119
}
131120

132-
private void storeSource(BibEntry entry) {
121+
private void storeSource() {
133122
if (codeArea.getText().isEmpty()) {
134123
return;
135124
}
@@ -157,42 +146,42 @@ private void storeSource(BibEntry entry) {
157146
String newKey = newEntry.getCiteKeyOptional().orElse(null);
158147

159148
if (newKey != null) {
160-
entry.setCiteKey(newKey);
149+
currentEntry.setCiteKey(newKey);
161150
} else {
162-
entry.clearCiteKey();
151+
currentEntry.clearCiteKey();
163152
}
164153

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

170159
if (InternalBibtexFields.isDisplayableField(fieldName) && !newEntry.hasField(fieldName)) {
171160
compound.addEdit(
172-
new UndoableFieldChange(entry, fieldName, fieldValue, null));
173-
entry.clearField(fieldName);
161+
new UndoableFieldChange(currentEntry, fieldName, fieldValue, null));
162+
currentEntry.clearField(fieldName);
174163
}
175164
}
176165

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

187-
compound.addEdit(new UndoableFieldChange(entry, fieldName, oldValue, newValue));
188-
entry.setField(fieldName, newValue);
176+
compound.addEdit(new UndoableFieldChange(currentEntry, fieldName, oldValue, newValue));
177+
currentEntry.setField(fieldName, newValue);
189178
}
190179
}
191180

192181
// See if the user has changed the entry type:
193-
if (!Objects.equals(newEntry.getType(), entry.getType())) {
194-
compound.addEdit(new UndoableChangeType(entry, entry.getType(), newEntry.getType()));
195-
entry.setType(newEntry.getType());
182+
if (!Objects.equals(newEntry.getType(), currentEntry.getType())) {
183+
compound.addEdit(new UndoableChangeType(currentEntry, currentEntry.getType(), newEntry.getType()));
184+
currentEntry.setType(newEntry.getType());
196185
}
197186
compound.end();
198187
undoManager.addEdit(compound);
@@ -214,7 +203,7 @@ private void storeSource(BibEntry entry) {
214203
if (!keepEditing) {
215204
// Revert
216205
try {
217-
codeArea.replaceText(0, codeArea.getText().length(), getSourceString(entry, mode));
206+
codeArea.replaceText(0, codeArea.getText().length(), getSourceString(this.currentEntry, mode));
218207
} catch (IOException e) {
219208
LOGGER.debug("Incorrect source", e);
220209
}

0 commit comments

Comments
 (0)