From fae3f8b8087e38dca6e61631d1435e544d5a22bf Mon Sep 17 00:00:00 2001 From: MrMachiei Date: Sat, 20 Jan 2024 17:31:16 +0100 Subject: [PATCH] move logic to viewModel (#560) --- .../gui/importer/ImportEntriesDialog.java | 29 ++++--------------- .../gui/importer/ImportEntriesViewModel.java | 19 ++++++++++++ 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/jabref/gui/importer/ImportEntriesDialog.java b/src/main/java/org/jabref/gui/importer/ImportEntriesDialog.java index e4ccb98f746..a2259549c75 100644 --- a/src/main/java/org/jabref/gui/importer/ImportEntriesDialog.java +++ b/src/main/java/org/jabref/gui/importer/ImportEntriesDialog.java @@ -1,7 +1,5 @@ package org.jabref.gui.importer; -import java.io.IOException; -import java.io.StringWriter; import java.util.EnumSet; import java.util.Optional; @@ -40,13 +38,9 @@ import org.jabref.gui.util.TaskExecutor; import org.jabref.gui.util.TextFlowLimited; import org.jabref.gui.util.ViewModelListCellFactory; -import org.jabref.logic.bibtex.BibEntryWriter; -import org.jabref.logic.bibtex.FieldWriter; -import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.l10n.Localization; import org.jabref.logic.shared.DatabaseLocation; -import org.jabref.logic.util.OS; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; @@ -182,7 +176,7 @@ private void initialize() { }) .withOnMouseClickedEvent((entry, event) -> { entriesListView.getCheckModel().toggleCheckState(entry); - displayBibTeX(entry); + displayBibTeX(entry, viewModel.getSourceString(entry)); }) .withPseudoClass(entrySelected, entriesListView::getItemBooleanProperty) .install(entriesListView); @@ -193,17 +187,12 @@ private void initialize() { initBibTeX(); } - private void displayBibTeX(BibEntry entry) { + private void displayBibTeX(BibEntry entry, String bibTeX) { if (entriesListView.getCheckModel().isChecked(entry)) { bibTeXData.clear(); - try { - bibTeXData.appendText(getSourceString(entry)); - bibTeXData.moveTo(0); - bibTeXData.requestFollowCaret(); - } catch ( - IOException aE) { - bibTeXData.clear(); - } + bibTeXData.appendText(bibTeX); + bibTeXData.moveTo(0); + bibTeXData.requestFollowCaret(); } else { bibTeXData.clear(); } @@ -218,14 +207,6 @@ private void initBibTeX() { }); } - private String getSourceString(BibEntry entry) throws IOException { - StringWriter writer = new StringWriter(); - BibWriter bibWriter = new BibWriter(writer, OS.NEWLINE); - FieldWriter fieldWriter = FieldWriter.buildIgnoreHashes(preferences.getFieldPreferences()); - new BibEntryWriter(fieldWriter, entryTypesManager).write(entry, bibWriter, database.getMode()); - return writer.toString(); - } - private Node getEntryNode(BibEntry entry) { Node entryType = getIcon(entry.getType()).getGraphicNode(); entryType.getStyleClass().add("type"); diff --git a/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java b/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java index eeffe49a74c..cdcb6512285 100644 --- a/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java +++ b/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java @@ -1,5 +1,7 @@ package org.jabref.gui.importer; +import java.io.IOException; +import java.io.StringWriter; import java.util.List; import java.util.Optional; @@ -20,10 +22,14 @@ import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.TaskExecutor; +import org.jabref.logic.bibtex.BibEntryWriter; +import org.jabref.logic.bibtex.FieldWriter; import org.jabref.logic.database.DatabaseMerger; import org.jabref.logic.database.DuplicateCheck; +import org.jabref.logic.exporter.BibWriter; import org.jabref.logic.importer.ParserResult; import org.jabref.logic.l10n.Localization; +import org.jabref.logic.util.OS; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.BibEntryTypesManager; @@ -117,6 +123,19 @@ public boolean hasDuplicate(BibEntry entry) { .containsDuplicate(selectedDb.getValue().getDatabase(), entry, selectedDb.getValue().getMode()).isPresent(); } + public String getSourceString(BibEntry entry) { + StringWriter writer = new StringWriter(); + BibWriter bibWriter = new BibWriter(writer, OS.NEWLINE); + FieldWriter fieldWriter = FieldWriter.buildIgnoreHashes(preferences.getFieldPreferences()); + try { + new BibEntryWriter(fieldWriter, entryTypesManager).write(entry, bibWriter, selectedDb.getValue().getMode()); + } catch ( + IOException ioException) { + return null; + } + return writer.toString(); + } + /** * Called after the user selected the entries to import. Does the real import stuff. *