Skip to content
Open
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
175 changes: 175 additions & 0 deletions jabgui/src/main/java/org/jabref/gui/cleanup/CleanupPresetPanel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
package org.jabref.gui.cleanup;

import java.nio.file.Path;
import java.util.EnumSet;
import java.util.Optional;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;

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

import com.airhacks.afterburner.views.ViewLoader;
import org.jspecify.annotations.NonNull;

public class CleanupPresetPanel extends VBox {

private final BibDatabaseContext databaseContext;
@FXML private Label cleanupRenamePDFLabel;
@FXML private CheckBox cleanUpDOI;
@FXML private CheckBox cleanUpEprint;
@FXML private CheckBox cleanUpURL;
@FXML private CheckBox cleanUpMovePDF;
@FXML private CheckBox cleanUpMakePathsRelative;
@FXML private CheckBox cleanUpRenamePDF;
@FXML private CheckBox cleanUpRenamePDFonlyRelativePaths;
@FXML private CheckBox cleanUpDeletedFiles;
@FXML private CheckBox cleanUpUpgradeExternalLinks;
@FXML private CheckBox cleanUpBiblatex;
@FXML private CheckBox cleanUpBibtex;
@FXML private CheckBox cleanUpTimestampToCreationDate;
@FXML private CheckBox cleanUpTimestampToModificationDate;
@FXML private FieldFormatterCleanupsPanel formatterCleanupsPanel;

public CleanupPresetPanel(@NonNull BibDatabaseContext databaseContext,
CleanupPreferences cleanupPreferences,
FilePreferences filePreferences) {
this.databaseContext = databaseContext;

ViewLoader.view(this)
.root(this)
.load();

init(cleanupPreferences, 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()));
} else {
cleanUpMovePDF.setText(Localization.lang("Move linked files to default file directory %0", "..."));

// Since the directory does not exist, we cannot move it to there. So, this option is not checked - regardless of the presets stored in the preferences.
cleanUpMovePDF.setDisable(true);
cleanUpMovePDF.setSelected(false);
}

cleanUpRenamePDFonlyRelativePaths.disableProperty().bind(cleanUpRenamePDF.selectedProperty().not());

cleanUpUpgradeExternalLinks.setText(Localization.lang("Upgrade external PDF/PS links to use the '%0' field.", FieldTextMapper.getDisplayName(StandardField.FILE)));

String currentPattern = Localization.lang("Filename format pattern (from preferences)")
.concat(filePreferences.getFileNamePattern());
cleanupRenamePDFLabel.setText(currentPattern);

cleanUpBibtex.selectedProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue) {
cleanUpBiblatex.selectedProperty().setValue(false);
}
});
cleanUpBiblatex.selectedProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue) {
cleanUpBibtex.selectedProperty().setValue(false);
}
});

cleanUpTimestampToCreationDate.selectedProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue) {
cleanUpTimestampToModificationDate.selectedProperty().setValue(false);
}
});
cleanUpTimestampToModificationDate.selectedProperty().addListener(
(observable, oldValue, newValue) -> {
if (newValue) {
cleanUpTimestampToCreationDate.selectedProperty().setValue(false);
}
});
updateDisplay(cleanupPreferences);
}

private void updateDisplay(CleanupPreferences preset) {
cleanUpDOI.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEAN_UP_DOI));
cleanUpEprint.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEANUP_EPRINT));
cleanUpURL.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEAN_UP_URL));
if (!cleanUpMovePDF.isDisabled()) {
cleanUpMovePDF.setSelected(preset.isActive(CleanupPreferences.CleanupStep.MOVE_PDF));
}
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));
cleanUpDeletedFiles.setSelected(preset.isActive(CleanupPreferences.CleanupStep.CLEAN_UP_DELETED_LINKED_FILES));
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));
formatterCleanupsPanel.cleanupsDisableProperty().setValue(!preset.getFieldFormatterCleanups().isEnabled());
formatterCleanupsPanel.cleanupsProperty().setValue(FXCollections.observableArrayList(preset.getFieldFormatterCleanups().getConfiguredActions()));
}

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

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

activeJobs.add(CleanupPreferences.CleanupStep.FIX_FILE_LINKS);

