From 30108dc0e91ef3698d9c5ef35395143a7d30d172 Mon Sep 17 00:00:00 2001 From: Houssem Nasri Date: Thu, 11 Aug 2022 22:46:40 +0100 Subject: [PATCH 1/5] Fix dead link (#9047) --- docs/decisions/0001-use-crowdin-for-translations.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/decisions/0001-use-crowdin-for-translations.md b/docs/decisions/0001-use-crowdin-for-translations.md index 5ded77562e0e..af8ba405b1be 100644 --- a/docs/decisions/0001-use-crowdin-for-translations.md +++ b/docs/decisions/0001-use-crowdin-for-translations.md @@ -13,7 +13,7 @@ The JabRef UI is offered in multiple languages. It should be easy for translator * Use [Crowdin](http://crowdin.com/) * Use [popeye](https://github.com/JabRef/popeye) * Use [Lingohub](https://lingohub.com/) -* Keep current GitHub flow. See the [Step-by-step guide](https://docs.jabref.org/faq/how-to-translate-the-ui). +* Keep current GitHub flow. See the [Step-by-step guide](https://docs.jabref.org/contributing/how-to-translate-the-ui). ## Decision Outcome From 507a2635f40807d8882efdbe5c68454e00917c16 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Fri, 12 Aug 2022 14:22:12 +0200 Subject: [PATCH 2/5] ExternalFileTypes singleton refactor (#9044) * Simplify singleton * Fixed factory methods * Reworded for consistency * Moved externalFileTypes to preferences * Added deprecated notice * Moved preferences to FilePreferences * Extract static call in ExternalFileTypes methods * Made util functions static * Removed call to getExternalFileTypes * Removed singleton * Added tests * Cleanup * Fixed test --- .../org/jabref/cli/ArgumentProcessor.java | 4 +- .../org/jabref/gui/EntryTypeViewModel.java | 2 - src/main/java/org/jabref/gui/JabRefFrame.java | 3 +- src/main/java/org/jabref/gui/LibraryTab.java | 11 +- .../BibtexExtractorViewModel.java | 2 - .../org/jabref/gui/desktop/JabRefDesktop.java | 2 +- .../java/org/jabref/gui/desktop/os/Linux.java | 3 +- .../java/org/jabref/gui/desktop/os/OSX.java | 3 +- .../org/jabref/gui/desktop/os/Windows.java | 3 +- .../gui/entryeditor/DeprecatedFieldsTab.java | 4 +- .../jabref/gui/entryeditor/EntryEditor.java | 20 ++- .../gui/entryeditor/FieldsEditorTab.java | 6 +- .../gui/entryeditor/OptionalFields2Tab.java | 3 - .../gui/entryeditor/OptionalFieldsTab.java | 3 - .../entryeditor/OptionalFieldsTabBase.java | 3 - .../gui/entryeditor/OtherFieldsTab.java | 3 - .../jabref/gui/entryeditor/PreviewTab.java | 8 +- .../gui/entryeditor/RequiredFieldsTab.java | 4 +- .../gui/entryeditor/UserDefinedFieldsTab.java | 6 +- .../externalfiles/AutoLinkFilesAction.java | 4 +- .../externalfiles/AutoSetFileLinksUtil.java | 14 +- .../externalfiles/DownloadFullTextAction.java | 4 +- .../ExternalFilesEntryLinker.java | 6 +- .../externalfiles/FileExtensionViewModel.java | 9 +- .../gui/externalfiles/ImportHandler.java | 4 +- .../UnlinkedFilesDialogView.java | 3 +- .../UnlinkedFilesDialogViewModel.java | 9 +- .../externalfiletype/ExternalFileType.java | 17 +- .../externalfiletype/ExternalFileTypes.java | 150 +++++++----------- .../gui/fieldeditors/LinkedFileViewModel.java | 20 +-- .../LinkedFilesEditorViewModel.java | 33 ++-- .../org/jabref/gui/fieldeditors/URLUtil.java | 5 +- .../importer/GenerateEntryFromIdAction.java | 11 +- .../gui/importer/ImportEntriesViewModel.java | 5 +- .../gui/linkedfile/AttachFileAction.java | 6 +- .../linkedfile/LinkedFileEditDialogView.java | 5 +- .../LinkedFilesEditDialogViewModel.java | 17 +- .../org/jabref/gui/maintable/CellFactory.java | 5 +- .../org/jabref/gui/maintable/MainTable.java | 5 +- .../gui/maintable/MainTableColumnFactory.java | 8 +- .../gui/maintable/OpenExternalFileAction.java | 7 +- .../gui/maintable/OpenFolderAction.java | 9 +- .../gui/maintable/columns/FileColumn.java | 20 +-- .../StyleSelectDialogViewModel.java | 2 +- .../ExternalFileTypesTab.java | 3 +- .../ExternalFileTypesTabViewModel.java | 12 +- .../ProtectedTermsTabViewModel.java | 4 +- .../preferences/table/TableTabViewModel.java | 9 +- .../org/jabref/gui/preview/PreviewPanel.java | 4 +- .../jabref/gui/search/GlobalSearchBar.java | 3 +- .../gui/search/GlobalSearchResultDialog.java | 7 +- .../jabref/gui/search/SearchResultsTable.java | 5 +- .../jabref/preferences/FilePreferences.java | 15 +- .../jabref/preferences/JabRefPreferences.java | 17 +- .../preferences/PreferencesService.java | 5 - .../AutoSetFileLinksUtilTest.java | 17 +- .../ExternalFileTypesTest.java | 121 ++++++++++++++ .../fieldeditors/LinkedFileViewModelTest.java | 39 ++--- 58 files changed, 370 insertions(+), 362 deletions(-) create mode 100644 src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index f1aaa0653d7c..e99e62fe40f5 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -15,7 +15,6 @@ import org.jabref.gui.Globals; import org.jabref.gui.externalfiles.AutoSetFileLinksUtil; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.undo.NamedCompound; import org.jabref.logic.JabRefException; import org.jabref.logic.bibtex.FieldWriterPreferences; @@ -640,8 +639,7 @@ private void automaticallySetFileLinks(List loaded) { AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( parserResult.getDatabaseContext(), preferencesService.getFilePreferences(), - preferencesService.getAutoLinkPreferences(), - ExternalFileTypes.getInstance()); + preferencesService.getAutoLinkPreferences()); util.linkAssociatedFiles(database.getEntries(), new NamedCompound("")); } } diff --git a/src/main/java/org/jabref/gui/EntryTypeViewModel.java b/src/main/java/org/jabref/gui/EntryTypeViewModel.java index 4e18e4d87c05..7e08b6857e24 100644 --- a/src/main/java/org/jabref/gui/EntryTypeViewModel.java +++ b/src/main/java/org/jabref/gui/EntryTypeViewModel.java @@ -15,7 +15,6 @@ import javafx.concurrent.Worker; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.importer.NewEntryAction; import org.jabref.logic.importer.FetcherClientException; import org.jabref.logic.importer.FetcherException; @@ -169,7 +168,6 @@ public void runFetcherWorker() { ImportHandler handler = new ImportHandler( libraryTab.getBibDatabaseContext(), - ExternalFileTypes.getInstance(), preferencesService, Globals.getFileUpdateMonitor(), libraryTab.getUndoManager(), diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 638a7761f100..4d07c12aca24 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -81,7 +81,6 @@ import org.jabref.gui.externalfiles.AutoLinkFilesAction; import org.jabref.gui.externalfiles.DownloadFullTextAction; import org.jabref.gui.externalfiles.FindUnlinkedFilesAction; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.help.AboutAction; import org.jabref.gui.help.ErrorConsoleAction; import org.jabref.gui.help.HelpAction; @@ -1143,7 +1142,7 @@ private void trackOpenNewDatabase(LibraryTab libraryTab) { public LibraryTab addTab(BibDatabaseContext databaseContext, boolean raisePanel) { Objects.requireNonNull(databaseContext); - LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, themeManager, databaseContext, ExternalFileTypes.getInstance(), importFormatReader); + LibraryTab libraryTab = new LibraryTab(this, prefs, stateManager, themeManager, databaseContext, importFormatReader); addTab(libraryTab, raisePanel); return libraryTab; } diff --git a/src/main/java/org/jabref/gui/LibraryTab.java b/src/main/java/org/jabref/gui/LibraryTab.java index f037135f6d90..ad40925caccc 100644 --- a/src/main/java/org/jabref/gui/LibraryTab.java +++ b/src/main/java/org/jabref/gui/LibraryTab.java @@ -29,7 +29,6 @@ import org.jabref.gui.collab.DatabaseChangeMonitor; import org.jabref.gui.dialogs.AutosaveUiManager; import org.jabref.gui.entryeditor.EntryEditor; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.importer.actions.OpenDatabaseAction; import org.jabref.gui.maintable.MainTable; import org.jabref.gui.maintable.MainTableDataModel; @@ -84,7 +83,6 @@ public class LibraryTab extends Tab { private static final Logger LOGGER = LoggerFactory.getLogger(LibraryTab.class); private final JabRefFrame frame; private final CountingUndoManager undoManager; - private final ExternalFileTypes externalFileTypes; private final DialogService dialogService; private final PreferencesService preferencesService; private final StateManager stateManager; @@ -122,11 +120,9 @@ public LibraryTab(JabRefFrame frame, StateManager stateManager, ThemeManager themeManager, BibDatabaseContext bibDatabaseContext, - ExternalFileTypes externalFileTypes, ImportFormatReader importFormatReader) { this.frame = Objects.requireNonNull(frame); this.bibDatabaseContext = Objects.requireNonNull(bibDatabaseContext); - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.undoManager = frame.getUndoManager(); this.dialogService = frame.getDialogService(); this.preferencesService = Objects.requireNonNull(preferencesService); @@ -156,7 +152,7 @@ public LibraryTab(JabRefFrame frame, this.getDatabase().registerListener(new UpdateTimestampListener(preferencesService)); - this.entryEditor = new EntryEditor(this, externalFileTypes); + this.entryEditor = new EntryEditor(this); // set LibraryTab ID for drag'n'drop // ID content doesn't matter, we only need different tabs to have different ID @@ -257,7 +253,7 @@ public void feedData(BibDatabaseContext bibDatabaseContext) { this.getDatabase().registerListener(new UpdateTimestampListener(preferencesService)); - this.entryEditor = new EntryEditor(this, externalFileTypes); + this.entryEditor = new EntryEditor(this); Platform.runLater(() -> { EasyBind.subscribe(changedProperty, this::updateTabTitle); @@ -488,7 +484,6 @@ private void createMainTable() { preferencesService, dialogService, stateManager, - externalFileTypes, Globals.getKeyPrefs(), Globals.getClipboardManager(), Globals.IMPORT_FORMAT_READER); @@ -808,7 +803,7 @@ public LibraryTab createLibraryTab(JabRefFrame frame, PreferencesService prefere BibDatabaseContext context = new BibDatabaseContext(); context.setDatabasePath(file); - LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, themeManager, context, ExternalFileTypes.getInstance(), importFormatReader); + LibraryTab newTab = new LibraryTab(frame, preferencesService, stateManager, themeManager, context, importFormatReader); newTab.setDataLoadingTask(dataLoadingTask); dataLoadingTask.onRunning(newTab::onDatabaseLoadingStarted) diff --git a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java index 8224873ad709..61db2e93b472 100644 --- a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java +++ b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java @@ -13,7 +13,6 @@ import org.jabref.gui.Globals; import org.jabref.gui.StateManager; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.importer.FetcherException; @@ -52,7 +51,6 @@ public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext, this.taskExecutor = taskExecutor; this.importHandler = new ImportHandler( bibdatabaseContext, - ExternalFileTypes.getInstance(), preferencesService, fileUpdateMonitor, undoManager, diff --git a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java index 96bc258e1346..517ffc1689d2 100644 --- a/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java +++ b/src/main/java/org/jabref/gui/desktop/JabRefDesktop.java @@ -219,7 +219,7 @@ public static void openFolderAndSelectFile(Path fileLink, PreferencesService pre * @param url the URL to open */ public static void openBrowser(String url) throws IOException { - Optional fileType = ExternalFileTypes.getInstance().getExternalFileTypeByExt("html"); + Optional fileType = ExternalFileTypes.getExternalFileTypeByExt("html", Globals.prefs.getFilePreferences()); openExternalFilePlatformIndependent(fileType, url); } diff --git a/src/main/java/org/jabref/gui/desktop/os/Linux.java b/src/main/java/org/jabref/gui/desktop/os/Linux.java index c522f172c550..289efb4f576c 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Linux.java +++ b/src/main/java/org/jabref/gui/desktop/os/Linux.java @@ -12,6 +12,7 @@ import org.jabref.architecture.AllowedToUseAwt; import org.jabref.gui.DialogService; +import org.jabref.gui.Globals; import org.jabref.gui.JabRefExecutorService; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; @@ -48,7 +49,7 @@ private void nativeOpenFile(String filePath) { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType); + Optional type = ExternalFileTypes.getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); String viewer; if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { diff --git a/src/main/java/org/jabref/gui/desktop/os/OSX.java b/src/main/java/org/jabref/gui/desktop/os/OSX.java index 2bb731619fa2..d7a3c6cd7101 100644 --- a/src/main/java/org/jabref/gui/desktop/os/OSX.java +++ b/src/main/java/org/jabref/gui/desktop/os/OSX.java @@ -6,6 +6,7 @@ import org.jabref.architecture.AllowedToUseAwt; import org.jabref.gui.DialogService; +import org.jabref.gui.Globals; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; @@ -14,7 +15,7 @@ public class OSX implements NativeDesktop { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType); + Optional type = ExternalFileTypes.getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { openFileWithApplication(filePath, type.get().getOpenWithApplication()); } else { diff --git a/src/main/java/org/jabref/gui/desktop/os/Windows.java b/src/main/java/org/jabref/gui/desktop/os/Windows.java index a5a6acfe79a8..d85722740d69 100644 --- a/src/main/java/org/jabref/gui/desktop/os/Windows.java +++ b/src/main/java/org/jabref/gui/desktop/os/Windows.java @@ -7,6 +7,7 @@ import java.util.Optional; import org.jabref.gui.DialogService; +import org.jabref.gui.Globals; import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.gui.externalfiletype.ExternalFileTypes; @@ -15,7 +16,7 @@ public class Windows implements NativeDesktop { @Override public void openFile(String filePath, String fileType) throws IOException { - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType); + Optional type = ExternalFileTypes.getExternalFileTypeByExt(fileType, Globals.prefs.getFilePreferences()); if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) { openFileWithApplication(filePath, type.get().getOpenWithApplication()); diff --git a/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java index 7127c0af919b..d5bc20da9e6f 100644 --- a/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/DeprecatedFieldsTab.java @@ -12,7 +12,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -39,10 +38,9 @@ public DeprecatedFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { - super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); + super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, taskExecutor, journalAbbreviationRepository, indexingTaskManager); this.entryTypesManager = entryTypesManager; setText(Localization.lang("Deprecated fields")); diff --git a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java index 5066e762b1c8..b5f2d72ca398 100644 --- a/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java +++ b/src/main/java/org/jabref/gui/entryeditor/EntryEditor.java @@ -34,7 +34,6 @@ import org.jabref.gui.entryeditor.fileannotationtab.FileAnnotationTab; import org.jabref.gui.entryeditor.fileannotationtab.FulltextSearchResultsTab; import org.jabref.gui.externalfiles.ExternalFilesEntryLinker; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.help.HelpAction; import org.jabref.gui.importer.GrobidOptInDialogHelper; import org.jabref.gui.keyboard.KeyBinding; @@ -113,7 +112,7 @@ public class EntryEditor extends BorderPane { private final List entryEditorTabs = new LinkedList<>(); - public EntryEditor(LibraryTab libraryTab, ExternalFileTypes externalFileTypes) { + public EntryEditor(LibraryTab libraryTab) { this.libraryTab = libraryTab; this.databaseContext = libraryTab.getBibDatabaseContext(); @@ -122,8 +121,7 @@ public EntryEditor(LibraryTab libraryTab, ExternalFileTypes externalFileTypes) { .load(); this.entryEditorPreferences = preferencesService.getEntryEditorPreferences(); - this.fileLinker = new ExternalFilesEntryLinker(externalFileTypes, preferencesService.getFilePreferences(), - databaseContext); + this.fileLinker = new ExternalFilesEntryLinker(preferencesService.getFilePreferences(), databaseContext); EasyBind.subscribe(tabbed.getSelectionModel().selectedItemProperty(), tab -> { EntryEditorTab activeTab = (EntryEditorTab) tab; @@ -242,22 +240,22 @@ private void navigateToNextEntry() { private List createTabs() { // Preview tab - entryEditorTabs.add(new PreviewTab(databaseContext, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), ExternalFileTypes.getInstance())); + entryEditorTabs.add(new PreviewTab(databaseContext, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager())); // Required fields - entryEditorTabs.add(new RequiredFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new RequiredFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); // Optional fields - entryEditorTabs.add(new OptionalFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); - entryEditorTabs.add(new OptionalFields2Tab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); - entryEditorTabs.add(new DeprecatedFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new OptionalFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new OptionalFields2Tab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new DeprecatedFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); // Other fields - entryEditorTabs.add(new OtherFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new OtherFieldsTab(databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, taskExecutor, journalAbbreviationRepository)); // General fields from preferences for (Map.Entry> tab : entryEditorPreferences.getEntryEditorTabList().entrySet()) { - entryEditorTabs.add(new UserDefinedFieldsTab(tab.getKey(), tab.getValue(), databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), bibEntryTypesManager, ExternalFileTypes.getInstance(), taskExecutor, journalAbbreviationRepository)); + entryEditorTabs.add(new UserDefinedFieldsTab(tab.getKey(), tab.getValue(), databaseContext, libraryTab.getSuggestionProviders(), undoManager, dialogService, preferencesService, stateManager, themeManager, libraryTab.getIndexingTaskManager(), taskExecutor, journalAbbreviationRepository)); } // Special tabs diff --git a/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java b/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java index 342a519d6b7e..49fc1d8715bf 100644 --- a/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/FieldsEditorTab.java @@ -26,7 +26,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.FieldEditorFX; import org.jabref.gui.fieldeditors.FieldEditors; import org.jabref.gui.fieldeditors.FieldNameLabel; @@ -53,7 +52,6 @@ abstract class FieldsEditorTab extends EntryEditorTab { private final DialogService dialogService; private final PreferencesService preferences; private final ThemeManager themeManager; - private final ExternalFileTypes externalFileTypes; private final TaskExecutor taskExecutor; private final JournalAbbreviationRepository journalAbbreviationRepository; private final StateManager stateManager; @@ -71,7 +69,6 @@ public FieldsEditorTab(boolean compressed, PreferencesService preferences, StateManager stateManager, ThemeManager themeManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository, IndexingTaskManager indexingTaskManager) { @@ -82,7 +79,6 @@ public FieldsEditorTab(boolean compressed, this.dialogService = Objects.requireNonNull(dialogService); this.preferences = Objects.requireNonNull(preferences); this.themeManager = themeManager; - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.taskExecutor = Objects.requireNonNull(taskExecutor); this.journalAbbreviationRepository = Objects.requireNonNull(journalAbbreviationRepository); this.stateManager = stateManager; @@ -246,7 +242,7 @@ private void initPanel() { scrollPane.setFitToHeight(true); SplitPane container = new SplitPane(scrollPane); - previewPanel = new PreviewPanel(databaseContext, dialogService, externalFileTypes, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); + previewPanel = new PreviewPanel(databaseContext, dialogService, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); EasyBind.subscribe(preferences.getPreviewPreferences().showPreviewAsExtraTabProperty(), show -> { if (show) { container.getItems().remove(previewPanel); diff --git a/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java b/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java index 07090e543ee5..97c38da58dd0 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java +++ b/src/main/java/org/jabref/gui/entryeditor/OptionalFields2Tab.java @@ -5,7 +5,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.journals.JournalAbbreviationRepository; @@ -25,7 +24,6 @@ public OptionalFields2Tab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super( @@ -40,7 +38,6 @@ public OptionalFields2Tab(BibDatabaseContext databaseContext, themeManager, indexingTaskManager, entryTypesManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository ); diff --git a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java index 0035e8062581..93a6ff4b6f17 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTab.java @@ -5,7 +5,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.journals.JournalAbbreviationRepository; @@ -25,7 +24,6 @@ public OptionalFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super( @@ -40,7 +38,6 @@ public OptionalFieldsTab(BibDatabaseContext databaseContext, themeManager, indexingTaskManager, entryTypesManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository ); diff --git a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java index 52c88a8c809b..af1a5f9f0b65 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java +++ b/src/main/java/org/jabref/gui/entryeditor/OptionalFieldsTabBase.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -40,7 +39,6 @@ public OptionalFieldsTabBase(String title, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super(true, @@ -51,7 +49,6 @@ public OptionalFieldsTabBase(String title, preferences, stateManager, themeManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); diff --git a/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java index 08802ceb27ce..0fe1281c0774 100644 --- a/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/OtherFieldsTab.java @@ -14,7 +14,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -44,7 +43,6 @@ public OtherFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super(false, @@ -55,7 +53,6 @@ public OtherFieldsTab(BibDatabaseContext databaseContext, preferences, stateManager, themeManager, - externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); diff --git a/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java b/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java index b66b3c49a18f..4212506b8f66 100644 --- a/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/PreviewTab.java @@ -2,7 +2,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.preview.PreviewPanel; import org.jabref.gui.theme.ThemeManager; @@ -19,7 +18,6 @@ public class PreviewTab extends EntryEditorTab { private final StateManager stateManager; private final ThemeManager themeManager; private final IndexingTaskManager indexingTaskManager; - private final ExternalFileTypes externalFileTypes; private PreviewPanel previewPanel; public PreviewTab(BibDatabaseContext databaseContext, @@ -27,15 +25,13 @@ public PreviewTab(BibDatabaseContext databaseContext, PreferencesService preferences, StateManager stateManager, ThemeManager themeManager, - IndexingTaskManager indexingTaskManager, - ExternalFileTypes externalFileTypes) { + IndexingTaskManager indexingTaskManager) { this.databaseContext = databaseContext; this.dialogService = dialogService; this.preferences = preferences; this.stateManager = stateManager; this.themeManager = themeManager; this.indexingTaskManager = indexingTaskManager; - this.externalFileTypes = externalFileTypes; setGraphic(IconTheme.JabRefIcons.TOGGLE_ENTRY_PREVIEW.getGraphicNode()); setText(Localization.lang("Preview")); @@ -63,7 +59,7 @@ public boolean shouldShow(BibEntry entry) { @Override protected void bindToEntry(BibEntry entry) { if (previewPanel == null) { - previewPanel = new PreviewPanel(databaseContext, dialogService, externalFileTypes, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); + previewPanel = new PreviewPanel(databaseContext, dialogService, preferences.getKeyBindingRepository(), preferences, stateManager, themeManager, indexingTaskManager); setContent(previewPanel); } diff --git a/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java index 5c078dea5b59..3451f2c8f53e 100644 --- a/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/RequiredFieldsTab.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -40,11 +39,10 @@ public RequiredFieldsTab(BibDatabaseContext databaseContext, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { super(false, databaseContext, suggestionProviders, undoManager, dialogService, - preferences, stateManager, themeManager, externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); + preferences, stateManager, themeManager, taskExecutor, journalAbbreviationRepository, indexingTaskManager); this.entryTypesManager = entryTypesManager; setText(Localization.lang("Required fields")); diff --git a/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java b/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java index bc084a8e77b0..aa4e65b48e34 100644 --- a/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/UserDefinedFieldsTab.java @@ -8,7 +8,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.autocompleter.SuggestionProviders; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.TaskExecutor; @@ -16,7 +15,6 @@ import org.jabref.logic.pdf.search.indexing.IndexingTaskManager; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.entry.BibEntry; -import org.jabref.model.entry.BibEntryTypesManager; import org.jabref.model.entry.field.Field; import org.jabref.preferences.PreferencesService; @@ -33,11 +31,9 @@ public UserDefinedFieldsTab(String name, StateManager stateManager, ThemeManager themeManager, IndexingTaskManager indexingTaskManager, - BibEntryTypesManager entryTypesManager, - ExternalFileTypes externalFileTypes, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { - super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, externalFileTypes, taskExecutor, journalAbbreviationRepository, indexingTaskManager); + super(false, databaseContext, suggestionProviders, undoManager, dialogService, preferences, stateManager, themeManager, taskExecutor, journalAbbreviationRepository, indexingTaskManager); this.fields = new LinkedHashSet<>(fields); diff --git a/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java b/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java index 6c51f8889dd8..689d79dca692 100644 --- a/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java +++ b/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java @@ -9,7 +9,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.undo.NamedCompound; import org.jabref.gui.util.BindingsHelper; import org.jabref.gui.util.TaskExecutor; @@ -51,8 +50,7 @@ public void execute() { final AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( database, preferences.getFilePreferences(), - preferences.getAutoLinkPreferences(), - ExternalFileTypes.getInstance()); + preferences.getAutoLinkPreferences()); final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links")); Task linkFilesTask = new Task<>() { diff --git a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java index e783ee0aed39..4d1f35e3b40d 100644 --- a/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java +++ b/src/main/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtil.java @@ -55,16 +55,16 @@ public List getFileExceptions() { private static final Logger LOGGER = LoggerFactory.getLogger(AutoSetFileLinksUtil.class); private final List directories; private final AutoLinkPreferences autoLinkPreferences; - private final ExternalFileTypes externalFileTypes; + private final FilePreferences filePreferences; - public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) { - this(databaseContext.getFileDirectories(filePreferences), autoLinkPreferences, externalFileTypes); + public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences) { + this(databaseContext.getFileDirectories(filePreferences), filePreferences, autoLinkPreferences); } - private AutoSetFileLinksUtil(List directories, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) { + private AutoSetFileLinksUtil(List directories, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences) { this.directories = directories; this.autoLinkPreferences = autoLinkPreferences; - this.externalFileTypes = externalFileTypes; + this.filePreferences = filePreferences; } public LinkFilesResult linkAssociatedFiles(List entries, NamedCompound ce) { @@ -107,7 +107,7 @@ public LinkFilesResult linkAssociatedFiles(List entries, NamedCompound public List findAssociatedNotLinkedFiles(BibEntry entry) throws IOException { List linkedFiles = new ArrayList<>(); - List extensions = externalFileTypes.getExternalFileTypeSelection().stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); + List extensions = filePreferences.getExternalFileTypes().stream().map(ExternalFileType::getExtension).collect(Collectors.toList()); // Run the search operation FileFinder fileFinder = FileFinders.constructFromConfiguration(autoLinkPreferences); @@ -128,7 +128,7 @@ public List findAssociatedNotLinkedFiles(BibEntry entry) throws IOEx if (!fileAlreadyLinked) { Optional type = FileHelper.getFileExtension(foundFile) - .map(externalFileTypes::getExternalFileTypeByExt) + .map(extension -> ExternalFileTypes.getExternalFileTypeByExt(extension, filePreferences)) .orElse(Optional.of(new UnknownExternalFileType(""))); String strType = type.isPresent() ? type.get().getName() : ""; diff --git a/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java b/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java index 53be917cea18..be8b74ede447 100644 --- a/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java +++ b/src/main/java/org/jabref/gui/externalfiles/DownloadFullTextAction.java @@ -14,7 +14,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.logic.importer.FulltextFetchers; import org.jabref.logic.l10n.Localization; @@ -146,8 +145,7 @@ private void addLinkedFileFromURL(BibDatabaseContext databaseContext, URL url, B databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferences, - ExternalFileTypes.getInstance()); + preferences); onlineFile.download(); } else { diff --git a/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java b/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java index b62748a49597..7f9591c60f4c 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java +++ b/src/main/java/org/jabref/gui/externalfiles/ExternalFilesEntryLinker.java @@ -26,14 +26,12 @@ public class ExternalFilesEntryLinker { private static final Logger LOGGER = LoggerFactory.getLogger(ExternalFilesEntryLinker.class); - private final ExternalFileTypes externalFileTypes; private final FilePreferences filePreferences; private final BibDatabaseContext bibDatabaseContext; private final MoveFilesCleanup moveFilesCleanup; private final RenamePdfCleanup renameFilesCleanup; - public ExternalFilesEntryLinker(ExternalFileTypes externalFileTypes, FilePreferences filePreferences, BibDatabaseContext bibDatabaseContext) { - this.externalFileTypes = externalFileTypes; + public ExternalFilesEntryLinker(FilePreferences filePreferences, BibDatabaseContext bibDatabaseContext) { this.filePreferences = filePreferences; this.bibDatabaseContext = bibDatabaseContext; this.moveFilesCleanup = new MoveFilesCleanup(bibDatabaseContext, filePreferences); @@ -62,7 +60,7 @@ public void moveLinkedFilesToFileDir(BibEntry entry) { public void addFilesToEntry(BibEntry entry, List files) { for (Path file : files) { FileUtil.getFileExtension(file).ifPresent(ext -> { - ExternalFileType type = externalFileTypes.getExternalFileTypeByExt(ext) + ExternalFileType type = ExternalFileTypes.getExternalFileTypeByExt(ext, filePreferences) .orElse(new UnknownExternalFileType(ext)); Path relativePath = FileUtil.relativize(file, bibDatabaseContext.getFileDirectories(filePreferences)); LinkedFile linkedfile = new LinkedFile("", relativePath, type.getName()); diff --git a/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java b/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java index 121517bf5bc3..6344babd9059 100644 --- a/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java +++ b/src/main/java/org/jabref/gui/externalfiles/FileExtensionViewModel.java @@ -11,17 +11,18 @@ import org.jabref.gui.util.FileFilterConverter; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.FileType; +import org.jabref.preferences.FilePreferences; public class FileExtensionViewModel { private final String description; private final List extensions; - private final ExternalFileTypes externalFileTypes; + private final FilePreferences filePreferences; - FileExtensionViewModel(FileType fileType, ExternalFileTypes externalFileTypes) { + FileExtensionViewModel(FileType fileType, FilePreferences filePreferences) { this.description = Localization.lang("%0 file", fileType.getName()); this.extensions = fileType.getExtensionsWithDot(); - this.externalFileTypes = externalFileTypes; + this.filePreferences = filePreferences; } public String getDescription() { @@ -29,7 +30,7 @@ public String getDescription() { } public JabRefIcon getIcon() { - return externalFileTypes.getExternalFileTypeByExt(extensions.get(0)) + return ExternalFileTypes.getExternalFileTypeByExt(extensions.get(0), filePreferences) .map(ExternalFileType::getIcon) .orElse(null); } diff --git a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java index 61cc7afd2e87..f8ab4c2354cb 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java +++ b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java @@ -17,7 +17,6 @@ import org.jabref.gui.Globals; import org.jabref.gui.StateManager; import org.jabref.gui.duplicationFinder.DuplicateResolverDialog; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.undo.UndoableInsertEntries; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.DefaultTaskExecutor; @@ -65,7 +64,6 @@ public class ImportHandler { private final ImportFormatReader importFormatReader; public ImportHandler(BibDatabaseContext database, - ExternalFileTypes externalFileTypes, PreferencesService preferencesService, FileUpdateMonitor fileupdateMonitor, UndoManager undoManager, @@ -80,7 +78,7 @@ public ImportHandler(BibDatabaseContext database, this.dialogService = dialogService; this.importFormatReader = importFormatReader; - this.linker = new ExternalFilesEntryLinker(externalFileTypes, preferencesService.getFilePreferences(), database); + this.linker = new ExternalFilesEntryLinker(preferencesService.getFilePreferences(), database); this.contentImporter = new ExternalFilesContentImporter( preferencesService.getGeneralPreferences(), preferencesService.getImporterPreferences(), diff --git a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java index 0805c8a1f220..9ccdfa8ec82d 100644 --- a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java +++ b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogView.java @@ -31,7 +31,6 @@ import org.jabref.gui.actions.ActionFactory; import org.jabref.gui.actions.SimpleCommand; import org.jabref.gui.actions.StandardActions; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.theme.ThemeManager; import org.jabref.gui.util.BaseDialog; @@ -108,7 +107,7 @@ public UnlinkedFilesDialogView() { @FXML private void initialize() { - viewModel = new UnlinkedFilesDialogViewModel(dialogService, ExternalFileTypes.getInstance(), undoManager, fileUpdateMonitor, preferencesService, stateManager, taskExecutor, importFormatReader); + viewModel = new UnlinkedFilesDialogViewModel(dialogService, undoManager, fileUpdateMonitor, preferencesService, stateManager, taskExecutor, importFormatReader); progressDisplay.progressProperty().bind(viewModel.progressValueProperty()); progressText.textProperty().bind(viewModel.progressTextProperty()); diff --git a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java index fd942a615087..2129e473c4ac 100644 --- a/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java +++ b/src/main/java/org/jabref/gui/externalfiles/UnlinkedFilesDialogViewModel.java @@ -30,7 +30,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.DirectoryDialogConfiguration; import org.jabref.gui.util.FileDialogConfiguration; @@ -82,7 +81,6 @@ public class UnlinkedFilesDialogViewModel { private final FunctionBasedValidator scanDirectoryValidator; public UnlinkedFilesDialogViewModel(DialogService dialogService, - ExternalFileTypes externalFileTypes, UndoManager undoManager, FileUpdateMonitor fileUpdateMonitor, PreferencesService preferences, @@ -95,7 +93,6 @@ public UnlinkedFilesDialogViewModel(DialogService dialogService, this.bibDatabase = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null")); importHandler = new ImportHandler( bibDatabase, - externalFileTypes, preferences, fileUpdateMonitor, undoManager, @@ -104,9 +101,9 @@ public UnlinkedFilesDialogViewModel(DialogService dialogService, importFormatReader); this.fileFilterList = FXCollections.observableArrayList( - new FileExtensionViewModel(StandardFileType.ANY_FILE, externalFileTypes), - new FileExtensionViewModel(StandardFileType.BIBTEX_DB, externalFileTypes), - new FileExtensionViewModel(StandardFileType.PDF, externalFileTypes)); + new FileExtensionViewModel(StandardFileType.ANY_FILE, preferences.getFilePreferences()), + new FileExtensionViewModel(StandardFileType.BIBTEX_DB, preferences.getFilePreferences()), + new FileExtensionViewModel(StandardFileType.PDF, preferences.getFilePreferences())); this.dateFilterList = FXCollections.observableArrayList(DateRange.values()); diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java index 15e4a869b514..0a5a44259cfb 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileType.java @@ -11,6 +11,10 @@ public interface ExternalFileType { String getMimeType(); + String getOpenWithApplication(); + + JabRefIcon getIcon(); + /** * Get the bibtex field name used for this file type. Currently we assume that field name equals filename extension. * @@ -20,7 +24,14 @@ default Field getField() { return FieldFactory.parseField(getExtension()); } - String getOpenWithApplication(); - - JabRefIcon getIcon(); + /** + * Return a String array representing this file type. This is used for storage into + * Preferences, and the same array can be used to construct the file type later, + * using the String[] constructor. + * + * @return A String[] containing all information about this file type. + */ + default String[] toStringArray() { + return new String[]{getName(), getExtension(), getMimeType(), getOpenWithApplication(), getIcon().name()}; + } } diff --git a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java index 89209f2cffa5..3cba8ebd4faf 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java +++ b/src/main/java/org/jabref/gui/externalfiletype/ExternalFileTypes.java @@ -3,18 +3,18 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; -import java.util.Comparator; +import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Optional; import java.util.Set; -import java.util.TreeSet; -import org.jabref.gui.Globals; import org.jabref.logic.bibtex.FileFieldWriter; import org.jabref.model.entry.LinkedFile; import org.jabref.model.strings.StringUtil; import org.jabref.model.util.FileHelper; +import org.jabref.preferences.FilePreferences; // Do not make this class final, as it otherwise can't be mocked for tests public class ExternalFileTypes { @@ -22,40 +22,23 @@ public class ExternalFileTypes { // This String is used in the encoded list in prefs of external file type // modifications, in order to indicate a removed default file type: private static final String FILE_TYPE_REMOVED_FLAG = "REMOVED"; - // The only instance of this class: - private static ExternalFileTypes singleton; - // Map containing all registered external file types: - private final Set externalFileTypes = new TreeSet<>(Comparator.comparing(ExternalFileType::getName)); - - private final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; + private static final ExternalFileType HTML_FALLBACK_TYPE = StandardExternalFileType.URL; private ExternalFileTypes() { - updateExternalFileTypes(); - } - - public static ExternalFileTypes getInstance() { - if (ExternalFileTypes.singleton == null) { - ExternalFileTypes.singleton = new ExternalFileTypes(); - } - return ExternalFileTypes.singleton; } public static List getDefaultExternalFileTypes() { return Arrays.asList(StandardExternalFileType.values()); } - public Set getExternalFileTypeSelection() { - return externalFileTypes; - } - /** * Look up the external file type registered with this name, if any. * * @param name The file type name. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeByName(String name) { - Optional externalFileType = externalFileTypes.stream().filter(type -> type.getName().equals(name)).findFirst(); + public static Optional getExternalFileTypeByName(String name, FilePreferences filePreferences) { + Optional externalFileType = filePreferences.getExternalFileTypes().stream().filter(type -> type.getName().equals(name)).findFirst(); if (externalFileType.isPresent()) { return externalFileType; } @@ -69,9 +52,9 @@ public Optional getExternalFileTypeByName(String name) { * @param extension The file extension. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeByExt(String extension) { + public static Optional getExternalFileTypeByExt(String extension, FilePreferences filePreferences) { String extensionCleaned = extension.replace(".", "").replace("*", ""); - return externalFileTypes.stream().filter(type -> type.getExtension().equalsIgnoreCase(extensionCleaned)).findFirst(); + return filePreferences.getExternalFileTypes().stream().filter(type -> type.getExtension().equalsIgnoreCase(extensionCleaned)).findFirst(); } /** @@ -80,8 +63,8 @@ public Optional getExternalFileTypeByExt(String extension) { * @param extension The file extension. * @return true if an ExternalFileType with the extension exists, false otherwise */ - public boolean isExternalFileTypeByExt(String extension) { - return externalFileTypes.stream().anyMatch(type -> type.getExtension().equalsIgnoreCase(extension)); + public static boolean isExternalFileTypeByExt(String extension, FilePreferences filePreferences) { + return filePreferences.getExternalFileTypes().stream().anyMatch(type -> type.getExtension().equalsIgnoreCase(extension)); } /** @@ -90,10 +73,10 @@ public boolean isExternalFileTypeByExt(String extension) { * @param filename The name of the file whose type to look up. * @return The ExternalFileType registered, or null if none. */ - public Optional getExternalFileTypeForName(String filename) { + public static Optional getExternalFileTypeForName(String filename, FilePreferences filePreferences) { int longestFound = -1; ExternalFileType foundType = null; - for (ExternalFileType type : externalFileTypes) { + for (ExternalFileType type : filePreferences.getExternalFileTypes()) { if (!type.getExtension().isEmpty() && filename.toLowerCase(Locale.ROOT).endsWith(type.getExtension().toLowerCase(Locale.ROOT)) && (type.getExtension().length() > longestFound)) { longestFound = type.getExtension().length(); @@ -110,12 +93,12 @@ public Optional getExternalFileTypeForName(String filename) { * @return The ExternalFileType registered, or null if none. For the mime type "text/html", a valid file type is * guaranteed to be returned. */ - public Optional getExternalFileTypeByMimeType(String mimeType) { + public static Optional getExternalFileTypeByMimeType(String mimeType, FilePreferences filePreferences) { // Ignores parameters according to link: (https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types) if (mimeType.indexOf(';') != -1) { mimeType = mimeType.substring(0, mimeType.indexOf(';')).trim(); } - for (ExternalFileType type : externalFileTypes) { + for (ExternalFileType type : filePreferences.getExternalFileTypes()) { if (type.getMimeType().equalsIgnoreCase(mimeType)) { return Optional.of(type); } @@ -127,21 +110,44 @@ public Optional getExternalFileTypeByMimeType(String mimeType) } } + public static Optional getExternalFileTypeByFile(Path file, FilePreferences filePreferences) { + final String filePath = file.toString(); + final Optional extension = FileHelper.getFileExtension(filePath); + return extension.flatMap(ext -> getExternalFileTypeByExt(ext, filePreferences)); + } + + public static Optional getExternalFileTypeByLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType, FilePreferences filePreferences) { + Optional type = getExternalFileTypeByName(linkedFile.getFileType(), filePreferences); + boolean isUnknownType = type.isEmpty() || (type.get() instanceof UnknownExternalFileType); + + if (isUnknownType && deduceUnknownType) { + // No file type was recognized. Try to find a usable file type based on mime type: + Optional mimeType = getExternalFileTypeByMimeType(linkedFile.getFileType(), filePreferences); + if (mimeType.isPresent()) { + return mimeType; + } + + // No type could be found from mime type. Try based on the extension: + return FileHelper.getFileExtension(linkedFile.getLink()) + .flatMap(extension -> getExternalFileTypeByExt(extension, filePreferences)); + } else { + return type; + } + } + /** - * Reset the List of external file types after user customization. - * - * @param types The new List of external file types. This is the complete list, not just new entries. + * @return A StringList of customized and removed file types compared to the default list of external file types for storing */ - public void setExternalFileTypes(List types) { + public static String toStringList(Collection fileTypes) { // First find a list of the default types: List defTypes = new ArrayList<>(getDefaultExternalFileTypes()); // Make a list of types that are unchanged: List unchanged = new ArrayList<>(); + // Create a result list + List results = new ArrayList<>(); - externalFileTypes.clear(); - for (ExternalFileType type : types) { - externalFileTypes.add(type); - + for (ExternalFileType type : fileTypes) { + results.add(type); // See if we can find a type with matching name in the default type list: ExternalFileType found = null; for (ExternalFileType defType : defTypes) { @@ -166,52 +172,38 @@ public void setExternalFileTypes(List types) { // and from the list of defaults, since we don't need to mention these in prefs: for (ExternalFileType type : unchanged) { defTypes.remove(type); - types.remove(type); + results.remove(type); } // Now set up the array to write to prefs, containing all new types, all modified // types, and a flag denoting each default type that has been removed: - String[][] array = new String[types.size() + defTypes.size()][]; + String[][] array = new String[results.size() + defTypes.size()][]; int i = 0; - for (ExternalFileType type : types) { - array[i] = getStringArrayRepresentation(type); + for (ExternalFileType type : results) { + array[i] = type.toStringArray(); i++; } for (ExternalFileType type : defTypes) { array[i] = new String[] {type.getName(), FILE_TYPE_REMOVED_FLAG}; i++; } - Globals.prefs.storeExternalFileTypes(FileFieldWriter.encodeStringArray(array)); + return FileFieldWriter.encodeStringArray(array); } /** - * Return a String array representing this file type. This is used for storage into - * Preferences, and the same array can be used to construct the file type later, - * using the String[] constructor. - * - * @return A String[] containing all information about this file type. + * Set up the list of external file types, either from default values, or from values recorded in PreferencesService. */ - private String[] getStringArrayRepresentation(ExternalFileType type) { - return new String[] {type.getName(), type.getExtension(), type.getMimeType(), type.getOpenWithApplication(), type.getIcon().name()}; - } - - /** - * Set up the list of external file types, either from default values, or from values recorded in Preferences. - */ - private void updateExternalFileTypes() { + public static Set fromString(String storedFileTypes) { // First get a list of the default file types as a starting point: - List types = new ArrayList<>(getDefaultExternalFileTypes()); + Set types = new HashSet<>(getDefaultExternalFileTypes()); + // If no changes have been stored, simply use the defaults: - Optional storedFileTypes = Globals.prefs.getExternalFileTypes(); - if (storedFileTypes.isEmpty()) { - externalFileTypes.clear(); - externalFileTypes.addAll(types); - return; + if (StringUtil.isBlank(storedFileTypes)) { + return types; } // Read the prefs information for file types: - String[][] vals = StringUtil - .decodeStringDoubleArray(storedFileTypes.orElse("")); + String[][] vals = StringUtil.decodeStringDoubleArray(storedFileTypes); for (String[] val : vals) { if ((val.length == 2) && val[1].equals(FILE_TYPE_REMOVED_FLAG)) { // This entry indicates that a default entry type should be removed: @@ -248,32 +240,6 @@ private void updateExternalFileTypes() { } } - // Finally, build the list of types based on the modified defaults list: - externalFileTypes.addAll(types); - } - - public Optional getExternalFileTypeByFile(Path file) { - final String filePath = file.toString(); - final Optional extension = FileHelper.getFileExtension(filePath); - return extension.flatMap(this::getExternalFileTypeByExt); - } - - public Optional fromLinkedFile(LinkedFile linkedFile, boolean deduceUnknownType) { - Optional type = getExternalFileTypeByName(linkedFile.getFileType()); - boolean isUnknownType = type.isEmpty() || (type.get() instanceof UnknownExternalFileType); - - if (isUnknownType && deduceUnknownType) { - // No file type was recognized. Try to find a usable file type based on mime type: - Optional mimeType = getExternalFileTypeByMimeType(linkedFile.getFileType()); - if (mimeType.isPresent()) { - return mimeType; - } - - // No type could be found from mime type. Try based on the extension: - return FileHelper.getFileExtension(linkedFile.getLink()) - .flatMap(this::getExternalFileTypeByExt); - } else { - return type; - } + return types; } } diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java index 89a943d9e1fa..b2f9e1749d78 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFileViewModel.java @@ -82,7 +82,6 @@ public class LinkedFileViewModel extends AbstractViewModel { private final TaskExecutor taskExecutor; private final PreferencesService preferences; private final LinkedFileHandler linkedFileHandler; - private final ExternalFileTypes externalFileTypes; private final Validator fileExistsValidator; @@ -91,8 +90,7 @@ public LinkedFileViewModel(LinkedFile linkedFile, BibDatabaseContext databaseContext, TaskExecutor taskExecutor, DialogService dialogService, - PreferencesService preferences, - ExternalFileTypes externalFileTypes) { + PreferencesService preferences) { this.linkedFile = linkedFile; this.preferences = preferences; @@ -101,7 +99,6 @@ public LinkedFileViewModel(LinkedFile linkedFile, this.entry = entry; this.dialogService = dialogService; this.taskExecutor = taskExecutor; - this.externalFileTypes = externalFileTypes; fileExistsValidator = new FunctionBasedValidator<>( linkedFile.linkProperty(), @@ -176,7 +173,7 @@ public Optional findIn(List directories) { } public JabRefIcon getTypeIcon() { - return externalFileTypes.fromLinkedFile(linkedFile, false) + return ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, preferences.getFilePreferences()) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE); } @@ -199,7 +196,7 @@ public Observable[] getObservables() { public void open() { try { - Optional type = ExternalFileTypes.getInstance().fromLinkedFile(linkedFile, true); + Optional type = ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, true, preferences.getFilePreferences()); boolean successful = JabRefDesktop.openExternalFileAnyFormat(databaseContext, preferences, linkedFile.getLink(), type); if (!successful) { dialogService.showErrorDialogAndWait(Localization.lang("File not found"), Localization.lang("Could not find file '%0'.", linkedFile.getLink())); @@ -444,7 +441,10 @@ public void download() { } if (!isDuplicate) { - LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, databaseContext.getFileDirectories(preferences.getFilePreferences()), externalFileTypes); + LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile( + destination, + databaseContext.getFileDirectories(preferences.getFilePreferences()), + preferences.getFilePreferences()); List linkedFiles = entry.getFiles(); entry.addLinkedFile(entry, linkedFile, newLinkedFile, linkedFiles); @@ -523,15 +523,15 @@ private Optional inferFileTypeFromMimeType(URLDownload urlDown if (mimeType != null) { LOGGER.debug("MIME Type suggested: " + mimeType); - return externalFileTypes.getExternalFileTypeByMimeType(mimeType); + return ExternalFileTypes.getExternalFileTypeByMimeType(mimeType, preferences.getFilePreferences()); } else { return Optional.empty(); } } private Optional inferFileTypeFromURL(String url) { - return URLUtil.getSuffix(url) - .flatMap(externalFileTypes::getExternalFileTypeByExt); + return URLUtil.getSuffix(url, preferences.getFilePreferences()) + .flatMap(extension -> ExternalFileTypes.getExternalFileTypeByExt(extension, preferences.getFilePreferences())); } public LinkedFile getFile() { diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java index 4525c7f050c0..44a3f15d5386 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java @@ -38,6 +38,7 @@ import org.jabref.model.entry.LinkedFile; import org.jabref.model.entry.field.Field; import org.jabref.model.util.FileHelper; +import org.jabref.preferences.FilePreferences; import org.jabref.preferences.PreferencesService; public class LinkedFilesEditorViewModel extends AbstractEditorViewModel { @@ -48,7 +49,6 @@ public class LinkedFilesEditorViewModel extends AbstractEditorViewModel { private final BibDatabaseContext databaseContext; private final TaskExecutor taskExecutor; private final PreferencesService preferences; - private final ExternalFileTypes externalFileTypes = ExternalFileTypes.getInstance(); public LinkedFilesEditorViewModel(Field field, SuggestionProvider suggestionProvider, DialogService dialogService, @@ -87,27 +87,25 @@ private static String getStringRepresentation(List files) { * * TODO: Move this method to {@link LinkedFile} as soon as {@link CustomExternalFileType} lives in model. */ - public static LinkedFile fromFile(Path file, List fileDirectories, ExternalFileTypes externalFileTypesFile) { + public static LinkedFile fromFile(Path file, List fileDirectories, FilePreferences filePreferences) { String fileExtension = FileHelper.getFileExtension(file).orElse(""); - ExternalFileType suggestedFileType = externalFileTypesFile - .getExternalFileTypeByExt(fileExtension) - .orElse(new UnknownExternalFileType(fileExtension)); + ExternalFileType suggestedFileType = ExternalFileTypes.getExternalFileTypeByExt(fileExtension, filePreferences) + .orElse(new UnknownExternalFileType(fileExtension)); Path relativePath = FileUtil.relativize(file, fileDirectories); return new LinkedFile("", relativePath, suggestedFileType.getName()); } - public LinkedFileViewModel fromFile(Path file) { + public LinkedFileViewModel fromFile(Path file, FilePreferences filePreferences) { List fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences()); - LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes); + LinkedFile linkedFile = fromFile(file, fileDirectories, filePreferences); return new LinkedFileViewModel( linkedFile, entry, databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes); + preferences); } public boolean isFulltextLookupInProgress() { @@ -122,8 +120,7 @@ private List parseToFileViewModel(String stringValue) { databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes)) + preferences)) .collect(Collectors.toList()); } @@ -145,15 +142,14 @@ public void addNewFile() { List fileDirectories = databaseContext.getFileDirectories(preferences.getFilePreferences()); dialogService.showFileOpenDialogAndGetMultipleFiles(fileDialogConfiguration).forEach(newFile -> { - LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes); + LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, preferences.getFilePreferences()); files.add(new LinkedFileViewModel( newLinkedFile, entry, databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes)); + preferences)); }); } @@ -178,8 +174,7 @@ private List findAssociatedNotLinkedFiles(BibEntry entry) { AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( databaseContext, preferences.getFilePreferences(), - preferences.getAutoLinkPreferences(), - ExternalFileTypes.getInstance()); + preferences.getAutoLinkPreferences()); try { List linkedFiles = util.findAssociatedNotLinkedFiles(entry); for (LinkedFile linkedFile : linkedFiles) { @@ -189,8 +184,7 @@ private List findAssociatedNotLinkedFiles(BibEntry entry) { databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes); + preferences); newLinkedFile.markAsAutomaticallyFound(); result.add(newLinkedFile); } @@ -241,8 +235,7 @@ private void addFromURL(URL url) { databaseContext, taskExecutor, dialogService, - preferences, - externalFileTypes); + preferences); files.add(onlineFile); onlineFile.download(); } diff --git a/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java b/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java index 1ee8e8b2652e..d7f58db7c002 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java +++ b/src/main/java/org/jabref/gui/fieldeditors/URLUtil.java @@ -8,6 +8,7 @@ import java.util.Optional; import org.jabref.gui.externalfiletype.ExternalFileTypes; +import org.jabref.preferences.FilePreferences; public class URLUtil { private static final String URL_EXP = "^(https?|ftp)://.+"; @@ -90,7 +91,7 @@ public static boolean isURL(String url) { * @param link The link * @return The suffix, excluding the dot (e.g. "pdf") */ - public static Optional getSuffix(final String link) { + public static Optional getSuffix(final String link, FilePreferences filePreferences) { String strippedLink = link; try { // Try to strip the query string, if any, to get the correct suffix: @@ -110,7 +111,7 @@ public static Optional getSuffix(final String link) { } else { suffix = strippedLink.substring(strippedLinkIndex + 1); } - if (!ExternalFileTypes.getInstance().isExternalFileTypeByExt(suffix)) { + if (!ExternalFileTypes.isExternalFileTypeByExt(suffix, filePreferences)) { // If the suffix doesn't seem to give any reasonable file type, try // with the non-stripped link: int index = link.lastIndexOf('.'); diff --git a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java index 619ec5a8e356..95e2eb99d530 100644 --- a/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java +++ b/src/main/java/org/jabref/gui/importer/GenerateEntryFromIdAction.java @@ -8,7 +8,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.SimpleCommand; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.TaskExecutor; import org.jabref.logic.importer.CompositeIdFetcher; @@ -32,7 +31,13 @@ public class GenerateEntryFromIdAction extends SimpleCommand { private final PopOver entryFromIdPopOver; private final StateManager stateManager; - public GenerateEntryFromIdAction(LibraryTab libraryTab, DialogService dialogService, PreferencesService preferencesService, TaskExecutor taskExecutor, PopOver entryFromIdPopOver, String identifier, StateManager stateManager) { + public GenerateEntryFromIdAction(LibraryTab libraryTab, + DialogService dialogService, + PreferencesService preferencesService, + TaskExecutor taskExecutor, + PopOver entryFromIdPopOver, + String identifier, + StateManager stateManager) { this.libraryTab = libraryTab; this.dialogService = dialogService; this.preferencesService = preferencesService; @@ -69,7 +74,7 @@ public void execute() { Optional result = bibEntry; if (result.isPresent()) { final BibEntry entry = result.get(); - ImportHandler handler = new ImportHandler(libraryTab.getBibDatabaseContext(), ExternalFileTypes.getInstance(), preferencesService, Globals.getFileUpdateMonitor(), libraryTab.getUndoManager(), stateManager, dialogService, null); + ImportHandler handler = new ImportHandler(libraryTab.getBibDatabaseContext(), preferencesService, Globals.getFileUpdateMonitor(), libraryTab.getUndoManager(), stateManager, dialogService, null); handler.importEntryWithDuplicateCheck(libraryTab.getBibDatabaseContext(), entry); } else { dialogService.notify("No entry found or import canceled"); diff --git a/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java b/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java index 877a677234c5..0f75519e9109 100644 --- a/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java +++ b/src/main/java/org/jabref/gui/importer/ImportEntriesViewModel.java @@ -17,7 +17,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.duplicationFinder.DuplicateResolverDialog; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.gui.util.BackgroundTask; import org.jabref.gui.util.TaskExecutor; @@ -149,8 +148,7 @@ public void importEntries(List entriesToImport, boolean shouldDownload databaseContext, taskExecutor, dialogService, - preferences, - ExternalFileTypes.getInstance()).download()); + preferences).download()); } } @@ -166,7 +164,6 @@ public void importEntries(List entriesToImport, boolean shouldDownload private void buildImportHandlerThenImportEntries(List entriesToImport) { ImportHandler importHandler = new ImportHandler( databaseContext, - ExternalFileTypes.getInstance(), preferences, fileUpdateMonitor, undoManager, diff --git a/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java b/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java index 00e0c1f3b4bc..8dad9f0399c4 100644 --- a/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java +++ b/src/main/java/org/jabref/gui/linkedfile/AttachFileAction.java @@ -8,7 +8,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFilesEditorViewModel; import org.jabref.gui.undo.UndoableFieldChange; import org.jabref.gui.util.FileDialogConfiguration; @@ -62,9 +61,10 @@ public void execute() { .build(); dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> { - LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile(newFile, + LinkedFile linkedFile = LinkedFilesEditorViewModel.fromFile( + newFile, databaseContext.getFileDirectories(filePreferences), - ExternalFileTypes.getInstance()); + filePreferences); LinkedFileEditDialogView dialog = new LinkedFileEditDialogView(linkedFile); diff --git a/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java b/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java index f8ad3e1f7bc0..6ed39ce1ae94 100644 --- a/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java +++ b/src/main/java/org/jabref/gui/linkedfile/LinkedFileEditDialogView.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.util.BaseDialog; import org.jabref.model.entry.LinkedFile; import org.jabref.preferences.PreferencesService; @@ -32,12 +31,10 @@ public class LinkedFileEditDialogView extends BaseDialog { private LinkedFilesEditDialogViewModel viewModel; private final LinkedFile linkedFile; - private final ExternalFileTypes externalFileTypes; public LinkedFileEditDialogView(LinkedFile linkedFile) { this.linkedFile = linkedFile; - this.externalFileTypes = ExternalFileTypes.getInstance(); ViewLoader.view(this) .load() .setAsContent(this.getDialogPane()); @@ -55,7 +52,7 @@ public LinkedFileEditDialogView(LinkedFile linkedFile) { @FXML private void initialize() { - viewModel = new LinkedFilesEditDialogViewModel(linkedFile, stateManager.getActiveDatabase().get(), dialogService, preferences.getFilePreferences(), externalFileTypes); + viewModel = new LinkedFilesEditDialogViewModel(linkedFile, stateManager.getActiveDatabase().get(), dialogService, preferences.getFilePreferences()); fileType.itemsProperty().bindBidirectional(viewModel.externalFileTypeProperty()); description.textProperty().bindBidirectional(viewModel.descriptionProperty()); link.textProperty().bindBidirectional(viewModel.linkProperty()); diff --git a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java index 12d3f3f45cef..a46e908c3585 100644 --- a/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java +++ b/src/main/java/org/jabref/gui/linkedfile/LinkedFilesEditDialogViewModel.java @@ -41,14 +41,15 @@ public class LinkedFilesEditDialogViewModel extends AbstractViewModel { private final BibDatabaseContext database; private final DialogService dialogService; private final FilePreferences filePreferences; - private final ExternalFileTypes externalFileTypes; - public LinkedFilesEditDialogViewModel(LinkedFile linkedFile, BibDatabaseContext database, DialogService dialogService, FilePreferences filePreferences, ExternalFileTypes externalFileTypes) { + public LinkedFilesEditDialogViewModel(LinkedFile linkedFile, + BibDatabaseContext database, + DialogService dialogService, + FilePreferences filePreferences) { this.database = database; this.dialogService = dialogService; this.filePreferences = filePreferences; - this.externalFileTypes = externalFileTypes; - allExternalFileTypes.set(FXCollections.observableArrayList(externalFileTypes.getExternalFileTypeSelection())); + allExternalFileTypes.set(FXCollections.observableArrayList(filePreferences.getExternalFileTypes())); monadicSelectedExternalFileType = EasyBind.wrapNullable(selectedExternalFileType); setValues(linkedFile); @@ -58,12 +59,14 @@ private void setExternalFileTypeByExtension(String link) { if (!link.isEmpty()) { // Check if this looks like a remote link: if (REMOTE_LINK_PATTERN.matcher(link).matches()) { - externalFileTypes.getExternalFileTypeByExt("html").ifPresent(selectedExternalFileType::setValue); + ExternalFileTypes.getExternalFileTypeByExt("html", filePreferences) + .ifPresent(selectedExternalFileType::setValue); } // Try to guess the file type: String theLink = link.trim(); - externalFileTypes.getExternalFileTypeForName(theLink).ifPresent(selectedExternalFileType::setValue); + ExternalFileTypes.getExternalFileTypeForName(theLink, filePreferences) + .ifPresent(selectedExternalFileType::setValue); } } @@ -101,7 +104,7 @@ public void setValues(LinkedFile linkedFile) { selectedExternalFileType.setValue(null); // See what is a reasonable selection for the type combobox: - Optional fileType = externalFileTypes.fromLinkedFile(linkedFile, false); + Optional fileType = ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFile, false, filePreferences); if (fileType.isPresent() && !(fileType.get() instanceof UnknownExternalFileType)) { selectedExternalFileType.setValue(fileType.get()); } else if ((linkedFile.getLink() != null) && (!linkedFile.getLink().isEmpty())) { diff --git a/src/main/java/org/jabref/gui/maintable/CellFactory.java b/src/main/java/org/jabref/gui/maintable/CellFactory.java index 9f0940831d43..13c65f6a336a 100644 --- a/src/main/java/org/jabref/gui/maintable/CellFactory.java +++ b/src/main/java/org/jabref/gui/maintable/CellFactory.java @@ -8,7 +8,6 @@ import javafx.scene.Node; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.specialfields.SpecialFieldViewModel; @@ -22,7 +21,7 @@ public class CellFactory { private final Map TABLE_ICONS = new HashMap<>(); - public CellFactory(ExternalFileTypes externalFileTypes, PreferencesService preferencesService, UndoManager undoManager) { + public CellFactory(PreferencesService preferencesService, UndoManager undoManager) { JabRefIcon icon; icon = IconTheme.JabRefIcons.PDF_FILE; // icon.setToo(Localization.lang("Open") + " PDF"); @@ -56,7 +55,7 @@ public CellFactory(ExternalFileTypes externalFileTypes, PreferencesService prefe // icon.setToolTipText(Localization.lang("Open file")); TABLE_ICONS.put(StandardField.FILE, icon); - for (ExternalFileType fileType : externalFileTypes.getExternalFileTypeSelection()) { + for (ExternalFileType fileType : preferencesService.getFilePreferences().getExternalFileTypes()) { icon = fileType.getIcon(); // icon.setToolTipText(Localization.lang("Open %0 file", fileType.getName())); TABLE_ICONS.put(fileType.getField(), icon); diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index d37f3bb09c4b..8f7fff993558 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -34,7 +34,6 @@ import org.jabref.gui.actions.StandardActions; import org.jabref.gui.edit.EditAction; import org.jabref.gui.externalfiles.ImportHandler; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.gui.keyboard.KeyBindingRepository; import org.jabref.gui.maintable.columns.LibraryColumn; @@ -79,7 +78,6 @@ public MainTable(MainTableDataModel model, PreferencesService preferencesService, DialogService dialogService, StateManager stateManager, - ExternalFileTypes externalFileTypes, KeyBindingRepository keyBindingRepository, ClipBoardManager clipBoardManager, ImportFormatReader importFormatReader) { @@ -95,7 +93,7 @@ public MainTable(MainTableDataModel model, MainTablePreferences mainTablePreferences = preferencesService.getMainTablePreferences(); importHandler = new ImportHandler( - database, externalFileTypes, + database, preferencesService, Globals.getFileUpdateMonitor(), undoManager, @@ -113,7 +111,6 @@ public MainTable(MainTableDataModel model, database, preferencesService, preferencesService.getColumnPreferences(), - externalFileTypes, libraryTab.getUndoManager(), dialogService, stateManager).createColumns()); diff --git a/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java b/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java index ece234f9ff42..52cf6a2f04f0 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java +++ b/src/main/java/org/jabref/gui/maintable/MainTableColumnFactory.java @@ -23,7 +23,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.maintable.columns.FieldColumn; import org.jabref.gui.maintable.columns.FileColumn; @@ -54,7 +53,6 @@ public class MainTableColumnFactory { private final PreferencesService preferencesService; private final ColumnPreferences columnPreferences; - private final ExternalFileTypes externalFileTypes; private final BibDatabaseContext database; private final CellFactory cellFactory; private final UndoManager undoManager; @@ -64,16 +62,14 @@ public class MainTableColumnFactory { public MainTableColumnFactory(BibDatabaseContext database, PreferencesService preferencesService, ColumnPreferences abstractColumnPrefs, - ExternalFileTypes externalFileTypes, UndoManager undoManager, DialogService dialogService, StateManager stateManager) { this.database = Objects.requireNonNull(database); this.preferencesService = Objects.requireNonNull(preferencesService); this.columnPreferences = abstractColumnPrefs; - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.dialogService = dialogService; - this.cellFactory = new CellFactory(externalFileTypes, preferencesService, undoManager); + this.cellFactory = new CellFactory(preferencesService, undoManager); this.undoManager = undoManager; this.stateManager = stateManager; } @@ -233,7 +229,6 @@ private TableColumn private TableColumn> createFilesColumn(MainTableColumnModel columnModel) { return new FileColumn(columnModel, database, - externalFileTypes, dialogService, preferencesService); } @@ -244,7 +239,6 @@ private TableColumn> createFilesColumn( private TableColumn> createExtraFileColumn(MainTableColumnModel columnModel) { return new FileColumn(columnModel, database, - externalFileTypes, dialogService, preferencesService, columnModel.getQualifier()); diff --git a/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java b/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java index 9f669a18431f..499f07a3526b 100644 --- a/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java +++ b/src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java @@ -8,7 +8,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.logic.l10n.Localization; import org.jabref.model.entry.BibEntry; @@ -68,8 +67,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance()); + preferencesService); linkedFileViewModelList.add(linkedFileViewModel); } @@ -93,8 +91,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance()); + preferencesService); linkedFileViewModel.open(); } }); diff --git a/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java b/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java index 8ce16f62f1bf..92c8404ca392 100644 --- a/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java +++ b/src/main/java/org/jabref/gui/maintable/OpenFolderAction.java @@ -5,7 +5,6 @@ import org.jabref.gui.StateManager; import org.jabref.gui.actions.ActionHelper; import org.jabref.gui.actions.SimpleCommand; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.fieldeditors.LinkedFileViewModel; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.LinkedFile; @@ -49,9 +48,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance() - ); + preferencesService); linkedFileViewModel.openFolder(); }); } else { @@ -61,9 +58,7 @@ public void execute() { databaseContext, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - ExternalFileTypes.getInstance() - ); + preferencesService); linkedFileViewModel.openFolder(); } }); diff --git a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java index c325637af32a..5aaeeb207a06 100644 --- a/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java +++ b/src/main/java/org/jabref/gui/maintable/columns/FileColumn.java @@ -32,7 +32,6 @@ */ public class FileColumn extends MainTableColumn> { - private final ExternalFileTypes externalFileTypes; private final DialogService dialogService; private final BibDatabaseContext database; private final PreferencesService preferencesService; @@ -42,11 +41,9 @@ public class FileColumn extends MainTableColumn> { */ public FileColumn(MainTableColumnModel model, BibDatabaseContext database, - ExternalFileTypes externalFileTypes, DialogService dialogService, PreferencesService preferencesService) { super(model); - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.database = Objects.requireNonNull(database); this.dialogService = dialogService; this.preferencesService = preferencesService; @@ -68,8 +65,7 @@ public FileColumn(MainTableColumnModel model, entry.getEntry(), database, Globals.TASK_EXECUTOR, dialogService, - preferencesService, - externalFileTypes); + preferencesService); linkedFileViewModel.open(); } }) @@ -81,22 +77,19 @@ public FileColumn(MainTableColumnModel model, */ public FileColumn(MainTableColumnModel model, BibDatabaseContext database, - ExternalFileTypes externalFileTypes, DialogService dialogService, PreferencesService preferencesService, String fileType) { super(model); - this.externalFileTypes = Objects.requireNonNull(externalFileTypes); this.database = Objects.requireNonNull(database); this.dialogService = dialogService; this.preferencesService = preferencesService; setCommonSettings(); - this.setGraphic(externalFileTypes - .getExternalFileTypeByName(fileType) - .map(ExternalFileType::getIcon).orElse(IconTheme.JabRefIcons.FILE) - .getGraphicNode()); + this.setGraphic(ExternalFileTypes.getExternalFileTypeByName(fileType, preferencesService.getFilePreferences()) + .map(ExternalFileType::getIcon).orElse(IconTheme.JabRefIcons.FILE) + .getGraphicNode()); new ValueTableCellFactory>() .withGraphic(linkedFiles -> createFileIcon(linkedFiles.stream().filter(linkedFile -> @@ -131,8 +124,7 @@ private ContextMenu createFileMenu(BibEntryTableViewModel entry, List linkedFiles) { if (linkedFiles.size() > 1) { return IconTheme.JabRefIcons.FILE_MULTIPLE.getGraphicNode(); } else if (linkedFiles.size() == 1) { - return externalFileTypes.fromLinkedFile(linkedFiles.get(0), true) + return ExternalFileTypes.getExternalFileTypeByLinkedFile(linkedFiles.get(0), true, preferencesService.getFilePreferences()) .map(ExternalFileType::getIcon) .orElse(IconTheme.JabRefIcons.FILE) .getGraphicNode(); diff --git a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java index 92bd6dd607fd..b78e69aeba98 100644 --- a/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java +++ b/src/main/java/org/jabref/gui/openoffice/StyleSelectDialogViewModel.java @@ -93,7 +93,7 @@ public void deleteStyle() { public void editStyle() { OOBibStyle style = selectedItem.getValue().getStyle(); - Optional type = ExternalFileTypes.getInstance().getExternalFileTypeByExt("jstyle"); + Optional type = ExternalFileTypes.getExternalFileTypeByExt("jstyle", preferencesService.getFilePreferences()); try { JabRefDesktop.openExternalFileAnyFormat(new BibDatabaseContext(), preferencesService, style.getPath(), type); } catch (IOException e) { diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java index 419989756608..0347047dee93 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTab.java @@ -5,7 +5,6 @@ import javafx.scene.control.TableView; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.icon.JabRefIcon; import org.jabref.gui.preferences.AbstractPreferenceTabView; @@ -43,7 +42,7 @@ public String getTabName() { @FXML public void initialize() { - viewModel = new ExternalFileTypesTabViewModel(ExternalFileTypes.getInstance()); + viewModel = new ExternalFileTypesTabViewModel(preferencesService.getFilePreferences()); fileTypesTable.setItems(viewModel.getFileTypes()); diff --git a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java index ee669177d6df..e1ff1973bcb3 100644 --- a/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypesTabViewModel.java @@ -14,26 +14,28 @@ import org.jabref.gui.icon.IconTheme; import org.jabref.gui.preferences.PreferenceTabViewModel; import org.jabref.logic.l10n.Localization; +import org.jabref.preferences.FilePreferences; import com.airhacks.afterburner.injection.Injector; public class ExternalFileTypesTabViewModel implements PreferenceTabViewModel { - private final ExternalFileTypes externalFileTypes; + private final FilePreferences filePreferences; private final ObservableList fileTypes = FXCollections.observableArrayList(); - public ExternalFileTypesTabViewModel(ExternalFileTypes externalFileTypes) { - this.externalFileTypes = externalFileTypes; + public ExternalFileTypesTabViewModel(FilePreferences filePreferences) { + this.filePreferences = filePreferences; } @Override public void setValues() { - fileTypes.setAll(externalFileTypes.getExternalFileTypeSelection()); + fileTypes.setAll(filePreferences.getExternalFileTypes()); fileTypes.sort(Comparator.comparing(ExternalFileType::getName)); } public void storeSettings() { - externalFileTypes.setExternalFileTypes(fileTypes); + filePreferences.getExternalFileTypes().clear(); + filePreferences.getExternalFileTypes().addAll(fileTypes); } public void resetToDefaults() { diff --git a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java index ca6b668ab5d5..62c09375df6f 100644 --- a/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/protectedterms/ProtectedTermsTabViewModel.java @@ -125,8 +125,8 @@ public void createNewFile() { public void edit(ProtectedTermsListItemModel file) { Optional termsFileType = OptionalUtil.orElse( - ExternalFileTypes.getInstance().getExternalFileTypeByExt("terms"), - ExternalFileTypes.getInstance().getExternalFileTypeByExt("txt") + ExternalFileTypes.getExternalFileTypeByExt("terms", preferences.getFilePreferences()), + ExternalFileTypes.getExternalFileTypeByExt("txt", preferences.getFilePreferences()) ); String fileName = file.getTermsList().getLocation(); diff --git a/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java b/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java index 2184469c9331..ecae4a7d091c 100644 --- a/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/table/TableTabViewModel.java @@ -14,7 +14,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.externalfiletype.ExternalFileType; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.maintable.ColumnPreferences; import org.jabref.gui.maintable.MainTableColumnModel; import org.jabref.gui.maintable.MainTableNameFormatPreferences; @@ -180,10 +179,10 @@ private void removeSpecialFieldColumns() { } private void insertExtraFileColumns() { - ExternalFileTypes.getInstance().getExternalFileTypeSelection().stream() - .map(ExternalFileType::getName) - .map(name -> new MainTableColumnModel(MainTableColumnModel.Type.EXTRAFILE, name)) - .forEach(item -> availableColumnsProperty.getValue().add(item)); + preferences.getFilePreferences().getExternalFileTypes().stream() + .map(ExternalFileType::getName) + .map(name -> new MainTableColumnModel(MainTableColumnModel.Type.EXTRAFILE, name)) + .forEach(item -> availableColumnsProperty.getValue().add(item)); } private void removeExtraFileColumns() { diff --git a/src/main/java/org/jabref/gui/preview/PreviewPanel.java b/src/main/java/org/jabref/gui/preview/PreviewPanel.java index f16b27d8b135..94cf57cd1e38 100644 --- a/src/main/java/org/jabref/gui/preview/PreviewPanel.java +++ b/src/main/java/org/jabref/gui/preview/PreviewPanel.java @@ -19,7 +19,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; import org.jabref.gui.externalfiles.ExternalFilesEntryLinker; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.gui.keyboard.KeyBindingRepository; @@ -50,7 +49,6 @@ public class PreviewPanel extends VBox { public PreviewPanel(BibDatabaseContext database, DialogService dialogService, - ExternalFileTypes externalFileTypes, KeyBindingRepository keyBindingRepository, PreferencesService preferences, StateManager stateManager, @@ -61,7 +59,7 @@ public PreviewPanel(BibDatabaseContext database, this.stateManager = stateManager; this.previewPreferences = preferences.getPreviewPreferences(); this.indexingTaskManager = indexingTaskManager; - this.fileLinker = new ExternalFilesEntryLinker(externalFileTypes, preferences.getFilePreferences(), database); + this.fileLinker = new ExternalFilesEntryLinker(preferences.getFilePreferences(), database); PreviewPreferences previewPreferences = preferences.getPreviewPreferences(); previewView = new PreviewViewer(database, dialogService, stateManager, themeManager); diff --git a/src/main/java/org/jabref/gui/search/GlobalSearchBar.java b/src/main/java/org/jabref/gui/search/GlobalSearchBar.java index d6b3bfc03643..d42bcd34cb74 100644 --- a/src/main/java/org/jabref/gui/search/GlobalSearchBar.java +++ b/src/main/java/org/jabref/gui/search/GlobalSearchBar.java @@ -47,7 +47,6 @@ import org.jabref.gui.autocompleter.AutoCompletionTextInputBinding; import org.jabref.gui.autocompleter.PersonNameStringConverter; import org.jabref.gui.autocompleter.SuggestionProvider; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.keyboard.KeyBinding; import org.jabref.gui.keyboard.KeyBindingRepository; @@ -239,7 +238,7 @@ private void initSearchModifierButtons() { initSearchModifierButton(openGlobalSearchButton); openGlobalSearchButton.setOnAction(evt -> { globalSearchActive.setValue(true); - globalSearchResultDialog = new GlobalSearchResultDialog(ExternalFileTypes.getInstance(), undoManager); + globalSearchResultDialog = new GlobalSearchResultDialog(undoManager); performSearch(); globalSearchResultDialog.showAndWait(); globalSearchActive.setValue(false); diff --git a/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java b/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java index 59843b2e4cb2..b23dc8ab2dfa 100644 --- a/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java +++ b/src/main/java/org/jabref/gui/search/GlobalSearchResultDialog.java @@ -11,7 +11,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.icon.IconTheme; import org.jabref.gui.maintable.columns.SpecialFieldColumn; import org.jabref.gui.preview.PreviewViewer; @@ -28,7 +27,6 @@ public class GlobalSearchResultDialog extends BaseDialog { @FXML private SplitPane container; @FXML private ToggleButton keepOnTop; - private final ExternalFileTypes externalFileTypes; private final UndoManager undoManager; @Inject private PreferencesService preferencesService; @@ -38,9 +36,8 @@ public class GlobalSearchResultDialog extends BaseDialog { private GlobalSearchResultDialogViewModel viewModel; - public GlobalSearchResultDialog(ExternalFileTypes externalFileTypes, UndoManager undoManager) { + public GlobalSearchResultDialog(UndoManager undoManager) { this.undoManager = undoManager; - this.externalFileTypes = externalFileTypes; setTitle(Localization.lang("Search results from open libraries")); ViewLoader.view(this) @@ -57,7 +54,7 @@ private void initialize() { previewViewer.setLayout(preferencesService.getPreviewPreferences().getSelectedPreviewLayout()); SearchResultsTableDataModel model = new SearchResultsTableDataModel(viewModel.getSearchDatabaseContext(), preferencesService, stateManager); - SearchResultsTable resultsTable = new SearchResultsTable(model, viewModel.getSearchDatabaseContext(), preferencesService, undoManager, dialogService, stateManager, externalFileTypes); + SearchResultsTable resultsTable = new SearchResultsTable(model, viewModel.getSearchDatabaseContext(), preferencesService, undoManager, dialogService, stateManager); resultsTable.getColumns().removeIf(col -> col instanceof SpecialFieldColumn); resultsTable.getSelectionModel().selectFirst(); diff --git a/src/main/java/org/jabref/gui/search/SearchResultsTable.java b/src/main/java/org/jabref/gui/search/SearchResultsTable.java index 09083921f7ac..a0f3ac20ea63 100644 --- a/src/main/java/org/jabref/gui/search/SearchResultsTable.java +++ b/src/main/java/org/jabref/gui/search/SearchResultsTable.java @@ -10,7 +10,6 @@ import org.jabref.gui.DialogService; import org.jabref.gui.StateManager; -import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.maintable.BibEntryTableViewModel; import org.jabref.gui.maintable.MainTable; import org.jabref.gui.maintable.MainTableColumnFactory; @@ -28,8 +27,7 @@ public SearchResultsTable(SearchResultsTableDataModel model, PreferencesService preferencesService, UndoManager undoManager, DialogService dialogService, - StateManager stateManager, - ExternalFileTypes externalFileTypes) { + StateManager stateManager) { super(); MainTablePreferences mainTablePreferences = preferencesService.getMainTablePreferences(); @@ -38,7 +36,6 @@ public SearchResultsTable(SearchResultsTableDataModel model, database, preferencesService, preferencesService.getSearchDialogColumnPreferences(), - externalFileTypes, undoManager, dialogService, stateManager).createColumns(); diff --git a/src/main/java/org/jabref/preferences/FilePreferences.java b/src/main/java/org/jabref/preferences/FilePreferences.java index a2f2c9e4a4b6..037324fc1dd5 100644 --- a/src/main/java/org/jabref/preferences/FilePreferences.java +++ b/src/main/java/org/jabref/preferences/FilePreferences.java @@ -1,7 +1,10 @@ package org.jabref.preferences; import java.nio.file.Path; +import java.util.Comparator; import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; import javafx.beans.property.BooleanProperty; import javafx.beans.property.ObjectProperty; @@ -9,7 +12,10 @@ import javafx.beans.property.SimpleObjectProperty; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableSet; +import org.jabref.gui.externalfiletype.ExternalFileType; import org.jabref.model.strings.StringUtil; public class FilePreferences { @@ -24,6 +30,7 @@ public class FilePreferences { private final BooleanProperty downloadLinkedFiles = new SimpleBooleanProperty(); private final BooleanProperty fulltextIndexLinkedFiles = new SimpleBooleanProperty(); private final ObjectProperty workingDirectory = new SimpleObjectProperty<>(); + private final ObservableSet externalFileTypes = FXCollections.observableSet(new TreeSet<>(Comparator.comparing(ExternalFileType::getName))); public FilePreferences(String user, String mainFileDirectory, @@ -32,7 +39,8 @@ public FilePreferences(String user, String fileDirectoryPattern, boolean downloadLinkedFiles, boolean fulltextIndexLinkedFiles, - Path workingDirectory) { + Path workingDirectory, + Set externalFileTypes) { this.user.setValue(user); this.mainFileDirectory.setValue(mainFileDirectory); this.storeFilesRelativeToBibFile.setValue(storeFilesRelativeToBibFile); @@ -41,6 +49,7 @@ public FilePreferences(String user, this.downloadLinkedFiles.setValue(downloadLinkedFiles); this.fulltextIndexLinkedFiles.setValue(fulltextIndexLinkedFiles); this.workingDirectory.setValue(workingDirectory); + this.externalFileTypes.addAll(externalFileTypes); } public String getUser() { // Read only @@ -134,4 +143,8 @@ public ObjectProperty workingDirectoryProperty() { public void setWorkingDirectory(Path workingDirectory) { this.workingDirectory.set(workingDirectory); } + + public ObservableSet getExternalFileTypes() { + return this.externalFileTypes; + } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index d204f2f8900e..b954e8836835 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -42,6 +42,8 @@ import org.jabref.gui.autocompleter.AutoCompletePreferences; import org.jabref.gui.desktop.JabRefDesktop; import org.jabref.gui.entryeditor.EntryEditorPreferences; +import org.jabref.gui.externalfiletype.ExternalFileType; +import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.gui.groups.GroupViewMode; import org.jabref.gui.groups.GroupsPreferences; import org.jabref.gui.keyboard.KeyBindingRepository; @@ -2208,7 +2210,8 @@ public FilePreferences getFilePreferences() { get(IMPORT_FILEDIRPATTERN), getBoolean(DOWNLOAD_LINKED_FILES), getBoolean(FULLTEXT_INDEX_LINKED_FILES), - Path.of(get(WORKING_DIRECTORY)) + Path.of(get(WORKING_DIRECTORY)), + ExternalFileTypes.fromString(get(EXTERNAL_FILE_TYPES)) ); EasyBind.listen(filePreferences.mainFileDirectoryProperty(), (obs, oldValue, newValue) -> put(MAIN_FILE_DIRECTORY, newValue)); @@ -2218,6 +2221,8 @@ public FilePreferences getFilePreferences() { EasyBind.listen(filePreferences.downloadLinkedFilesProperty(), (obs, oldValue, newValue) -> putBoolean(DOWNLOAD_LINKED_FILES, newValue)); EasyBind.listen(filePreferences.fulltextIndexLinkedFilesProperty(), (obs, oldValue, newValue) -> putBoolean(FULLTEXT_INDEX_LINKED_FILES, newValue)); EasyBind.listen(filePreferences.workingDirectoryProperty(), (obs, oldValue, newValue) -> put(WORKING_DIRECTORY, newValue.toString())); + filePreferences.getExternalFileTypes().addListener((SetChangeListener) c -> + put(EXTERNAL_FILE_TYPES, ExternalFileTypes.toStringList(filePreferences.getExternalFileTypes()))); return filePreferences; } @@ -2759,16 +2764,6 @@ public void storeLastPreferencesExportPath(Path exportFile) { put(PREFS_EXPORT_PATH, exportFile.toString()); } - @Override - public Optional getExternalFileTypes() { - return Optional.ofNullable(get(EXTERNAL_FILE_TYPES, null)); - } - - @Override - public void storeExternalFileTypes(String externalFileTypes) { - put(EXTERNAL_FILE_TYPES, externalFileTypes); - } - @Override public MrDlibPreferences getMrDlibPreferences() { if (Objects.nonNull(mrDlibPreferences)) { diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 3e80aa2dde33..d3f7c692248a 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -3,7 +3,6 @@ import java.nio.file.Path; import java.util.List; import java.util.Map; -import java.util.Optional; import java.util.Set; import java.util.prefs.BackingStoreException; @@ -278,10 +277,6 @@ public interface PreferencesService { void storeLastPreferencesExportPath(Path exportFile); - Optional getExternalFileTypes(); - - void storeExternalFileTypes(String externalFileTypes); - MrDlibPreferences getMrDlibPreferences(); ProtectedTermsPreferences getProtectedTermsPreferences(); diff --git a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java index 22c731098761..b429b879454d 100644 --- a/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java +++ b/src/test/java/org/jabref/gui/externalfiles/AutoSetFileLinksUtilTest.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.TreeSet; +import javafx.collections.FXCollections; + import org.jabref.gui.externalfiletype.ExternalFileTypes; import org.jabref.logic.util.io.AutoLinkPreferences; import org.jabref.model.database.BibDatabaseContext; @@ -25,14 +27,13 @@ public class AutoSetFileLinksUtilTest { - private final FilePreferences fileDirPrefs = mock(FilePreferences.class); + private final FilePreferences filePreferences = mock(FilePreferences.class); private final AutoLinkPreferences autoLinkPrefs = new AutoLinkPreferences( AutoLinkPreferences.CitationKeyDependency.START, "", false, ';'); private final BibDatabaseContext databaseContext = mock(BibDatabaseContext.class); - private final ExternalFileTypes externalFileTypes = mock(ExternalFileTypes.class); private final BibEntry entry = new BibEntry(StandardEntryType.Article); private Path path = null; @@ -41,23 +42,23 @@ public void setUp(@TempDir Path folder) throws Exception { path = folder.resolve("CiteKey.pdf"); Files.createFile(path); entry.setCitationKey("CiteKey"); - when(externalFileTypes.getExternalFileTypeSelection()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); + when(filePreferences.getExternalFileTypes()) + .thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); } @Test public void testFindAssociatedNotLinkedFilesSuccess() throws Exception { - // Due to mocking the externalFileType class, the file extension will not be found when(databaseContext.getFileDirectories(any())).thenReturn(Collections.singletonList(path.getParent())); - List expected = Collections.singletonList(new LinkedFile("", Path.of("CiteKey.pdf"), "")); - AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, fileDirPrefs, autoLinkPrefs, externalFileTypes); + List expected = Collections.singletonList(new LinkedFile("", Path.of("CiteKey.pdf"), "PDF")); + AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, filePreferences, autoLinkPrefs); List actual = util.findAssociatedNotLinkedFiles(entry); assertEquals(expected, actual); } @Test public void testFindAssociatedNotLinkedFilesForEmptySearchDir() throws Exception { - when(fileDirPrefs.shouldStoreFilesRelativeToBibFile()).thenReturn(false); - AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, fileDirPrefs, autoLinkPrefs, externalFileTypes); + when(filePreferences.shouldStoreFilesRelativeToBibFile()).thenReturn(false); + AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, filePreferences, autoLinkPrefs); List actual = util.findAssociatedNotLinkedFiles(entry); assertEquals(Collections.emptyList(), actual); } diff --git a/src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java b/src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java new file mode 100644 index 000000000000..dadcba319fce --- /dev/null +++ b/src/test/java/org/jabref/gui/externalfiletype/ExternalFileTypesTest.java @@ -0,0 +1,121 @@ +package org.jabref.gui.externalfiletype; + +import java.nio.file.Path; +import java.util.Arrays; +import java.util.Optional; +import java.util.Set; + +import javafx.collections.FXCollections; + +import org.jabref.gui.icon.IconTheme; +import org.jabref.model.entry.LinkedFile; +import org.jabref.preferences.FilePreferences; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class ExternalFileTypesTest { + private static final Set TEST_LIST = Set.of( + StandardExternalFileType.PDF, + StandardExternalFileType.URL, + StandardExternalFileType.JPG, + StandardExternalFileType.TXT); + + private static final String TEST_STRINGLIST = "PostScript:REMOVED;" + + "Word:REMOVED;" + + "Word 2007+:REMOVED;" + + "OpenDocument text:REMOVED;" + + "Excel:REMOVED;" + + "Excel 2007+:REMOVED;" + + "OpenDocument spreadsheet:REMOVED;" + + "PowerPoint:REMOVED;" + + "PowerPoint 2007+:REMOVED;" + + "OpenDocument presentation:REMOVED;" + + "Rich Text Format:REMOVED;" + + "PNG image:REMOVED;" + + "GIF image:REMOVED;" + + "Djvu:REMOVED;" + + "LaTeX:REMOVED;" + + "CHM:REMOVED;" + + "TIFF image:REMOVED;" + + "MHT:REMOVED;" + + "ePUB:REMOVED"; + + private final FilePreferences filePreferences = mock(FilePreferences.class); + + @BeforeEach + void setUp() { + when(filePreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(TEST_LIST)); + } + + @Test + void getExternalFileTypeByName() { + assertEquals(Optional.of(StandardExternalFileType.PDF), ExternalFileTypes.getExternalFileTypeByName("PDF", filePreferences)); + } + + @Test + void getExternalFileTypeByExt() { + assertEquals(Optional.of(StandardExternalFileType.URL), ExternalFileTypes.getExternalFileTypeByExt("html", filePreferences)); + } + + @Test + void isExternalFileTypeByExt() { + assertTrue(ExternalFileTypes.isExternalFileTypeByExt("html", filePreferences)); + assertFalse(ExternalFileTypes.isExternalFileTypeByExt("tst", filePreferences)); + } + + @Test + void getExternalFileTypeForName() { + assertEquals(Optional.of(StandardExternalFileType.JPG), ExternalFileTypes.getExternalFileTypeForName("testfile.jpg", filePreferences)); + } + + @Test + void getExternalFileTypeByMimeType() { + assertEquals(Optional.of(StandardExternalFileType.TXT), ExternalFileTypes.getExternalFileTypeByMimeType("text/plain", filePreferences)); + } + + @Test + void getExternalFileTypeByFile() { + Path testfile = Path.of("testfile.txt"); + assertEquals(Optional.of(StandardExternalFileType.TXT), ExternalFileTypes.getExternalFileTypeByFile(testfile, filePreferences)); + } + + @Test + void getExternalFileTypeByLinkedFile() { + LinkedFile testfile = new LinkedFile("A testfile", "https://testserver.com/testfile.pdf", "PDF"); + assertEquals(Optional.of(StandardExternalFileType.PDF), ExternalFileTypes.getExternalFileTypeByLinkedFile(testfile, false, filePreferences)); + } + + @Test + void toStringList() { + String testString = ExternalFileTypes.toStringList(TEST_LIST); + + assertEquals(TEST_STRINGLIST, testString); + } + + @Test + void fromString() { + Set testList = ExternalFileTypes.fromString(TEST_STRINGLIST); + + assertEquals(TEST_LIST, testList); + } + + @Test + void externalFileTypetoStringArray() { + ExternalFileType type = new CustomExternalFileType( + "testEntry", + "tst", + "text/plain", + "emacs", + "close", + IconTheme.JabRefIcons.CLOSE); + + assertEquals("[testEntry, tst, text/plain, emacs, CLOSE]", Arrays.toString(type.toStringArray())); + } +} diff --git a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java index e01d9f465946..e11100032c47 100644 --- a/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java +++ b/src/test/java/org/jabref/gui/fieldeditors/LinkedFileViewModelTest.java @@ -13,6 +13,7 @@ import java.util.Optional; import java.util.TreeSet; +import javafx.collections.FXCollections; import javafx.scene.control.Alert.AlertType; import javafx.scene.control.ButtonType; @@ -45,7 +46,6 @@ import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.ArgumentMatchers.contains; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -62,7 +62,6 @@ class LinkedFileViewModelTest { private BibDatabaseContext databaseContext; private TaskExecutor taskExecutor; private DialogService dialogService; - private final ExternalFileTypes externalFileType = mock(ExternalFileTypes.class); private final FilePreferences filePreferences = mock(FilePreferences.class); private final PreferencesService preferences = mock(PreferencesService.class); private CookieManager cookieManager; @@ -75,11 +74,7 @@ void setUp(@TempDir Path tempFolder) throws Exception { taskExecutor = mock(TaskExecutor.class); dialogService = mock(DialogService.class); - when(externalFileType.getExternalFileTypeSelection()).thenReturn(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes())); - when(externalFileType.getExternalFileTypeByMimeType("application/pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF)); - when(externalFileType.getExternalFileTypeByMimeType(contains("text/html"))).thenReturn(Optional.of(StandardExternalFileType.URL)); - when(externalFileType.getExternalFileTypeByExt("pdf")).thenReturn(Optional.of(StandardExternalFileType.PDF)); - when(externalFileType.getExternalFileTypeByExt("html")).thenReturn(Optional.of(StandardExternalFileType.URL)); + when(filePreferences.getExternalFileTypes()).thenReturn(FXCollections.observableSet(new TreeSet<>(ExternalFileTypes.getDefaultExternalFileTypes()))); when(preferences.getFilePreferences()).thenReturn(filePreferences); when(preferences.getXmpPreferences()).thenReturn(mock(XmpPreferences.class)); tempFile = tempFolder.resolve("temporaryFile"); @@ -106,7 +101,7 @@ void deleteWhenFilePathNotPresentReturnsTrue() { linkedFile = spy(new LinkedFile("", Path.of("nonexistent file"), "")); doReturn(Optional.empty()).when(linkedFile).findIn(any(BibDatabaseContext.class), any(FilePreferences.class)); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -124,7 +119,7 @@ void deleteWhenRemoveChosenReturnsTrueButDoesNotDeletesFile() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(3))); // first vararg - remove button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -142,7 +137,7 @@ void deleteWhenDeleteChosenReturnsTrueAndDeletesFile() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -160,7 +155,7 @@ void deleteMissingFileReturnsTrue() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(4))); // second vararg - delete button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertTrue(removed); @@ -177,7 +172,7 @@ void deleteWhenDialogCancelledReturnsFalseAndDoesNotRemoveFile() { any(ButtonType.class), any(ButtonType.class))).thenAnswer(invocation -> Optional.of(invocation.getArgument(5))); // third vararg - cancel button - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); boolean removed = viewModel.delete(); assertFalse(removed); @@ -195,7 +190,7 @@ void downloadHtmlFileCausesWarningDisplay() throws MalformedURLException { String fileType = StandardExternalFileType.URL.getName(); linkedFile = new LinkedFile(url, fileType); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); viewModel.download(); @@ -210,11 +205,11 @@ void downloadDoesNotOverwriteFileTypeExtension() throws MalformedURLException { when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]"); when(filePreferences.getFileDirectoryPattern()).thenReturn(""); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); BackgroundTask task = viewModel.prepareDownloadTask(tempFile.getParent(), new URLDownload("http://arxiv.org/pdf/1207.0408v1")); task.onSuccess(destination -> { - LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, Collections.singletonList(tempFile.getParent()), externalFileType); + LinkedFile newLinkedFile = LinkedFilesEditorViewModel.fromFile(destination, Collections.singletonList(tempFile.getParent()), filePreferences); assertEquals("asdf.pdf", newLinkedFile.getLink()); assertEquals("PDF", newLinkedFile.getFileType()); }); @@ -236,7 +231,7 @@ void downloadHtmlWhenLinkedFilePointsToHtml() throws MalformedURLException { databaseContext.setDatabasePath(tempFile); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); viewModel.download(); @@ -260,7 +255,7 @@ void isNotSamePath() { when(filePreferences.getFileDirectoryPattern()).thenReturn(""); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(Path.of("/home"))); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertFalse(viewModel.isGeneratedPathSameAsOriginal()); } @@ -272,7 +267,7 @@ void isSamePath() { when(filePreferences.getFileDirectoryPattern()).thenReturn(""); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent())); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertTrue(viewModel.isGeneratedPathSameAsOriginal()); } @@ -285,7 +280,7 @@ void isNotSamePathWithPattern() { when(filePreferences.getFileDirectoryPattern()).thenReturn("[entrytype]"); when(databaseContext.getFirstExistingFileDir(filePreferences)).thenReturn(Optional.of(tempFile.getParent())); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertFalse(viewModel.isGeneratedPathSameAsOriginal()); } @@ -301,14 +296,14 @@ void isSamePathWithPattern() throws IOException { LinkedFileHandler fileHandler = new LinkedFileHandler(linkedFile, entry, databaseContext, filePreferences); fileHandler.moveToDefaultDirectory(); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences); assertTrue(viewModel.isGeneratedPathSameAsOriginal()); } // Tests if added parameters to mimeType gets parsed to correct format. @Test void mimeTypeStringWithParameterIsReturnedAsWithoutParameter() { - Optional test = externalFileType.getExternalFileTypeByMimeType("text/html; charset=UTF-8"); + Optional test = ExternalFileTypes.getExternalFileTypeByMimeType("text/html; charset=UTF-8", filePreferences); String actual = test.get().toString(); assertEquals("URL", actual); } @@ -323,7 +318,7 @@ void downloadPdfFileWhenLinkedFilePointsToPdfUrl() throws MalformedURLException databaseContext.setDatabasePath(tempFile); - LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences, externalFileType); + LinkedFileViewModel viewModel = new LinkedFileViewModel(linkedFile, entry, databaseContext, new CurrentThreadTaskExecutor(), dialogService, preferences); viewModel.download(); // Loop through downloaded files to check for filetype='pdf' From c82fc171e344db1e2a970bccde1567f63299ce64 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 12 Aug 2022 17:01:15 +0200 Subject: [PATCH 3/5] Keep EOL setting at backups (#9048) * Fix org.jabref.logic.autosaveandbackup.BackupManager#performBackup to use line ending of library * Fix typos * fix checkstyle Co-authored-by: Siedlerchr --- .../gui/exporter/SaveDatabaseAction.java | 4 +++- .../autosaveandbackup/BackupManager.java | 20 +++++++++---------- .../org/jabref/logic/util/io/FileUtil.java | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java index 685e1c0d28d8..293e03d45be4 100644 --- a/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java +++ b/src/main/java/org/jabref/gui/exporter/SaveDatabaseAction.java @@ -228,11 +228,13 @@ private boolean save(Path targetPath, SaveDatabaseMode mode) { } private boolean saveDatabase(Path file, boolean selectedOnly, Charset encoding, SavePreferences.DatabaseSaveType saveType) throws SaveException { + // if this code is adapted, please also adapt org.jabref.logic.autosaveandbackup.BackupManager.performBackup + GeneralPreferences generalPreferences = this.preferences.getGeneralPreferences(); SavePreferences savePreferences = this.preferences.getSavePreferences() .withSaveType(saveType); + BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext(); try (AtomicFileWriter fileWriter = new AtomicFileWriter(file, encoding, savePreferences.shouldMakeBackup())) { - BibDatabaseContext bibDatabaseContext = libraryTab.getBibDatabaseContext(); BibWriter bibWriter = new BibWriter(fileWriter, bibDatabaseContext.getDatabase().getNewLineSeparator()); BibtexDatabaseWriter databaseWriter = new BibtexDatabaseWriter(bibWriter, generalPreferences, savePreferences, entryTypesManager); diff --git a/src/main/java/org/jabref/logic/autosaveandbackup/BackupManager.java b/src/main/java/org/jabref/logic/autosaveandbackup/BackupManager.java index 948aca58bf50..65656f71f9a3 100644 --- a/src/main/java/org/jabref/logic/autosaveandbackup/BackupManager.java +++ b/src/main/java/org/jabref/logic/autosaveandbackup/BackupManager.java @@ -1,7 +1,6 @@ package org.jabref.logic.autosaveandbackup; import java.io.IOException; -import java.io.Writer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -20,7 +19,6 @@ import org.jabref.logic.exporter.SavePreferences; import org.jabref.logic.util.CoarseChangeFilter; import org.jabref.logic.util.DelayTaskThrottler; -import org.jabref.logic.util.OS; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.database.BibDatabaseContext; import org.jabref.model.database.event.BibDatabaseContextChangedEvent; @@ -57,7 +55,7 @@ private BackupManager(BibDatabaseContext bibDatabaseContext, BibEntryTypesManage this.bibDatabaseContext = bibDatabaseContext; this.entryTypesManager = entryTypesManager; this.preferences = preferences; - this.throttler = new DelayTaskThrottler(15000); + this.throttler = new DelayTaskThrottler(15_000); changeFilter = new CoarseChangeFilter(bibDatabaseContext); changeFilter.registerListener(this); @@ -134,13 +132,13 @@ private Optional determineBackupPath() { } private void performBackup(Path backupPath) { - try { - Charset charset = bibDatabaseContext.getMetaData().getEncoding().orElse(StandardCharsets.UTF_8); - GeneralPreferences generalPreferences = preferences.getGeneralPreferences(); - SavePreferences savePreferences = preferences.getSavePreferences() - .withMakeBackup(false); - Writer writer = new AtomicFileWriter(backupPath, charset); - BibWriter bibWriter = new BibWriter(writer, OS.NEWLINE); + // code similar to org.jabref.gui.exporter.SaveDatabaseAction.saveDatabase + GeneralPreferences generalPreferences = preferences.getGeneralPreferences(); + SavePreferences savePreferences = preferences.getSavePreferences() + .withMakeBackup(false); + Charset encoding = bibDatabaseContext.getMetaData().getEncoding().orElse(StandardCharsets.UTF_8); + try (AtomicFileWriter fileWriter = new AtomicFileWriter(backupPath, encoding)) { + BibWriter bibWriter = new BibWriter(fileWriter, bibDatabaseContext.getDatabase().getNewLineSeparator()); new BibtexDatabaseWriter(bibWriter, generalPreferences, savePreferences, entryTypesManager) .saveDatabase(bibDatabaseContext); } catch (IOException e) { @@ -157,7 +155,7 @@ private void logIfCritical(Path backupPath, IOException e) { // do not print errors in field values into the log during autosave if (!isErrorInField) { - LOGGER.error("Error while saving to file" + backupPath, e); + LOGGER.error("Error while saving to file {}", backupPath, e); } } diff --git a/src/main/java/org/jabref/logic/util/io/FileUtil.java b/src/main/java/org/jabref/logic/util/io/FileUtil.java index ae6be08ba150..f60ff2b110cd 100644 --- a/src/main/java/org/jabref/logic/util/io/FileUtil.java +++ b/src/main/java/org/jabref/logic/util/io/FileUtil.java @@ -193,7 +193,7 @@ public static boolean copyFile(Path pathToSourceFile, Path pathToDestinationFile * * @param fromFile The source filename to rename * @param toFile The target fileName - * @return True if the rename was successful, false if an exception occurred + * @return True if rename was successful, false if an exception occurred */ public static boolean renameFile(Path fromFile, Path toFile) { return renameFile(fromFile, toFile, false); @@ -204,8 +204,8 @@ public static boolean renameFile(Path fromFile, Path toFile) { * * @param fromFile The source filename to rename * @param toFile The target fileName - * @param replaceExisting Wether to replace existing files or not - * @return True if the rename was successful, false if an exception occurred + * @param replaceExisting Whether to replace existing files or not + * @return True if rename was successful, false if an exception occurred * @deprecated Use {@link Files#move(Path, Path, CopyOption...)} instead and handle exception properly */ @Deprecated From 0b580790d7a323ccdb445b8a8a6e8dd4d5917e69 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Fri, 12 Aug 2022 17:02:25 +0200 Subject: [PATCH 4/5] Fixed table update in eft preferences (#9051) --- .../EditExternalFileTypeEntryDialog.fxml | 38 +++++------ .../EditExternalFileTypeEntryDialog.java | 11 +-- .../EditExternalFileTypeViewModel.java | 50 +++++++------- .../ExternalFileTypeItemViewModel.java | 68 +++++++++++++++++++ .../ExternalFileTypesTab.fxml | 2 +- .../ExternalFileTypesTab.java | 55 ++++++++++----- .../ExternalFileTypesTabViewModel.java | 62 ++++++++--------- 7 files changed, 185 insertions(+), 101 deletions(-) rename src/main/java/org/jabref/gui/{externalfiletype => preferences/externalfiletypes}/EditExternalFileTypeEntryDialog.fxml (51%) rename src/main/java/org/jabref/gui/{externalfiletype => preferences/externalfiletypes}/EditExternalFileTypeEntryDialog.java (90%) rename src/main/java/org/jabref/gui/{externalfiletype => preferences/externalfiletypes}/EditExternalFileTypeViewModel.java (65%) create mode 100644 src/main/java/org/jabref/gui/preferences/externalfiletypes/ExternalFileTypeItemViewModel.java diff --git a/src/main/java/org/jabref/gui/externalfiletype/EditExternalFileTypeEntryDialog.fxml b/src/main/java/org/jabref/gui/preferences/externalfiletypes/EditExternalFileTypeEntryDialog.fxml similarity index 51% rename from src/main/java/org/jabref/gui/externalfiletype/EditExternalFileTypeEntryDialog.fxml rename to src/main/java/org/jabref/gui/preferences/externalfiletypes/EditExternalFileTypeEntryDialog.fxml index 2e885d600119..d197a8296487 100644 --- a/src/main/java/org/jabref/gui/externalfiletype/EditExternalFileTypeEntryDialog.fxml +++ b/src/main/java/org/jabref/gui/preferences/externalfiletypes/EditExternalFileTypeEntryDialog.fxml @@ -10,7 +10,7 @@ - + @@ -27,25 +27,23 @@ - -