From 3f761d4d9797af88c1d658abb98eec482f10eb75 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Mon, 23 Mar 2020 15:56:10 +0100 Subject: [PATCH 1/2] Refactored general preferences to a new preferences class --- src/main/java/org/jabref/Globals.java | 4 +- src/main/java/org/jabref/gui/BasePanel.java | 6 +- src/main/java/org/jabref/gui/JabRefFrame.java | 2 +- .../BibtexExtractorViewModel.java | 19 +- .../FindUnlinkedFilesDialog.java | 4 +- .../gui/externalfiles/ImportHandler.java | 21 +- .../org/jabref/gui/importer/ImportAction.java | 2 +- .../gui/importer/ImportEntriesViewModel.java | 4 +- .../actions/AppendDatabaseAction.java | 9 +- .../org/jabref/gui/maintable/MainTable.java | 4 +- .../gui/preferences/GeneralTabViewModel.java | 113 +++++---- .../logic/preferences/OwnerPreferences.java | 27 ++ .../preferences/TimestampPreferences.java | 19 +- .../org/jabref/logic/util/UpdateField.java | 46 ++-- .../logic/util/UpdateFieldPreferences.java | 55 ---- .../preferences/GeneralPreferences.java | 74 ++++++ .../jabref/preferences/JabRefPreferences.java | 239 +++++++++++------- .../preferences/PreferencesService.java | 43 +++- .../org/jabref/JabRefPreferencesTest.java | 7 +- 19 files changed, 427 insertions(+), 271 deletions(-) create mode 100644 src/main/java/org/jabref/logic/preferences/OwnerPreferences.java delete mode 100644 src/main/java/org/jabref/logic/util/UpdateFieldPreferences.java create mode 100644 src/main/java/org/jabref/preferences/GeneralPreferences.java diff --git a/src/main/java/org/jabref/Globals.java b/src/main/java/org/jabref/Globals.java index 5f8177ffbca..46bc330b69e 100644 --- a/src/main/java/org/jabref/Globals.java +++ b/src/main/java/org/jabref/Globals.java @@ -99,7 +99,7 @@ public static void startBackgroundTasks() throws JabRefException { themeLoader = new ThemeLoader(fileUpdateMonitor, prefs); - if (Globals.prefs.shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) { + if (Globals.prefs.getShouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) { startTelemetryClient(); } } @@ -114,7 +114,7 @@ private static void stopTelemetryClient() { private static void startTelemetryClient() { TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive(); telemetryConfiguration.setInstrumentationKey(Globals.BUILD_INFO.azureInstrumentationKey); - telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.shouldCollectTelemetry()); + telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.getShouldCollectTelemetry()); telemetryClient = new TelemetryClient(telemetryConfiguration); telemetryClient.getContext().getProperties().put("JabRef version", Globals.BUILD_INFO.version.toString()); telemetryClient.getContext().getProperties().put("Java version", StandardSystemProperty.JAVA_VERSION.value()); diff --git a/src/main/java/org/jabref/gui/BasePanel.java b/src/main/java/org/jabref/gui/BasePanel.java index 330463448ed..934f93c8133 100644 --- a/src/main/java/org/jabref/gui/BasePanel.java +++ b/src/main/java/org/jabref/gui/BasePanel.java @@ -261,7 +261,11 @@ public void insertEntries(final List entries) { // Set owner and timestamp for (BibEntry entry : entries) { - UpdateField.setAutomaticFields(entry, true, true, Globals.prefs.getUpdateFieldPreferences()); + UpdateField.setAutomaticFields(entry, + true, + true, + Globals.prefs.getOwnerPreferences(), + Globals.prefs.getTimestampPreferences()); } // Create an UndoableInsertEntries object. getUndoManager().addEdit(new UndoableInsertEntries(bibDatabaseContext.getDatabase(), entries)); diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index 09892d6d971..d9b78ab8358 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -264,7 +264,7 @@ public void run() { } private Void showTrackingNotification() { - if (!Globals.prefs.shouldCollectTelemetry()) { + if (!Globals.prefs.getShouldCollectTelemetry()) { boolean shouldCollect = dialogService.showConfirmationDialogAndWait( Localization.lang("Telemetry: Help make JabRef better"), Localization.lang("To improve the user experience, we would like to collect anonymous statistics on the features you use. We will only record what features you access and how often you do it. We will neither collect any personal data nor the content of bibliographic items. If you choose to allow data collection, you can later disable it via Options -> Preferences -> General."), diff --git a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java index c68ae79c3a8..f69dfd51675 100644 --- a/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java +++ b/src/main/java/org/jabref/gui/bibtexextractor/BibtexExtractorViewModel.java @@ -30,12 +30,25 @@ public class BibtexExtractorViewModel { private TaskExecutor taskExecutor; private ImportHandler importHandler; - public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext, DialogService dialogService, - JabRefPreferences jabRefPreferences, FileUpdateMonitor fileUpdateMonitor, TaskExecutor taskExecutor, UndoManager undoManager, StateManager stateManager) { + public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext, + DialogService dialogService, + JabRefPreferences jabRefPreferences, + FileUpdateMonitor fileUpdateMonitor, + TaskExecutor taskExecutor, + UndoManager undoManager, + StateManager stateManager) { + this.dialogService = dialogService; currentCitationfetcher = new GrobidCitationFetcher(jabRefPreferences.getImportFormatPreferences()); this.taskExecutor = taskExecutor; - this.importHandler = new ImportHandler(dialogService, bibdatabaseContext, ExternalFileTypes.getInstance(), jabRefPreferences.getFilePreferences(), jabRefPreferences.getImportFormatPreferences(), jabRefPreferences.getUpdateFieldPreferences(), fileUpdateMonitor, undoManager, stateManager); + this.importHandler = new ImportHandler( + dialogService, + bibdatabaseContext, + ExternalFileTypes.getInstance(), + jabRefPreferences, + fileUpdateMonitor, + undoManager, + stateManager); } public StringProperty inputTextProperty() { diff --git a/src/main/java/org/jabref/gui/externalfiles/FindUnlinkedFilesDialog.java b/src/main/java/org/jabref/gui/externalfiles/FindUnlinkedFilesDialog.java index ff250cb7a6b..9c2698126db 100644 --- a/src/main/java/org/jabref/gui/externalfiles/FindUnlinkedFilesDialog.java +++ b/src/main/java/org/jabref/gui/externalfiles/FindUnlinkedFilesDialog.java @@ -86,9 +86,7 @@ public FindUnlinkedFilesDialog(BibDatabaseContext database, DialogService dialog dialogService, databaseContext, ExternalFileTypes.getInstance(), - Globals.prefs.getFilePreferences(), - Globals.prefs.getImportFormatPreferences(), - Globals.prefs.getUpdateFieldPreferences(), + preferences, Globals.getFileUpdateMonitor(), undoManager, Globals.stateManager); diff --git a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java index 6e155fee70e..2739d923945 100644 --- a/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java +++ b/src/main/java/org/jabref/gui/externalfiles/ImportHandler.java @@ -15,9 +15,7 @@ import org.jabref.gui.undo.UndoableInsertEntries; import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator; import org.jabref.logic.externalfiles.ExternalFilesContentImporter; -import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.util.UpdateField; -import org.jabref.logic.util.UpdateFieldPreferences; import org.jabref.logic.util.io.FileUtil; import org.jabref.model.FieldChange; import org.jabref.model.database.BibDatabaseContext; @@ -25,13 +23,13 @@ import org.jabref.model.entry.field.StandardField; import org.jabref.model.groups.GroupEntryChanger; import org.jabref.model.groups.GroupTreeNode; -import org.jabref.model.metadata.FilePreferences; import org.jabref.model.util.FileUpdateMonitor; +import org.jabref.preferences.PreferencesService; public class ImportHandler { private final BibDatabaseContext database; - private final UpdateFieldPreferences updateFieldPreferences; + private final PreferencesService preferencesService; private final DialogService dialogService; private final FileUpdateMonitor fileUpdateMonitor; private final ExternalFilesEntryLinker linker; @@ -42,21 +40,20 @@ public class ImportHandler { public ImportHandler(DialogService dialogService, BibDatabaseContext database, ExternalFileTypes externalFileTypes, - FilePreferences filePreferences, - ImportFormatPreferences importFormatPreferences, - UpdateFieldPreferences updateFieldPreferences, + PreferencesService preferencesService, FileUpdateMonitor fileupdateMonitor, UndoManager undoManager, StateManager stateManager) { this.dialogService = dialogService; this.database = database; - this.updateFieldPreferences = updateFieldPreferences; + + this.preferencesService = preferencesService; this.fileUpdateMonitor = fileupdateMonitor; this.stateManager = stateManager; - this.linker = new ExternalFilesEntryLinker(externalFileTypes, filePreferences, database); - this.contentImporter = new ExternalFilesContentImporter(importFormatPreferences); + this.linker = new ExternalFilesEntryLinker(externalFileTypes, preferencesService.getFilePreferences(), database); + this.contentImporter = new ExternalFilesContentImporter(preferencesService.getImportFormatPreferences()); this.undoManager = undoManager; } @@ -114,7 +111,9 @@ public void importEntries(List entries) { database.getDatabase().insertEntries(entries); // Set owner/timestamp - UpdateField.setAutomaticFields(entries, updateFieldPreferences); + UpdateField.setAutomaticFields(entries, + preferencesService.getOwnerPreferences(), + preferencesService.getTimestampPreferences()); // Generate bibtex keys generateKeys(entries); diff --git a/src/main/java/org/jabref/gui/importer/ImportAction.java b/src/main/java/org/jabref/gui/importer/ImportAction.java index 2221c2e439c..e2cda21768e 100644 --- a/src/main/java/org/jabref/gui/importer/ImportAction.java +++ b/src/main/java/org/jabref/gui/importer/ImportAction.java @@ -153,7 +153,7 @@ private ParserResult mergeImportResults(List entriesToImport) dialogService, database, ExternalFileTypes.getInstance(), - preferences.getFilePreferences(), - preferences.getImportFormatPreferences(), - preferences.getUpdateFieldPreferences(), + preferences, fileUpdateMonitor, undoManager, stateManager); diff --git a/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java b/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java index 60feebd3548..2f1825631ff 100644 --- a/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java +++ b/src/main/java/org/jabref/gui/importer/actions/AppendDatabaseAction.java @@ -73,12 +73,15 @@ private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult, if (importEntries) { // Add entries boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER); - boolean overwriteTimeStamp = Globals.prefs.getTimestampPreferences().overwriteTimestamp(); + boolean overwriteTimeStamp = Globals.prefs.getTimestampPreferences().isOverwriteTimestamp(); for (BibEntry originalEntry : fromDatabase.getEntries()) { BibEntry entry = (BibEntry) originalEntry.clone(); - UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp, - Globals.prefs.getUpdateFieldPreferences()); + UpdateField.setAutomaticFields(entry, + overwriteOwner, + overwriteTimeStamp, + Globals.prefs.getOwnerPreferences(), + Globals.prefs.getTimestampPreferences()); entriesToAppend.add(entry); } database.insertEntries(entriesToAppend); diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index 24fa8753183..f182dbaf07d 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -69,9 +69,7 @@ public MainTable(MainTableDataModel model, JabRefFrame frame, importHandler = new ImportHandler( frame.getDialogService(), database, externalFileTypes, - Globals.prefs.getFilePreferences(), - Globals.prefs.getImportFormatPreferences(), - Globals.prefs.getUpdateFieldPreferences(), + Globals.prefs, Globals.getFileUpdateMonitor(), undoManager, Globals.stateManager); diff --git a/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java b/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java index ff32611f19a..3a255fc0426 100644 --- a/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java @@ -19,8 +19,12 @@ import org.jabref.logic.l10n.Encodings; import org.jabref.logic.l10n.Language; import org.jabref.logic.l10n.Localization; +import org.jabref.logic.preferences.OwnerPreferences; +import org.jabref.logic.preferences.TimestampPreferences; import org.jabref.model.database.BibDatabaseMode; -import org.jabref.preferences.JabRefPreferences; +import org.jabref.model.entry.field.FieldFactory; +import org.jabref.preferences.GeneralPreferences; +import org.jabref.preferences.PreferencesService; import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator; import de.saxsys.mvvmfx.utils.validation.ValidationMessage; @@ -54,14 +58,20 @@ public class GeneralTabViewModel implements PreferenceTabViewModel { private Validator markTimeStampFormatValidator; private final DialogService dialogService; - private final JabRefPreferences preferences; + private final PreferencesService preferencesService; + private final GeneralPreferences initialGeneralPreferences; + private final OwnerPreferences initialOwnerPreferences; + private final TimestampPreferences initialTimestampPreferences; private List restartWarning = new ArrayList<>(); @SuppressWarnings("ReturnValueIgnored") - public GeneralTabViewModel(DialogService dialogService, JabRefPreferences preferences) { + public GeneralTabViewModel(DialogService dialogService, PreferencesService preferencesService) { this.dialogService = dialogService; - this.preferences = preferences; + this.preferencesService = preferencesService; + this.initialGeneralPreferences = preferencesService.getGeneralPreferences(); + this.initialOwnerPreferences = preferencesService.getOwnerPreferences(); + this.initialTimestampPreferences = preferencesService.getTimestampPreferences(); markTimeStampFormatValidator = new FunctionBasedValidator<>( markTimeStampFormatProperty, @@ -85,70 +95,69 @@ public GeneralTabViewModel(DialogService dialogService, JabRefPreferences prefer public void setValues() { languagesListProperty.setValue(FXCollections.observableArrayList(Language.values())); - selectedLanguageProperty.setValue(preferences.getLanguage()); + selectedLanguageProperty.setValue(preferencesService.getLanguage()); encodingsListProperty.setValue(FXCollections.observableArrayList(Encodings.getCharsets())); - selectedEncodingProperty.setValue(preferences.getDefaultEncoding()); + selectedEncodingProperty.setValue(initialGeneralPreferences.getDefaultEncoding()); bibliographyModeListProperty.setValue(FXCollections.observableArrayList(BibDatabaseMode.values())); - if (preferences.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)) { - selectedBiblatexModeProperty.setValue(BibDatabaseMode.BIBLATEX); - } else { - selectedBiblatexModeProperty.setValue(BibDatabaseMode.BIBTEX); - } - - inspectionWarningDuplicateProperty.setValue(preferences.getBoolean(JabRefPreferences.WARN_ABOUT_DUPLICATES_IN_INSPECTION)); - confirmDeleteProperty.setValue(preferences.getBoolean(JabRefPreferences.CONFIRM_DELETE)); - enforceLegalKeysProperty.setValue(preferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY)); - allowIntegerEditionProperty.setValue(preferences.getBoolean(JabRefPreferences.ALLOW_INTEGER_EDITION_BIBTEX)); - memoryStickModeProperty.setValue(preferences.getBoolean(JabRefPreferences.MEMORY_STICK_MODE)); - collectTelemetryProperty.setValue(preferences.shouldCollectTelemetry()); - showAdvancedHintsProperty.setValue(preferences.getBoolean(JabRefPreferences.SHOW_ADVANCED_HINTS)); - - markOwnerProperty.setValue(preferences.getBoolean(JabRefPreferences.USE_OWNER)); - markOwnerNameProperty.setValue(preferences.get(JabRefPreferences.DEFAULT_OWNER)); - markOwnerOverwriteProperty.setValue(preferences.getBoolean(JabRefPreferences.OVERWRITE_OWNER)); - - markTimestampProperty.setValue(preferences.getBoolean(JabRefPreferences.USE_TIME_STAMP)); - markTimeStampFormatProperty.setValue(preferences.get(JabRefPreferences.TIME_STAMP_FORMAT)); - markTimeStampOverwriteProperty.setValue(preferences.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP)); - markTimeStampFieldNameProperty.setValue(preferences.get(JabRefPreferences.TIME_STAMP_FIELD)); - updateTimeStampProperty.setValue(preferences.getBoolean(JabRefPreferences.UPDATE_TIMESTAMP)); + selectedBiblatexModeProperty.setValue(initialGeneralPreferences.getDefaultBibDatabaseMode()); + + inspectionWarningDuplicateProperty.setValue(initialGeneralPreferences.isWarnAboutDuplicatesInInspection()); + confirmDeleteProperty.setValue(initialGeneralPreferences.isConfirmDelete()); + enforceLegalKeysProperty.setValue(initialGeneralPreferences.isEnforceLegalBibtexKey()); + allowIntegerEditionProperty.setValue(initialGeneralPreferences.isAllowIntegerEditionBibtex()); + memoryStickModeProperty.setValue(initialGeneralPreferences.isMemoryStickMode()); + collectTelemetryProperty.setValue(preferencesService.getShouldCollectTelemetry()); + showAdvancedHintsProperty.setValue(initialGeneralPreferences.isShowAdvancedHints()); + + markOwnerProperty.setValue(initialOwnerPreferences.isUseOwner()); + markOwnerNameProperty.setValue(initialOwnerPreferences.getDefaultOwner()); + markOwnerOverwriteProperty.setValue(initialOwnerPreferences.isOverwriteOwner()); + + markTimestampProperty.setValue(initialTimestampPreferences.isUseTimestamps()); + markTimeStampFormatProperty.setValue(initialTimestampPreferences.getTimestampFormat()); + markTimeStampOverwriteProperty.setValue(initialTimestampPreferences.isOverwriteTimestamp()); + markTimeStampFieldNameProperty.setValue(initialTimestampPreferences.getTimestampField().getName()); + updateTimeStampProperty.setValue(initialTimestampPreferences.isUpdateTimestamp()); } public void storeSettings() { Language newLanguage = selectedLanguageProperty.getValue(); - if (newLanguage != preferences.getLanguage()) { - preferences.setLanguage(newLanguage); + if (newLanguage != preferencesService.getLanguage()) { + preferencesService.setLanguage(newLanguage); Localization.setLanguage(newLanguage); restartWarning.add(Localization.lang("Changed language") + ": " + newLanguage.getDisplayName()); } - preferences.setDefaultEncoding(selectedEncodingProperty.getValue()); - preferences.putBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE, selectedBiblatexModeProperty.getValue() == BibDatabaseMode.BIBLATEX); - - preferences.putBoolean(JabRefPreferences.WARN_ABOUT_DUPLICATES_IN_INSPECTION, inspectionWarningDuplicateProperty.getValue()); - preferences.putBoolean(JabRefPreferences.CONFIRM_DELETE, confirmDeleteProperty.getValue()); - preferences.putBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY, enforceLegalKeysProperty.getValue()); - preferences.putBoolean(JabRefPreferences.ALLOW_INTEGER_EDITION_BIBTEX, allowIntegerEditionProperty.getValue()); - if (preferences.getBoolean(JabRefPreferences.MEMORY_STICK_MODE) && !memoryStickModeProperty.getValue()) { + if (initialGeneralPreferences.isMemoryStickMode() && !memoryStickModeProperty.getValue()) { dialogService.showInformationDialogAndWait(Localization.lang("Memory stick mode"), Localization.lang("To disable the memory stick mode" + " rename or remove the jabref.xml file in the same folder as JabRef.")); } - preferences.putBoolean(JabRefPreferences.MEMORY_STICK_MODE, memoryStickModeProperty.getValue()); - preferences.setShouldCollectTelemetry(collectTelemetryProperty.getValue()); - preferences.putBoolean(JabRefPreferences.SHOW_ADVANCED_HINTS, showAdvancedHintsProperty.getValue()); - - preferences.putBoolean(JabRefPreferences.USE_OWNER, markOwnerProperty.getValue()); - preferences.put(JabRefPreferences.DEFAULT_OWNER, markOwnerNameProperty.getValue().trim()); - preferences.putBoolean(JabRefPreferences.OVERWRITE_OWNER, markOwnerOverwriteProperty.getValue()); - - preferences.putBoolean(JabRefPreferences.USE_TIME_STAMP, markTimestampProperty.getValue()); - preferences.put(JabRefPreferences.TIME_STAMP_FORMAT, markTimeStampFormatProperty.getValue().trim()); - preferences.put(JabRefPreferences.TIME_STAMP_FIELD, markTimeStampFieldNameProperty.getValue().trim()); - preferences.putBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP, markTimeStampOverwriteProperty.getValue()); - preferences.putBoolean(JabRefPreferences.UPDATE_TIMESTAMP, updateTimeStampProperty.getValue()); + + preferencesService.storeGeneralPreferences(new GeneralPreferences( + selectedEncodingProperty.getValue(), + selectedBiblatexModeProperty.getValue(), + inspectionWarningDuplicateProperty.getValue(), + confirmDeleteProperty.getValue(), + enforceLegalKeysProperty.getValue(), + allowIntegerEditionProperty.getValue(), + memoryStickModeProperty.getValue(), + collectTelemetryProperty.getValue(), + showAdvancedHintsProperty.getValue())); + + preferencesService.storeOwnerPreferences(new OwnerPreferences( + markOwnerProperty.getValue(), + markOwnerNameProperty.getValue().trim(), + markOwnerOverwriteProperty.getValue())); + + preferencesService.storeTimestampPreferences(new TimestampPreferences( + markTimestampProperty.getValue(), + updateTimeStampProperty.getValue(), + FieldFactory.parseField(markTimeStampFieldNameProperty.getValue().trim()), + markTimeStampFormatProperty.getValue().trim(), + markTimeStampOverwriteProperty.getValue())); } public ValidationStatus markTimeStampFormatValidationStatus() { diff --git a/src/main/java/org/jabref/logic/preferences/OwnerPreferences.java b/src/main/java/org/jabref/logic/preferences/OwnerPreferences.java new file mode 100644 index 00000000000..8c18d099124 --- /dev/null +++ b/src/main/java/org/jabref/logic/preferences/OwnerPreferences.java @@ -0,0 +1,27 @@ +package org.jabref.logic.preferences; + +public class OwnerPreferences { + private final boolean useOwner; + private final String defaultOwner; + private final boolean overwriteOwner; + + public OwnerPreferences(boolean useOwner, + String defaultOwner, + boolean overwriteOwner) { + this.useOwner = useOwner; + this.defaultOwner = defaultOwner; + this.overwriteOwner = overwriteOwner; + } + + public boolean isUseOwner() { + return useOwner; + } + + public String getDefaultOwner() { + return defaultOwner; + } + + public boolean isOverwriteOwner() { + return overwriteOwner; + } +} diff --git a/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java b/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java index cb143709d58..fc24b3f5d1d 100644 --- a/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java +++ b/src/main/java/org/jabref/logic/preferences/TimestampPreferences.java @@ -7,25 +7,25 @@ public class TimestampPreferences { private final boolean useTimestamps; - private final boolean useModifiedTimestamp; + private final boolean updateTimestamp; private final Field timestampField; private final String timestampFormat; private final boolean overwriteTimestamp; - public TimestampPreferences(boolean useTimestamps, boolean useModifiedTimestamp, Field timestampField, String timestampFormat, boolean overwriteTimestamp) { + public TimestampPreferences(boolean useTimestamps, boolean updateTimestamp, Field timestampField, String timestampFormat, boolean overwriteTimestamp) { this.useTimestamps = useTimestamps; - this.useModifiedTimestamp = useModifiedTimestamp; + this.updateTimestamp = updateTimestamp; this.timestampField = timestampField; this.timestampFormat = timestampFormat; this.overwriteTimestamp = overwriteTimestamp; } - public boolean includeCreatedTimestamp() { + public boolean isUseTimestamps() { return useTimestamps; } - public boolean includeModifiedTimestamp() { - return useModifiedTimestamp; + public boolean isUpdateTimestamp() { + return updateTimestamp; } public Field getTimestampField() { @@ -36,16 +36,15 @@ public String getTimestampFormat() { return timestampFormat; } - public boolean overwriteTimestamp() { + public boolean isOverwriteTimestamp() { return overwriteTimestamp; } public boolean includeTimestamps() { - return useTimestamps && useModifiedTimestamp; + return useTimestamps && updateTimestamp; } public String now() { - String timeStampFormat = timestampFormat; - return DateTimeFormatter.ofPattern(timeStampFormat).format(LocalDateTime.now()); + return DateTimeFormatter.ofPattern(timestampFormat).format(LocalDateTime.now()); } } diff --git a/src/main/java/org/jabref/logic/util/UpdateField.java b/src/main/java/org/jabref/logic/util/UpdateField.java index d16c0ffc70e..681571cb53c 100644 --- a/src/main/java/org/jabref/logic/util/UpdateField.java +++ b/src/main/java/org/jabref/logic/util/UpdateField.java @@ -5,6 +5,8 @@ import java.util.Collection; import java.util.Optional; +import org.jabref.logic.preferences.OwnerPreferences; +import org.jabref.logic.preferences.TimestampPreferences; import org.jabref.model.FieldChange; import org.jabref.model.entry.BibEntry; import org.jabref.model.entry.field.Field; @@ -89,18 +91,23 @@ public static Optional updateField(BibEntry be, Field field, String * @param overwriteTimestamp Indicates whether timestamp should be set if it is already set. */ public static void setAutomaticFields(BibEntry entry, boolean overwriteOwner, boolean overwriteTimestamp, - UpdateFieldPreferences prefs) { - String defaultOwner = prefs.getDefaultOwner(); - String timestamp = DateTimeFormatter.ofPattern(prefs.getTimeStampFormat()).format(LocalDateTime.now()); - Field timeStampField = prefs.getTimeStampField(); - boolean setOwner = prefs.isUseOwner() && (overwriteOwner || (!entry.hasField(InternalField.OWNER))); - boolean setTimeStamp = prefs.isUseTimeStamp() && (overwriteTimestamp || (!entry.hasField(timeStampField))); + OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) { + String defaultOwner = ownerPreferences.getDefaultOwner(); + String timestamp = DateTimeFormatter.ofPattern(timestampPreferences.getTimestampFormat()).format(LocalDateTime.now()); + Field timeStampField = timestampPreferences.getTimestampField(); + boolean setOwner = ownerPreferences.isUseOwner() && (overwriteOwner || (!entry.hasField(InternalField.OWNER))); + boolean setTimeStamp = timestampPreferences.isUseTimestamps() && (overwriteTimestamp || (!entry.hasField(timeStampField))); setAutomaticFields(entry, setOwner, defaultOwner, setTimeStamp, timeStampField, timestamp); } - public static void setAutomaticFields(BibEntry entry, UpdateFieldPreferences prefs) { - UpdateField.setAutomaticFields(entry, prefs.isOverwriteOwner(), prefs.isOverwriteTimeStamp(), prefs); + public static void setAutomaticFields(BibEntry entry, + OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) { + UpdateField.setAutomaticFields(entry, + ownerPreferences.isOverwriteOwner(), + timestampPreferences.isOverwriteTimestamp(), + ownerPreferences, + timestampPreferences); } private static void setAutomaticFields(BibEntry entry, boolean setOwner, String owner, boolean setTimeStamp, @@ -123,20 +130,20 @@ private static void setAutomaticFields(BibEntry entry, boolean setOwner, String * * @param bibs List of bibtex entries */ - public static void setAutomaticFields(Collection bibs, boolean overwriteOwner, - boolean overwriteTimestamp, UpdateFieldPreferences prefs) { + public static void setAutomaticFields(Collection bibs, boolean overwriteOwner, boolean overwriteTimestamp, + OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) { - boolean globalSetOwner = prefs.isUseOwner(); - boolean globalSetTimeStamp = prefs.isUseTimeStamp(); + boolean globalSetOwner = ownerPreferences.isUseOwner(); + boolean globalSetTimeStamp = timestampPreferences.isUseTimestamps(); // Do not need to do anything if all options are disabled if (!(globalSetOwner || globalSetTimeStamp)) { return; } - Field timeStampField = prefs.getTimeStampField(); - String defaultOwner = prefs.getDefaultOwner(); - String timestamp = DateTimeFormatter.ofPattern(prefs.getTimeStampFormat()).format(LocalDateTime.now()); + Field timeStampField = timestampPreferences.getTimestampField(); + String defaultOwner = ownerPreferences.getDefaultOwner(); + String timestamp = DateTimeFormatter.ofPattern(timestampPreferences.getTimestampFormat()).format(LocalDateTime.now()); // Iterate through all entries for (BibEntry curEntry : bibs) { @@ -146,7 +153,12 @@ public static void setAutomaticFields(Collection bibs, boolean overwri } } - public static void setAutomaticFields(Collection bibs, UpdateFieldPreferences prefs) { - UpdateField.setAutomaticFields(bibs, prefs.isOverwriteOwner(), prefs.isOverwriteTimeStamp(), prefs); + public static void setAutomaticFields(Collection bibs, + OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) { + UpdateField.setAutomaticFields(bibs, + ownerPreferences.isOverwriteOwner(), + timestampPreferences.isOverwriteTimestamp(), + ownerPreferences, + timestampPreferences); } } diff --git a/src/main/java/org/jabref/logic/util/UpdateFieldPreferences.java b/src/main/java/org/jabref/logic/util/UpdateFieldPreferences.java deleted file mode 100644 index 4274a4477b1..00000000000 --- a/src/main/java/org/jabref/logic/util/UpdateFieldPreferences.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.jabref.logic.util; - -import org.jabref.model.entry.field.Field; - -public class UpdateFieldPreferences { - - private final boolean useOwner; - private final boolean useTimeStamp; - private final boolean overwriteOwner; - private final boolean overwriteTimeStamp; - private final Field timeStampField; - private final String timeStampFormat; - private final String defaultOwner; - - - public UpdateFieldPreferences(boolean useOwner, boolean overwriteOwner, String defaultOwner, boolean useTimeStamp, - boolean overwriteTimeStamp, Field timeStampField, - String timeStampFormat) { - this.useOwner = useOwner; - this.overwriteOwner = overwriteOwner; - this.defaultOwner = defaultOwner; - this.useTimeStamp = useTimeStamp; - this.overwriteTimeStamp = overwriteTimeStamp; - this.timeStampField = timeStampField; - this.timeStampFormat = timeStampFormat; - } - - public boolean isUseOwner() { - return useOwner; - } - - public boolean isUseTimeStamp() { - return useTimeStamp; - } - - public Field getTimeStampField() { - return timeStampField; - } - - public String getDefaultOwner() { - return defaultOwner; - } - - public String getTimeStampFormat() { - return timeStampFormat; - } - - public boolean isOverwriteOwner() { - return overwriteOwner; - } - - public boolean isOverwriteTimeStamp() { - return overwriteTimeStamp; - } -} diff --git a/src/main/java/org/jabref/preferences/GeneralPreferences.java b/src/main/java/org/jabref/preferences/GeneralPreferences.java new file mode 100644 index 00000000000..40e0092361e --- /dev/null +++ b/src/main/java/org/jabref/preferences/GeneralPreferences.java @@ -0,0 +1,74 @@ +package org.jabref.preferences; + +import java.nio.charset.Charset; + +import org.jabref.model.database.BibDatabaseMode; + +public class GeneralPreferences { + private final Charset defaultEncoding; + private final BibDatabaseMode defaultBibDatabaseMode; + + private final boolean warnAboutDuplicatesInInspection; + private final boolean confirmDelete; + private final boolean enforceLegalBibtexKey; + private final boolean allowIntegerEditionBibtex; + private final boolean memoryStickMode; + private final boolean collectTelemetry; + private final boolean showAdvancedHints; + + public GeneralPreferences(Charset defaultEncoding, + BibDatabaseMode defaultBibDatabaseMode, + boolean warnAboutDuplicatesInInspection, + boolean confirmDelete, + boolean enforceLegalBibtexKey, + boolean allowIntegerEditionBibtex, + boolean memoryStickMode, + boolean collectTelemetry, + boolean showAdvancedHints) { + this.defaultEncoding = defaultEncoding; + this.defaultBibDatabaseMode = defaultBibDatabaseMode; + this.warnAboutDuplicatesInInspection = warnAboutDuplicatesInInspection; + this.confirmDelete = confirmDelete; + this.enforceLegalBibtexKey = enforceLegalBibtexKey; + this.allowIntegerEditionBibtex = allowIntegerEditionBibtex; + this.memoryStickMode = memoryStickMode; + this.collectTelemetry = collectTelemetry; + this.showAdvancedHints = showAdvancedHints; + } + + public Charset getDefaultEncoding() { + return defaultEncoding; + } + + public BibDatabaseMode getDefaultBibDatabaseMode() { + return defaultBibDatabaseMode; + } + + public boolean isWarnAboutDuplicatesInInspection() { + return warnAboutDuplicatesInInspection; + } + + public boolean isConfirmDelete() { + return confirmDelete; + } + + public boolean isEnforceLegalBibtexKey() { + return enforceLegalBibtexKey; + } + + public boolean isAllowIntegerEditionBibtex() { + return allowIntegerEditionBibtex; + } + + public boolean isMemoryStickMode() { + return memoryStickMode; + } + + public boolean isCollectTelemetry() { + return collectTelemetry; + } + + public boolean isShowAdvancedHints() { + return showAdvancedHints; + } +} diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 1ef1fa19bf3..d753199f131 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -79,6 +79,7 @@ import org.jabref.logic.net.ProxyPreferences; import org.jabref.logic.openoffice.OpenOfficePreferences; import org.jabref.logic.openoffice.StyleLoader; +import org.jabref.logic.preferences.OwnerPreferences; import org.jabref.logic.preferences.TimestampPreferences; import org.jabref.logic.protectedterms.ProtectedTermsList; import org.jabref.logic.protectedterms.ProtectedTermsLoader; @@ -86,7 +87,6 @@ import org.jabref.logic.remote.RemotePreferences; import org.jabref.logic.shared.prefs.SharedDatabasePreferences; import org.jabref.logic.util.OS; -import org.jabref.logic.util.UpdateFieldPreferences; import org.jabref.logic.util.Version; import org.jabref.logic.util.io.AutoLinkPreferences; import org.jabref.logic.util.io.FileHistory; @@ -185,9 +185,12 @@ public class JabRefPreferences implements PreferencesService { public static final String RENAME_ON_MOVE_FILE_TO_FILE_DIR = "renameOnMoveFileToFileDir"; public static final String MEMORY_STICK_MODE = "memoryStickMode"; public static final String SHOW_ADVANCED_HINTS = "showAdvancedHints"; - public static final String DEFAULT_OWNER = "defaultOwner"; public static final String DEFAULT_ENCODING = "defaultEncoding"; - // Timestamp preferences + + public static final String USE_OWNER = "useOwner"; + public static final String DEFAULT_OWNER = "defaultOwner"; + public static final String OVERWRITE_OWNER = "overwriteOwner"; + public static final String USE_TIME_STAMP = "useTimeStamp"; public static final String UPDATE_TIMESTAMP = "updateTimestamp"; public static final String TIME_STAMP_FIELD = "timeStampField"; @@ -215,8 +218,6 @@ public class JabRefPreferences implements PreferencesService { public static final String CONFIRM_DELETE = "confirmDelete"; public static final String WARN_BEFORE_OVERWRITING_KEY = "warnBeforeOverwritingKey"; public static final String AVOID_OVERWRITING_KEY = "avoidOverwritingKey"; - public static final String OVERWRITE_OWNER = "overwriteOwner"; - public static final String USE_OWNER = "useOwner"; public static final String AUTOLINK_EXACT_KEY_ONLY = "autolinkExactKeyOnly"; public static final String SHOW_FILE_LINKS_UPGRADE_WARNING = "showFileLinksUpgradeWarning"; public static final String SIDE_PANE_WIDTH = "sidePaneWidthFX"; @@ -1206,16 +1207,6 @@ public Map> getEntryEditorTabList() { return tabList; } - @Override - public boolean getEnforceLegalKeys() { - return getBoolean(ENFORCE_LEGAL_BIBTEX_KEY); - } - - @Override - public boolean getAllowIntegerEdition() { - return getBoolean(ALLOW_INTEGER_EDITION_BIBTEX); - } - @Override public void updateEntryEditorTabList() { tabList = EntryEditorTabList.create(this); @@ -1274,16 +1265,6 @@ public String getWrappedUsername() { return '[' + get(DEFAULT_OWNER) + ']'; } - @Override - public Charset getDefaultEncoding() { - return Charset.forName(get(DEFAULT_ENCODING)); - } - - @Override - public void setDefaultEncoding(Charset encoding) { - put(DEFAULT_ENCODING, encoding.name()); - } - public FileHistory getFileHistory() { return new FileHistory(getStringList(RECENT_DATABASES).stream().map(Paths::get).collect(Collectors.toList())); } @@ -1299,20 +1280,13 @@ public FilePreferences getFilePreferences() { Map fieldDirectories = Stream.of(StandardField.FILE, StandardField.PDF, StandardField.PS) .collect(Collectors.toMap(field -> field, field -> get(field.getName() + FilePreferences.DIR_SUFFIX, ""))); return new FilePreferences( - getUser(), - fieldDirectories, - getBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR), - get(IMPORT_FILENAMEPATTERN), + getUser(), + fieldDirectories, + getBoolean(JabRefPreferences.BIB_LOC_AS_PRIMARY_DIR), + get(IMPORT_FILENAMEPATTERN), get(IMPORT_FILEDIRPATTERN)); } - @Override - public UpdateFieldPreferences getUpdateFieldPreferences() { - return new UpdateFieldPreferences(getBoolean(USE_OWNER), getBoolean(OVERWRITE_OWNER), get(DEFAULT_OWNER), - getBoolean(USE_TIME_STAMP), getBoolean(OVERWRITE_TIME_STAMP), FieldFactory.parseField(get(TIME_STAMP_FIELD)), - get(TIME_STAMP_FORMAT)); - } - public FieldWriterPreferences getFieldWriterPreferences() { return new FieldWriterPreferences( getBoolean(RESOLVE_STRINGS_ALL_FIELDS), @@ -1396,10 +1370,6 @@ public BibtexKeyPatternPreferences getBibtexKeyPatternPreferences() { getKeywordDelimiter()); } - public TimestampPreferences getTimestampPreferences() { - return new TimestampPreferences(getBoolean(USE_TIME_STAMP), getBoolean(UPDATE_TIMESTAMP), FieldFactory.parseField(get(TIME_STAMP_FIELD)), get(TIME_STAMP_FORMAT), getBoolean(OVERWRITE_TIME_STAMP)); - } - @Override public LayoutFormatterPreferences getLayoutFormatterPreferences(JournalAbbreviationLoader journalAbbreviationLoader) { Objects.requireNonNull(journalAbbreviationLoader); @@ -1416,26 +1386,26 @@ public XmpPreferences getXMPPreferences() { @Override public OpenOfficePreferences getOpenOfficePreferences() { return new OpenOfficePreferences( - this.get(JabRefPreferences.OO_JARS_PATH), - this.get(JabRefPreferences.OO_EXECUTABLE_PATH), - this.get(JabRefPreferences.OO_PATH), - this.getBoolean(JabRefPreferences.OO_USE_ALL_OPEN_BASES), - this.getBoolean(JabRefPreferences.OO_SYNC_WHEN_CITING), - this.getBoolean(JabRefPreferences.OO_SHOW_PANEL), - this.getStringList(JabRefPreferences.OO_EXTERNAL_STYLE_FILES), - this.get(JabRefPreferences.OO_BIBLIOGRAPHY_STYLE_FILE)); + get(JabRefPreferences.OO_JARS_PATH), + get(JabRefPreferences.OO_EXECUTABLE_PATH), + get(JabRefPreferences.OO_PATH), + getBoolean(JabRefPreferences.OO_USE_ALL_OPEN_BASES), + getBoolean(JabRefPreferences.OO_SYNC_WHEN_CITING), + getBoolean(JabRefPreferences.OO_SHOW_PANEL), + getStringList(JabRefPreferences.OO_EXTERNAL_STYLE_FILES), + get(JabRefPreferences.OO_BIBLIOGRAPHY_STYLE_FILE)); } @Override public void setOpenOfficePreferences(OpenOfficePreferences openOfficePreferences) { - this.put(JabRefPreferences.OO_JARS_PATH, openOfficePreferences.getJarsPath()); - this.put(JabRefPreferences.OO_EXECUTABLE_PATH, openOfficePreferences.getExecutablePath()); - this.put(JabRefPreferences.OO_PATH, openOfficePreferences.getInstallationPath()); - this.putBoolean(JabRefPreferences.OO_USE_ALL_OPEN_BASES, openOfficePreferences.getUseAllDatabases()); - this.putBoolean(JabRefPreferences.OO_SYNC_WHEN_CITING, openOfficePreferences.getSyncWhenCiting()); - this.putBoolean(JabRefPreferences.OO_SHOW_PANEL, openOfficePreferences.getShowPanel()); - this.putStringList(JabRefPreferences.OO_EXTERNAL_STYLE_FILES, openOfficePreferences.getExternalStyles()); - this.put(JabRefPreferences.OO_BIBLIOGRAPHY_STYLE_FILE, openOfficePreferences.getCurrentStyle()); + put(JabRefPreferences.OO_JARS_PATH, openOfficePreferences.getJarsPath()); + put(JabRefPreferences.OO_EXECUTABLE_PATH, openOfficePreferences.getExecutablePath()); + put(JabRefPreferences.OO_PATH, openOfficePreferences.getInstallationPath()); + putBoolean(JabRefPreferences.OO_USE_ALL_OPEN_BASES, openOfficePreferences.getUseAllDatabases()); + putBoolean(JabRefPreferences.OO_SYNC_WHEN_CITING, openOfficePreferences.getSyncWhenCiting()); + putBoolean(JabRefPreferences.OO_SHOW_PANEL, openOfficePreferences.getShowPanel()); + putStringList(JabRefPreferences.OO_EXTERNAL_STYLE_FILES, openOfficePreferences.getExternalStyles()); + put(JabRefPreferences.OO_BIBLIOGRAPHY_STYLE_FILE, openOfficePreferences.getCurrentStyle()); } private NameFormatterPreferences getNameFormatterPreferences() { @@ -1655,22 +1625,6 @@ public String getOrCreateUserId() { } } - public Boolean shouldCollectTelemetry() { - return getBoolean(COLLECT_TELEMETRY); - } - - public void setShouldCollectTelemetry(boolean value) { - putBoolean(COLLECT_TELEMETRY, value); - } - - public Boolean shouldAskToCollectTelemetry() { - return getBoolean(ALREADY_ASKED_TO_COLLECT_TELEMETRY); - } - - public void askedToCollectTelemetry() { - putBoolean(ALREADY_ASKED_TO_COLLECT_TELEMETRY, true); - } - @Override public void storeKeyBindingRepository(KeyBindingRepository keyBindingRepository) { putStringList(JabRefPreferences.BIND_NAMES, keyBindingRepository.getBindNames()); @@ -1864,10 +1818,6 @@ public Optional getFontSize() { } } - public String getFontFamily() { - return get(FONT_FAMILY); - } - public String setLastPreferencesExportPath() { return get(PREFS_EXPORT_PATH); } @@ -1876,23 +1826,6 @@ public void setLastPreferencesExportPath(Path exportFile) { put(PREFS_EXPORT_PATH, exportFile.toString()); } - public Language getLanguage() { - String languageId = get(LANGUAGE); - return Stream.of(Language.values()) - .filter(language -> language.getId().equalsIgnoreCase(languageId)) - .findFirst() - .orElse(Language.ENGLISH); - } - - public void setLanguage(Language language) { - Language oldLanguage = getLanguage(); - put(LANGUAGE, language.getId()); - if (language != oldLanguage) { - // Update any defaults that might be language dependent: - setLanguageDependentDefaultValues(); - } - } - public void setIdBasedFetcherForEntryGenerator(String fetcherName) { put(ID_ENTRY_GENERATOR, fetcherName); } @@ -1980,7 +1913,6 @@ private void saveCustomEntryTypes(BibDatabaseMode bibDatabaseMode) { .map(entryType -> entryType).collect(Collectors.toList()); storeBibEntryTypes(customBiblatexBibTexTypes, bibDatabaseMode); - } public PushToApplication getActivePushToApplication(PushToApplicationsManager manager) { @@ -2005,4 +1937,123 @@ public void setNewLineSeparator(NewLineSeparator newLineSeparator) { // we also have to change Globals variable as globals is not a getter, but a constant OS.NEWLINE = escapeChars; } + + //************************************************************************************************************* + // GeneralPreferences + //************************************************************************************************************* + + @Override + public Language getLanguage() { + String languageId = get(LANGUAGE); + return Stream.of(Language.values()) + .filter(language -> language.getId().equalsIgnoreCase(languageId)) + .findFirst() + .orElse(Language.ENGLISH); + } + + @Override + public void setLanguage(Language language) { + Language oldLanguage = getLanguage(); + put(LANGUAGE, language.getId()); + if (language != oldLanguage) { + // Update any defaults that might be language dependent: + setLanguageDependentDefaultValues(); + } + } + + @Override + public Charset getDefaultEncoding() { + return Charset.forName(get(DEFAULT_ENCODING)); + } + + @Override + public boolean getShouldCollectTelemetry() { + return getBoolean(COLLECT_TELEMETRY); + } + + @Override + public void setShouldCollectTelemetry(boolean value) { + putBoolean(COLLECT_TELEMETRY, value); + } + + @Override + public boolean shouldAskToCollectTelemetry() { + return getBoolean(ALREADY_ASKED_TO_COLLECT_TELEMETRY); + } + + @Override + public void askedToCollectTelemetry() { + putBoolean(ALREADY_ASKED_TO_COLLECT_TELEMETRY, true); + } + + @Override + public boolean getEnforceLegalKeys() { + return getBoolean(ENFORCE_LEGAL_BIBTEX_KEY); + } + + @Override + public boolean getAllowIntegerEdition() { + return getBoolean(ALLOW_INTEGER_EDITION_BIBTEX); + } + + @Override + public GeneralPreferences getGeneralPreferences() { + return new GeneralPreferences( + getDefaultEncoding(), + getBoolean(BIBLATEX_DEFAULT_MODE) ? BibDatabaseMode.BIBLATEX : BibDatabaseMode.BIBTEX, + getBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION), + getBoolean(CONFIRM_DELETE), + getBoolean(ENFORCE_LEGAL_BIBTEX_KEY), + getBoolean(ALLOW_INTEGER_EDITION_BIBTEX), + getBoolean(MEMORY_STICK_MODE), + getShouldCollectTelemetry(), + getBoolean(SHOW_ADVANCED_HINTS)); + } + + @Override + public void storeGeneralPreferences(GeneralPreferences preferences) { + put(DEFAULT_ENCODING, preferences.getDefaultEncoding().name()); + putBoolean(BIBLATEX_DEFAULT_MODE, (preferences.getDefaultBibDatabaseMode() == BibDatabaseMode.BIBLATEX)); + putBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION, preferences.isWarnAboutDuplicatesInInspection()); + putBoolean(CONFIRM_DELETE, preferences.isConfirmDelete()); + putBoolean(ENFORCE_LEGAL_BIBTEX_KEY, preferences.isEnforceLegalBibtexKey()); + putBoolean(ALLOW_INTEGER_EDITION_BIBTEX, preferences.isAllowIntegerEditionBibtex()); + putBoolean(MEMORY_STICK_MODE, preferences.isMemoryStickMode()); + setShouldCollectTelemetry(preferences.isCollectTelemetry()); + putBoolean(SHOW_ADVANCED_HINTS, preferences.isShowAdvancedHints()); + } + + @Override + public OwnerPreferences getOwnerPreferences() { + return new OwnerPreferences( + getBoolean(USE_OWNER), + get(DEFAULT_OWNER), + getBoolean(OVERWRITE_OWNER)); + } + + @Override + public void storeOwnerPreferences(OwnerPreferences preferences) { + putBoolean(USE_OWNER, preferences.isUseOwner()); + put(DEFAULT_OWNER, preferences.getDefaultOwner()); + putBoolean(OVERWRITE_OWNER, preferences.isOverwriteOwner()); + } + + @Override + public TimestampPreferences getTimestampPreferences() { + return new TimestampPreferences( + getBoolean(USE_TIME_STAMP), + getBoolean(UPDATE_TIMESTAMP), + FieldFactory.parseField(get(TIME_STAMP_FIELD)), + get(TIME_STAMP_FORMAT), + getBoolean(OVERWRITE_TIME_STAMP)); + } + + @Override + public void storeTimestampPreferences(TimestampPreferences preferences) { + putBoolean(USE_TIME_STAMP, preferences.isUseTimestamps()); + putBoolean(UPDATE_TIMESTAMP, preferences.isUpdateTimestamp()); + put(TIME_STAMP_FIELD, preferences.getTimestampField().getName()); + put(TIME_STAMP_FORMAT, preferences.getTimestampFormat()); + putBoolean(OVERWRITE_TIME_STAMP, preferences.isOverwriteTimestamp()); + } } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 39dbb37ea1d..920ba0a778d 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -15,10 +15,12 @@ import org.jabref.logic.importer.ImportFormatPreferences; import org.jabref.logic.journals.JournalAbbreviationLoader; import org.jabref.logic.journals.JournalAbbreviationPreferences; +import org.jabref.logic.l10n.Language; import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.openoffice.OpenOfficePreferences; +import org.jabref.logic.preferences.OwnerPreferences; +import org.jabref.logic.preferences.TimestampPreferences; import org.jabref.logic.protectedterms.ProtectedTermsLoader; -import org.jabref.logic.util.UpdateFieldPreferences; import org.jabref.logic.util.io.AutoLinkPreferences; import org.jabref.logic.xmp.XmpPreferences; import org.jabref.model.database.BibDatabaseMode; @@ -59,8 +61,6 @@ public interface PreferencesService { Map> getEntryEditorTabList(); - boolean getEnforceLegalKeys(); - Map getCustomTabsNamesAndFields(); void setCustomTabsNameAndFields(String name, String fields, int defNumber); @@ -75,8 +75,6 @@ public interface PreferencesService { LayoutFormatterPreferences getLayoutFormatterPreferences(JournalAbbreviationLoader loader); - UpdateFieldPreferences getUpdateFieldPreferences(); - ImportFormatPreferences getImportFormatPreferences(); boolean isKeywordSyncEnabled(); @@ -89,8 +87,6 @@ public interface PreferencesService { Charset getDefaultEncoding(); - void setDefaultEncoding(Charset encoding); - String getUser(); String getTheme(); @@ -105,8 +101,6 @@ public interface PreferencesService { void saveCustomEntryTypes(); - boolean getAllowIntegerEdition(); - EntryEditorPreferences getEntryEditorPreferences(); List loadBibEntryTypes(BibDatabaseMode mode); @@ -116,4 +110,35 @@ public interface PreferencesService { CleanupPreset getCleanupPreset(); void setCleanupPreset(CleanupPreset cleanupPreset); + + // General + + Language getLanguage(); + + void setLanguage(Language language); + + boolean getShouldCollectTelemetry(); + + void setShouldCollectTelemetry(boolean value); + + boolean shouldAskToCollectTelemetry(); + + void askedToCollectTelemetry(); + + boolean getEnforceLegalKeys(); + + boolean getAllowIntegerEdition(); + + GeneralPreferences getGeneralPreferences(); + + void storeGeneralPreferences(GeneralPreferences preferences); + + OwnerPreferences getOwnerPreferences(); + + void storeOwnerPreferences(OwnerPreferences preferences); + + TimestampPreferences getTimestampPreferences(); + + void storeTimestampPreferences(TimestampPreferences preferences); + } diff --git a/src/test/java/org/jabref/JabRefPreferencesTest.java b/src/test/java/org/jabref/JabRefPreferencesTest.java index 187bab026a6..eb2557a5030 100644 --- a/src/test/java/org/jabref/JabRefPreferencesTest.java +++ b/src/test/java/org/jabref/JabRefPreferencesTest.java @@ -7,7 +7,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -21,9 +20,11 @@ void setUp() { when(prefs.getDefaultEncoding()).thenReturn(StandardCharsets.UTF_8); } + // FIXME: This test is not testing anything. prefs.getDefaultEncoding() will always return StandardCharsets.UTF_8 + // because of setUp(): when(prefs.getDefaultEnconding()).thenReturn(StandardCharsets.UTF_8); @Test void getDefaultEncodingReturnsPreviouslyStoredEncoding() { - prefs.setDefaultEncoding(StandardCharsets.UTF_8); - assertEquals(StandardCharsets.UTF_8, prefs.getDefaultEncoding()); + // prefs.setDefaultEncoding(StandardCharsets.UTF_8); + // assertEquals(StandardCharsets.UTF_8, prefs.getDefaultEncoding()); } } From 4bc28a0ede246cd34117369a7ce167457a74e108 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage Date: Tue, 24 Mar 2020 11:01:03 +0100 Subject: [PATCH 2/2] Rewording to reduce unnecessary changes --- src/main/java/org/jabref/Globals.java | 4 ++-- src/main/java/org/jabref/gui/JabRefFrame.java | 2 +- .../java/org/jabref/gui/preferences/GeneralTabViewModel.java | 2 +- src/main/java/org/jabref/preferences/JabRefPreferences.java | 4 ++-- src/main/java/org/jabref/preferences/PreferencesService.java | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/jabref/Globals.java b/src/main/java/org/jabref/Globals.java index 46bc330b69e..5f8177ffbca 100644 --- a/src/main/java/org/jabref/Globals.java +++ b/src/main/java/org/jabref/Globals.java @@ -99,7 +99,7 @@ public static void startBackgroundTasks() throws JabRefException { themeLoader = new ThemeLoader(fileUpdateMonitor, prefs); - if (Globals.prefs.getShouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) { + if (Globals.prefs.shouldCollectTelemetry() && !GraphicsEnvironment.isHeadless()) { startTelemetryClient(); } } @@ -114,7 +114,7 @@ private static void stopTelemetryClient() { private static void startTelemetryClient() { TelemetryConfiguration telemetryConfiguration = TelemetryConfiguration.getActive(); telemetryConfiguration.setInstrumentationKey(Globals.BUILD_INFO.azureInstrumentationKey); - telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.getShouldCollectTelemetry()); + telemetryConfiguration.setTrackingIsDisabled(!Globals.prefs.shouldCollectTelemetry()); telemetryClient = new TelemetryClient(telemetryConfiguration); telemetryClient.getContext().getProperties().put("JabRef version", Globals.BUILD_INFO.version.toString()); telemetryClient.getContext().getProperties().put("Java version", StandardSystemProperty.JAVA_VERSION.value()); diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index d9b78ab8358..09892d6d971 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -264,7 +264,7 @@ public void run() { } private Void showTrackingNotification() { - if (!Globals.prefs.getShouldCollectTelemetry()) { + if (!Globals.prefs.shouldCollectTelemetry()) { boolean shouldCollect = dialogService.showConfirmationDialogAndWait( Localization.lang("Telemetry: Help make JabRef better"), Localization.lang("To improve the user experience, we would like to collect anonymous statistics on the features you use. We will only record what features you access and how often you do it. We will neither collect any personal data nor the content of bibliographic items. If you choose to allow data collection, you can later disable it via Options -> Preferences -> General."), diff --git a/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java b/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java index 3a255fc0426..270325f036a 100644 --- a/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java @@ -108,7 +108,7 @@ public void setValues() { enforceLegalKeysProperty.setValue(initialGeneralPreferences.isEnforceLegalBibtexKey()); allowIntegerEditionProperty.setValue(initialGeneralPreferences.isAllowIntegerEditionBibtex()); memoryStickModeProperty.setValue(initialGeneralPreferences.isMemoryStickMode()); - collectTelemetryProperty.setValue(preferencesService.getShouldCollectTelemetry()); + collectTelemetryProperty.setValue(preferencesService.shouldCollectTelemetry()); showAdvancedHintsProperty.setValue(initialGeneralPreferences.isShowAdvancedHints()); markOwnerProperty.setValue(initialOwnerPreferences.isUseOwner()); diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index d753199f131..096396915f3 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1967,7 +1967,7 @@ public Charset getDefaultEncoding() { } @Override - public boolean getShouldCollectTelemetry() { + public boolean shouldCollectTelemetry() { return getBoolean(COLLECT_TELEMETRY); } @@ -2006,7 +2006,7 @@ public GeneralPreferences getGeneralPreferences() { getBoolean(ENFORCE_LEGAL_BIBTEX_KEY), getBoolean(ALLOW_INTEGER_EDITION_BIBTEX), getBoolean(MEMORY_STICK_MODE), - getShouldCollectTelemetry(), + shouldCollectTelemetry(), getBoolean(SHOW_ADVANCED_HINTS)); } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 920ba0a778d..0bfba40b231 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -117,7 +117,7 @@ public interface PreferencesService { void setLanguage(Language language); - boolean getShouldCollectTelemetry(); + boolean shouldCollectTelemetry(); void setShouldCollectTelemetry(boolean value);