Skip to content

Commit

Permalink
Observable Preferences C (General) (#8047)
Browse files Browse the repository at this point in the history
  • Loading branch information
btut authored Sep 1, 2021
1 parent 18f083b commit ac84e5d
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 148 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ private void showTrackingNotification() {
Localization.lang("Don't share"));
}

prefs.storeTelemetryPreferences(telemetryPreferences.withCollectTelemetry(shouldCollect)
.withAskToCollectTelemetry(false));
telemetryPreferences.setCollectTelemetry(shouldCollect);
telemetryPreferences.setAskToCollectTelemetry(false);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -665,8 +665,7 @@ private boolean showDeleteConfirmationDialog(int numberOfEntries) {
okButton,
cancelButton,
Localization.lang("Do not ask again"),
optOut -> preferencesService.storeGeneralPreferences(
preferencesService.getGeneralPreferences().withConfirmDelete(!optOut)));
optOut -> preferencesService.getGeneralPreferences().setConfirmDelete(!optOut));
} else {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String getTabName() {
}

public void initialize() {
this.viewModel = new GeneralTabViewModel(dialogService, preferencesService);
this.viewModel = new GeneralTabViewModel(dialogService, preferencesService, preferencesService.getGeneralPreferences(), preferencesService.getTelemetryPreferences(), preferencesService.getOwnerPreferences(), preferencesService.getTimestampPreferences());

new ViewModelListCellFactory<Language>()
.withText(Language::getDisplayName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,46 @@ public class GeneralTabViewModel implements PreferenceTabViewModel {
private final BooleanProperty addModificationDateProperty = new SimpleBooleanProperty();

private final DialogService dialogService;
private final GeneralPreferences generalPreferences;
private final PreferencesService preferencesService;
private final GeneralPreferences initialGeneralPreferences;
private final TelemetryPreferences initialTelemetryPreferences;
private final OwnerPreferences initialOwnerPreferences;
private final TimestampPreferences initialTimestampPreferences;
private final TelemetryPreferences telemetryPreferences;
private final OwnerPreferences ownerPreferences;
private final TimestampPreferences timestampPreferences;

private List<String> restartWarning = new ArrayList<>();

@SuppressWarnings("ReturnValueIgnored")
public GeneralTabViewModel(DialogService dialogService, PreferencesService preferencesService) {
public GeneralTabViewModel(DialogService dialogService, PreferencesService preferencesService, GeneralPreferences generalPreferences, TelemetryPreferences telemetryPreferences, OwnerPreferences ownerPreferences, TimestampPreferences timestampPreferences) {
this.dialogService = dialogService;
this.generalPreferences = generalPreferences;
this.preferencesService = preferencesService;
this.initialGeneralPreferences = preferencesService.getGeneralPreferences();
this.initialTelemetryPreferences = preferencesService.getTelemetryPreferences();
this.initialOwnerPreferences = preferencesService.getOwnerPreferences();
this.initialTimestampPreferences = preferencesService.getTimestampPreferences();
this.telemetryPreferences = telemetryPreferences;
this.ownerPreferences = ownerPreferences;
this.timestampPreferences = timestampPreferences;
}

public void setValues() {
languagesListProperty.setValue(new SortedList<>(FXCollections.observableArrayList(Language.values()), Comparator.comparing(Language::getDisplayName)));
selectedLanguageProperty.setValue(preferencesService.getLanguage());

encodingsListProperty.setValue(FXCollections.observableArrayList(Encodings.getCharsets()));
selectedEncodingProperty.setValue(initialGeneralPreferences.getDefaultEncoding());
selectedEncodingProperty.setValue(generalPreferences.getDefaultEncoding());

bibliographyModeListProperty.setValue(FXCollections.observableArrayList(BibDatabaseMode.values()));
selectedBiblatexModeProperty.setValue(initialGeneralPreferences.getDefaultBibDatabaseMode());
selectedBiblatexModeProperty.setValue(generalPreferences.getDefaultBibDatabaseMode());

inspectionWarningDuplicateProperty.setValue(initialGeneralPreferences.isWarnAboutDuplicatesInInspection());
confirmDeleteProperty.setValue(initialGeneralPreferences.shouldConfirmDelete());
memoryStickModeProperty.setValue(initialGeneralPreferences.isMemoryStickMode());
collectTelemetryProperty.setValue(initialTelemetryPreferences.shouldCollectTelemetry());
showAdvancedHintsProperty.setValue(initialGeneralPreferences.shouldShowAdvancedHints());
inspectionWarningDuplicateProperty.setValue(generalPreferences.warnAboutDuplicatesInInspection());
confirmDeleteProperty.setValue(generalPreferences.shouldConfirmDelete());
memoryStickModeProperty.setValue(generalPreferences.isMemoryStickMode());
collectTelemetryProperty.setValue(telemetryPreferences.shouldCollectTelemetry());
showAdvancedHintsProperty.setValue(generalPreferences.shouldShowAdvancedHints());

markOwnerProperty.setValue(initialOwnerPreferences.isUseOwner());
markOwnerNameProperty.setValue(initialOwnerPreferences.getDefaultOwner());
markOwnerOverwriteProperty.setValue(initialOwnerPreferences.isOverwriteOwner());
markOwnerProperty.setValue(ownerPreferences.isUseOwner());
markOwnerNameProperty.setValue(ownerPreferences.getDefaultOwner());
markOwnerOverwriteProperty.setValue(ownerPreferences.isOverwriteOwner());

addCreationDateProperty.setValue(initialTimestampPreferences.shouldAddCreationDate());
addModificationDateProperty.setValue(initialTimestampPreferences.shouldAddModificationDate());
addCreationDateProperty.setValue(timestampPreferences.shouldAddCreationDate());
addModificationDateProperty.setValue(timestampPreferences.shouldAddModificationDate());
}

public void storeSettings() {
Expand All @@ -98,34 +98,27 @@ public void storeSettings() {
restartWarning.add(Localization.lang("Changed language") + ": " + newLanguage.getDisplayName());
}

if (initialGeneralPreferences.isMemoryStickMode() && !memoryStickModeProperty.getValue()) {
if (generalPreferences.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."));
}

preferencesService.storeGeneralPreferences(new GeneralPreferences(
selectedEncodingProperty.getValue(),
selectedBiblatexModeProperty.getValue(),
inspectionWarningDuplicateProperty.getValue(),
confirmDeleteProperty.getValue(),
memoryStickModeProperty.getValue(),
showAdvancedHintsProperty.getValue()));

preferencesService.storeTelemetryPreferences(
initialTelemetryPreferences.withCollectTelemetry(collectTelemetryProperty.getValue()));

preferencesService.storeOwnerPreferences(new OwnerPreferences(
markOwnerProperty.getValue(),
markOwnerNameProperty.getValue().trim(),
markOwnerOverwriteProperty.getValue()));

preferencesService.storeTimestampPreferences(new TimestampPreferences(
addCreationDateProperty.getValue(),
addModificationDateProperty.getValue(),
initialTimestampPreferences.shouldUpdateTimestamp(),
initialTimestampPreferences.getTimestampField(),
initialTimestampPreferences.getTimestampFormat()));
generalPreferences.setDefaultEncoding(selectedEncodingProperty.getValue());
generalPreferences.setDefaultBibDatabaseMode(selectedBiblatexModeProperty.getValue());
generalPreferences.setWarnAboutDuplicatesInInspection(inspectionWarningDuplicateProperty.getValue());
generalPreferences.setConfirmDelete(confirmDeleteProperty.getValue());
generalPreferences.setMemoryStickMode(memoryStickModeProperty.getValue());
generalPreferences.setShowAdvancedHints(showAdvancedHintsProperty.getValue());

telemetryPreferences.setCollectTelemetry(collectTelemetryProperty.getValue());

ownerPreferences.setUseOwner(markOwnerProperty.getValue());
ownerPreferences.setDefaultOwner(markOwnerNameProperty.getValue());
ownerPreferences.setOverwriteOwner(markOwnerOverwriteProperty.getValue());

timestampPreferences.setAddCreationDate(addCreationDateProperty.getValue());
timestampPreferences.setAddModificationDate(addModificationDateProperty.getValue());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public String getTabName() {
}

public void initialize() {
this.viewModel = new ImportExportTabViewModel(preferencesService);
this.viewModel = new ImportExportTabViewModel(preferencesService, preferencesService.getDOIPreferences());

useCustomDOI.selectedProperty().bindBidirectional(viewModel.useCustomDOIProperty());
useCustomDOIName.textProperty().bindBidirectional(viewModel.useCustomDOINameProperty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ public class ImportExportTabViewModel implements PreferenceTabViewModel {
private final StringProperty grobidURLProperty = new SimpleStringProperty("");

private final PreferencesService preferencesService;
private final DOIPreferences initialDOIPreferences;
private final DOIPreferences doiPreferences;
private final ImporterPreferences importerPreferences;
private final SaveOrderConfig initialExportOrder;

public ImportExportTabViewModel(PreferencesService preferencesService) {
public ImportExportTabViewModel(PreferencesService preferencesService, DOIPreferences doiPreferences) {
this.preferencesService = preferencesService;
this.importerPreferences = preferencesService.getImporterPreferences();
this.initialDOIPreferences = preferencesService.getDOIPreferences();
this.doiPreferences = doiPreferences;
this.initialExportOrder = preferencesService.getExportSaveOrder();
}

@Override
public void setValues() {
generateKeyOnImportProperty.setValue(importerPreferences.isGenerateNewKeyOnImport());
useCustomDOIProperty.setValue(initialDOIPreferences.isUseCustom());
useCustomDOINameProperty.setValue(initialDOIPreferences.getDefaultBaseURI());
useCustomDOIProperty.setValue(doiPreferences.isUseCustom());
useCustomDOINameProperty.setValue(doiPreferences.getDefaultBaseURI());

switch (initialExportOrder.getOrderType()) {
case SPECIFIED -> exportInSpecifiedOrderProperty.setValue(true);
Expand All @@ -82,15 +82,13 @@ public void storeSettings() {
importerPreferences.setGrobidOptOut(importerPreferences.isGrobidOptOut());
importerPreferences.setGrobidURL(grobidURLProperty.getValue());

preferencesService.storeDOIPreferences(new DOIPreferences(
useCustomDOIProperty.getValue(),
useCustomDOINameProperty.getValue().trim()));
doiPreferences.setUseCustom(useCustomDOIProperty.get());
doiPreferences.setDefaultBaseURI(useCustomDOINameProperty.getValue().trim());

SaveOrderConfig newSaveOrderConfig = new SaveOrderConfig(
SaveOrderConfig.OrderType.fromBooleans(exportInSpecifiedOrderProperty.getValue(), exportInTableOrderProperty.getValue()),
sortCriteriaProperty.stream().map(SortCriterionViewModel::getCriterion).toList());
preferencesService.storeExportSaveOrder(newSaveOrderConfig);

}

public BooleanProperty generateKeyOnImportProperty() {
Expand Down
29 changes: 25 additions & 4 deletions src/main/java/org/jabref/logic/preferences/DOIPreferences.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,40 @@
package org.jabref.logic.preferences;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class DOIPreferences {
private final boolean useCustom;
private final String defaultBaseURI;
private BooleanProperty useCustom;
private final StringProperty defaultBaseURI;

public DOIPreferences(boolean useCustom, String defaultBaseURI) {
this.useCustom = useCustom;
this.defaultBaseURI = defaultBaseURI;
this.useCustom = new SimpleBooleanProperty(useCustom);
this.defaultBaseURI = new SimpleStringProperty(defaultBaseURI);
}

public boolean isUseCustom() {
return useCustom.get();
}

public BooleanProperty useCustomProperty() {
return useCustom;
}

public void setUseCustom(boolean useCustom) {
this.useCustom.set(useCustom);
}

public String getDefaultBaseURI() {
return defaultBaseURI.get();
}

public StringProperty defaultBaseURIProperty() {
return defaultBaseURI;
}

public void setDefaultBaseURI(String defaultBaseURI) {
this.defaultBaseURI.set(defaultBaseURI);
}
}
41 changes: 35 additions & 6 deletions src/main/java/org/jabref/logic/preferences/OwnerPreferences.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
package org.jabref.logic.preferences;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class OwnerPreferences {
private final boolean useOwner;
private final String defaultOwner;
private final boolean overwriteOwner;
private final BooleanProperty useOwner;
private final StringProperty defaultOwner;
private final BooleanProperty overwriteOwner;

public OwnerPreferences(boolean useOwner,
String defaultOwner,
boolean overwriteOwner) {
this.useOwner = useOwner;
this.defaultOwner = defaultOwner;
this.overwriteOwner = overwriteOwner;
this.useOwner = new SimpleBooleanProperty(useOwner);
this.defaultOwner = new SimpleStringProperty(defaultOwner);
this.overwriteOwner = new SimpleBooleanProperty(overwriteOwner);
}

public boolean isUseOwner() {
return useOwner.getValue();
}

public BooleanProperty useOwnerProperty() {
return useOwner;
}

public void setUseOwner(boolean useOwner) {
this.useOwner.set(useOwner);
}

public String getDefaultOwner() {
return defaultOwner.getValue();
}

public StringProperty defaultOwnerProperty() {
return defaultOwner;
}

public void setDefaultOwner(String defaultOwner) {
this.defaultOwner.set(defaultOwner);
}

public boolean isOverwriteOwner() {
return overwriteOwner.getValue();
}

public BooleanProperty overwriteOwnerProperty() {
return overwriteOwner;
}

public void setOverwriteOwner(boolean overwriteOwner) {
this.overwriteOwner.set(overwriteOwner);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;

import org.jabref.model.entry.field.Field;

public class TimestampPreferences {
private final boolean addCreationDate;
private final boolean addModificationDate;
private final BooleanProperty addCreationDate;
private final BooleanProperty addModificationDate;

// These are old preferences. They are used for migration only.
private final boolean updateTimestamp;
private final Field timestampField;
private final String timestampFormat;

public TimestampPreferences(boolean addCreationDate, boolean modifyTimestamp, boolean updateTimestamp, Field timestampField, String timestampFormat) {
this.addCreationDate = addCreationDate;
this.addModificationDate = modifyTimestamp;
this.addCreationDate = new SimpleBooleanProperty(addCreationDate);
this.addModificationDate = new SimpleBooleanProperty(modifyTimestamp);
this.updateTimestamp = updateTimestamp;
this.timestampField = timestampField;
this.timestampFormat = timestampFormat;
Expand All @@ -29,13 +32,29 @@ public String now() {
}

public boolean shouldAddCreationDate() {
return addCreationDate.get();
}

public BooleanProperty addCreationDateProperty() {
return addCreationDate;
}

public void setAddCreationDate(boolean addCreationDate) {
this.addCreationDate.set(addCreationDate);
}

public boolean shouldAddModificationDate() {
return addModificationDate.get();
}

public BooleanProperty addModificationDateProperty() {
return addModificationDate;
}

public void setAddModificationDate(boolean addModificationDate) {
this.addModificationDate.set(addModificationDate);
}

/**
* Required for migration only.
*/
Expand Down
Loading

0 comments on commit ac84e5d

Please sign in to comment.