From 956c1750c98afbdd44e187c0e23bd74bbb6a9cc9 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 10 Sep 2022 15:24:55 +0200 Subject: [PATCH 1/8] Included localization prefs in GeneralPreferences --- .../gui/entryeditor/RelatedArticlesTab.java | 2 +- .../general/GeneralTabViewModel.java | 6 +++--- .../jabref/preferences/GeneralPreferences.java | 18 +++++++++++++++++- .../jabref/preferences/JabRefPreferences.java | 7 +++---- .../jabref/preferences/PreferencesService.java | 5 ----- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/jabref/gui/entryeditor/RelatedArticlesTab.java b/src/main/java/org/jabref/gui/entryeditor/RelatedArticlesTab.java index c5384437c51..54f21252406 100644 --- a/src/main/java/org/jabref/gui/entryeditor/RelatedArticlesTab.java +++ b/src/main/java/org/jabref/gui/entryeditor/RelatedArticlesTab.java @@ -65,7 +65,7 @@ private StackPane getRelatedArticlesPane(BibEntry entry) { ProgressIndicator progress = new ProgressIndicator(); progress.setMaxSize(100, 100); - MrDLibFetcher fetcher = new MrDLibFetcher(preferencesService.getLanguage().name(), + MrDLibFetcher fetcher = new MrDLibFetcher(preferencesService.getGeneralPreferences().getLanguage().name(), Globals.BUILD_INFO.version, preferencesService.getMrDlibPreferences()); BackgroundTask .wrap(() -> fetcher.performSearch(entry)) diff --git a/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java b/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java index 68ea3b24c7a..6e2143f31c4 100644 --- a/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java +++ b/src/main/java/org/jabref/gui/preferences/general/GeneralTabViewModel.java @@ -68,7 +68,7 @@ public GeneralTabViewModel(DialogService dialogService, PreferencesService prefe public void setValues() { languagesListProperty.setValue(new SortedList<>(FXCollections.observableArrayList(Language.values()), Comparator.comparing(Language::getDisplayName))); - selectedLanguageProperty.setValue(preferencesService.getLanguage()); + selectedLanguageProperty.setValue(preferencesService.getGeneralPreferences().getLanguage()); encodingsListProperty.setValue(FXCollections.observableArrayList(Encodings.getCharsets())); @@ -91,8 +91,8 @@ public void setValues() { public void storeSettings() { Language newLanguage = selectedLanguageProperty.getValue(); - if (newLanguage != preferencesService.getLanguage()) { - preferencesService.setLanguage(newLanguage); + if (newLanguage != preferencesService.getGeneralPreferences().getLanguage()) { + preferencesService.getGeneralPreferences().setLanguage(newLanguage); Localization.setLanguage(newLanguage); restartWarning.add(Localization.lang("Changed language") + ": " + newLanguage.getDisplayName()); } diff --git a/src/main/java/org/jabref/preferences/GeneralPreferences.java b/src/main/java/org/jabref/preferences/GeneralPreferences.java index 1383e6c11b4..62d4971a6d6 100644 --- a/src/main/java/org/jabref/preferences/GeneralPreferences.java +++ b/src/main/java/org/jabref/preferences/GeneralPreferences.java @@ -5,9 +5,11 @@ import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.property.SimpleObjectProperty; +import org.jabref.logic.l10n.Language; import org.jabref.model.database.BibDatabaseMode; public class GeneralPreferences { + private final ObjectProperty language; private final ObjectProperty defaultBibDatabaseMode; private final BooleanProperty warnAboutDuplicatesInInspection; private final BooleanProperty confirmDelete; @@ -15,11 +17,13 @@ public class GeneralPreferences { private final BooleanProperty memoryStickMode; private final BooleanProperty showAdvancedHints; - public GeneralPreferences(BibDatabaseMode defaultBibDatabaseMode, + public GeneralPreferences(Language language, + BibDatabaseMode defaultBibDatabaseMode, boolean warnAboutDuplicatesInInspection, boolean confirmDelete, boolean memoryStickMode, boolean showAdvancedHints) { + this.language = new SimpleObjectProperty<>(language); this.defaultBibDatabaseMode = new SimpleObjectProperty<>(defaultBibDatabaseMode); this.warnAboutDuplicatesInInspection = new SimpleBooleanProperty(warnAboutDuplicatesInInspection); this.confirmDelete = new SimpleBooleanProperty(confirmDelete); @@ -28,6 +32,18 @@ public GeneralPreferences(BibDatabaseMode defaultBibDatabaseMode, this.showAdvancedHints = new SimpleBooleanProperty(showAdvancedHints); } + public Language getLanguage() { + return language.get(); + } + + public ObjectProperty languageProperty() { + return language; + } + + public void setLanguage(Language language) { + this.language.set(language); + } + public BibDatabaseMode getDefaultBibDatabaseMode() { return defaultBibDatabaseMode.get(); } diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 4f50fe85351..87fa26f7f5f 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1254,8 +1254,7 @@ private void storeBibEntryTypes(Collection bibEntryTypes, BibDatab // GeneralPreferences //************************************************************************************************************* - @Override - public Language getLanguage() { + private Language getLanguage() { if (language == null) { updateLanguage(); } @@ -1270,8 +1269,7 @@ private void updateLanguage() { .orElse(Language.ENGLISH); } - @Override - public void setLanguage(Language language) { + private void setLanguage(Language language) { Language oldLanguage = getLanguage(); put(LANGUAGE, language.getId()); if (language != oldLanguage) { @@ -1288,6 +1286,7 @@ public GeneralPreferences getGeneralPreferences() { } generalPreferences = new GeneralPreferences( + getLanguage(), getBoolean(BIBLATEX_DEFAULT_MODE) ? BibDatabaseMode.BIBLATEX : BibDatabaseMode.BIBTEX, getBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION), getBoolean(CONFIRM_DELETE), diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index 7da5fdda1a9..b9e6363018b 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -26,7 +26,6 @@ import org.jabref.logic.importer.fetcher.GrobidPreferences; import org.jabref.logic.journals.JournalAbbreviationPreferences; import org.jabref.logic.journals.JournalAbbreviationRepository; -import org.jabref.logic.l10n.Language; import org.jabref.logic.layout.LayoutFormatterPreferences; import org.jabref.logic.layout.format.FileLinkPreferences; import org.jabref.logic.layout.format.NameFormatterPreferences; @@ -111,10 +110,6 @@ public interface PreferencesService { // GeneralPreferences //************************************************************************************************************* - Language getLanguage(); - - void setLanguage(Language language); - GeneralPreferences getGeneralPreferences(); TelemetryPreferences getTelemetryPreferences(); From 9722ee3ca35617bdf75c21a4a72b2b1bbbc4064c Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 10 Sep 2022 15:35:26 +0200 Subject: [PATCH 2/8] Convertet getLanguage to new prefs pattern --- .../jabref/preferences/JabRefPreferences.java | 43 +++++++------------ 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 87fa26f7f5f..021daed43e0 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -414,7 +414,6 @@ public class JabRefPreferences implements PreferencesService { /** * Cache variables */ - private Language language; private GeneralPreferences generalPreferences; private TelemetryPreferences telemetryPreferences; private DOIPreferences doiPreferences; @@ -1254,31 +1253,6 @@ private void storeBibEntryTypes(Collection bibEntryTypes, BibDatab // GeneralPreferences //************************************************************************************************************* - private Language getLanguage() { - if (language == null) { - updateLanguage(); - } - return language; - } - - private void updateLanguage() { - String languageId = get(LANGUAGE); - language = Stream.of(Language.values()) - .filter(language -> language.getId().equalsIgnoreCase(languageId)) - .findFirst() - .orElse(Language.ENGLISH); - } - - private void setLanguage(Language language) { - Language oldLanguage = getLanguage(); - put(LANGUAGE, language.getId()); - if (language != oldLanguage) { - // Update any defaults that might be language dependent: - setLanguageDependentDefaultValues(); - } - updateLanguage(); - } - @Override public GeneralPreferences getGeneralPreferences() { if (Objects.nonNull(generalPreferences)) { @@ -1293,6 +1267,12 @@ public GeneralPreferences getGeneralPreferences() { getBoolean(MEMORY_STICK_MODE), getBoolean(SHOW_ADVANCED_HINTS)); + EasyBind.listen(generalPreferences.languageProperty(), (obs, oldValue, newValue) -> { + put(LANGUAGE, newValue.getId()); + if (oldValue != newValue) { + setLanguageDependentDefaultValues(); + } + }); EasyBind.listen(generalPreferences.defaultBibDatabaseModeProperty(), (obs, oldValue, newValue) -> putBoolean(BIBLATEX_DEFAULT_MODE, (newValue == BibDatabaseMode.BIBLATEX))); EasyBind.listen(generalPreferences.isWarnAboutDuplicatesInInspectionProperty(), (obs, oldValue, newValue) -> putBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION, newValue)); EasyBind.listen(generalPreferences.confirmDeleteProperty(), (obs, oldValue, newValue) -> putBoolean(CONFIRM_DELETE, newValue)); @@ -1302,6 +1282,13 @@ public GeneralPreferences getGeneralPreferences() { return generalPreferences; } + private Language getLanguage() { + return Stream.of(Language.values()) + .filter(language -> language.getId().equalsIgnoreCase(get(LANGUAGE))) + .findFirst() + .orElse(Language.ENGLISH); + } + @Override public TelemetryPreferences getTelemetryPreferences() { if (Objects.nonNull(telemetryPreferences)) { @@ -1311,7 +1298,7 @@ public TelemetryPreferences getTelemetryPreferences() { telemetryPreferences = new TelemetryPreferences( getBoolean(COLLECT_TELEMETRY), !getBoolean(ALREADY_ASKED_TO_COLLECT_TELEMETRY), // mind the ! - getOrCreateUserId() + getTelemetryUserId() ); EasyBind.listen(telemetryPreferences.collectTelemetryProperty(), (obs, oldValue, newValue) -> putBoolean(COLLECT_TELEMETRY, newValue)); @@ -1320,7 +1307,7 @@ public TelemetryPreferences getTelemetryPreferences() { return telemetryPreferences; } - private String getOrCreateUserId() { + private String getTelemetryUserId() { Optional userId = getAsOptional(USER_ID); if (userId.isPresent()) { return userId.get(); From 70a76c0c9ef6b3ee03a52c9fdf42393c68eafbdc Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Sat, 10 Sep 2022 15:21:55 +0200 Subject: [PATCH 3/8] Extracted localization from MetaDataDiff in logic --- .../MetadataChangeDetailsView.java | 34 ++++++++++++++- .../logic/bibtex/comparator/MetaDataDiff.java | 43 ++++++++++++------- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/jabref/gui/collab/metedatachange/MetadataChangeDetailsView.java b/src/main/java/org/jabref/gui/collab/metedatachange/MetadataChangeDetailsView.java index 43023cab438..078ef89fabd 100644 --- a/src/main/java/org/jabref/gui/collab/metedatachange/MetadataChangeDetailsView.java +++ b/src/main/java/org/jabref/gui/collab/metedatachange/MetadataChangeDetailsView.java @@ -4,6 +4,7 @@ import javafx.scene.layout.VBox; import org.jabref.gui.collab.ExternalChangeDetailsView; +import org.jabref.logic.bibtex.comparator.MetaDataDiff; import org.jabref.logic.l10n.Localization; import org.jabref.preferences.PreferencesService; @@ -16,8 +17,8 @@ public MetadataChangeDetailsView(MetadataChange metadataChange, PreferencesServi header.getStyleClass().add("sectionHeader"); container.getChildren().add(header); - for (String change : metadataChange.getMetaDataDiff().getDifferences(preferencesService)) { - container.getChildren().add(new Label(change)); + for (MetaDataDiff.Difference change : metadataChange.getMetaDataDiff().getDifferences(preferencesService)) { + container.getChildren().add(new Label(getDifferenceString(change))); } setLeftAnchor(container, 8d); @@ -27,4 +28,33 @@ public MetadataChangeDetailsView(MetadataChange metadataChange, PreferencesServi getChildren().setAll(container); } + + private String getDifferenceString(MetaDataDiff.Difference change) { + return switch (change) { + case PROTECTED -> + Localization.lang("Library protection"); + case GROUPS_ALTERED -> + Localization.lang("Modified groups tree"); + case ENCODING -> + Localization.lang("Library encoding"); + case SAVE_SORT_ORDER -> + Localization.lang("Save sort order"); + case KEY_PATTERNS -> + Localization.lang("Key patterns"); + case USER_FILE_DIRECTORY -> + Localization.lang("User-specific file directory"); + case LATEX_FILE_DIRECTORY -> + Localization.lang("LaTeX file directory"); + case DEFAULT_KEY_PATTERN -> + Localization.lang("Default pattern"); + case SAVE_ACTIONS -> + Localization.lang("Save actions"); + case MODE -> + Localization.lang("Library mode"); + case GENERAL_FILE_DIRECTORY -> + Localization.lang("General file directory"); + case CONTENT_SELECTOR -> + Localization.lang("Content selectors"); + }; + } } diff --git a/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java b/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java index 7a3c5db3ba0..0847ccee292 100644 --- a/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java +++ b/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java @@ -5,11 +5,24 @@ import java.util.Objects; import java.util.Optional; -import org.jabref.logic.l10n.Localization; import org.jabref.model.metadata.MetaData; import org.jabref.preferences.PreferencesService; public class MetaDataDiff { + public enum Difference { + PROTECTED, + GROUPS_ALTERED, + ENCODING, + SAVE_SORT_ORDER, + KEY_PATTERNS, + USER_FILE_DIRECTORY, + LATEX_FILE_DIRECTORY, + DEFAULT_KEY_PATTERN, + SAVE_ACTIONS, + MODE, + GENERAL_FILE_DIRECTORY, + CONTENT_SELECTOR + } private final Optional groupDiff; private final MetaData originalMetaData; @@ -32,44 +45,44 @@ public static Optional compare(MetaData originalMetaData, MetaData /** * @implNote Should be kept in sync with {@link MetaData#equals(Object)} */ - public List getDifferences(PreferencesService preferences) { - List changes = new ArrayList<>(); + public List getDifferences(PreferencesService preferences) { + List changes = new ArrayList<>(); if (originalMetaData.isProtected() != newMetaData.isProtected()) { - changes.add(Localization.lang("Library protection")); + changes.add(Difference.PROTECTED); } if (!Objects.equals(originalMetaData.getGroups(), newMetaData.getGroups())) { - changes.add(Localization.lang("Modified groups tree")); + changes.add(Difference.GROUPS_ALTERED); } if (!Objects.equals(originalMetaData.getEncoding(), newMetaData.getEncoding())) { - changes.add(Localization.lang("Library encoding")); + changes.add(Difference.ENCODING); } if (!Objects.equals(originalMetaData.getSaveOrderConfig(), newMetaData.getSaveOrderConfig())) { - changes.add(Localization.lang("Save sort order")); + changes.add(Difference.SAVE_SORT_ORDER); } if (!Objects.equals(originalMetaData.getCiteKeyPattern(preferences.getGlobalCitationKeyPattern()), newMetaData.getCiteKeyPattern(preferences.getGlobalCitationKeyPattern()))) { - changes.add(Localization.lang("Key patterns")); + changes.add(Difference.KEY_PATTERNS); } if (!Objects.equals(originalMetaData.getUserFileDirectories(), newMetaData.getUserFileDirectories())) { - changes.add(Localization.lang("User-specific file directory")); + changes.add(Difference.USER_FILE_DIRECTORY); } if (!Objects.equals(originalMetaData.getLatexFileDirectories(), newMetaData.getLatexFileDirectories())) { - changes.add(Localization.lang("LaTeX file directory")); + changes.add(Difference.LATEX_FILE_DIRECTORY); } if (!Objects.equals(originalMetaData.getDefaultCiteKeyPattern(), newMetaData.getDefaultCiteKeyPattern())) { - changes.add(Localization.lang("Default pattern")); + changes.add(Difference.DEFAULT_KEY_PATTERN); } if (!Objects.equals(originalMetaData.getSaveActions(), newMetaData.getSaveActions())) { - changes.add(Localization.lang("Save actions")); + changes.add(Difference.SAVE_ACTIONS); } if (originalMetaData.getMode() != newMetaData.getMode()) { - changes.add(Localization.lang("Library mode")); + changes.add(Difference.MODE); } if (!Objects.equals(originalMetaData.getDefaultFileDirectory(), newMetaData.getDefaultFileDirectory())) { - changes.add(Localization.lang("General file directory")); + changes.add(Difference.GENERAL_FILE_DIRECTORY); } if (!Objects.equals(originalMetaData.getContentSelectors(), newMetaData.getContentSelectors())) { - changes.add(Localization.lang("Content selectors")); + changes.add(Difference.CONTENT_SELECTOR); } return changes; } From d0687402768916bdd11afde93320360c41349da1 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Fri, 23 Sep 2022 22:22:50 +0200 Subject: [PATCH 4/8] Refactored FileHistory and converted getFileHistory to new prefs model --- src/main/java/org/jabref/gui/JabRefFrame.java | 4 +- src/main/java/org/jabref/gui/JabRefGUI.java | 2 +- .../org/jabref/gui/menus/FileHistoryMenu.java | 13 +++--- .../org/jabref/logic/util/io/FileHistory.java | 45 ++++++++++++------- .../jabref/preferences/GuiPreferences.java | 4 +- .../jabref/preferences/JabRefPreferences.java | 36 +++++++-------- .../preferences/PreferencesService.java | 2 - .../jabref/logic/util/io/FileHistoryTest.java | 12 ++--- 8 files changed, 61 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/jabref/gui/JabRefFrame.java b/src/main/java/org/jabref/gui/JabRefFrame.java index b931773824d..5312cd7580b 100644 --- a/src/main/java/org/jabref/gui/JabRefFrame.java +++ b/src/main/java/org/jabref/gui/JabRefFrame.java @@ -192,7 +192,7 @@ public JabRefFrame(Stage mainStage) { this.undoManager = Globals.undoManager; this.globalSearchBar = new GlobalSearchBar(this, stateManager, prefs, undoManager); this.pushToApplicationCommand = new PushToApplicationCommand(stateManager, dialogService, prefs); - this.fileHistory = new FileHistoryMenu(prefs, dialogService, getOpenDatabaseAction()); + this.fileHistory = new FileHistoryMenu(prefs.getGuiPreferences().getFileHistory(), dialogService, getOpenDatabaseAction()); this.taskExecutor = Globals.TASK_EXECUTOR; this.importFormatReader = Globals.IMPORT_FORMAT_READER; this.setOnKeyTyped(key -> { @@ -402,7 +402,7 @@ private void tearDownJabRef(List filenames) { // Here we store the names of all current files. If there is no current file, we remove any // previously stored filename. if (filenames.isEmpty()) { - prefs.clearEditedFiles(); + prefs.getGuiPreferences().getLastFilesOpened().clear(); } else { Path focusedDatabase = getCurrentLibraryTab().getBibDatabaseContext() .getDatabasePath() diff --git a/src/main/java/org/jabref/gui/JabRefGUI.java b/src/main/java/org/jabref/gui/JabRefGUI.java index 2acb97939c1..29883a51ebb 100644 --- a/src/main/java/org/jabref/gui/JabRefGUI.java +++ b/src/main/java/org/jabref/gui/JabRefGUI.java @@ -135,7 +135,7 @@ private void openDatabases() { // Remove invalid databases List invalidDatabases = bibDatabases.stream() .filter(ParserResult::isInvalid) - .collect(Collectors.toList()); + .toList(); failed.addAll(invalidDatabases); bibDatabases.removeAll(invalidDatabases); diff --git a/src/main/java/org/jabref/gui/menus/FileHistoryMenu.java b/src/main/java/org/jabref/gui/menus/FileHistoryMenu.java index 43a80641bf1..23554b66fc6 100644 --- a/src/main/java/org/jabref/gui/menus/FileHistoryMenu.java +++ b/src/main/java/org/jabref/gui/menus/FileHistoryMenu.java @@ -11,22 +11,19 @@ import org.jabref.gui.importer.actions.OpenDatabaseAction; import org.jabref.logic.l10n.Localization; import org.jabref.logic.util.io.FileHistory; -import org.jabref.preferences.PreferencesService; public class FileHistoryMenu extends Menu { private final FileHistory history; - private final PreferencesService preferences; private final DialogService dialogService; private final OpenDatabaseAction openDatabaseAction; - public FileHistoryMenu(PreferencesService preferences, DialogService dialogService, OpenDatabaseAction openDatabaseAction) { + public FileHistoryMenu(FileHistory fileHistory, DialogService dialogService, OpenDatabaseAction openDatabaseAction) { setText(Localization.lang("Recent libraries")); - this.preferences = preferences; + this.history = fileHistory; this.dialogService = dialogService; this.openDatabaseAction = openDatabaseAction; - history = preferences.getGuiPreferences().getFileHistory(); if (history.isEmpty()) { setDisable(true); } else { @@ -46,10 +43,10 @@ public boolean openFileByKey(KeyEvent keyEvent) { } char key = keyEvent.getCharacter().charAt(0); int num = Character.getNumericValue(key); - if (num <= 0 || num > history.getHistory().size()) { + if (num <= 0 || num > history.size()) { return false; } - this.openFile(history.getFileAt(Integer.parseInt(keyEvent.getCharacter()) - 1)); + this.openFile(history.get(Integer.parseInt(keyEvent.getCharacter()) - 1)); return true; } @@ -66,7 +63,7 @@ public void newFile(Path file) { private void setItems() { getItems().clear(); for (int index = 0; index < history.size(); index++) { - addItem(history.getFileAt(index), index + 1); + addItem(history.get(index), index + 1); } } diff --git a/src/main/java/org/jabref/logic/util/io/FileHistory.java b/src/main/java/org/jabref/logic/util/io/FileHistory.java index 04af2003c92..4281c36306e 100644 --- a/src/main/java/org/jabref/logic/util/io/FileHistory.java +++ b/src/main/java/org/jabref/logic/util/io/FileHistory.java @@ -1,28 +1,43 @@ package org.jabref.logic.util.io; import java.nio.file.Path; +import java.util.ArrayList; import java.util.List; -import java.util.Objects; -import javafx.collections.FXCollections; -import javafx.collections.ObservableList; +import javafx.collections.ModifiableObservableListBase; -public class FileHistory { +public class FileHistory extends ModifiableObservableListBase { private static final int HISTORY_SIZE = 8; - private final ObservableList history; + private final List history; - public FileHistory(List files) { - history = FXCollections.observableList(Objects.requireNonNull(files)); + private FileHistory(List list) { + history = new ArrayList<>(list); + } + + @Override + public Path get(int index) { + return history.get(index); } public int size() { return history.size(); } - public boolean isEmpty() { - return history.isEmpty(); + @Override + protected void doAdd(int index, Path element) { + history.add(index, element); + } + + @Override + protected Path doSet(int index, Path element) { + return history.set(index, element); + } + + @Override + protected Path doRemove(int index) { + return history.remove(index); } /** @@ -30,21 +45,17 @@ public boolean isEmpty() { */ public void newFile(Path file) { removeItem(file); - history.add(0, file); + this.add(0, file); while (size() > HISTORY_SIZE) { history.remove(HISTORY_SIZE); } } - public Path getFileAt(int index) { - return history.get(index); - } - public void removeItem(Path file) { - history.remove(file); + this.remove(file); } - public ObservableList getHistory() { - return history; + public static FileHistory of(List list) { + return new FileHistory(new ArrayList<>(list)); } } diff --git a/src/main/java/org/jabref/preferences/GuiPreferences.java b/src/main/java/org/jabref/preferences/GuiPreferences.java index 08f9e199c84..b8ca00d69d0 100644 --- a/src/main/java/org/jabref/preferences/GuiPreferences.java +++ b/src/main/java/org/jabref/preferences/GuiPreferences.java @@ -25,14 +25,14 @@ public class GuiPreferences { private final BooleanProperty windowMaximised; + // the last libraries that were open when jabref closes and should be reopened on startup private final ObservableList lastFilesOpened; private final ObjectProperty lastFocusedFile; + // observable list last files opened in the file menu private final FileHistory fileHistory; private final StringProperty lastSelectedIdBasedFetcher; - private final ObjectProperty mergeDiffMode; - private final DoubleProperty sidePaneWidth; public GuiPreferences(double positionX, diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 021daed43e0..95ebc694213 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -34,6 +34,7 @@ import java.util.stream.Stream; import javafx.beans.InvalidationListener; +import javafx.collections.ListChangeListener; import javafx.collections.SetChangeListener; import javafx.scene.control.TableColumn.SortType; @@ -2188,7 +2189,7 @@ private String determineMainFileDirectory(String originalDirectory) { // A non-empty directory is kept return originalDirectory; } - return JabRefDesktop.getDefaultFileChooserDirectory().toString(); + return JabRefDesktop.getDefaultFileChooserDirectory(); } @Override @@ -2560,8 +2561,13 @@ public GuiPreferences getGuiPreferences() { EasyBind.listen(guiPreferences.sizeXProperty(), (obs, oldValue, newValue) -> putDouble(SIZE_X, newValue.doubleValue())); EasyBind.listen(guiPreferences.sizeYProperty(), (obs, oldValue, newValue) -> putDouble(SIZE_Y, newValue.doubleValue())); EasyBind.listen(guiPreferences.windowMaximisedProperty(), (obs, oldValue, newValue) -> putBoolean(WINDOW_MAXIMISED, newValue)); - guiPreferences.getLastFilesOpened().addListener((InvalidationListener) change -> - putStringList(LAST_EDITED, guiPreferences.getLastFilesOpened())); + guiPreferences.getLastFilesOpened().addListener((ListChangeListener) change -> { + if (change.getList().isEmpty()) { + prefs.remove(LAST_EDITED); + } else { + putStringList(LAST_EDITED, guiPreferences.getLastFilesOpened()); + } + }); EasyBind.listen(guiPreferences.lastFocusedFileProperty(), (obs, oldValue, newValue) -> { if (newValue != null) { put(LAST_FOCUSED, newValue.toAbsolutePath().toString()); @@ -2569,7 +2575,7 @@ public GuiPreferences getGuiPreferences() { remove(LAST_EDITED); } }); - guiPreferences.getFileHistory().getHistory().addListener((InvalidationListener) change -> storeFileHistory(guiPreferences.getFileHistory())); + guiPreferences.getFileHistory().addListener((InvalidationListener) change -> storeFileHistory(guiPreferences.getFileHistory())); 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())); @@ -2578,24 +2584,16 @@ public GuiPreferences getGuiPreferences() { } private FileHistory getFileHistory() { - return new FileHistory(getStringList(RECENT_DATABASES).stream() - .map(Path::of) - .collect(Collectors.toList())); + return FileHistory.of(getStringList(RECENT_DATABASES).stream() + .map(Path::of) + .toList()); } private void storeFileHistory(FileHistory history) { - if (!history.isEmpty()) { - putStringList(RECENT_DATABASES, history.getHistory() - .stream() - .map(Path::toAbsolutePath) - .map(Path::toString) - .collect(Collectors.toList())); - } - } - - @Override - public void clearEditedFiles() { - prefs.remove(LAST_EDITED); + putStringList(RECENT_DATABASES, history.stream() + .map(Path::toAbsolutePath) + .map(Path::toString) + .toList()); } //************************************************************************************************************* diff --git a/src/main/java/org/jabref/preferences/PreferencesService.java b/src/main/java/org/jabref/preferences/PreferencesService.java index b9e6363018b..678ecfe37a3 100644 --- a/src/main/java/org/jabref/preferences/PreferencesService.java +++ b/src/main/java/org/jabref/preferences/PreferencesService.java @@ -242,8 +242,6 @@ public interface PreferencesService { GuiPreferences getGuiPreferences(); - void clearEditedFiles(); - //************************************************************************************************************* // Misc preferences //************************************************************************************************************* diff --git a/src/test/java/org/jabref/logic/util/io/FileHistoryTest.java b/src/test/java/org/jabref/logic/util/io/FileHistoryTest.java index 38f63a6c87d..6d37300cfa3 100644 --- a/src/test/java/org/jabref/logic/util/io/FileHistoryTest.java +++ b/src/test/java/org/jabref/logic/util/io/FileHistoryTest.java @@ -1,8 +1,8 @@ package org.jabref.logic.util.io; import java.nio.file.Path; -import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -16,14 +16,14 @@ class FileHistoryTest { @BeforeEach void setUp() { - history = new FileHistory(new ArrayList<>()); + history = FileHistory.of(Collections.emptyList()); } @Test void newItemsAreAddedInRightOrder() { history.newFile(Path.of("aa")); history.newFile(Path.of("bb")); - assertEquals(Arrays.asList(Path.of("bb"), Path.of("aa")), history.getHistory()); + assertEquals(Arrays.asList(Path.of("bb"), Path.of("aa")), history); } @Test @@ -31,7 +31,7 @@ void itemsAlreadyInListIsMovedToTop() { history.newFile(Path.of("aa")); history.newFile(Path.of("bb")); history.newFile(Path.of("aa")); - assertEquals(Arrays.asList(Path.of("aa"), Path.of("bb")), history.getHistory()); + assertEquals(Arrays.asList(Path.of("aa"), Path.of("bb")), history); } @Test @@ -42,7 +42,7 @@ void removeItemsLeavesOtherItemsInRightOrder() { history.removeItem(Path.of("bb")); - assertEquals(Arrays.asList(Path.of("cc"), Path.of("aa")), history.getHistory()); + assertEquals(Arrays.asList(Path.of("cc"), Path.of("aa")), history); } @Test @@ -66,7 +66,7 @@ void getFileAtTest() { history.newFile(Path.of("aa")); history.newFile(Path.of("bb")); history.newFile(Path.of("cc")); - assertEquals(Path.of("bb"), history.getFileAt(1)); + assertEquals(Path.of("bb"), history.get(1)); } } From 999121a4bcbfdee22c4670306aa0991a06dd51ae Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Fri, 23 Sep 2022 22:41:14 +0200 Subject: [PATCH 5/8] Partial localization without restart --- src/main/java/org/jabref/preferences/JabRefPreferences.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/jabref/preferences/JabRefPreferences.java b/src/main/java/org/jabref/preferences/JabRefPreferences.java index 95ebc694213..166e5d9f187 100644 --- a/src/main/java/org/jabref/preferences/JabRefPreferences.java +++ b/src/main/java/org/jabref/preferences/JabRefPreferences.java @@ -1272,6 +1272,7 @@ public GeneralPreferences getGeneralPreferences() { put(LANGUAGE, newValue.getId()); if (oldValue != newValue) { setLanguageDependentDefaultValues(); + Localization.setLanguage(newValue); } }); EasyBind.listen(generalPreferences.defaultBibDatabaseModeProperty(), (obs, oldValue, newValue) -> putBoolean(BIBLATEX_DEFAULT_MODE, (newValue == BibDatabaseMode.BIBLATEX))); From 089108cc9ab4c965039cdddd364d5a8926e48b6f Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 28 Sep 2022 15:25:54 +0200 Subject: [PATCH 6/8] Convert list to EnumSet --- .../org/jabref/logic/bibtex/comparator/MetaDataDiff.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java b/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java index 0847ccee292..4257fdf3435 100644 --- a/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java +++ b/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java @@ -1,7 +1,6 @@ package org.jabref.logic.bibtex.comparator; -import java.util.ArrayList; -import java.util.List; +import java.util.EnumSet; import java.util.Objects; import java.util.Optional; @@ -45,8 +44,8 @@ public static Optional compare(MetaData originalMetaData, MetaData /** * @implNote Should be kept in sync with {@link MetaData#equals(Object)} */ - public List getDifferences(PreferencesService preferences) { - List changes = new ArrayList<>(); + public EnumSet getDifferences(PreferencesService preferences) { + EnumSet changes = EnumSet.noneOf(Difference.class); if (originalMetaData.isProtected() != newMetaData.isProtected()) { changes.add(Difference.PROTECTED); From 34c84cca8b7f95f6e5d454eb2cece1a54a0eb117 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 28 Sep 2022 16:10:03 +0200 Subject: [PATCH 7/8] Used pattern matching and applied IDE suggestions --- .../jabref/logic/cleanup/FieldFormatterCleanup.java | 11 +++++------ .../jabref/logic/cleanup/FieldFormatterCleanups.java | 12 +++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanup.java b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanup.java index 1594bdb4aab..3696ac294ae 100644 --- a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanup.java +++ b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanup.java @@ -57,7 +57,7 @@ private List cleanupSingleField(Field fieldKey, BibEntry entry) { // Run formatter String newValue = formatter.format(oldValue); - if (oldValue.equals(newValue)) { + if (newValue.equals(oldValue)) { return Collections.emptyList(); } else { if (newValue.isEmpty()) { @@ -86,7 +86,7 @@ private List cleanupAllFields(BibEntry entry) { private List cleanupAllTextFields(BibEntry entry) { List fieldChanges = new ArrayList<>(); Set fields = new HashSet<>(entry.getFields()); - fields.removeAll(FieldFactory.getNotTextFieldNames()); + FieldFactory.getNotTextFieldNames().forEach(fields::remove); for (Field fieldKey : fields) { if (!fieldKey.equals(InternalField.KEY_FIELD)) { fieldChanges.addAll(cleanupSingleField(fieldKey, entry)); @@ -109,11 +109,10 @@ public boolean equals(Object obj) { if (this == obj) { return true; } - if (!(obj instanceof FieldFormatterCleanup)) { - return false; + if (obj instanceof FieldFormatterCleanup that) { + return Objects.equals(field, that.field) && Objects.equals(formatter, that.formatter); } - FieldFormatterCleanup other = (FieldFormatterCleanup) obj; - return Objects.equals(field, other.field) && Objects.equals(formatter, other.formatter); + return false; } @Override diff --git a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java index 153ee3be486..17e88ab6955 100644 --- a/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java +++ b/src/main/java/org/jabref/logic/cleanup/FieldFormatterCleanups.java @@ -217,15 +217,17 @@ public boolean equals(Object obj) { if (this == obj) { return true; } - if (!(obj instanceof FieldFormatterCleanups)) { - return false; + if (obj instanceof FieldFormatterCleanups other) { + return Objects.equals(actions, other.actions) && (enabled == other.enabled); } - FieldFormatterCleanups other = (FieldFormatterCleanups) obj; - return Objects.equals(actions, other.actions) && (enabled == other.enabled); + return false; } @Override public String toString() { - return "FieldFormatterCleanups [enabled=" + enabled + ", actions=" + actions + "]"; + return "FieldFormatterCleanups{" + + "enabled=" + enabled + "," + + "actions=" + actions + + "}"; } } From b3a1d47cd3dffef0d75f5f6a21bf52856dc85385 Mon Sep 17 00:00:00 2001 From: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com> Date: Wed, 28 Sep 2022 16:39:42 +0200 Subject: [PATCH 8/8] Fixed comparison of optionals --- .../java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java b/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java index 4257fdf3435..09c639e6e64 100644 --- a/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java +++ b/src/main/java/org/jabref/logic/bibtex/comparator/MetaDataDiff.java @@ -74,7 +74,7 @@ public EnumSet getDifferences(PreferencesService preferences) { if (!Objects.equals(originalMetaData.getSaveActions(), newMetaData.getSaveActions())) { changes.add(Difference.SAVE_ACTIONS); } - if (originalMetaData.getMode() != newMetaData.getMode()) { + if (!originalMetaData.getMode().equals(newMetaData.getMode())) { changes.add(Difference.MODE); } if (!Objects.equals(originalMetaData.getDefaultFileDirectory(), newMetaData.getDefaultFileDirectory())) {