From fac9f9907cb6d1e0443cc211001e840e50e2c562 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Sat, 22 Aug 2020 19:02:56 +0200 Subject: [PATCH] Refactored for more clean-ups and naming consistency --- .../org/jabref/cli/ArgumentProcessor.java | 6 +- .../externalfiles/AutoLinkFilesAction.java | 8 ++- .../LinkedFilesEditorViewModel.java | 56 ++++++++++++++++-- .../gui/preferences/FileTabViewModel.java | 8 ++- .../PreferencesDialogViewModel.java | 6 +- .../jabref/preferences/JabRefPreferences.java | 57 +++++++++++-------- .../preferences/PreferencesService.java | 12 ++++ 7 files changed, 114 insertions(+), 39 deletions(-) diff --git a/src/main/java/org/jabref/cli/ArgumentProcessor.java b/src/main/java/org/jabref/cli/ArgumentProcessor.java index 35ec633ed30..9aaecfc92de 100644 --- a/src/main/java/org/jabref/cli/ArgumentProcessor.java +++ b/src/main/java/org/jabref/cli/ArgumentProcessor.java @@ -495,7 +495,11 @@ private void automaticallySetFileLinks(List loaded) { for (ParserResult parserResult : loaded) { BibDatabase database = parserResult.getDatabase(); LOGGER.info(Localization.lang("Automatically setting file links")); - AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(parserResult.getDatabaseContext(), Globals.prefs.getFilePreferences(), Globals.prefs.getAutoLinkPreferences(), ExternalFileTypes.getInstance()); + AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( + parserResult.getDatabaseContext(), + Globals.prefs.getFilePreferences(), + Globals.prefs.getAutoLinkPreferences(), + ExternalFileTypes.getInstance()); util.linkAssociatedFiles(database.getEntries(), new NamedCompound("")); } } diff --git a/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java b/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java index 505137edc35..487e9250a33 100644 --- a/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java +++ b/src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java @@ -52,8 +52,12 @@ public void execute() { List entries = stateManager.getSelectedEntries(); final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links")); - AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(database, preferences.getFilePreferences(), preferences.getAutoLinkPreferences(), ExternalFileTypes.getInstance()); - Task> linkFilesTask = new Task>() { + AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( + database, + preferences.getFilePreferences(), + preferences.getAutoLinkPreferences(), + ExternalFileTypes.getInstance()); + Task> linkFilesTask = new Task<>() { @Override protected List call() { diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java index 23b8c6b7dc5..50f69c0c0d5 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java @@ -101,7 +101,15 @@ public LinkedFileViewModel fromFile(Path file) { List fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences()); LinkedFile linkedFile = fromFile(file, fileDirectories, externalFileTypes); - return new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes); + return new LinkedFileViewModel( + linkedFile, + entry, + databaseContext, + taskExecutor, + dialogService, + preferences.getXMPPreferences(), + preferences.getFilePreferences(), + externalFileTypes); } public boolean isFulltextLookupInProgress() { @@ -114,7 +122,15 @@ public BooleanProperty fulltextLookupInProgressProperty() { private List parseToFileViewModel(String stringValue) { return FileFieldParser.parse(stringValue).stream() - .map(linkedFile -> new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes)) + .map(linkedFile -> new LinkedFileViewModel( + linkedFile, + entry, + databaseContext, + taskExecutor, + dialogService, + preferences.getXMPPreferences(), + preferences.getFilePreferences(), + externalFileTypes)) .collect(Collectors.toList()); } @@ -137,7 +153,15 @@ public void addNewFile() { List fileDirectories = databaseContext.getFileDirectoriesAsPaths(preferences.getFilePreferences()); dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(newFile -> { LinkedFile newLinkedFile = fromFile(newFile, fileDirectories, externalFileTypes); - files.add(new LinkedFileViewModel(newLinkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes)); + files.add(new LinkedFileViewModel( + newLinkedFile, + entry, + databaseContext, + taskExecutor, + dialogService, + preferences.getXMPPreferences(), + preferences.getFilePreferences(), + externalFileTypes)); }); } @@ -159,11 +183,23 @@ public void bindToEntry(BibEntry entry) { private List findAssociatedNotLinkedFiles(BibEntry entry) { List result = new ArrayList<>(); - AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(databaseContext, preferences.getFilePreferences(), preferences.getAutoLinkPreferences(), ExternalFileTypes.getInstance()); + AutoSetFileLinksUtil util = new AutoSetFileLinksUtil( + databaseContext, + preferences.getFilePreferences(), + preferences.getAutoLinkPreferences(), + ExternalFileTypes.getInstance()); try { List linkedFiles = util.findAssociatedNotLinkedFiles(entry); for (LinkedFile linkedFile : linkedFiles) { - LinkedFileViewModel newLinkedFile = new LinkedFileViewModel(linkedFile, entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes); + LinkedFileViewModel newLinkedFile = new LinkedFileViewModel( + linkedFile, + entry, + databaseContext, + taskExecutor, + dialogService, + preferences.getXMPPreferences(), + preferences.getFilePreferences(), + externalFileTypes); newLinkedFile.markAsAutomaticallyFound(); result.add(newLinkedFile); } @@ -206,7 +242,15 @@ public void addFromURL() { } private void addFromURL(URL url) { - LinkedFileViewModel onlineFile = new LinkedFileViewModel(new LinkedFile(url, ""), entry, databaseContext, taskExecutor, dialogService, preferences.getXMPPreferences(), preferences.getFilePreferences(), externalFileTypes); + LinkedFileViewModel onlineFile = new LinkedFileViewModel( + new LinkedFile(url, ""), + entry, + databaseContext, + taskExecutor, + dialogService, + preferences.getXMPPreferences(), + preferences.getFilePreferences(), + externalFileTypes); files.add(onlineFile); onlineFile.download(); } diff --git a/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java b/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java index ad26e3d1966..820050c1674 100644 --- a/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/FileTabViewModel.java @@ -102,6 +102,8 @@ public void setValues() { @Override public void storeSettings() { + + // NO TITLE preferences.putBoolean(JabRefPreferences.OPEN_LAST_EDITED, openLastStartupProperty.getValue()); if (!noWrapFilesProperty.getValue().trim().equals(preferences.get(JabRefPreferences.NON_WRAPPABLE_FIELDS))) { preferences.put(JabRefPreferences.NON_WRAPPABLE_FIELDS, noWrapFilesProperty.getValue()); @@ -112,16 +114,18 @@ public void storeSettings() { if (autolinkUseRegexProperty.getValue()) { preferences.put(JabRefPreferences.AUTOLINK_REG_EXP_SEARCH_EXPRESSION_KEY, autolinkRegexKeyProperty.getValue()); } - preferences.setNewLineSeparator(selectedNewLineSeparatorProperty.getValue()); + preferences.storeNewLineSeparator(selectedNewLineSeparatorProperty.getValue()); preferences.putBoolean(JabRefPreferences.REFORMAT_FILE_ON_SAVE_AND_EXPORT, alwaysReformatBibProperty.getValue()); + // EXTERNAL FILE LINKS preferences.put(JabRefPreferences.MAIN_FILE_DIRECTORY, mainFileDirProperty.getValue()); preferences.putBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR, useBibLocationAsPrimaryProperty.getValue()); preferences.putBoolean(JabRefPreferences.AUTOLINK_USE_REG_EXP_SEARCH_KEY, autolinkUseRegexProperty.getValue()); - preferences.putBoolean(JabRefPreferences.AUTOLINK_EXACT_KEY_ONLY, autolinkFileExactBibtexProperty.getValue()); + preferences.putBoolean(JabRefPreferences.RUN_AUTOMATIC_FILE_SEARCH, searchFilesOnOpenProperty.getValue()); preferences.putBoolean(JabRefPreferences.ALLOW_FILE_AUTO_OPEN_BROWSE, openBrowseOnCreateProperty.getValue()); + // Autosave preferences.putBoolean(JabRefPreferences.LOCAL_AUTO_SAVE, autosaveLocalLibraries.getValue()); } diff --git a/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java b/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java index 849eaa198a5..6d1d7f4a77c 100644 --- a/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/PreferencesDialogViewModel.java @@ -69,7 +69,7 @@ public void importPreferences() { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() .addExtensionFilter(StandardFileType.XML) .withDefaultExtension(StandardFileType.XML) - .withInitialDirectory(preferences.setLastPreferencesExportPath()).build(); + .withInitialDirectory(preferences.getLastPreferencesExportPath()).build(); dialogService.showFileOpenDialog(fileDialogConfiguration).ifPresent(file -> { try { @@ -89,7 +89,7 @@ public void exportPreferences() { FileDialogConfiguration fileDialogConfiguration = new FileDialogConfiguration.Builder() .addExtensionFilter(StandardFileType.XML) .withDefaultExtension(StandardFileType.XML) - .withInitialDirectory(preferences.setLastPreferencesExportPath()) + .withInitialDirectory(preferences.getLastPreferencesExportPath()) .build(); dialogService.showFileSaveDialog(fileDialogConfiguration) @@ -97,7 +97,7 @@ public void exportPreferences() { try { storeAllSettings(); preferences.exportPreferences(exportFile); - preferences.setLastPreferencesExportPath(exportFile); + preferences.storeLastPreferencesExportPath(exportFile); } catch (JabRefException ex) { LOGGER.warn(ex.getMessage(), ex); dialogService.showErrorDialogAndWait(Localization.lang("Export preferences"), ex); diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index cf23a5c7f48..bef713e6e90 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1458,14 +1458,6 @@ public Optional getFontSize() { } } - public String setLastPreferencesExportPath() { - return get(PREFS_EXPORT_PATH); - } - - public void setLastPreferencesExportPath(Path exportFile) { - put(PREFS_EXPORT_PATH, exportFile.toString()); - } - public void setIdBasedFetcherForEntryGenerator(String fetcherName) { put(ID_ENTRY_GENERATOR, fetcherName); } @@ -1554,18 +1546,6 @@ private void saveCustomEntryTypes(BibDatabaseMode bibDatabaseMode, BibEntryTypes storeBibEntryTypes(customBiblatexBibTexTypes, bibDatabaseMode); } - public NewLineSeparator getNewLineSeparator() { - return NewLineSeparator.parse(get(JabRefPreferences.NEWLINE)); - } - - public void setNewLineSeparator(NewLineSeparator newLineSeparator) { - String escapeChars = newLineSeparator.toString(); - put(JabRefPreferences.NEWLINE, escapeChars); - - // we also have to change Globals variable as globals is not a getter, but a constant - OS.NEWLINE = escapeChars; - } - //************************************************************************************************************* // GeneralPreferences //************************************************************************************************************* @@ -2209,6 +2189,17 @@ public void storeMainTableNameFormatPreferences(MainTableNameFormatPreferences p // File preferences //************************************************************************************************************* + @Override + public String getLastPreferencesExportPath() { + return get(PREFS_EXPORT_PATH); + } + + @Override + public void storeLastPreferencesExportPath(Path exportFile) { + put(PREFS_EXPORT_PATH, exportFile.toString()); + } + + // ToDo: Can this be disbanded? @Override public ImportFormatPreferences getImportFormatPreferences() { return new ImportFormatPreferences( @@ -2252,12 +2243,20 @@ public SavePreferences getSavePreferences() { getCitationKeyPatternPreferences()); } + @Override - public FieldWriterPreferences getFieldWriterPreferences() { - return new FieldWriterPreferences( - getBoolean(RESOLVE_STRINGS_ALL_FIELDS), - getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()), - getFieldContentParserPreferences()); + public NewLineSeparator getNewLineSeparator() { + return NewLineSeparator.parse(get(JabRefPreferences.NEWLINE)); + } + + // ToDo: Can this be disbanded? + @Override + public void storeNewLineSeparator(NewLineSeparator newLineSeparator) { + String escapeChars = newLineSeparator.toString(); + put(JabRefPreferences.NEWLINE, escapeChars); + + // we also have to change Globals variable as globals is not a getter, but a constant + OS.NEWLINE = escapeChars; } @Override @@ -2266,6 +2265,14 @@ public FieldContentFormatterPreferences getFieldContentParserPreferences() { getStringList(NON_WRAPPABLE_FIELDS).stream().map(FieldFactory::parseField).collect(Collectors.toList())); } + @Override + public FieldWriterPreferences getFieldWriterPreferences() { + return new FieldWriterPreferences( + getBoolean(RESOLVE_STRINGS_ALL_FIELDS), + getStringList(DO_NOT_RESOLVE_STRINGS_FOR).stream().map(FieldFactory::parseField).collect(Collectors.toList()), + getFieldContentParserPreferences()); + } + @Override public FileHistory getFileHistory() { return new FileHistory(getStringList(RECENT_DATABASES).stream().map(Path::of).collect(Collectors.toList())); diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 8fa8baf67b6..24882c38d9c 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -237,6 +237,18 @@ public interface PreferencesService { void storeMainTableNameFormatPreferences(MainTableNameFormatPreferences preferences); + //************************************************************************************************************* + // File preferences + //************************************************************************************************************* + + NewLineSeparator getNewLineSeparator(); + + void storeNewLineSeparator(NewLineSeparator newLineSeparator); + + void storeLastPreferencesExportPath(Path exportFile); + + String getLastPreferencesExportPath(); + //************************************************************************************************************* // ToDo: Misc preferences //*************************************************************************************************************