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

Observable Preferences R (CitationKeyPatternPreferences) #9486

Merged
merged 4 commits into from
Dec 20, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ public static boolean confirmOverwriteKeys(DialogService dialogService, Preferen
Localization.lang("Overwrite keys"),
Localization.lang("Cancel"),
Localization.lang("Do not ask again"),
optOut -> preferencesService.storeCitationKeyPatternPreferences(
preferencesService.getCitationKeyPatternPreferences().withWarnBeforeOverwriteCiteKey(!optOut)));
optOut -> preferencesService.getCitationKeyPatternPreferences().setWarnBeforeOverwriteCiteKey(!optOut));
} else {
// Always overwrite keys by default
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public CitationKeyPatternPanel() {

@FXML
private void initialize() {
viewModel = new CitationKeyPatternPanelViewModel(preferences);
viewModel = new CitationKeyPatternPanelViewModel(preferences.getCitationKeyPatternPreferences());

this.setEditable(true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
import javafx.collections.FXCollections;

import org.jabref.logic.citationkeypattern.AbstractCitationKeyPattern;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntryType;
import org.jabref.model.entry.types.EntryType;
import org.jabref.preferences.PreferencesService;

public class CitationKeyPatternPanelViewModel {

Expand All @@ -37,10 +37,10 @@ public class CitationKeyPatternPanelViewModel {
private final ListProperty<CitationKeyPatternPanelItemModel> patternListProperty = new SimpleListProperty<>();
private final ObjectProperty<CitationKeyPatternPanelItemModel> defaultItemProperty = new SimpleObjectProperty<>();

private final PreferencesService preferences;
private final CitationKeyPatternPreferences keyPatternPreferences;

public CitationKeyPatternPanelViewModel(PreferencesService preferences) {
this.preferences = preferences;
public CitationKeyPatternPanelViewModel(CitationKeyPatternPreferences keyPatternPreferences) {
this.keyPatternPreferences = keyPatternPreferences;
}

public void setValues(Collection<BibEntryType> entryTypeList, AbstractCitationKeyPattern initialKeyPattern) {
Expand Down Expand Up @@ -69,12 +69,12 @@ public void setValues(Collection<BibEntryType> entryTypeList, AbstractCitationKe
}

public void setItemToDefaultPattern(CitationKeyPatternPanelItemModel item) {
item.setPattern(preferences.getDefaultsDefaultCitationKeyPattern());
item.setPattern(keyPatternPreferences.getDefaultPattern());
}

public void resetAll() {
patternListProperty.forEach(item -> item.setPattern(""));
defaultItemProperty.getValue().setPattern(preferences.getDefaultsDefaultCitationKeyPattern());
defaultItemProperty.getValue().setPattern(keyPatternPreferences.getDefaultPattern());
}

public ListProperty<CitationKeyPatternPanelItemModel> patternListProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void setValues() {
bibEntryTypesManager.getAllTypes(databaseContext.getMetaData().getMode()
.orElse(preferencesService.getGeneralPreferences()
.getDefaultBibDatabaseMode())),
databaseContext.getMetaData().getCiteKeyPattern(preferencesService.getGlobalCitationKeyPattern()));
databaseContext.getMetaData().getCiteKeyPattern(preferencesService.getCitationKeyPatternPreferences().getKeyPattern()));
}

@FXML
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ public void resetPreferences() {
*/
private void updateAfterPreferenceChanges() {
// Reload internal preferences cache
preferences.updateGlobalCitationKeyPattern();
preferences.updateMainTableColumns();

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

public void initialize() {
this.viewModel = new CitationKeyPatternTabViewModel(dialogService, preferencesService);
this.viewModel = new CitationKeyPatternTabViewModel(preferencesService.getCitationKeyPatternPreferences());

overwriteAllow.selectedProperty().bindBidirectional(viewModel.overwriteAllowProperty());
overwriteWarning.selectedProperty().bindBidirectional(viewModel.overwriteWarningProperty());
Expand All @@ -68,7 +68,7 @@ public void setValues() {
viewModel.setValues();
bibtexKeyPatternTable.setValues(
Globals.entryTypesManager.getAllTypes(preferencesService.getGeneralPreferences().getDefaultBibDatabaseMode()),
preferencesService.getGlobalCitationKeyPattern());
preferencesService.getCitationKeyPatternPreferences().getKeyPattern());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;

import org.jabref.gui.DialogService;
import org.jabref.gui.commonfxcontrols.CitationKeyPatternPanelItemModel;
import org.jabref.gui.commonfxcontrols.CitationKeyPatternPanelViewModel;
import org.jabref.gui.preferences.PreferenceTabViewModel;
import org.jabref.logic.citationkeypattern.CitationKeyPatternPreferences;
import org.jabref.logic.citationkeypattern.GlobalCitationKeyPattern;
import org.jabref.preferences.PreferencesService;

public class CitationKeyPatternTabViewModel implements PreferenceTabViewModel {

Expand All @@ -36,28 +34,24 @@ public class CitationKeyPatternTabViewModel implements PreferenceTabViewModel {
private final ObjectProperty<CitationKeyPatternPanelItemModel> defaultKeyPatternProperty = new SimpleObjectProperty<>(
new CitationKeyPatternPanelItemModel(new CitationKeyPatternPanelViewModel.DefaultEntryType(), ""));

private final DialogService dialogService;
private final PreferencesService preferences;
private final CitationKeyPatternPreferences initialCitationKeyPatternPreferences;
private final CitationKeyPatternPreferences keyPatternPreferences;

public CitationKeyPatternTabViewModel(DialogService dialogService, PreferencesService preferences) {
this.dialogService = dialogService;
this.preferences = preferences;
this.initialCitationKeyPatternPreferences = preferences.getCitationKeyPatternPreferences();
public CitationKeyPatternTabViewModel(CitationKeyPatternPreferences keyPatternPreferences) {
this.keyPatternPreferences = keyPatternPreferences;
}

@Override
public void setValues() {
overwriteAllowProperty.setValue(!initialCitationKeyPatternPreferences.shouldAvoidOverwriteCiteKey());
overwriteWarningProperty.setValue(initialCitationKeyPatternPreferences.shouldWarnBeforeOverwriteCiteKey());
generateOnSaveProperty.setValue(initialCitationKeyPatternPreferences.shouldGenerateCiteKeysBeforeSaving());
overwriteAllowProperty.setValue(!keyPatternPreferences.shouldAvoidOverwriteCiteKey());
overwriteWarningProperty.setValue(keyPatternPreferences.shouldWarnBeforeOverwriteCiteKey());
generateOnSaveProperty.setValue(keyPatternPreferences.shouldGenerateCiteKeysBeforeSaving());

if (initialCitationKeyPatternPreferences.getKeySuffix()
if (keyPatternPreferences.getKeySuffix()
== CitationKeyPatternPreferences.KeySuffix.ALWAYS) {
letterAlwaysAddProperty.setValue(true);
letterStartAProperty.setValue(false);
letterStartBProperty.setValue(false);
} else if (initialCitationKeyPatternPreferences.getKeySuffix()
} else if (keyPatternPreferences.getKeySuffix()
== CitationKeyPatternPreferences.KeySuffix.SECOND_WITH_A) {
letterAlwaysAddProperty.setValue(false);
letterStartAProperty.setValue(true);
Expand All @@ -68,15 +62,15 @@ public void setValues() {
letterStartBProperty.setValue(true);
}

keyPatternRegexProperty.setValue(initialCitationKeyPatternPreferences.getKeyPatternRegex());
keyPatternReplacementProperty.setValue(initialCitationKeyPatternPreferences.getKeyPatternReplacement());
unwantedCharactersProperty.setValue(initialCitationKeyPatternPreferences.getUnwantedCharacters());
keyPatternRegexProperty.setValue(keyPatternPreferences.getKeyPatternRegex());
keyPatternReplacementProperty.setValue(keyPatternPreferences.getKeyPatternReplacement());
unwantedCharactersProperty.setValue(keyPatternPreferences.getUnwantedCharacters());
}

@Override
public void storeSettings() {
GlobalCitationKeyPattern newKeyPattern =
new GlobalCitationKeyPattern(initialCitationKeyPatternPreferences.getKeyPattern().getDefaultValue());
new GlobalCitationKeyPattern(keyPatternPreferences.getKeyPattern().getDefaultValue());
patternListProperty.forEach(item -> {
String patternString = item.getPattern();
if (!item.getEntryType().getName().equals("default")) {
Expand All @@ -100,16 +94,14 @@ public void storeSettings() {
keySuffix = CitationKeyPatternPreferences.KeySuffix.SECOND_WITH_B;
}

preferences.storeCitationKeyPatternPreferences(new CitationKeyPatternPreferences(
!overwriteAllowProperty.getValue(),
overwriteWarningProperty.getValue(),
generateOnSaveProperty.getValue(),
keySuffix,
keyPatternRegexProperty.getValue(),
keyPatternReplacementProperty.getValue(),
unwantedCharactersProperty.getValue(),
newKeyPattern,
initialCitationKeyPatternPreferences.getKeywordDelimiter()));
keyPatternPreferences.setAvoidOverwriteCiteKey(!overwriteAllowProperty.getValue());
keyPatternPreferences.setWarnBeforeOverwriteCiteKey(overwriteWarningProperty.getValue());
keyPatternPreferences.setGenerateCiteKeysBeforeSaving(generateOnSaveProperty.getValue());
keyPatternPreferences.setKeySuffix(keySuffix);
keyPatternPreferences.setKeyPatternRegex(keyPatternRegexProperty.getValue());
keyPatternPreferences.setKeyPatternReplacement(keyPatternReplacementProperty.getValue());
keyPatternPreferences.setUnwantedCharacters(unwantedCharactersProperty.getValue());
keyPatternPreferences.setKeyPattern(newKeyPattern);
}

public BooleanProperty overwriteAllowProperty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public LibraryTab openNewSharedDatabaseTab(DBMSConnectionProperties dbmsConnecti
DBMSSynchronizer synchronizer = new DBMSSynchronizer(
bibDatabaseContext,
preferencesService.getBibEntryPreferences().getKeywordSeparator(),
preferencesService.getGlobalCitationKeyPattern(),
preferencesService.getCitationKeyPatternPreferences().getKeyPattern(),
Globals.getFileUpdateMonitor());
bibDatabaseContext.convertToSharedDatabase(synchronizer);

Expand Down Expand Up @@ -169,7 +169,7 @@ public void openSharedDatabaseFromParserResult(ParserResult parserResult)
DBMSSynchronizer synchronizer = new DBMSSynchronizer(
bibDatabaseContext,
preferencesService.getBibEntryPreferences().getKeywordSeparator(),
preferencesService.getGlobalCitationKeyPattern(),
preferencesService.getCitationKeyPatternPreferences().getKeyPattern(),
Globals.getFileUpdateMonitor());
bibDatabaseContext.convertToSharedDatabase(synchronizer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public EnumSet<Difference> getDifferences(PreferencesService preferences) {
if (!Objects.equals(originalMetaData.getSaveOrderConfig(), newMetaData.getSaveOrderConfig())) {
changes.add(Difference.SAVE_SORT_ORDER);
}
if (!Objects.equals(originalMetaData.getCiteKeyPattern(preferences.getGlobalCitationKeyPattern()), newMetaData.getCiteKeyPattern(preferences.getGlobalCitationKeyPattern()))) {
if (!Objects.equals(
originalMetaData.getCiteKeyPattern(preferences.getCitationKeyPatternPreferences().getKeyPattern()),
newMetaData.getCiteKeyPattern(preferences.getCitationKeyPatternPreferences().getKeyPattern()))) {
changes.add(Difference.KEY_PATTERNS);
}
if (!Objects.equals(originalMetaData.getUserFileDirectories(), newMetaData.getUserFileDirectories())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.logic.citationkeypattern;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -48,29 +47,6 @@ public CitationKeyGenerator(AbstractCitationKeyPattern citeKeyPattern, BibDataba
this.unwantedCharacters = citationKeyPatternPreferences.getUnwantedCharacters();
}

@Deprecated
static String generateKey(BibEntry entry, String pattern) {
return generateKey(entry, pattern, new BibDatabase());
}

@Deprecated
static String generateKey(BibEntry entry, String pattern, BibDatabase database) {
GlobalCitationKeyPattern keyPattern = new GlobalCitationKeyPattern(Collections.emptyList());
keyPattern.setDefaultValue("[" + pattern + "]");
CitationKeyPatternPreferences patternPreferences = new CitationKeyPatternPreferences(
false,
false,
false,
CitationKeyPatternPreferences.KeySuffix.SECOND_WITH_A,
"",
"",
DEFAULT_UNWANTED_CHARACTERS,
keyPattern,
',');

return new CitationKeyGenerator(keyPattern, database, patternPreferences).generateKey(entry);
}

/**
* Computes an appendix to a citation key that could make it unique. We use a-z for numbers 0-25, and then aa-az, ba-bz, etc.
*
Expand Down
Loading