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

Oberservable Preferences M (CleanupPreferences) #9126

Merged
merged 14 commits into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from 11 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
21 changes: 10 additions & 11 deletions src/main/java/org/jabref/gui/cleanup/CleanupAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
import org.jabref.gui.undo.NamedCompound;
import org.jabref.gui.undo.UndoableFieldChange;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.logic.cleanup.CleanupPreset;
import org.jabref.logic.cleanup.CleanupWorker;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.preferences.CleanupPreferences;
import org.jabref.preferences.PreferencesService;

public class CleanupAction extends SimpleCommand {
Expand Down Expand Up @@ -58,14 +58,14 @@ public void execute() {

CleanupDialog cleanupDialog = new CleanupDialog(
stateManager.getActiveDatabase().get(),
preferences.getCleanupPreset(),
preferences.getCleanupPreferences(),
preferences.getFilePreferences()
);

Optional<CleanupPreset> chosenPreset = dialogService.showCustomDialogAndWait(cleanupDialog);
Optional<CleanupPreferences> chosenPreset = dialogService.showCustomDialogAndWait(cleanupDialog);

chosenPreset.ifPresent(preset -> {
if (preset.isRenamePDFActive() && preferences.getAutoLinkPreferences().shouldAskAutoNamingPdfs()) {
if (preset.isActive(CleanupPreferences.CleanupStep.RENAME_PDF) && preferences.getAutoLinkPreferences().shouldAskAutoNamingPdfs()) {
boolean confirmed = dialogService.showConfirmationDialogWithOptOutAndWait(Localization.lang("Autogenerate PDF Names"),
Localization.lang("Auto-generating PDF-Names does not support undo. Continue?"),
Localization.lang("Autogenerate PDF Names"),
Expand All @@ -78,7 +78,8 @@ public void execute() {
}
}

preferences.setCleanupPreset(preset);
preferences.getCleanupPreferences().setActiveJobs(preset.getActiveJobs());
preferences.getCleanupPreferences().setFieldFormatterCleanups(preset.getFieldFormatterCleanups());

BackgroundTask.wrap(() -> cleanup(stateManager.getActiveDatabase().get(), preset))
.onSuccess(result -> showResults())
Expand All @@ -89,11 +90,11 @@ public void execute() {
/**
* Runs the cleanup on the entry and records the change.
*/
private void doCleanup(BibDatabaseContext databaseContext, CleanupPreset preset, BibEntry entry, NamedCompound ce) {
private void doCleanup(BibDatabaseContext databaseContext, CleanupPreferences preset, BibEntry entry, NamedCompound ce) {
// Create and run cleaner
CleanupWorker cleaner = new CleanupWorker(
databaseContext,
preferences.getCleanupPreferences(Globals.journalAbbreviationRepository),
preferences.getFilePreferences(),
preferences.getTimestampPreferences());

List<FieldChange> changes = cleaner.cleanup(preset, entry);
Expand Down Expand Up @@ -123,14 +124,12 @@ private void showResults() {
}
}

private void cleanup(BibDatabaseContext databaseContext, CleanupPreset cleanupPreset) {
preferences.setCleanupPreset(cleanupPreset);

private void cleanup(BibDatabaseContext databaseContext, CleanupPreferences cleanupPreferences) {
for (BibEntry entry : stateManager.getSelectedEntries()) {
// undo granularity is on entry level
NamedCompound ce = new NamedCompound(Localization.lang("Cleanup entry"));

doCleanup(databaseContext, cleanupPreset, entry, ce);
doCleanup(databaseContext, cleanupPreferences, entry, ce);

ce.end();
if (ce.hasEdits()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/cleanup/CleanupDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import javafx.scene.control.ButtonType;

import org.jabref.gui.util.BaseDialog;
import org.jabref.logic.cleanup.CleanupPreset;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.preferences.CleanupPreferences;
import org.jabref.preferences.FilePreferences;

public class CleanupDialog extends BaseDialog<CleanupPreset> {
public class CleanupDialog extends BaseDialog<CleanupPreferences> {

public CleanupDialog(BibDatabaseContext databaseContext, CleanupPreset initialPreset, FilePreferences filePreferences) {
public CleanupDialog(BibDatabaseContext databaseContext, CleanupPreferences initialPreset, FilePreferences filePreferences) {
setTitle(Localization.lang("Cleanup entries"));
getDialogPane().setPrefSize(600, 600);
getDialogPane().getButtonTypes().setAll(ButtonType.OK, ButtonType.CANCEL);
Expand Down
75 changes: 37 additions & 38 deletions src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.EnumSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
Expand All @@ -13,11 +12,11 @@
import javafx.scene.layout.VBox;

import org.jabref.gui.commonfxcontrols.FieldFormatterCleanupsPanel;
import org.jabref.logic.cleanup.CleanupPreset;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.CleanupPreferences;
import org.jabref.preferences.FilePreferences;

import com.airhacks.afterburner.views.ViewLoader;
Expand All @@ -40,18 +39,18 @@ public class CleanupPresetPanel extends VBox {
@FXML private CheckBox cleanUpTimestampToModificationDate;
@FXML private FieldFormatterCleanupsPanel formatterCleanupsPanel;

public CleanupPresetPanel(BibDatabaseContext databaseContext, CleanupPreset cleanupPreset, FilePreferences filePreferences) {
public CleanupPresetPanel(BibDatabaseContext databaseContext, CleanupPreferences cleanupPreferences, FilePreferences filePreferences) {
this.databaseContext = Objects.requireNonNull(databaseContext);

// Load FXML
ViewLoader.view(this)
.root(this)
.load();

init(cleanupPreset, filePreferences);
init(cleanupPreferences, filePreferences);
}

private void init(CleanupPreset cleanupPreset, FilePreferences filePreferences) {
private void init(CleanupPreferences cleanupPreferences, FilePreferences filePreferences) {
Optional<Path> firstExistingDir = databaseContext.getFirstExistingFileDir(filePreferences);
if (firstExistingDir.isPresent()) {
cleanUpMovePDF.setText(Localization.lang("Move linked files to default file directory %0", firstExistingDir.get().toString()));
Expand Down Expand Up @@ -95,73 +94,73 @@ private void init(CleanupPreset cleanupPreset, FilePreferences filePreferences)
cleanUpTimestampToCreationDate.selectedProperty().setValue(false);
}
});
updateDisplay(cleanupPreset);
updateDisplay(cleanupPreferences);
}

private void updateDisplay(CleanupPreset preset) {
cleanUpDOI.setSelected(preset.isActive(CleanupPreset.CleanupStep.CLEAN_UP_DOI));
cleanUpEprint.setSelected(preset.isActive(CleanupPreset.CleanupStep.CLEANUP_EPRINT));
private void updateDisplay(CleanupPreferences preset) {
cleanUpDOI.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEAN_UP_DOI));
cleanUpEprint.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEANUP_EPRINT));
if (!cleanUpMovePDF.isDisabled()) {
cleanUpMovePDF.setSelected(preset.isActive(CleanupPreset.CleanupStep.MOVE_PDF));
cleanUpMovePDF.setSelected(preset.isActive(CleanupPreferences.CleanupStep.MOVE_PDF));
}
cleanUpMakePathsRelative.setSelected(preset.isActive(CleanupPreset.CleanupStep.MAKE_PATHS_RELATIVE));
cleanUpRenamePDF.setSelected(preset.isRenamePDFActive());
cleanUpRenamePDFonlyRelativePaths.setSelected(preset.isActive(CleanupPreset.CleanupStep.RENAME_PDF_ONLY_RELATIVE_PATHS));
cleanUpUpgradeExternalLinks.setSelected(preset.isActive(CleanupPreset.CleanupStep.CLEAN_UP_UPGRADE_EXTERNAL_LINKS));
cleanUpBiblatex.setSelected(preset.isActive(CleanupPreset.CleanupStep.CONVERT_TO_BIBLATEX));
cleanUpBibtex.setSelected(preset.isActive(CleanupPreset.CleanupStep.CONVERT_TO_BIBTEX));
cleanUpTimestampToCreationDate.setSelected(preset.isActive(CleanupPreset.CleanupStep.CONVERT_TIMESTAMP_TO_CREATIONDATE));
cleanUpTimestampToModificationDate.setSelected(preset.isActive(CleanupPreset.CleanupStep.CONVERT_TIMESTAMP_TO_MODIFICATIONDATE));
cleanUpTimestampToModificationDate.setSelected(preset.isActive(CleanupPreset.CleanupStep.DO_NOT_CONVERT_TIMESTAMP));
cleanUpISSN.setSelected(preset.isActive(CleanupPreset.CleanupStep.CLEAN_UP_ISSN));
formatterCleanupsPanel.cleanupsDisableProperty().setValue(!preset.getFormatterCleanups().isEnabled());
formatterCleanupsPanel.cleanupsProperty().setValue(FXCollections.observableArrayList(preset.getFormatterCleanups().getConfiguredActions()));
cleanUpMakePathsRelative.setSelected(preset.isActive(CleanupPreferences.CleanupStep.MAKE_PATHS_RELATIVE));
cleanUpRenamePDF.setSelected(preset.isActive(CleanupPreferences.CleanupStep.RENAME_PDF));
cleanUpRenamePDFonlyRelativePaths.setSelected(preset.isActive(CleanupPreferences.CleanupStep.RENAME_PDF_ONLY_RELATIVE_PATHS));
cleanUpUpgradeExternalLinks.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEAN_UP_UPGRADE_EXTERNAL_LINKS));
cleanUpBiblatex.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CONVERT_TO_BIBLATEX));
cleanUpBibtex.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CONVERT_TO_BIBTEX));
cleanUpTimestampToCreationDate.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CONVERT_TIMESTAMP_TO_CREATIONDATE));
cleanUpTimestampToModificationDate.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CONVERT_TIMESTAMP_TO_MODIFICATIONDATE));
cleanUpTimestampToModificationDate.setSelected(preset.isActive(CleanupPreferences.CleanupStep.DO_NOT_CONVERT_TIMESTAMP));
cleanUpISSN.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEAN_UP_ISSN));
formatterCleanupsPanel.cleanupsDisableProperty().setValue(!preset.getFieldFormatterCleanups().isEnabled());
formatterCleanupsPanel.cleanupsProperty().setValue(FXCollections.observableArrayList(preset.getFieldFormatterCleanups().getConfiguredActions()));
}

public CleanupPreset getCleanupPreset() {
Set<CleanupPreset.CleanupStep> activeJobs = EnumSet.noneOf(CleanupPreset.CleanupStep.class);
public CleanupPreferences getCleanupPreset() {
EnumSet<CleanupPreferences.CleanupStep> activeJobs = EnumSet.noneOf(CleanupPreferences.CleanupStep.class);

if (cleanUpMovePDF.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.MOVE_PDF);
activeJobs.add(CleanupPreferences.CleanupStep.MOVE_PDF);
}
if (cleanUpDOI.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CLEAN_UP_DOI);
activeJobs.add(CleanupPreferences.CleanupStep.CLEAN_UP_DOI);
}
if (cleanUpEprint.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CLEANUP_EPRINT);
activeJobs.add(CleanupPreferences.CleanupStep.CLEANUP_EPRINT);
}
if (cleanUpISSN.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CLEAN_UP_ISSN);
activeJobs.add(CleanupPreferences.CleanupStep.CLEAN_UP_ISSN);
}
if (cleanUpMakePathsRelative.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.MAKE_PATHS_RELATIVE);
activeJobs.add(CleanupPreferences.CleanupStep.MAKE_PATHS_RELATIVE);
}
if (cleanUpRenamePDF.isSelected()) {
if (cleanUpRenamePDFonlyRelativePaths.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.RENAME_PDF_ONLY_RELATIVE_PATHS);
activeJobs.add(CleanupPreferences.CleanupStep.RENAME_PDF_ONLY_RELATIVE_PATHS);
} else {
activeJobs.add(CleanupPreset.CleanupStep.RENAME_PDF);
activeJobs.add(CleanupPreferences.CleanupStep.RENAME_PDF);
}
}
if (cleanUpUpgradeExternalLinks.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CLEAN_UP_UPGRADE_EXTERNAL_LINKS);
activeJobs.add(CleanupPreferences.CleanupStep.CLEAN_UP_UPGRADE_EXTERNAL_LINKS);
}
if (cleanUpBiblatex.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CONVERT_TO_BIBLATEX);
activeJobs.add(CleanupPreferences.CleanupStep.CONVERT_TO_BIBLATEX);
}
if (cleanUpBibtex.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CONVERT_TO_BIBTEX);
activeJobs.add(CleanupPreferences.CleanupStep.CONVERT_TO_BIBTEX);
}
if (cleanUpTimestampToCreationDate.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CONVERT_TIMESTAMP_TO_CREATIONDATE);
activeJobs.add(CleanupPreferences.CleanupStep.CONVERT_TIMESTAMP_TO_CREATIONDATE);
}
if (cleanUpTimestampToModificationDate.isSelected()) {
activeJobs.add(CleanupPreset.CleanupStep.CONVERT_TIMESTAMP_TO_MODIFICATIONDATE);
activeJobs.add(CleanupPreferences.CleanupStep.CONVERT_TIMESTAMP_TO_MODIFICATIONDATE);
}

