Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a "view as BibTeX" option before importing an entry from the cita… #11847

Merged
merged 8 commits into from
Oct 3, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.collab.entrychange.PreviewWithSourceTab;
import org.jabref.gui.desktop.os.NativeDesktop;
import org.jabref.gui.entryeditor.EntryEditor;
import org.jabref.gui.entryeditor.EntryEditorTab;
import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.CitationFetcher;
import org.jabref.gui.entryeditor.citationrelationtab.semanticscholar.SemanticScholarFetcher;
Expand All @@ -43,6 +43,7 @@
import org.jabref.gui.util.NoSelectionModel;
import org.jabref.gui.util.ViewModelListCellFactory;
import org.jabref.logic.bibtex.BibEntryWriter;
import org.jabref.logic.bibtex.FieldPreferences;
import org.jabref.logic.bibtex.FieldWriter;
import org.jabref.logic.database.DuplicateCheck;
import org.jabref.logic.exporter.BibWriter;
Expand Down Expand Up @@ -87,9 +88,7 @@ public class CitationRelationsTab extends EntryEditorTab {
private final BibEntryRelationsRepository bibEntryRelationsRepository;
private final CitationsRelationsTabViewModel citationsRelationsTabViewModel;
private final DuplicateCheck duplicateCheck;
private EntryEditor entryEditor;

private StateManager stateManager;
private final BibEntryTypesManager entryTypesManager;

public CitationRelationsTab(DialogService dialogService,
BibDatabaseContext databaseContext,
Expand All @@ -103,12 +102,12 @@ public CitationRelationsTab(DialogService dialogService,
this.databaseContext = databaseContext;
this.preferences = preferences;
this.libraryTab = libraryTab;
this.stateManager = stateManager;
this.taskExecutor = taskExecutor;
setText(Localization.lang("Citation relations"));
setTooltip(new Tooltip(Localization.lang("Show articles related by citation")));

this.duplicateCheck = new DuplicateCheck(new BibEntryTypesManager());
this.entryTypesManager = new BibEntryTypesManager();
JamseBonde007 marked this conversation as resolved.
Show resolved Hide resolved
this.duplicateCheck = new DuplicateCheck(entryTypesManager);
this.bibEntryRelationsRepository = new BibEntryRelationsRepository(new SemanticScholarFetcher(preferences.getImporterPreferences()),
new BibEntryRelationsCache());
citationsRelationsTabViewModel = new CitationsRelationsTabViewModel(databaseContext, preferences, undoManager, stateManager, dialogService, fileUpdateMonitor, taskExecutor);
Expand Down Expand Up @@ -272,7 +271,7 @@ private void styleFetchedListView(CheckListView<CitationRelationItem> listView)
}

Button showEntrySource = IconTheme.JabRefIcons.SOURCE.asButton();
showEntrySource.setTooltip(new Tooltip(Localization.lang("%0 source", databaseContext.getMode().getFormattedName())));
showEntrySource.setTooltip(new Tooltip(Localization.lang("%0 source", "BibTeX")));
showEntrySource.setOnMouseClicked(event -> {
showEntrySourceDialog(entry.entry());
});
Expand All @@ -295,18 +294,21 @@ private void styleFetchedListView(CheckListView<CitationRelationItem> listView)
listView.setSelectionModel(new NoSelectionModel<>());
}

private String getSourceString(BibEntry entry, BibDatabaseMode type) throws IOException {
/**
* @implNote This code is similar to {@link PreviewWithSourceTab#getSourceString(BibEntry, BibDatabaseMode, FieldPreferences, BibEntryTypesManager)}.
*/
private String getSourceString(BibEntry entry, BibDatabaseMode type, FieldPreferences fieldPreferences, BibEntryTypesManager entryTypesManager) throws IOException {
StringWriter writer = new StringWriter();
BibWriter bibWriter = new BibWriter(writer, OS.NEWLINE);
FieldWriter fieldWriter = FieldWriter.buildIgnoreHashes(this.preferences.getFieldPreferences());
new BibEntryWriter(fieldWriter, new BibEntryTypesManager()).write(entry, bibWriter, type);
FieldWriter fieldWriter = FieldWriter.buildIgnoreHashes(fieldPreferences);
new BibEntryWriter(fieldWriter, entryTypesManager).write(entry, bibWriter, type);
return writer.toString();
}

private void showEntrySourceDialog(BibEntry entry) {
CodeArea ca = new CodeArea();
try {
ca.appendText(getSourceString(entry, databaseContext.getMode()));
ca.appendText(getSourceString(entry, databaseContext.getMode(), preferences.getFieldPreferences(), this.entryTypesManager));
} catch (IOException e) {
LOGGER.warn("Incorrect entry, could not load source:", e);
return;
Expand All @@ -324,7 +326,7 @@ private void showEntrySourceDialog(BibEntry entry) {
DialogPane dialogPane = new DialogPane();
dialogPane.setPrefSize(800, 400);
dialogPane.setContent(scrollPane);
String title = Localization.lang("%0 source", "Show BibTeX");
String title = Localization.lang("Show BibTeX source");

dialogService.showCustomDialogAndWait(title, dialogPane, ButtonType.OK);
}
Expand Down
Loading