Skip to content

Commit

Permalink
Fix problems in source editor (#3398)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasdiez authored and Siedlerchr committed Nov 4, 2017
1 parent c6f39cb commit 248995f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 22 deletions.
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 @@ -339,7 +339,7 @@ private List<EntryEditorTab> createTabs() {
tabs.add(new RelatedArticlesTab(Globals.prefs));

// Source tab
sourceTab = new SourceTab(panel);
sourceTab = new SourceTab(panel.getBibDatabaseContext(), panel.getUndoManager(), Globals.prefs.getLatexFieldFormatterPreferences());
tabs.add(sourceTab);
return tabs;
}
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/org/jabref/gui/entryeditor/SourceTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
import javafx.scene.control.Tooltip;

import org.jabref.Globals;
import org.jabref.gui.BasePanel;
import org.jabref.gui.IconTheme;
import org.jabref.gui.undo.CountingUndoManager;
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableChangeType;
import org.jabref.gui.undo.UndoableFieldChange;
Expand All @@ -24,10 +24,12 @@
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.InvalidFieldValueException;
import org.jabref.logic.bibtex.LatexFieldFormatter;
import org.jabref.logic.bibtex.LatexFieldFormatterPreferences;
import org.jabref.logic.importer.ParserResult;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.InternalBibtexFields;
Expand All @@ -43,23 +45,24 @@
public class SourceTab extends EntryEditorTab {

private static final Log LOGGER = LogFactory.getLog(SourceTab.class);
private final LatexFieldFormatterPreferences fieldFormatterPreferences;
private final BibDatabaseMode mode;
private UndoManager undoManager;
private final ObjectProperty<ValidationMessage> sourceIsValid = new SimpleObjectProperty<>();
private final ObservableRuleBasedValidator sourceValidator = new ObservableRuleBasedValidator(sourceIsValid);

public SourceTab(BasePanel panel) {
this.mode = panel.getBibDatabaseContext().getMode();
public SourceTab(BibDatabaseContext bibDatabaseContext, CountingUndoManager undoManager, LatexFieldFormatterPreferences fieldFormatterPreferences) {
this.mode = bibDatabaseContext.getMode();
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());
this.undoManager = panel.getUndoManager();
this.undoManager = undoManager;
this.fieldFormatterPreferences = fieldFormatterPreferences;
}

private static String getSourceString(BibEntry entry, BibDatabaseMode type) throws IOException {
private static String getSourceString(BibEntry entry, BibDatabaseMode type, LatexFieldFormatterPreferences fieldFormatterPreferences) throws IOException {
StringWriter stringWriter = new StringWriter(200);
LatexFieldFormatter formatter = LatexFieldFormatter
.buildIgnoreHashes(Globals.prefs.getLatexFieldFormatterPreferences());
LatexFieldFormatter formatter = LatexFieldFormatter.buildIgnoreHashes(fieldFormatterPreferences);
new BibEntryWriter(formatter, false).writeWithoutPrependedNewlines(entry, stringWriter, type);

return stringWriter.getBuffer().toString();
Expand All @@ -68,8 +71,7 @@ private static String getSourceString(BibEntry entry, BibDatabaseMode type) thro
private CodeArea createSourceEditor() {
CodeArea codeArea = new CodeArea();
codeArea.setWrapText(true);
codeArea.lookup(".styled-text-area").setStyle(
"-fx-font-size: " + Globals.prefs.getFontSizeFX() + "pt;");
codeArea.lookup(".styled-text-area").setStyle("-fx-font-size: " + Globals.prefs.getFontSizeFX() + "pt;");
return codeArea;
}

Expand All @@ -91,15 +93,15 @@ protected void bindToEntry(BibEntry entry) {
sourceValidator.getValidationStatus().getHighestMessage().ifPresent(validationMessage -> notificationPane.show(validationMessage.getMessage()));
}
});
this.setContent(notificationPane);
this.setContent(codeArea);

// Store source for every change in the source code
// and update source code for every change of entry field values
BindingsHelper.bindContentBidirectional(entry.getFieldsObservable(), codeArea.textProperty(), this::storeSource, fields -> {
DefaultTaskExecutor.runInJavaFXThread(() -> {
codeArea.clear();
try {
codeArea.appendText(getSourceString(entry, mode));
codeArea.appendText(getSourceString(entry, mode, fieldFormatterPreferences));
} catch (IOException ex) {
codeArea.setEditable(false);
codeArea.appendText(ex.getMessage() + "\n\n" +
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/org/jabref/gui/util/DefaultTaskExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ public class DefaultTaskExecutor implements TaskExecutor {
public static <V> V runInJavaFXThread(Callable<V> callable) {
FutureTask<V> task = new FutureTask<>(callable);

if (!Platform.isFxApplicationThread()) {
Platform.runLater(task);
} else {
EXECUTOR.submit(task);
}
Platform.runLater(task);

try {
return task.get();
Expand All @@ -43,11 +39,7 @@ public static <V> V runInJavaFXThread(Callable<V> callable) {
}

public static void runInJavaFXThread(Runnable runnable) {
if (!Platform.isFxApplicationThread()) {
Platform.runLater(runnable);
} else {
EXECUTOR.submit(runnable);
}
Platform.runLater(runnable);
}

@Override
Expand Down

0 comments on commit 248995f

Please sign in to comment.