Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 36 additions & 15 deletions jabgui/src/main/java/org/jabref/gui/WorkspacePreferences.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jabref.gui;

import java.util.List;
import java.util.Locale;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.IntegerProperty;
Expand All @@ -23,7 +24,6 @@ public class WorkspacePreferences {
private final BooleanProperty themeSyncOs;
private final BooleanProperty shouldOpenLastEdited;
private final BooleanProperty showAdvancedHints;
private final BooleanProperty warnAboutDuplicatesInInspection;
private final BooleanProperty confirmDelete;
private final BooleanProperty confirmHideTabBar;
private final ObservableList<String> selectedSlrCatalogs;
Expand All @@ -36,7 +36,6 @@ public WorkspacePreferences(Language language,
boolean themeSyncOs,
boolean shouldOpenLastEdited,
boolean showAdvancedHints,
boolean warnAboutDuplicatesInInspection,
boolean confirmDelete,
boolean confirmHideTabBar,
List<String> selectedSlrCatalogs) {
Expand All @@ -48,12 +47,46 @@ public WorkspacePreferences(Language language,
this.themeSyncOs = new SimpleBooleanProperty(themeSyncOs);
this.shouldOpenLastEdited = new SimpleBooleanProperty(shouldOpenLastEdited);
this.showAdvancedHints = new SimpleBooleanProperty(showAdvancedHints);
this.warnAboutDuplicatesInInspection = new SimpleBooleanProperty(warnAboutDuplicatesInInspection);
this.confirmDelete = new SimpleBooleanProperty(confirmDelete);
this.confirmHideTabBar = new SimpleBooleanProperty(confirmHideTabBar);
this.selectedSlrCatalogs = FXCollections.observableArrayList(selectedSlrCatalogs);
}

/// Creates Object with default values
private WorkspacePreferences() {
this(
Language.getLanguageFor(Locale.getDefault().getLanguage()), // Default language
false, // Default font size override
9, // Default font size
9, // FixMe: main default and default default is weird
new Theme(Theme.BASE_CSS), // Default theme
false, // Default theme sync with OS
true, // Default open last edited
true, // Default show advanced hints
true, // Default confirm delete
true, // Default confirm hide tab bar
List.of() // Default selected SLR catalogs
);
}

public static WorkspacePreferences getDefault() {
return new WorkspacePreferences();
}

public void setAll(WorkspacePreferences preferences) {
this.language.set(preferences.getLanguage());
this.shouldOverrideDefaultFontSize.set(preferences.shouldOverrideDefaultFontSize());
this.mainFontSize.set(preferences.getMainFontSize());
this.defaultFontSize.set(preferences.getDefaultFontSize());
this.theme.set(preferences.getTheme());
this.themeSyncOs.set(preferences.shouldThemeSyncOs());
this.shouldOpenLastEdited.set(preferences.shouldOpenLastEdited());
this.showAdvancedHints.set(preferences.shouldShowAdvancedHints());
this.confirmDelete.set(preferences.shouldConfirmDelete());
this.confirmHideTabBar.set(preferences.shouldHideTabBar());
this.selectedSlrCatalogs.setAll(preferences.getSelectedSlrCatalogs());
}

public Language getLanguage() {
return language.get();
}
Expand Down Expand Up @@ -142,18 +175,6 @@ public void setShowAdvancedHints(boolean showAdvancedHints) {
this.showAdvancedHints.set(showAdvancedHints);
}

public boolean shouldWarnAboutDuplicatesInInspection() {
return warnAboutDuplicatesInInspection.get();
}

public BooleanProperty warnAboutDuplicatesInInspectionProperty() {
return warnAboutDuplicatesInInspection;
}

public void setWarnAboutDuplicatesInInspection(boolean warnAboutDuplicatesInInspection) {
this.warnAboutDuplicatesInInspection.set(warnAboutDuplicatesInInspection);
}

public boolean shouldConfirmDelete() {
return confirmDelete.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Objects;
import java.util.SequencedMap;
import java.util.Set;
import java.util.prefs.BackingStoreException;
import java.util.stream.Collectors;

import javafx.beans.InvalidationListener;
Expand Down Expand Up @@ -45,6 +46,7 @@
import org.jabref.gui.sidepane.SidePaneType;
import org.jabref.gui.specialfields.SpecialFieldsPreferences;
import org.jabref.gui.theme.Theme;
import org.jabref.logic.JabRefException;
import org.jabref.logic.bst.BstPreviewLayout;
import org.jabref.logic.citationstyle.CSLStyleLoader;
import org.jabref.logic.citationstyle.CSLStyleUtils;
Expand Down Expand Up @@ -83,7 +85,6 @@ public class JabRefGuiPreferences extends JabRefCliPreferences implements GuiPre

// Public because needed for pref migration
public static final String AUTOCOMPLETER_COMPLETE_FIELDS = "autoCompleteFields";
public static final String MAIN_FONT_SIZE = "mainFontSize";

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

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

// region WorkspacePreferences
private static final String OVERRIDE_DEFAULT_FONT_SIZE = "overrideDefaultFontSize";
private static final String MAIN_FONT_SIZE = "mainFontSize";
private static final String THEME = "fxTheme";
private static final String THEME_SYNC_OS = "themeSyncOs";
private static final String OPEN_LAST_EDITED = "openLastEdited";
private static final String SHOW_ADVANCED_HINTS = "showAdvancedHints";
private static final String CONFIRM_DELETE = "confirmDelete";
private static final String CONFIRM_HIDE_TAB_BAR = "confirmHideTabBar";
private static final String SELECTED_SLR_CATALOGS = "selectedSlrCatalogs";
// endregion

// region core GUI preferences
private static final String MAIN_WINDOW_POS_X = "mainWindowPosX";
private static final String MAIN_WINDOW_POS_Y = "mainWindowPosY";
Expand Down Expand Up @@ -150,16 +163,6 @@ public class JabRefGuiPreferences extends JabRefCliPreferences implements GuiPre
private static final String FILE_BROWSER_COMMAND = "fileBrowserCommand";
// endregion

// region workspace
private static final String THEME = "fxTheme";
private static final String THEME_SYNC_OS = "themeSyncOs";
private static final String OPEN_LAST_EDITED = "openLastEdited";
private static final String OVERRIDE_DEFAULT_FONT_SIZE = "overrideDefaultFontSize";
private static final String SHOW_ADVANCED_HINTS = "showAdvancedHints";
private static final String CONFIRM_DELETE = "confirmDelete";
private static final String CONFIRM_HIDE_TAB_BAR = "confirmHideTabBar";
// endregion

private static final String ENTRY_EDITOR_HEIGHT = "entryEditorHeightFX";

/**
Expand Down Expand Up @@ -196,7 +199,6 @@ public class JabRefGuiPreferences extends JabRefCliPreferences implements GuiPre
private static final String SPECIALFIELDSENABLED = "specialFieldsEnabled";
// endregion

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

// region workspace
defaults.put(MAIN_FONT_SIZE, 9);
defaults.put(OVERRIDE_DEFAULT_FONT_SIZE, false);
defaults.put(OPEN_LAST_EDITED, Boolean.TRUE);
defaults.put(THEME, Theme.BASE_CSS);
defaults.put(THEME_SYNC_OS, Boolean.FALSE);
defaults.put(CONFIRM_DELETE, Boolean.TRUE);
defaults.put(CONFIRM_HIDE_TAB_BAR, Boolean.TRUE);
defaults.put(SHOW_ADVANCED_HINTS, Boolean.TRUE);
// endregion

// region unlinkedFilesDialogPreferences
defaults.put(UNLINKED_FILES_SELECTED_EXTENSION, StandardFileType.ANY_FILE.getName());
defaults.put(UNLINKED_FILES_SELECTED_DATE_RANGE, DateRange.ALL_TIME.name());
Expand Down Expand Up @@ -429,6 +420,21 @@ public CopyToPreferences getCopyToPreferences() {
return copyToPreferences;
}

@Override
public void clear() throws BackingStoreException {
super.clear();

getWorkspacePreferences().setAll(WorkspacePreferences.getDefault());
}

@Override
public void importPreferences(Path file) throws JabRefException {
super.importPreferences(file);

// in case of incomplete or corrupt xml fall back to current preferences
getWorkspacePreferences().setAll(getWorkspacePreferencesFromLowLevelApi(getWorkspacePreferences()));
}

// region EntryEditorPreferences
public EntryEditorPreferences getEntryEditorPreferences() {
if (entryEditorPreferences != null) {
Expand Down Expand Up @@ -641,42 +647,52 @@ public WorkspacePreferences getWorkspacePreferences() {
return workspacePreferences;
}

workspacePreferences = new WorkspacePreferences(
getLanguage(),
getBoolean(OVERRIDE_DEFAULT_FONT_SIZE),
getInt(MAIN_FONT_SIZE),
(Integer) defaults.get(MAIN_FONT_SIZE),
new Theme(get(THEME)),
getBoolean(THEME_SYNC_OS),
getBoolean(OPEN_LAST_EDITED),
getBoolean(SHOW_ADVANCED_HINTS),
getBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION),
getBoolean(CONFIRM_DELETE),
getBoolean(CONFIRM_HIDE_TAB_BAR),
getStringList(SELECTED_SLR_CATALOGS));
workspacePreferences = getWorkspacePreferencesFromLowLevelApi(WorkspacePreferences.getDefault());

EasyBind.listen(workspacePreferences.languageProperty(), (obs, oldValue, newValue) -> {
EasyBind.listen(workspacePreferences.languageProperty(), (_, oldValue, newValue) -> {
put(LANGUAGE, newValue.getId());
if (oldValue != newValue) {
setLanguageDependentDefaultValues();
Localization.setLanguage(newValue);
}
});

EasyBind.listen(workspacePreferences.shouldOverrideDefaultFontSizeProperty(), (obs, oldValue, newValue) -> putBoolean(OVERRIDE_DEFAULT_FONT_SIZE, newValue));
EasyBind.listen(workspacePreferences.mainFontSizeProperty(), (obs, oldValue, newValue) -> putInt(MAIN_FONT_SIZE, newValue));
EasyBind.listen(workspacePreferences.themeProperty(), (obs, oldValue, newValue) -> put(THEME, newValue.getName()));
EasyBind.listen(workspacePreferences.themeSyncOsProperty(), (obs, oldValue, newValue) -> putBoolean(THEME_SYNC_OS, newValue));
EasyBind.listen(workspacePreferences.openLastEditedProperty(), (obs, oldValue, newValue) -> putBoolean(OPEN_LAST_EDITED, newValue));
EasyBind.listen(workspacePreferences.showAdvancedHintsProperty(), (obs, oldValue, newValue) -> putBoolean(SHOW_ADVANCED_HINTS, newValue));
EasyBind.listen(workspacePreferences.warnAboutDuplicatesInInspectionProperty(), (obs, oldValue, newValue) -> putBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION, newValue));
EasyBind.listen(workspacePreferences.confirmDeleteProperty(), (obs, oldValue, newValue) -> putBoolean(CONFIRM_DELETE, newValue));
EasyBind.listen(workspacePreferences.hideTabBarProperty(), (obs, oldValue, newValue) -> putBoolean(CONFIRM_HIDE_TAB_BAR, newValue));
workspacePreferences.getSelectedSlrCatalogs().addListener((ListChangeListener<String>) change ->
EasyBind.listen(workspacePreferences.shouldOverrideDefaultFontSizeProperty(), (_, _, newValue) ->
putBoolean(OVERRIDE_DEFAULT_FONT_SIZE, newValue));
EasyBind.listen(workspacePreferences.mainFontSizeProperty(), (_, _, newValue) ->
putInt(MAIN_FONT_SIZE, newValue));
EasyBind.listen(workspacePreferences.themeProperty(), (_, _, newValue) ->
put(THEME, newValue.getName()));
EasyBind.listen(workspacePreferences.themeSyncOsProperty(), (_, _, newValue) ->
putBoolean(THEME_SYNC_OS, newValue));
EasyBind.listen(workspacePreferences.openLastEditedProperty(), (_, _, newValue) ->
putBoolean(OPEN_LAST_EDITED, newValue));
EasyBind.listen(workspacePreferences.showAdvancedHintsProperty(), (_, _, newValue) ->
putBoolean(SHOW_ADVANCED_HINTS, newValue));
EasyBind.listen(workspacePreferences.confirmDeleteProperty(), (_, _, newValue) ->
putBoolean(CONFIRM_DELETE, newValue));
EasyBind.listen(workspacePreferences.hideTabBarProperty(), (_, _, newValue) ->
putBoolean(CONFIRM_HIDE_TAB_BAR, newValue));
workspacePreferences.getSelectedSlrCatalogs().addListener((ListChangeListener<String>) _ ->
putStringList(SELECTED_SLR_CATALOGS, workspacePreferences.getSelectedSlrCatalogs()));
return workspacePreferences;
}

private WorkspacePreferences getWorkspacePreferencesFromLowLevelApi(WorkspacePreferences defaults) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be renamed in followup to "getWorkspacePreferencesFromBackingStore"

return new WorkspacePreferences(
getLanguage(),
getBoolean(OVERRIDE_DEFAULT_FONT_SIZE, defaults.shouldOverrideDefaultFontSize()),
getInt(MAIN_FONT_SIZE, defaults.getMainFontSize()),
defaults.getDefaultFontSize(), // FixMe
new Theme(get(THEME, Theme.BASE_CSS)),
getBoolean(THEME_SYNC_OS, defaults.shouldThemeSyncOs()),
getBoolean(OPEN_LAST_EDITED, defaults.shouldOpenLastEdited()),
getBoolean(SHOW_ADVANCED_HINTS, defaults.shouldShowAdvancedHints()),
getBoolean(CONFIRM_DELETE, defaults.shouldConfirmDelete()),
getBoolean(CONFIRM_HIDE_TAB_BAR, defaults.shouldHideTabBar()),
getStringList(SELECTED_SLR_CATALOGS));
}

@Override
public UnlinkedFilesDialogPreferences getUnlinkedFilesDialogPreferences() {
if (unlinkedFilesDialogPreferences != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class PreferencesDialogView extends BaseDialog<PreferencesDialogViewModel
@FXML private ListView<PreferencesTab> preferenceTabList;
@FXML private ScrollPane preferencesContainer;
@FXML private ButtonType saveButton;
@FXML private ButtonType cancelButton;
@FXML private ToggleButton memoryStickMode;

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

ControlHelper.setAction(saveButton, getDialogPane(), event -> savePreferencesAndCloseDialog());
ControlHelper.setAction(saveButton, getDialogPane(), _ -> savePreferencesAndCloseDialog());

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

@FXML
void importPreferences() {
viewModel.importPreferences();
if (viewModel.importPreferences()) {
// Hint the user that preferences are already loaded into the UI
// ToDo: Import into the ui directly and save changes on click on Save button
this.getDialogPane().lookupButton(cancelButton).setDisable(true);
}
}

@FXML
Expand All @@ -159,6 +164,10 @@ void showAllPreferences() {

@FXML
void resetPreferences() {
viewModel.resetPreferences();
if (viewModel.resetPreferences()) {
// Hint the user that preferences are already loaded into the UI
// ToDo: Reset the ui and save changes on click on Save button
this.getDialogPane().lookupButton(cancelButton).setDisable(true);
}
}
}
Loading
Loading