88
99import javax .swing .undo .UndoManager ;
1010
11- import javafx .beans .property .BooleanProperty ;
1211import javafx .scene .Node ;
1312import javafx .scene .control .Tooltip ;
1413
2019import org .jabref .gui .undo .NamedCompound ;
2120import org .jabref .gui .undo .UndoableChangeType ;
2221import org .jabref .gui .undo .UndoableFieldChange ;
23- import org .jabref .gui .util .DefaultTaskExecutor ;
2422import org .jabref .logic .bibtex .BibEntryWriter ;
2523import org .jabref .logic .bibtex .InvalidFieldValueException ;
2624import 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