Skip to content

Commit 75d5d52

Browse files
calixtuskoppor
andauthored
Observable Preferences Y (Resetting preferences) (#13894)
* Prototype * Codestyle * Move private constants back to JabRefGuiPreferences * Add comments * Add import * Apply merge conflict fixes * Fix migrations * Workaround for importing to ui disable cancel button * Add region marker * Fix out-of-bounds error * Codestyle * l10n --------- Co-authored-by: Carl Christian Snethlage <[email protected]> Co-authored-by: Oliver Kopp <[email protected]>
1 parent b071bfa commit 75d5d52

File tree

14 files changed

+218
-147
lines changed

14 files changed

+218
-147
lines changed

jabgui/src/main/java/org/jabref/gui/WorkspacePreferences.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jabref.gui;
22

33
import java.util.List;
4+
import java.util.Locale;
45

56
import javafx.beans.property.BooleanProperty;
67
import javafx.beans.property.IntegerProperty;
@@ -23,7 +24,6 @@ public class WorkspacePreferences {
2324
private final BooleanProperty themeSyncOs;
2425
private final BooleanProperty shouldOpenLastEdited;
2526
private final BooleanProperty showAdvancedHints;
26-
private final BooleanProperty warnAboutDuplicatesInInspection;
2727
private final BooleanProperty confirmDelete;
2828
private final BooleanProperty confirmHideTabBar;
2929
private final ObservableList<String> selectedSlrCatalogs;
@@ -36,7 +36,6 @@ public WorkspacePreferences(Language language,
3636
boolean themeSyncOs,
3737
boolean shouldOpenLastEdited,
3838
boolean showAdvancedHints,
39-
boolean warnAboutDuplicatesInInspection,
4039
boolean confirmDelete,
4140
boolean confirmHideTabBar,
4241
List<String> selectedSlrCatalogs) {
@@ -48,12 +47,46 @@ public WorkspacePreferences(Language language,
4847
this.themeSyncOs = new SimpleBooleanProperty(themeSyncOs);
4948
this.shouldOpenLastEdited = new SimpleBooleanProperty(shouldOpenLastEdited);
5049
this.showAdvancedHints = new SimpleBooleanProperty(showAdvancedHints);
51-
this.warnAboutDuplicatesInInspection = new SimpleBooleanProperty(warnAboutDuplicatesInInspection);
5250
this.confirmDelete = new SimpleBooleanProperty(confirmDelete);
5351
this.confirmHideTabBar = new SimpleBooleanProperty(confirmHideTabBar);
5452
this.selectedSlrCatalogs = FXCollections.observableArrayList(selectedSlrCatalogs);
5553
}
5654

55+
/// Creates Object with default values
56+
private WorkspacePreferences() {
57+
this(
58+
Language.getLanguageFor(Locale.getDefault().getLanguage()), // Default language
59+
false, // Default font size override
60+
9, // Default font size
61+
9, // FixMe: main default and default default is weird
62+
new Theme(Theme.BASE_CSS), // Default theme
63+
false, // Default theme sync with OS
64+
true, // Default open last edited
65+
true, // Default show advanced hints
66+
true, // Default confirm delete
67+
true, // Default confirm hide tab bar
68+
List.of() // Default selected SLR catalogs
69+
);
70+
}
71+
72+
public static WorkspacePreferences getDefault() {
73+
return new WorkspacePreferences();
74+
}
75+
76+
public void setAll(WorkspacePreferences preferences) {
77+
this.language.set(preferences.getLanguage());
78+
this.shouldOverrideDefaultFontSize.set(preferences.shouldOverrideDefaultFontSize());
79+
this.mainFontSize.set(preferences.getMainFontSize());
80+
this.defaultFontSize.set(preferences.getDefaultFontSize());
81+
this.theme.set(preferences.getTheme());
82+
this.themeSyncOs.set(preferences.shouldThemeSyncOs());
83+
this.shouldOpenLastEdited.set(preferences.shouldOpenLastEdited());
84+
this.showAdvancedHints.set(preferences.shouldShowAdvancedHints());
85+
this.confirmDelete.set(preferences.shouldConfirmDelete());
86+
this.confirmHideTabBar.set(preferences.shouldHideTabBar());
87+
this.selectedSlrCatalogs.setAll(preferences.getSelectedSlrCatalogs());
88+
}
89+
5790
public Language getLanguage() {
5891
return language.get();
5992
}
@@ -142,18 +175,6 @@ public void setShowAdvancedHints(boolean showAdvancedHints) {
142175
this.showAdvancedHints.set(showAdvancedHints);
143176
}
144177

145-
public boolean shouldWarnAboutDuplicatesInInspection() {
146-
return warnAboutDuplicatesInInspection.get();
147-
}
148-
149-
public BooleanProperty warnAboutDuplicatesInInspectionProperty() {
150-
return warnAboutDuplicatesInInspection;
151-
}
152-
153-
public void setWarnAboutDuplicatesInInspection(boolean warnAboutDuplicatesInInspection) {
154-
this.warnAboutDuplicatesInInspection.set(warnAboutDuplicatesInInspection);
155-
}
156-
157178
public boolean shouldConfirmDelete() {
158179
return confirmDelete.get();
159180
}

jabgui/src/main/java/org/jabref/gui/preferences/JabRefGuiPreferences.java

Lines changed: 63 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Objects;
1111
import java.util.SequencedMap;
1212
import java.util.Set;
13+
import java.util.prefs.BackingStoreException;
1314
import java.util.stream.Collectors;
1415

1516
import javafx.beans.InvalidationListener;
@@ -45,6 +46,7 @@
4546
import org.jabref.gui.sidepane.SidePaneType;
4647
import org.jabref.gui.specialfields.SpecialFieldsPreferences;
4748
import org.jabref.gui.theme.Theme;
49+
import org.jabref.logic.JabRefException;
4850
import org.jabref.logic.bst.BstPreviewLayout;
4951
import org.jabref.logic.citationstyle.CSLStyleLoader;
5052
import org.jabref.logic.citationstyle.CSLStyleUtils;
@@ -83,7 +85,6 @@ public class JabRefGuiPreferences extends JabRefCliPreferences implements GuiPre
8385

8486
// Public because needed for pref migration
8587
public static final String AUTOCOMPLETER_COMPLETE_FIELDS = "autoCompleteFields";
86-
public static final String MAIN_FONT_SIZE = "mainFontSize";
8788

8889
// region Preview - public for pref migrations
8990
public static final String PREVIEW_STYLE = "previewStyle";
@@ -111,6 +112,18 @@ public class JabRefGuiPreferences extends JabRefCliPreferences implements GuiPre
111112

112113
private static final Logger LOGGER = LoggerFactory.getLogger(JabRefGuiPreferences.class);
113114

115+
// region WorkspacePreferences
116+
private static final String OVERRIDE_DEFAULT_FONT_SIZE = "overrideDefaultFontSize";
117+
private static final String MAIN_FONT_SIZE = "mainFontSize";
118+
private static final String THEME = "fxTheme";
119+
private static final String THEME_SYNC_OS = "themeSyncOs";
120+
private static final String OPEN_LAST_EDITED = "openLastEdited";
121+
private static final String SHOW_ADVANCED_HINTS = "showAdvancedHints";
122+
private static final String CONFIRM_DELETE = "confirmDelete";
123+
private static final String CONFIRM_HIDE_TAB_BAR = "confirmHideTabBar";
124+
private static final String SELECTED_SLR_CATALOGS = "selectedSlrCatalogs";
125+
// endregion
126+
114127
// region core GUI preferences
115128
private static final String MAIN_WINDOW_POS_X = "mainWindowPosX";
116129
private static final String MAIN_WINDOW_POS_Y = "mainWindowPosY";
@@ -150,16 +163,6 @@ public class JabRefGuiPreferences extends JabRefCliPreferences implements GuiPre
150163
private static final String FILE_BROWSER_COMMAND = "fileBrowserCommand";
151164
// endregion
152165

153-
// region workspace
154-
private static final String THEME = "fxTheme";
155-
private static final String THEME_SYNC_OS = "themeSyncOs";
156-
private static final String OPEN_LAST_EDITED = "openLastEdited";
157-
private static final String OVERRIDE_DEFAULT_FONT_SIZE = "overrideDefaultFontSize";
158-
private static final String SHOW_ADVANCED_HINTS = "showAdvancedHints";
159-
private static final String CONFIRM_DELETE = "confirmDelete";
160-
private static final String CONFIRM_HIDE_TAB_BAR = "confirmHideTabBar";
161-
// endregion
162-
163166
private static final String ENTRY_EDITOR_HEIGHT = "entryEditorHeightFX";
164167

165168
/**
@@ -196,7 +199,6 @@ public class JabRefGuiPreferences extends JabRefCliPreferences implements GuiPre
196199
private static final String SPECIALFIELDSENABLED = "specialFieldsEnabled";
197200
// endregion
198201

199-
private static final String SELECTED_SLR_CATALOGS = "selectedSlrCatalogs";
200202
private static final String UNLINKED_FILES_SELECTED_EXTENSION = "unlinkedFilesSelectedExtension";
201203
private static final String UNLINKED_FILES_SELECTED_DATE_RANGE = "unlinkedFilesSelectedDateRange";
202204
private static final String UNLINKED_FILES_SELECTED_SORT = "unlinkedFilesSelectedSort";
@@ -269,17 +271,6 @@ private JabRefGuiPreferences() {
269271
defaults.put(AUTOCOMPLETER_COMPLETE_FIELDS, "author;editor;title;journal;publisher;keywords;crossref;related;entryset");
270272
// endregion
271273

272-
// region workspace
273-
defaults.put(MAIN_FONT_SIZE, 9);
274-
defaults.put(OVERRIDE_DEFAULT_FONT_SIZE, false);
275-
defaults.put(OPEN_LAST_EDITED, Boolean.TRUE);
276-
defaults.put(THEME, Theme.BASE_CSS);
277-
defaults.put(THEME_SYNC_OS, Boolean.FALSE);
278-
defaults.put(CONFIRM_DELETE, Boolean.TRUE);
279-
defaults.put(CONFIRM_HIDE_TAB_BAR, Boolean.TRUE);
280-
defaults.put(SHOW_ADVANCED_HINTS, Boolean.TRUE);
281-
// endregion
282-
283274
// region unlinkedFilesDialogPreferences
284275
defaults.put(UNLINKED_FILES_SELECTED_EXTENSION, StandardFileType.ANY_FILE.getName());
285276
defaults.put(UNLINKED_FILES_SELECTED_DATE_RANGE, DateRange.ALL_TIME.name());
@@ -429,6 +420,21 @@ public CopyToPreferences getCopyToPreferences() {
429420
return copyToPreferences;
430421
}
431422

423+
@Override
424+
public void clear() throws BackingStoreException {
425+
super.clear();
426+
427+
getWorkspacePreferences().setAll(WorkspacePreferences.getDefault());
428+
}
429+
430+
@Override
431+
public void importPreferences(Path file) throws JabRefException {
432+
super.importPreferences(file);
433+
434+
// in case of incomplete or corrupt xml fall back to current preferences
435+
getWorkspacePreferences().setAll(getWorkspacePreferencesFromLowLevelApi(getWorkspacePreferences()));
436+
}
437+
432438
// region EntryEditorPreferences
433439
public EntryEditorPreferences getEntryEditorPreferences() {
434440
if (entryEditorPreferences != null) {
@@ -641,42 +647,52 @@ public WorkspacePreferences getWorkspacePreferences() {
641647
return workspacePreferences;
642648
}
643649

644-
workspacePreferences = new WorkspacePreferences(
645-
getLanguage(),
646-
getBoolean(OVERRIDE_DEFAULT_FONT_SIZE),
647-
getInt(MAIN_FONT_SIZE),
648-
(Integer) defaults.get(MAIN_FONT_SIZE),
649-
new Theme(get(THEME)),
650-
getBoolean(THEME_SYNC_OS),
651-
getBoolean(OPEN_LAST_EDITED),
652-
getBoolean(SHOW_ADVANCED_HINTS),
653-
getBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION),
654-
getBoolean(CONFIRM_DELETE),
655-
getBoolean(CONFIRM_HIDE_TAB_BAR),
656-
getStringList(SELECTED_SLR_CATALOGS));
650+
workspacePreferences = getWorkspacePreferencesFromLowLevelApi(WorkspacePreferences.getDefault());
657651

658-
EasyBind.listen(workspacePreferences.languageProperty(), (obs, oldValue, newValue) -> {
652+
EasyBind.listen(workspacePreferences.languageProperty(), (_, oldValue, newValue) -> {
659653
put(LANGUAGE, newValue.getId());
660654
if (oldValue != newValue) {
661655
setLanguageDependentDefaultValues();
662656
Localization.setLanguage(newValue);
663657
}
664658
});
665659

666-
EasyBind.listen(workspacePreferences.shouldOverrideDefaultFontSizeProperty(), (obs, oldValue, newValue) -> putBoolean(OVERRIDE_DEFAULT_FONT_SIZE, newValue));
667-
EasyBind.listen(workspacePreferences.mainFontSizeProperty(), (obs, oldValue, newValue) -> putInt(MAIN_FONT_SIZE, newValue));
668-
EasyBind.listen(workspacePreferences.themeProperty(), (obs, oldValue, newValue) -> put(THEME, newValue.getName()));
669-
EasyBind.listen(workspacePreferences.themeSyncOsProperty(), (obs, oldValue, newValue) -> putBoolean(THEME_SYNC_OS, newValue));
670-
EasyBind.listen(workspacePreferences.openLastEditedProperty(), (obs, oldValue, newValue) -> putBoolean(OPEN_LAST_EDITED, newValue));
671-
EasyBind.listen(workspacePreferences.showAdvancedHintsProperty(), (obs, oldValue, newValue) -> putBoolean(SHOW_ADVANCED_HINTS, newValue));
672-
EasyBind.listen(workspacePreferences.warnAboutDuplicatesInInspectionProperty(), (obs, oldValue, newValue) -> putBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION, newValue));
673-
EasyBind.listen(workspacePreferences.confirmDeleteProperty(), (obs, oldValue, newValue) -> putBoolean(CONFIRM_DELETE, newValue));
674-
EasyBind.listen(workspacePreferences.hideTabBarProperty(), (obs, oldValue, newValue) -> putBoolean(CONFIRM_HIDE_TAB_BAR, newValue));
675-
workspacePreferences.getSelectedSlrCatalogs().addListener((ListChangeListener<String>) change ->
660+
EasyBind.listen(workspacePreferences.shouldOverrideDefaultFontSizeProperty(), (_, _, newValue) ->
661+
putBoolean(OVERRIDE_DEFAULT_FONT_SIZE, newValue));
662+
EasyBind.listen(workspacePreferences.mainFontSizeProperty(), (_, _, newValue) ->
663+
putInt(MAIN_FONT_SIZE, newValue));
664+
EasyBind.listen(workspacePreferences.themeProperty(), (_, _, newValue) ->
665+
put(THEME, newValue.getName()));
666+
EasyBind.listen(workspacePreferences.themeSyncOsProperty(), (_, _, newValue) ->
667+
putBoolean(THEME_SYNC_OS, newValue));
668+
EasyBind.listen(workspacePreferences.openLastEditedProperty(), (_, _, newValue) ->
669+
putBoolean(OPEN_LAST_EDITED, newValue));
670+
EasyBind.listen(workspacePreferences.showAdvancedHintsProperty(), (_, _, newValue) ->
671+
putBoolean(SHOW_ADVANCED_HINTS, newValue));
672+
EasyBind.listen(workspacePreferences.confirmDeleteProperty(), (_, _, newValue) ->
673+
putBoolean(CONFIRM_DELETE, newValue));
674+
EasyBind.listen(workspacePreferences.hideTabBarProperty(), (_, _, newValue) ->
675+
putBoolean(CONFIRM_HIDE_TAB_BAR, newValue));
676+
workspacePreferences.getSelectedSlrCatalogs().addListener((ListChangeListener<String>) _ ->
676677
putStringList(SELECTED_SLR_CATALOGS, workspacePreferences.getSelectedSlrCatalogs()));
677678
return workspacePreferences;
678679
}
679680

681+
private WorkspacePreferences getWorkspacePreferencesFromLowLevelApi(WorkspacePreferences defaults) {
682+
return new WorkspacePreferences(
683+
getLanguage(),
684+
getBoolean(OVERRIDE_DEFAULT_FONT_SIZE, defaults.shouldOverrideDefaultFontSize()),
685+
getInt(MAIN_FONT_SIZE, defaults.getMainFontSize()),
686+
defaults.getDefaultFontSize(), // FixMe
687+
new Theme(get(THEME, Theme.BASE_CSS)),
688+
getBoolean(THEME_SYNC_OS, defaults.shouldThemeSyncOs()),
689+
getBoolean(OPEN_LAST_EDITED, defaults.shouldOpenLastEdited()),
690+
getBoolean(SHOW_ADVANCED_HINTS, defaults.shouldShowAdvancedHints()),
691+
getBoolean(CONFIRM_DELETE, defaults.shouldConfirmDelete()),
692+
getBoolean(CONFIRM_HIDE_TAB_BAR, defaults.shouldHideTabBar()),
693+
getStringList(SELECTED_SLR_CATALOGS));
694+
}
695+
680696
@Override
681697
public UnlinkedFilesDialogPreferences getUnlinkedFilesDialogPreferences() {
682698
if (unlinkedFilesDialogPreferences != null) {

jabgui/src/main/java/org/jabref/gui/preferences/PreferencesDialogView.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class PreferencesDialogView extends BaseDialog<PreferencesDialogViewModel
3939
@FXML private ListView<PreferencesTab> preferenceTabList;
4040
@FXML private ScrollPane preferencesContainer;
4141
@FXML private ButtonType saveButton;
42+
@FXML private ButtonType cancelButton;
4243
@FXML private ToggleButton memoryStickMode;
4344

4445
@Inject private DialogService dialogService;
@@ -55,7 +56,7 @@ public PreferencesDialogView(Class<? extends PreferencesTab> preferencesTabToSel
5556
.load()
5657
.setAsDialogPane(this);
5758

58-
ControlHelper.setAction(saveButton, getDialogPane(), event -> savePreferencesAndCloseDialog());
59+
ControlHelper.setAction(saveButton, getDialogPane(), _ -> savePreferencesAndCloseDialog());
5960

6061
// Stop the default button from firing when the user hits enter within the search box
6162
searchBox.setOnKeyPressed(event -> {
@@ -149,7 +150,11 @@ void exportPreferences() {
149150

150151
@FXML
151152
void importPreferences() {
152-
viewModel.importPreferences();
153+
if (viewModel.importPreferences()) {
154+
// Hint the user that preferences are already loaded into the UI
155+
// ToDo: Import into the ui directly and save changes on click on Save button
156+
this.getDialogPane().lookupButton(cancelButton).setDisable(true);
157+
}
153158
}
154159

155160
@FXML
@@ -159,6 +164,10 @@ void showAllPreferences() {
159164

160165
@FXML
161166
void resetPreferences() {
162-
viewModel.resetPreferences();
167+
if (viewModel.resetPreferences()) {
168+
// Hint the user that preferences are already loaded into the UI
169+
// ToDo: Reset the ui and save changes on click on Save button
170+
this.getDialogPane().lookupButton(cancelButton).setDisable(true);
171+
}
163172
}
164173
}

0 commit comments

Comments
 (0)