activeJobs.add(CleanupPreset.CleanupStep.FIX_FILE_LINKS);
activeJobs.add(CleanupPreferences.CleanupStep.FIX_FILE_LINKS);

return new CleanupPreset(activeJobs, new FieldFormatterCleanups(
return new CleanupPreferences(activeJobs, new FieldFormatterCleanups(
!formatterCleanupsPanel.cleanupsDisableProperty().getValue(),
formatterCleanupsPanel.cleanupsProperty()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@

import org.jabref.gui.Globals;
import org.jabref.gui.util.NoSelectionModel;
import org.jabref.logic.cleanup.Cleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.Formatter;
import org.jabref.logic.formatter.Formatters;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;

Expand All @@ -27,7 +28,7 @@ public class FieldFormatterCleanupsPanelViewModel {
private final ObjectProperty<SelectionModel<FieldFormatterCleanup>> selectedCleanupProperty = new SimpleObjectProperty<>(new NoSelectionModel<>());
private final ListProperty<Field> availableFieldsProperty = new SimpleListProperty<>(new SortedList<>(FXCollections.observableArrayList(FieldFactory.getCommonFields()), Comparator.comparing(Field::getDisplayName)));
private final ObjectProperty<Field> selectedFieldProperty = new SimpleObjectProperty<>();
private final ListProperty<Formatter> availableFormattersProperty = new SimpleListProperty<>(new SortedList<>(FXCollections.observableArrayList(Cleanups.getBuiltInFormatters()), Comparator.comparing(Formatter::getName)));
private final ListProperty<Formatter> availableFormattersProperty = new SimpleListProperty<>(new SortedList<>(FXCollections.observableArrayList(Formatters.getAll()), Comparator.comparing(Formatter::getName)));
private final ObjectProperty<Formatter> selectedFormatterProperty = new SimpleObjectProperty<>();

public FieldFormatterCleanupsPanelViewModel() {
Expand All @@ -36,9 +37,9 @@ public FieldFormatterCleanupsPanelViewModel() {
public void resetToRecommended() {
Globals.stateManager.getActiveDatabase().ifPresent(databaseContext -> {
if (databaseContext.isBiblatexMode()) {
cleanupsListProperty.setAll(Cleanups.RECOMMEND_BIBLATEX_ACTIONS.getConfiguredActions());
cleanupsListProperty.setAll(FieldFormatterCleanups.RECOMMEND_BIBLATEX_ACTIONS);
} else {
cleanupsListProperty.setAll(Cleanups.RECOMMEND_BIBTEX_ACTIONS.getConfiguredActions());
cleanupsListProperty.setAll(FieldFormatterCleanups.RECOMMEND_BIBTEX_ACTIONS);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javafx.beans.property.BooleanProperty;
import javafx.beans.property.ListProperty;
Expand All @@ -14,7 +13,6 @@

import org.jabref.gui.commonfxcontrols.SortCriterionViewModel;
import org.jabref.gui.libraryproperties.PropertiesTabViewModel;
import org.jabref.logic.cleanup.Cleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.model.database.BibDatabaseContext;
Expand All @@ -23,6 +21,7 @@
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.metadata.MetaData;
import org.jabref.model.metadata.SaveOrderConfig;
import org.jabref.preferences.CleanupPreferences;
import org.jabref.preferences.PreferencesService;

public class SavingPropertiesViewModel implements PropertiesTabViewModel {
Expand All @@ -44,9 +43,11 @@ public class SavingPropertiesViewModel implements PropertiesTabViewModel {
private final BibDatabaseContext databaseContext;
private final MetaData initialMetaData;
private final SaveOrderConfig initialSaveOrderConfig;
private final PreferencesService preferencesService;

public SavingPropertiesViewModel(BibDatabaseContext databaseContext, PreferencesService preferencesService) {
this.databaseContext = databaseContext;
this.preferencesService = preferencesService;
this.initialMetaData = databaseContext.getMetaData();
this.initialSaveOrderConfig = initialMetaData.getSaveOrderConfig().orElseGet(preferencesService::getExportSaveOrder);
}
Expand All @@ -69,7 +70,7 @@ public void setValues() {
sortableFieldsProperty.addAll(fieldNames);
sortCriteriaProperty.addAll(initialSaveOrderConfig.getSortCriteria().stream()
.map(SortCriterionViewModel::new)
.collect(Collectors.toList()));
.toList());

// FieldFormatterCleanupsPanel

Expand All @@ -78,8 +79,9 @@ public void setValues() {
cleanupsDisableProperty.setValue(!value.isEnabled());
cleanupsProperty.setValue(FXCollections.observableArrayList(value.getConfiguredActions()));
}, () -> {
cleanupsDisableProperty.setValue(!Cleanups.DEFAULT_SAVE_ACTIONS.isEnabled());
cleanupsProperty.setValue(FXCollections.observableArrayList(Cleanups.DEFAULT_SAVE_ACTIONS.getConfiguredActions()));
CleanupPreferences defaultPreset = preferencesService.getDefaultCleanupPreset();
cleanupsDisableProperty.setValue(!defaultPreset.getFieldFormatterCleanups().isEnabled());
cleanupsProperty.setValue(FXCollections.observableArrayList(defaultPreset.getFieldFormatterCleanups().getConfiguredActions()));
});
}

Expand All @@ -97,7 +99,7 @@ public void storeSettings() {
!cleanupsDisableProperty().getValue(),
cleanupsProperty());

if (Cleanups.DEFAULT_SAVE_ACTIONS.equals(fieldFormatterCleanups)) {
if (FieldFormatterCleanups.DEFAULT_SAVE_ACTIONS.equals(fieldFormatterCleanups.getConfiguredActions())) {
newMetaData.clearSaveActions();
} else {
// if all actions have been removed, remove the save actions from the MetaData
Expand Down
Loading