Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Builder for preferences (with defaults) #671

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
62 changes: 60 additions & 2 deletions src/main/java/org/jabref/preferences/GeneralPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ public GeneralPreferences(BibDatabaseMode defaultBibDatabaseMode,
this.showAdvancedHints = new SimpleBooleanProperty(showAdvancedHints);
}

/**
* Creates Object with default values
*/
public GeneralPreferences() {
this(BibDatabaseMode.BIBTEX,
true,
true,
false,
true
);
}

public void setDefaults() {
GeneralPreferences defaults = new GeneralPreferences();
this.defaultBibDatabaseMode.setValue(defaults.getDefaultBibDatabaseMode());
this.warnAboutDuplicatesInInspection.setValue(defaults.shouldWarnAboutDuplicatesInInspection());
this.confirmDelete.setValue(shouldConfirmDelete());
this.memoryStickMode.setValue(defaults.isMemoryStickMode());
this.showAdvancedHints.setValue(defaults.shouldShowAdvancedHints());
}

public BibDatabaseMode getDefaultBibDatabaseMode() {
return defaultBibDatabaseMode.get();
}
Expand All @@ -40,11 +61,11 @@ public void setDefaultBibDatabaseMode(BibDatabaseMode defaultBibDatabaseMode) {
this.defaultBibDatabaseMode.set(defaultBibDatabaseMode);
}

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

public BooleanProperty isWarnAboutDuplicatesInInspectionProperty() {
public BooleanProperty warnAboutDuplicatesInInspectionProperty() {
return warnAboutDuplicatesInInspection;
}

Expand Down Expand Up @@ -87,4 +108,41 @@ public BooleanProperty showAdvancedHintsProperty() {
public void setShowAdvancedHints(boolean showAdvancedHints) {
this.showAdvancedHints.set(showAdvancedHints);
}

public static class Builder {
private final GeneralPreferences toBuild;

public Builder() {
toBuild = new GeneralPreferences();
}

public Builder withDefaultBibDatabaseMode(BibDatabaseMode defaultBibDatabaseMode) {
toBuild.setDefaultBibDatabaseMode(defaultBibDatabaseMode);
return this;
}

public Builder withWarnAboutDuplicatesInInspection(boolean warnAboutDuplicatesInInspection) {
toBuild.setWarnAboutDuplicatesInInspection(warnAboutDuplicatesInInspection);
return this;
}

public Builder withConfirmDelete(boolean confirmDelete) {
toBuild.setConfirmDelete(confirmDelete);
return this;
}

public Builder withMemoryStickMode(boolean memoryStickMode) {
toBuild.setMemoryStickMode(memoryStickMode);
return this;
}

public Builder withShowAdvancedHints(boolean showAdvancedHints) {
toBuild.setShowAdvancedHints(showAdvancedHints);
return this;
}

public GeneralPreferences build() {
return toBuild;
}
}
}
25 changes: 11 additions & 14 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,6 @@ private JabRefPreferences() {
defaults.put(PUSH_VIM_SERVER, "vim");
defaults.put(PUSH_EMACS_ADDITIONAL_PARAMETERS, "-n -e");

defaults.put(BIBLATEX_DEFAULT_MODE, Boolean.FALSE);

// Set DOI to be the default ID entry generator
defaults.put(ID_ENTRY_GENERATOR, DoiFetcher.NAME);

Expand Down Expand Up @@ -601,10 +599,7 @@ private JabRefPreferences() {
defaults.put(DISPLAY_GROUP_COUNT, Boolean.TRUE);
defaults.put(GROUP_INTERSECT_UNION_VIEW_MODE, GroupViewMode.INTERSECTION.name());
defaults.put(KEYWORD_SEPARATOR, ", ");
defaults.put(DEFAULT_ENCODING, StandardCharsets.UTF_8.name());
defaults.put(DEFAULT_OWNER, System.getProperty("user.name"));
defaults.put(MEMORY_STICK_MODE, Boolean.FALSE);
defaults.put(SHOW_ADVANCED_HINTS, Boolean.TRUE);

defaults.put(EXTRA_FILE_COLUMNS, Boolean.FALSE);

Expand Down Expand Up @@ -637,13 +632,11 @@ private JabRefPreferences() {
defaults.put(OVERWRITE_OWNER, Boolean.FALSE);
defaults.put(AVOID_OVERWRITING_KEY, Boolean.FALSE);
defaults.put(WARN_BEFORE_OVERWRITING_KEY, Boolean.TRUE);
defaults.put(CONFIRM_DELETE, Boolean.TRUE);
defaults.put(DEFAULT_CITATION_KEY_PATTERN, "[auth][year]");
defaults.put(UNWANTED_CITATION_KEY_CHARACTERS, "-`ʹ:!;?^+");
defaults.put(RESOLVE_STRINGS_FOR_FIELDS, "author;booktitle;editor;editora;editorb;editorc;institution;issuetitle;journal;journalsubtitle;journaltitle;mainsubtitle;month;publisher;shortauthor;shorteditor;subtitle;titleaddon");
defaults.put(DO_NOT_RESOLVE_STRINGS, Boolean.FALSE);
defaults.put(NON_WRAPPABLE_FIELDS, "pdf;ps;url;doi;file;isbn;issn");
defaults.put(WARN_ABOUT_DUPLICATES_IN_INSPECTION, Boolean.TRUE);
defaults.put(ADD_CREATION_DATE, Boolean.FALSE);
defaults.put(ADD_MODIFICATION_DATE, Boolean.FALSE);

Expand Down Expand Up @@ -959,6 +952,9 @@ public void clear() throws BackingStoreException {
clearCitationKeyPatterns();
clearTruststoreFromCustomCertificates();
prefs.clear();

generalPreferences.setDefaults();

new SharedDatabasePreferences().clear();
}

Expand Down Expand Up @@ -1309,15 +1305,16 @@ public GeneralPreferences getGeneralPreferences() {
return generalPreferences;
}

generalPreferences = new GeneralPreferences(
getBoolean(BIBLATEX_DEFAULT_MODE) ? BibDatabaseMode.BIBLATEX : BibDatabaseMode.BIBTEX,
getBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION),
getBoolean(CONFIRM_DELETE),
getBoolean(MEMORY_STICK_MODE),
getBoolean(SHOW_ADVANCED_HINTS));
generalPreferences = new GeneralPreferences.Builder()
.withDefaultBibDatabaseMode(getBoolean(BIBLATEX_DEFAULT_MODE) ? BibDatabaseMode.BIBLATEX : BibDatabaseMode.BIBTEX)
.withWarnAboutDuplicatesInInspection(getBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION))
.withConfirmDelete(getBoolean(CONFIRM_DELETE))
.withMemoryStickMode(getBoolean(MEMORY_STICK_MODE))
.withShowAdvancedHints(getBoolean(SHOW_ADVANCED_HINTS))
.build();

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.warnAboutDuplicatesInInspectionProperty(), (obs, oldValue, newValue) -> putBoolean(WARN_ABOUT_DUPLICATES_IN_INSPECTION, newValue));
EasyBind.listen(generalPreferences.confirmDeleteProperty(), (obs, oldValue, newValue) -> putBoolean(CONFIRM_DELETE, newValue));
EasyBind.listen(generalPreferences.memoryStickModeProperty(), (obs, oldValue, newValue) -> putBoolean(MEMORY_STICK_MODE, newValue));
EasyBind.listen(generalPreferences.showAdvancedHintsProperty(), (obs, oldValue, newValue) -> putBoolean(SHOW_ADVANCED_HINTS, newValue));
Expand Down