Skip to content

Commit

Permalink
move logic to viewModel (JabRef#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMachiei committed Jan 20, 2024
1 parent 654fd1c commit fae3f8b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
29 changes: 5 additions & 24 deletions src/main/java/org/jabref/gui/importer/ImportEntriesDialog.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
}
Expand All @@ -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");
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit fae3f8b

Please sign in to comment.