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

Refactored general preferences #6171

Merged
merged 3 commits into from
Mar 28, 2020
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
6 changes: 5 additions & 1 deletion src/main/java/org/jabref/gui/BasePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,11 @@ public void insertEntries(final List<BibEntry> entries) {

// Set owner and timestamp
for (BibEntry entry : entries) {
UpdateField.setAutomaticFields(entry, true, true, Globals.prefs.getUpdateFieldPreferences());
UpdateField.setAutomaticFields(entry,
true,
true,
Globals.prefs.getOwnerPreferences(),
Globals.prefs.getTimestampPreferences());
}
// Create an UndoableInsertEntries object.
getUndoManager().addEdit(new UndoableInsertEntries(bibDatabaseContext.getDatabase(), entries));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,25 @@ public class BibtexExtractorViewModel {
private TaskExecutor taskExecutor;
private ImportHandler importHandler;

public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext, DialogService dialogService,
JabRefPreferences jabRefPreferences, FileUpdateMonitor fileUpdateMonitor, TaskExecutor taskExecutor, UndoManager undoManager, StateManager stateManager) {
public BibtexExtractorViewModel(BibDatabaseContext bibdatabaseContext,
DialogService dialogService,
JabRefPreferences jabRefPreferences,
FileUpdateMonitor fileUpdateMonitor,
TaskExecutor taskExecutor,
UndoManager undoManager,
StateManager stateManager) {

this.dialogService = dialogService;
currentCitationfetcher = new GrobidCitationFetcher(jabRefPreferences.getImportFormatPreferences());
this.taskExecutor = taskExecutor;
this.importHandler = new ImportHandler(dialogService, bibdatabaseContext, ExternalFileTypes.getInstance(), jabRefPreferences.getFilePreferences(), jabRefPreferences.getImportFormatPreferences(), jabRefPreferences.getUpdateFieldPreferences(), fileUpdateMonitor, undoManager, stateManager);
this.importHandler = new ImportHandler(
dialogService,
bibdatabaseContext,
ExternalFileTypes.getInstance(),
jabRefPreferences,
fileUpdateMonitor,
undoManager,
stateManager);
}

public StringProperty inputTextProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ public FindUnlinkedFilesDialog(BibDatabaseContext database, DialogService dialog
dialogService,
databaseContext,
ExternalFileTypes.getInstance(),
Globals.prefs.getFilePreferences(),
Globals.prefs.getImportFormatPreferences(),
Globals.prefs.getUpdateFieldPreferences(),
preferences,
Globals.getFileUpdateMonitor(),
undoManager,
Globals.stateManager);
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/jabref/gui/externalfiles/ImportHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,21 @@
import org.jabref.gui.undo.UndoableInsertEntries;
import org.jabref.logic.bibtexkeypattern.BibtexKeyGenerator;
import org.jabref.logic.externalfiles.ExternalFilesContentImporter;
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.logic.util.UpdateField;
import org.jabref.logic.util.UpdateFieldPreferences;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.groups.GroupEntryChanger;
import org.jabref.model.groups.GroupTreeNode;
import org.jabref.model.metadata.FilePreferences;
import org.jabref.model.util.FileUpdateMonitor;
import org.jabref.preferences.PreferencesService;

public class ImportHandler {

private final BibDatabaseContext database;
private final UpdateFieldPreferences updateFieldPreferences;
private final PreferencesService preferencesService;
private final DialogService dialogService;
private final FileUpdateMonitor fileUpdateMonitor;
private final ExternalFilesEntryLinker linker;
Expand All @@ -42,21 +40,20 @@ public class ImportHandler {
public ImportHandler(DialogService dialogService,
BibDatabaseContext database,
ExternalFileTypes externalFileTypes,
FilePreferences filePreferences,
ImportFormatPreferences importFormatPreferences,
UpdateFieldPreferences updateFieldPreferences,
PreferencesService preferencesService,
FileUpdateMonitor fileupdateMonitor,
UndoManager undoManager,
StateManager stateManager) {

this.dialogService = dialogService;
this.database = database;
this.updateFieldPreferences = updateFieldPreferences;

this.preferencesService = preferencesService;
this.fileUpdateMonitor = fileupdateMonitor;
this.stateManager = stateManager;

this.linker = new ExternalFilesEntryLinker(externalFileTypes, filePreferences, database);
this.contentImporter = new ExternalFilesContentImporter(importFormatPreferences);
this.linker = new ExternalFilesEntryLinker(externalFileTypes, preferencesService.getFilePreferences(), database);
this.contentImporter = new ExternalFilesContentImporter(preferencesService.getImportFormatPreferences());
this.undoManager = undoManager;
}

Expand Down Expand Up @@ -114,7 +111,9 @@ public void importEntries(List<BibEntry> entries) {
database.getDatabase().insertEntries(entries);

// Set owner/timestamp
UpdateField.setAutomaticFields(entries, updateFieldPreferences);
UpdateField.setAutomaticFields(entries,
preferencesService.getOwnerPreferences(),
preferencesService.getTimestampPreferences());

// Generate bibtex keys
generateKeys(entries);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/importer/ImportAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ private ParserResult mergeImportResults(List<ImportFormatReader.UnknownFormatImp
anythingUseful = anythingUseful | !entries.isEmpty();

// set timestamp and owner
UpdateField.setAutomaticFields(entries, Globals.prefs.getUpdateFieldPreferences()); // set timestamp and owner
UpdateField.setAutomaticFields(entries, Globals.prefs.getOwnerPreferences(), Globals.prefs.getTimestampPreferences()); // set timestamp and owner

database.insertEntries(entries);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ private void buildImportHandlerThenImportEntries(List<BibEntry> entriesToImport)
dialogService,
database,
ExternalFileTypes.getInstance(),
preferences.getFilePreferences(),
preferences.getImportFormatPreferences(),
preferences.getUpdateFieldPreferences(),
preferences,
fileUpdateMonitor,
undoManager,
stateManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ private static void mergeFromBibtex(BasePanel panel, ParserResult parserResult,

if (importEntries) { // Add entries
boolean overwriteOwner = Globals.prefs.getBoolean(JabRefPreferences.OVERWRITE_OWNER);
boolean overwriteTimeStamp = Globals.prefs.getTimestampPreferences().overwriteTimestamp();
boolean overwriteTimeStamp = Globals.prefs.getTimestampPreferences().isOverwriteTimestamp();

for (BibEntry originalEntry : fromDatabase.getEntries()) {
BibEntry entry = (BibEntry) originalEntry.clone();
UpdateField.setAutomaticFields(entry, overwriteOwner, overwriteTimeStamp,
Globals.prefs.getUpdateFieldPreferences());
UpdateField.setAutomaticFields(entry,
overwriteOwner,
overwriteTimeStamp,
Globals.prefs.getOwnerPreferences(),
Globals.prefs.getTimestampPreferences());
entriesToAppend.add(entry);
}
database.insertEntries(entriesToAppend);
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public MainTable(MainTableDataModel model, JabRefFrame frame,

importHandler = new ImportHandler(
frame.getDialogService(), database, externalFileTypes,
Globals.prefs.getFilePreferences(),
Globals.prefs.getImportFormatPreferences(),
Globals.prefs.getUpdateFieldPreferences(),
Globals.prefs,
Globals.getFileUpdateMonitor(),
undoManager,
Globals.stateManager);
Expand Down
113 changes: 61 additions & 52 deletions src/main/java/org/jabref/gui/preferences/GeneralTabViewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
import org.jabref.logic.l10n.Encodings;
import org.jabref.logic.l10n.Language;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.preferences.OwnerPreferences;
import org.jabref.logic.preferences.TimestampPreferences;
import org.jabref.model.database.BibDatabaseMode;
import org.jabref.preferences.JabRefPreferences;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.preferences.GeneralPreferences;
import org.jabref.preferences.PreferencesService;

import de.saxsys.mvvmfx.utils.validation.FunctionBasedValidator;
import de.saxsys.mvvmfx.utils.validation.ValidationMessage;
Expand Down Expand Up @@ -54,14 +58,20 @@ public class GeneralTabViewModel implements PreferenceTabViewModel {
private Validator markTimeStampFormatValidator;

private final DialogService dialogService;
private final JabRefPreferences preferences;
private final PreferencesService preferencesService;
private final GeneralPreferences initialGeneralPreferences;
private final OwnerPreferences initialOwnerPreferences;
private final TimestampPreferences initialTimestampPreferences;

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

@SuppressWarnings("ReturnValueIgnored")
public GeneralTabViewModel(DialogService dialogService, JabRefPreferences preferences) {
public GeneralTabViewModel(DialogService dialogService, PreferencesService preferencesService) {
this.dialogService = dialogService;
this.preferences = preferences;
this.preferencesService = preferencesService;
this.initialGeneralPreferences = preferencesService.getGeneralPreferences();
this.initialOwnerPreferences = preferencesService.getOwnerPreferences();
this.initialTimestampPreferences = preferencesService.getTimestampPreferences();

markTimeStampFormatValidator = new FunctionBasedValidator<>(
markTimeStampFormatProperty,
Expand All @@ -85,70 +95,69 @@ public GeneralTabViewModel(DialogService dialogService, JabRefPreferences prefer

public void setValues() {
languagesListProperty.setValue(FXCollections.observableArrayList(Language.values()));
selectedLanguageProperty.setValue(preferences.getLanguage());
selectedLanguageProperty.setValue(preferencesService.getLanguage());

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

bibliographyModeListProperty.setValue(FXCollections.observableArrayList(BibDatabaseMode.values()));
if (preferences.getBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE)) {
selectedBiblatexModeProperty.setValue(BibDatabaseMode.BIBLATEX);
} else {
selectedBiblatexModeProperty.setValue(BibDatabaseMode.BIBTEX);
}

inspectionWarningDuplicateProperty.setValue(preferences.getBoolean(JabRefPreferences.WARN_ABOUT_DUPLICATES_IN_INSPECTION));
confirmDeleteProperty.setValue(preferences.getBoolean(JabRefPreferences.CONFIRM_DELETE));
enforceLegalKeysProperty.setValue(preferences.getBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY));
allowIntegerEditionProperty.setValue(preferences.getBoolean(JabRefPreferences.ALLOW_INTEGER_EDITION_BIBTEX));
memoryStickModeProperty.setValue(preferences.getBoolean(JabRefPreferences.MEMORY_STICK_MODE));
collectTelemetryProperty.setValue(preferences.shouldCollectTelemetry());
showAdvancedHintsProperty.setValue(preferences.getBoolean(JabRefPreferences.SHOW_ADVANCED_HINTS));

markOwnerProperty.setValue(preferences.getBoolean(JabRefPreferences.USE_OWNER));
markOwnerNameProperty.setValue(preferences.get(JabRefPreferences.DEFAULT_OWNER));
markOwnerOverwriteProperty.setValue(preferences.getBoolean(JabRefPreferences.OVERWRITE_OWNER));

markTimestampProperty.setValue(preferences.getBoolean(JabRefPreferences.USE_TIME_STAMP));
markTimeStampFormatProperty.setValue(preferences.get(JabRefPreferences.TIME_STAMP_FORMAT));
markTimeStampOverwriteProperty.setValue(preferences.getBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP));
markTimeStampFieldNameProperty.setValue(preferences.get(JabRefPreferences.TIME_STAMP_FIELD));
updateTimeStampProperty.setValue(preferences.getBoolean(JabRefPreferences.UPDATE_TIMESTAMP));
selectedBiblatexModeProperty.setValue(initialGeneralPreferences.getDefaultBibDatabaseMode());

inspectionWarningDuplicateProperty.setValue(initialGeneralPreferences.isWarnAboutDuplicatesInInspection());
confirmDeleteProperty.setValue(initialGeneralPreferences.isConfirmDelete());
enforceLegalKeysProperty.setValue(initialGeneralPreferences.isEnforceLegalBibtexKey());
allowIntegerEditionProperty.setValue(initialGeneralPreferences.isAllowIntegerEditionBibtex());
memoryStickModeProperty.setValue(initialGeneralPreferences.isMemoryStickMode());
collectTelemetryProperty.setValue(preferencesService.shouldCollectTelemetry());
showAdvancedHintsProperty.setValue(initialGeneralPreferences.isShowAdvancedHints());

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

markTimestampProperty.setValue(initialTimestampPreferences.isUseTimestamps());
markTimeStampFormatProperty.setValue(initialTimestampPreferences.getTimestampFormat());
markTimeStampOverwriteProperty.setValue(initialTimestampPreferences.isOverwriteTimestamp());
markTimeStampFieldNameProperty.setValue(initialTimestampPreferences.getTimestampField().getName());
updateTimeStampProperty.setValue(initialTimestampPreferences.isUpdateTimestamp());
}

public void storeSettings() {
Language newLanguage = selectedLanguageProperty.getValue();
if (newLanguage != preferences.getLanguage()) {
preferences.setLanguage(newLanguage);
if (newLanguage != preferencesService.getLanguage()) {
preferencesService.setLanguage(newLanguage);
Localization.setLanguage(newLanguage);
restartWarning.add(Localization.lang("Changed language") + ": " + newLanguage.getDisplayName());
}

preferences.setDefaultEncoding(selectedEncodingProperty.getValue());
preferences.putBoolean(JabRefPreferences.BIBLATEX_DEFAULT_MODE, selectedBiblatexModeProperty.getValue() == BibDatabaseMode.BIBLATEX);

preferences.putBoolean(JabRefPreferences.WARN_ABOUT_DUPLICATES_IN_INSPECTION, inspectionWarningDuplicateProperty.getValue());
preferences.putBoolean(JabRefPreferences.CONFIRM_DELETE, confirmDeleteProperty.getValue());
preferences.putBoolean(JabRefPreferences.ENFORCE_LEGAL_BIBTEX_KEY, enforceLegalKeysProperty.getValue());
preferences.putBoolean(JabRefPreferences.ALLOW_INTEGER_EDITION_BIBTEX, allowIntegerEditionProperty.getValue());
if (preferences.getBoolean(JabRefPreferences.MEMORY_STICK_MODE) && !memoryStickModeProperty.getValue()) {
if (initialGeneralPreferences.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."));
}
preferences.putBoolean(JabRefPreferences.MEMORY_STICK_MODE, memoryStickModeProperty.getValue());
preferences.setShouldCollectTelemetry(collectTelemetryProperty.getValue());
preferences.putBoolean(JabRefPreferences.SHOW_ADVANCED_HINTS, showAdvancedHintsProperty.getValue());

preferences.putBoolean(JabRefPreferences.USE_OWNER, markOwnerProperty.getValue());
preferences.put(JabRefPreferences.DEFAULT_OWNER, markOwnerNameProperty.getValue().trim());
preferences.putBoolean(JabRefPreferences.OVERWRITE_OWNER, markOwnerOverwriteProperty.getValue());

preferences.putBoolean(JabRefPreferences.USE_TIME_STAMP, markTimestampProperty.getValue());
preferences.put(JabRefPreferences.TIME_STAMP_FORMAT, markTimeStampFormatProperty.getValue().trim());
preferences.put(JabRefPreferences.TIME_STAMP_FIELD, markTimeStampFieldNameProperty.getValue().trim());
preferences.putBoolean(JabRefPreferences.OVERWRITE_TIME_STAMP, markTimeStampOverwriteProperty.getValue());
preferences.putBoolean(JabRefPreferences.UPDATE_TIMESTAMP, updateTimeStampProperty.getValue());

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

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

preferencesService.storeTimestampPreferences(new TimestampPreferences(
markTimestampProperty.getValue(),
updateTimeStampProperty.getValue(),
FieldFactory.parseField(markTimeStampFieldNameProperty.getValue().trim()),
markTimeStampFormatProperty.getValue().trim(),
markTimeStampOverwriteProperty.getValue()));
}

public ValidationStatus markTimeStampFormatValidationStatus() {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/org/jabref/logic/preferences/OwnerPreferences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.jabref.logic.preferences;

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

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

public boolean isUseOwner() {
return useOwner;
}

public String getDefaultOwner() {
return defaultOwner;
}

public boolean isOverwriteOwner() {
return overwriteOwner;
}
}
Loading