return new CleanupPreferences(activeJobs, new FieldFormatterCleanups(
!formatterCleanupsPanel.cleanupsDisableProperty().getValue(),
formatterCleanupsPanel.cleanupsProperty()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jabref.gui.StateManager;
import org.jabref.gui.util.NoSelectionModel;
import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;
import org.jabref.logic.formatter.Formatter;
import org.jabref.logic.formatter.Formatters;
import org.jabref.model.entry.field.Field;
Expand All @@ -41,9 +41,9 @@ public FieldFormatterCleanupsPanelViewModel(StateManager stateManager) {
public void resetToRecommended() {
stateManager.getActiveDatabase().ifPresent(databaseContext -> {
if (databaseContext.isBiblatexMode()) {
cleanupsListProperty.setAll(FieldFormatterCleanups.RECOMMEND_BIBLATEX_ACTIONS);
cleanupsListProperty.setAll(FieldFormatterCleanupActions.RECOMMEND_BIBLATEX_ACTIONS);
} else {
cleanupsListProperty.setAll(FieldFormatterCleanups.RECOMMEND_BIBTEX_ACTIONS);
cleanupsListProperty.setAll(FieldFormatterCleanupActions.RECOMMEND_BIBTEX_ACTIONS);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.jabref.gui.libraryproperties.PropertiesTabViewModel;
import org.jabref.logic.cleanup.CleanupPreferences;
import org.jabref.logic.cleanup.FieldFormatterCleanup;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;
import org.jabref.logic.preferences.CliPreferences;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.field.Field;
Expand Down Expand Up @@ -92,7 +92,7 @@ public void setValues() {

// FieldFormatterCleanupsPanel, included via <?import ...> in FXML

Optional<FieldFormatterCleanups> saveActions = initialMetaData.getSaveActions();
Optional<FieldFormatterCleanupActions> saveActions = initialMetaData.getSaveActions();
saveActions.ifPresentOrElse(value -> {
cleanupsDisableProperty.setValue(!value.isEnabled());
cleanupsProperty.setValue(FXCollections.observableArrayList(value.getConfiguredActions()));
Expand All @@ -113,18 +113,18 @@ public void storeSettings() {
newMetaData.markAsNotProtected();
}

FieldFormatterCleanups fieldFormatterCleanups = new FieldFormatterCleanups(
FieldFormatterCleanupActions fieldFormatterCleanupActions = new FieldFormatterCleanupActions(
!cleanupsDisableProperty().getValue(),
cleanupsProperty());

if (FieldFormatterCleanups.DEFAULT_SAVE_ACTIONS.equals(fieldFormatterCleanups.getConfiguredActions())) {
if (FieldFormatterCleanupActions.DEFAULT_SAVE_ACTIONS.equals(fieldFormatterCleanupActions.getConfiguredActions())) {
newMetaData.clearSaveActions();
} else {
// if all actions have been removed, remove the save actions from the MetaData
if (fieldFormatterCleanups.getConfiguredActions().isEmpty()) {
if (fieldFormatterCleanupActions.getConfiguredActions().isEmpty()) {
newMetaData.clearSaveActions();
} else {
newMetaData.setSaveActions(fieldFormatterCleanups);
newMetaData.setSaveActions(fieldFormatterCleanupActions);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.jabref.gui.preferences.JabRefGuiPreferences;
import org.jabref.logic.citationkeypattern.GlobalCitationKeyPatterns;
import org.jabref.logic.cleanup.CleanupPreferences;
import org.jabref.logic.cleanup.FieldFormatterCleanups;
import org.jabref.logic.cleanup.FieldFormatterCleanupActions;
import org.jabref.logic.l10n.Localization;
import org.jabref.logic.os.OS;
import org.jabref.logic.preferences.JabRefCliPreferences;
Expand Down Expand Up @@ -540,9 +540,9 @@ private static void upgradeCleanups(JabRefCliPreferences prefs) {
List<String> formatterCleanups = List.of(StringUtil.unifyLineBreaks(prefs.get(V5_8_CLEANUP_FIELD_FORMATTERS), "\n")
.split("\n"));
if (formatterCleanups.size() >= 2
&& (FieldFormatterCleanups.ENABLED.equals(formatterCleanups.getFirst())
|| FieldFormatterCleanups.DISABLED.equals(formatterCleanups.getFirst()))) {
prefs.putBoolean(V6_0_CLEANUP_FIELD_FORMATTERS_ENABLED, FieldFormatterCleanups.ENABLED.equals(formatterCleanups.getFirst())
&& (FieldFormatterCleanupActions.ENABLED.equals(formatterCleanups.getFirst())
|| FieldFormatterCleanupActions.DISABLED.equals(formatterCleanups.getFirst()))) {
prefs.putBoolean(V6_0_CLEANUP_FIELD_FORMATTERS_ENABLED, FieldFormatterCleanupActions.ENABLED.equals(formatterCleanups.getFirst())
? Boolean.TRUE
: Boolean.FALSE);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@
public class CleanupPreferences {

private final ObservableSet<CleanupStep> activeJobs;
private final ObjectProperty<FieldFormatterCleanups> fieldFormatterCleanups;
private final ObjectProperty<FieldFormatterCleanupActions> fieldFormatterCleanups;

public CleanupPreferences(EnumSet<CleanupStep> activeJobs) {
this(activeJobs, new FieldFormatterCleanups(false, new ArrayList<>()));
this(activeJobs, new FieldFormatterCleanupActions(false, new ArrayList<>()));
}

public CleanupPreferences(CleanupStep activeJob) {
this(EnumSet.of(activeJob));
}

public CleanupPreferences(FieldFormatterCleanups formatterCleanups) {
public CleanupPreferences(FieldFormatterCleanupActions formatterCleanups) {
this(EnumSet.noneOf(CleanupStep.class), formatterCleanups);
}

public CleanupPreferences(EnumSet<CleanupStep> activeJobs, FieldFormatterCleanups formatterCleanups) {
public CleanupPreferences(EnumSet<CleanupStep> activeJobs, FieldFormatterCleanupActions formatterCleanups) {
this.activeJobs = FXCollections.observableSet(activeJobs);
this.fieldFormatterCleanups = new SimpleObjectProperty<>(formatterCleanups);
}
Expand Down Expand Up @@ -60,15 +60,15 @@ public Boolean isActive(CleanupStep step) {
return activeJobs.contains(step);
}

public FieldFormatterCleanups getFieldFormatterCleanups() {
public FieldFormatterCleanupActions getFieldFormatterCleanups() {
return fieldFormatterCleanups.get();
}

public ObjectProperty<FieldFormatterCleanups> fieldFormatterCleanupsProperty() {
public ObjectProperty<FieldFormatterCleanupActions> fieldFormatterCleanupsProperty() {
return fieldFormatterCleanups;
}

public void setFieldFormatterCleanups(FieldFormatterCleanups fieldFormatters) {
public void setFieldFormatterCleanups(FieldFormatterCleanupActions fieldFormatters) {
fieldFormatterCleanups.setValue(fieldFormatters);
}

Expand Down
Loading
Loading