diff --git a/src/main/java/org/jabref/gui/journals/AbbreviateAction.java b/src/main/java/org/jabref/gui/journals/AbbreviateAction.java index ec609f1e18d..bf531de51dd 100644 --- a/src/main/java/org/jabref/gui/journals/AbbreviateAction.java +++ b/src/main/java/org/jabref/gui/journals/AbbreviateAction.java @@ -87,7 +87,7 @@ private String abbreviate(BibDatabaseContext databaseContext, List ent UndoableAbbreviator undoableAbbreviator = new UndoableAbbreviator( Globals.journalAbbreviationRepository, abbreviationType, - journalAbbreviationPreferences.useAMSFJournalFieldForAbbrevAndUnabbrev()); + journalAbbreviationPreferences.shouldUseFJournalField()); NamedCompound ce = new NamedCompound(Localization.lang("Abbreviate journal names")); diff --git a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java index a915c2b69a8..279961a7871 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTab.java @@ -73,7 +73,11 @@ public JournalAbbreviationsTab() { @FXML private void initialize() { - viewModel = new JournalAbbreviationsTabViewModel(preferencesService, dialogService, taskExecutor, abbreviationRepository); + viewModel = new JournalAbbreviationsTabViewModel( + preferencesService.getJournalAbbreviationPreferences(), + dialogService, + taskExecutor, + abbreviationRepository); filteredAbbreviations = new FilteredList<>(viewModel.abbreviationsProperty()); @@ -127,9 +131,8 @@ private void setBindings() { loadingLabel.visibleProperty().bind(viewModel.isLoadingProperty()); progressIndicator.visibleProperty().bind(viewModel.isLoadingProperty()); - searchBox.textProperty().addListener((observable, previousText, searchTerm) -> { - filteredAbbreviations.setPredicate(abbreviation -> searchTerm.isEmpty() || abbreviation.containsCaseIndependent(searchTerm)); - }); + searchBox.textProperty().addListener((observable, previousText, searchTerm) -> + filteredAbbreviations.setPredicate(abbreviation -> searchTerm.isEmpty() || abbreviation.containsCaseIndependent(searchTerm))); useFJournal.selectedProperty().bindBidirectional(viewModel.useFJournalProperty()); } @@ -145,9 +148,7 @@ private void setAnimations() { new KeyFrame(Duration.seconds(0), new KeyValue(flashingColor, Color.TRANSPARENT, Interpolator.LINEAR)), new KeyFrame(Duration.seconds(0.25), new KeyValue(flashingColor, Color.RED, Interpolator.LINEAR)), new KeyFrame(Duration.seconds(0.25), new KeyValue(searchBox.textProperty(), "", Interpolator.DISCRETE)), - new KeyFrame(Duration.seconds(0.25), (ActionEvent event) -> { - addAbbreviationActions(); - }), + new KeyFrame(Duration.seconds(0.25), (ActionEvent event) -> addAbbreviationActions()), new KeyFrame(Duration.seconds(0.5), new KeyValue(flashingColor, Color.TRANSPARENT, Interpolator.LINEAR)) ); } diff --git a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java index 3dc07113edf..78da755da3f 100644 --- a/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/journals/JournalAbbreviationsTabViewModel.java @@ -25,7 +25,6 @@ import org.jabref.logic.journals.JournalAbbreviationRepository; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.StandardFileType; -import org.jabref.preferences.PreferencesService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,7 +50,6 @@ public class JournalAbbreviationsTabViewModel implements PreferenceTabViewModel private final SimpleBooleanProperty isAbbreviationEditableAndRemovable = new SimpleBooleanProperty(false); private final SimpleBooleanProperty useFJournal = new SimpleBooleanProperty(true); - private final PreferencesService preferences; private final DialogService dialogService; private final TaskExecutor taskExecutor; @@ -59,15 +57,14 @@ public class JournalAbbreviationsTabViewModel implements PreferenceTabViewModel private final JournalAbbreviationRepository journalAbbreviationRepository; private boolean shouldWriteLists; - public JournalAbbreviationsTabViewModel(PreferencesService preferences, + public JournalAbbreviationsTabViewModel(JournalAbbreviationPreferences abbreviationsPreferences, DialogService dialogService, TaskExecutor taskExecutor, JournalAbbreviationRepository journalAbbreviationRepository) { - this.preferences = Objects.requireNonNull(preferences); this.dialogService = Objects.requireNonNull(dialogService); this.taskExecutor = Objects.requireNonNull(taskExecutor); this.journalAbbreviationRepository = Objects.requireNonNull(journalAbbreviationRepository); - this.abbreviationsPreferences = preferences.getJournalAbbreviationPreferences(); + this.abbreviationsPreferences = abbreviationsPreferences; abbreviationsCount.bind(abbreviations.sizeProperty()); currentAbbreviation.addListener((observable, oldValue, newValue) -> { @@ -336,11 +333,8 @@ public void storeSettings() { .map(path -> path.getAbsolutePath().get().toAbsolutePath().toString()) .collect(Collectors.toList()); - preferences.storeJournalAbbreviationPreferences(new JournalAbbreviationPreferences( - journalStringList, - abbreviationsPreferences.getDefaultEncoding(), - useFJournal.getValue() - )); + abbreviationsPreferences.setExternalJournalLists(journalStringList); + abbreviationsPreferences.setUseFJournalField(useFJournal.get()); if (shouldWriteLists) { saveJournalAbbreviationFiles(); @@ -348,7 +342,7 @@ public void storeSettings() { } }) .onSuccess((success) -> Globals.journalAbbreviationRepository = - JournalAbbreviationLoader.loadRepository(preferences.getJournalAbbreviationPreferences())) + JournalAbbreviationLoader.loadRepository(abbreviationsPreferences)) .onFailure(exception -> LOGGER.error("Failed to store journal preferences.", exception)) .executeWith(taskExecutor); } diff --git a/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java b/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java index 2c67a734a8b..1c0717a943c 100644 --- a/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java +++ b/src/main/java/org/jabref/logic/journals/JournalAbbreviationLoader.java @@ -1,7 +1,6 @@ package org.jabref.logic.journals; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; @@ -62,6 +61,6 @@ public static JournalAbbreviationRepository loadRepository(JournalAbbreviationPr } public static JournalAbbreviationRepository loadBuiltInRepository() { - return loadRepository(new JournalAbbreviationPreferences(Collections.emptyList(), StandardCharsets.UTF_8, true)); + return loadRepository(new JournalAbbreviationPreferences(Collections.emptyList(), true)); } } diff --git a/src/main/java/org/jabref/logic/journals/JournalAbbreviationPreferences.java b/src/main/java/org/jabref/logic/journals/JournalAbbreviationPreferences.java index 442fda33ddc..fb256bbe8ab 100644 --- a/src/main/java/org/jabref/logic/journals/JournalAbbreviationPreferences.java +++ b/src/main/java/org/jabref/logic/journals/JournalAbbreviationPreferences.java @@ -1,37 +1,41 @@ package org.jabref.logic.journals; -import java.nio.charset.Charset; import java.util.List; +import javafx.beans.property.BooleanProperty; +import javafx.beans.property.SimpleBooleanProperty; +import javafx.collections.FXCollections; +import javafx.collections.ObservableList; + public class JournalAbbreviationPreferences { - private final Charset defaultEncoding; - private List externalJournalLists; - private boolean useFJournalField; + private final ObservableList externalJournalLists; + private final BooleanProperty useFJournalField; - public JournalAbbreviationPreferences(List externalJournalLists, Charset defaultEncoding, boolean useFJournalField) { - this.externalJournalLists = externalJournalLists; - this.defaultEncoding = defaultEncoding; - this.useFJournalField = useFJournalField; + public JournalAbbreviationPreferences(List externalJournalLists, + boolean useFJournalField) { + this.externalJournalLists = FXCollections.observableArrayList(externalJournalLists); + this.useFJournalField = new SimpleBooleanProperty(useFJournalField); } - public List getExternalJournalLists() { + public ObservableList getExternalJournalLists() { return externalJournalLists; } - public void setExternalJournalLists(List externalJournalLists) { - this.externalJournalLists = externalJournalLists; + public void setExternalJournalLists(List list) { + externalJournalLists.clear(); + externalJournalLists.addAll(list); } - public Charset getDefaultEncoding() { - return defaultEncoding; + public boolean shouldUseFJournalField() { + return useFJournalField.get(); } - public boolean useAMSFJournalFieldForAbbrevAndUnabbrev() { + public BooleanProperty useFJournalFieldProperty() { return useFJournalField; } - public void setUseAMSFJournalFieldForAbbrevAndUnabbrev(boolean useFJournalField) { - this.useFJournalField = useFJournalField; + public void setUseFJournalField(boolean useFJournalField) { + this.useFJournalField.set(useFJournalField); } } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 0db7cef661f..428cc669ebf 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -451,6 +451,7 @@ public class JabRefPreferences implements PreferencesService { private MainTablePreferences mainTablePreferences; private ColumnPreferences mainTableColumnPreferences; private ColumnPreferences searchDialogColumnPreferences; + private JournalAbbreviationPreferences journalAbbreviationPreferences; // The constructor is made private to enforce this as a singleton class: private JabRefPreferences() { @@ -1113,12 +1114,20 @@ public LayoutFormatterPreferences getLayoutFormatterPreferences(JournalAbbreviat @Override public JournalAbbreviationPreferences getJournalAbbreviationPreferences() { - return new JournalAbbreviationPreferences(getStringList(EXTERNAL_JOURNAL_LISTS), StandardCharsets.UTF_8, getBoolean(USE_AMS_FJOURNAL)); - } + if (Objects.nonNull(journalAbbreviationPreferences)) { + return journalAbbreviationPreferences; + } - @Override - public void storeJournalAbbreviationPreferences(JournalAbbreviationPreferences abbreviationsPreferences) { - putStringList(EXTERNAL_JOURNAL_LISTS, abbreviationsPreferences.getExternalJournalLists()); + journalAbbreviationPreferences = new JournalAbbreviationPreferences( + getStringList(EXTERNAL_JOURNAL_LISTS), + getBoolean(USE_AMS_FJOURNAL)); + + journalAbbreviationPreferences.getExternalJournalLists().addListener((InvalidationListener) change -> + putStringList(EXTERNAL_JOURNAL_LISTS, journalAbbreviationPreferences.getExternalJournalLists())); + EasyBind.listen(journalAbbreviationPreferences.useFJournalFieldProperty(), + (obs, oldValue, newValue) -> putBoolean(USE_AMS_FJOURNAL, newValue)); + + return journalAbbreviationPreferences; } @Override @@ -2520,7 +2529,7 @@ public GuiPreferences getGuiPreferences() { EasyBind.listen(guiPreferences.lastSelectedIdBasedFetcherProperty(), (obs, oldValue, newValue) -> put(ID_ENTRY_GENERATOR, newValue)); EasyBind.listen(guiPreferences.mergeDiffModeProperty(), (obs, oldValue, newValue) -> put(MERGE_ENTRIES_DIFF_MODE, newValue.name())); EasyBind.listen(guiPreferences.sidePaneWidthProperty(), (obs, oldValue, newValue) -> putDouble(SIDE_PANE_WIDTH, newValue.doubleValue())); - EasyBind.listen(guiPreferences.mergeShowChangedFieldOnlyProperty(), (obs, oldValue, newValue) -> putBoolean(MERGE_SHOW_ONLY_CHANGED_FIELDS, newValue.booleanValue())); + EasyBind.listen(guiPreferences.mergeShowChangedFieldOnlyProperty(), (obs, oldValue, newValue) -> putBoolean(MERGE_SHOW_ONLY_CHANGED_FIELDS, newValue)); return guiPreferences; } diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 25132984662..5018e68d966 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -55,8 +55,6 @@ public interface PreferencesService { KeyBindingRepository getKeyBindingRepository(); - void storeJournalAbbreviationPreferences(JournalAbbreviationPreferences abbreviationsPreferences); - FilePreferences getFilePreferences(); FieldWriterPreferences getFieldWriterPreferences(); diff --git a/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java b/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java index c55822a5bdd..62cf94d2b7a 100644 --- a/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java +++ b/src/test/java/org/jabref/gui/preferences/journals/JournalAbbreviationsViewModelTabTest.java @@ -20,7 +20,6 @@ import org.jabref.logic.journals.JournalAbbreviationLoader; import org.jabref.logic.journals.JournalAbbreviationPreferences; import org.jabref.logic.journals.JournalAbbreviationRepository; -import org.jabref.preferences.PreferencesService; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -62,7 +61,6 @@ class JournalAbbreviationsViewModelTabTest { private JournalAbbreviationsTabViewModel viewModel; private Path emptyTestFile; private Path tempFolder; - private PreferencesService preferencesService; private final JournalAbbreviationRepository repository = JournalAbbreviationLoader.loadBuiltInRepository(); private DialogService dialogService; @@ -179,14 +177,12 @@ public static Stream provideTestFiles() { @BeforeEach void setUpViewModel(@TempDir Path tempFolder) throws Exception { JournalAbbreviationPreferences abbreviationPreferences = mock(JournalAbbreviationPreferences.class); - preferencesService = mock(PreferencesService.class); - when(preferencesService.getJournalAbbreviationPreferences()).thenReturn(abbreviationPreferences); dialogService = mock(DialogService.class); this.tempFolder = tempFolder; TaskExecutor taskExecutor = new CurrentThreadTaskExecutor(); - viewModel = new JournalAbbreviationsTabViewModel(preferencesService, dialogService, taskExecutor, repository); + viewModel = new JournalAbbreviationsTabViewModel(abbreviationPreferences, dialogService, taskExecutor, repository); emptyTestFile = createTestFile(new CsvFileNameAndContent("emptyTestFile.csv", "")); } @@ -515,13 +511,6 @@ void testSaveAbbreviationsToFilesCreatesNewFilesWithWrittenAbbreviations(TestDat assertEquals(testData.finalContentsOfFile3, actual); } - @ParameterizedTest - @MethodSource("provideTestFiles") - void testSaveExternalFilesListToPreferences(TestData testData) throws IOException { - addFourTestFileToViewModelAndPreferences(testData); - verify(preferencesService).storeJournalAbbreviationPreferences(any()); - } - /** * Select the last abbreviation in the list of abbreviations */