-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add "Import from Citavi" to Welcome Tab #14672
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
Closed
ItsYash1421
wants to merge
16
commits into
JabRef:main
from
ItsYash1421:add-citavi-import-welcome-tab
Closed
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
db68caf
Add 'Import from Citavi' to Welcome Tab
ItsYash1421 92f2c20
Fixes
ItsYash1421 153a5f4
Fix-2
ItsYash1421 eb9bce7
Simplify ImportCitaviAction and remember last directory
ItsYash1421 6868ad5
Fixes
ItsYash1421 50a32ee
Fixess
ItsYash1421 11cbfbd
Fix
ItsYash1421 00092ab
Fix
ItsYash1421 abe6ee6
Merge branch 'main' into add-citavi-import-welcome-tab
ItsYash1421 bb7be08
Trigger CI rebuild after reverting problematic commits
ItsYash1421 c7b1ed6
Trigger CI rebuild after reverting problematic commits
ItsYash1421 a1ed1ce
Merge previous commits to maintain history
ItsYash1421 881fd26
Merge branch 'main' into add-citavi-import-welcome-tab
ItsYash1421 48ef18c
Merge branch 'main' into add-citavi-import-welcome-tab
ItsYash1421 50dd949
Merge branch 'main' into add-citavi-import-welcome-tab
koppor 98227e7
Merge branch 'main' into add-citavi-import-welcome-tab
ItsYash1421 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
jabgui/src/main/java/org/jabref/gui/importer/actions/ImportCitaviAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| package org.jabref.gui.importer.actions; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
|
|
||
| import org.jabref.gui.DialogService; | ||
| import org.jabref.gui.LibraryTabContainer; | ||
| import org.jabref.gui.actions.SimpleCommand; | ||
| import org.jabref.gui.util.FileDialogConfiguration; | ||
| import org.jabref.logic.importer.ParserResult; | ||
| import org.jabref.logic.importer.fileformat.CitaviXmlImporter; | ||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.logic.preferences.CliPreferences; | ||
| import org.jabref.logic.util.BackgroundTask; | ||
| import org.jabref.logic.util.StandardFileType; | ||
| import org.jabref.logic.util.TaskExecutor; | ||
| import org.jabref.logic.util.UpdateField; | ||
| import org.jabref.model.util.FileUpdateMonitor; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** | ||
| * Action to import Citavi XML files into a new library tab. | ||
| * This action preserves citation keys from Citavi by temporarily disabling | ||
| * the "Generate a new key for imported entries" preference. | ||
| */ | ||
| public class ImportCitaviAction extends SimpleCommand { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(ImportCitaviAction.class); | ||
|
|
||
| private final LibraryTabContainer tabContainer; | ||
| private final DialogService dialogService; | ||
| private final CliPreferences preferences; | ||
| private final FileUpdateMonitor fileUpdateMonitor; | ||
| private final TaskExecutor taskExecutor; | ||
|
|
||
| public ImportCitaviAction(LibraryTabContainer tabContainer, | ||
| CliPreferences preferences, | ||
| FileUpdateMonitor fileUpdateMonitor, | ||
| TaskExecutor taskExecutor, | ||
| DialogService dialogService) { | ||
| this.tabContainer = tabContainer; | ||
| this.preferences = preferences; | ||
| this.fileUpdateMonitor = fileUpdateMonitor; | ||
| this.taskExecutor = taskExecutor; | ||
| this.dialogService = dialogService; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() | ||
| .addExtensionFilter(StandardFileType.CITAVI) | ||
| .withInitialDirectory(preferences.getImporterPreferences().getImportWorkingDirectory()) | ||
| .build(); | ||
|
|
||
| List<Path> selectedFiles = dialogService.showFileOpenDialogAndGetMultipleFiles(fileDialogConfiguration); | ||
|
|
||
| if (selectedFiles.isEmpty()) { | ||
| return; | ||
| } | ||
|
|
||
| importCitaviFiles(selectedFiles); | ||
| } | ||
|
|
||
| private void importCitaviFiles(List<Path> files) { | ||
| for (Path file : files) { | ||
| if (!Files.exists(file)) { | ||
| dialogService.showErrorDialogAndWait(Localization.lang("Import from Citavi"), | ||
| Localization.lang("File not found") + ": '" + file.getFileName() + "'."); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| BackgroundTask<ParserResult> task = BackgroundTask.wrap(() -> doImport(files)); | ||
|
|
||
| task.onSuccess(parserResult -> { | ||
| tabContainer.addTab(parserResult.getDatabaseContext(), true); | ||
| dialogService.notify(Localization.lang("%0 entry(s) imported", parserResult.getDatabase().getEntries().size())); | ||
| }) | ||
| .onFailure(ex -> { | ||
| LOGGER.error("Error importing from Citavi", ex); | ||
| dialogService.notify(Localization.lang("Error importing. See the error log for details.")); | ||
| }) | ||
| .executeWith(taskExecutor); | ||
|
|
||
| if (!files.isEmpty()) { | ||
| preferences.getImporterPreferences().setImportWorkingDirectory(files.getLast().getParent()); | ||
| } | ||
| } | ||
|
|
||
| private ParserResult doImport(List<Path> files) throws IOException { | ||
| CitaviXmlImporter importer = new CitaviXmlImporter(); | ||
| ParserResult result = new ParserResult(); | ||
|
|
||
| for (Path file : files) { | ||
| dialogService.notify(Localization.lang("Importing in %0 format", importer.getName()) + "..."); | ||
| ParserResult fileResult = importer.importDatabase(file); | ||
|
|
||
| result.getDatabaseContext().getDatabase().insertEntries(fileResult.getDatabase().getEntries()); | ||
| } | ||
|
|
||
| UpdateField.setAutomaticFields( | ||
| result.getDatabase().getEntries(), | ||
| preferences.getOwnerPreferences(), | ||
| preferences.getTimestampPreferences()); | ||
|
|
||
| return result; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
File %0 not found. See #14692There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ItsYash1421 Why didn't you follow-up here?