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

Fix for delete entries should ask user #10591

Merged
merged 59 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
42cf53d
Implemented the feature that deleting files which linked to selected …
shawn-jj Oct 27, 2023
4bfc322
Merge branch 'main' of https://github.com/shawn-jj/jabref into fix-fo…
shawn-jj Oct 27, 2023
0435c91
The following features are implemented: 1.Initializes a pop-up dialog…
shawn-jj Oct 27, 2023
898c2b2
update CHANGELOG.md
shawn-jj Oct 27, 2023
482c24b
Add language keys to english language file
shawn-jj Oct 28, 2023
799c693
restore files in src/main/resources/csl-locales and src/main/resource…
shawn-jj Oct 28, 2023
13d9d38
Merge branch 'JabRef:main' into fix-for-issue-10509
shawn-jj Oct 28, 2023
7368fe2
Removed unnecessary comments and finxed some requested changes. Added…
shawn-jj Oct 29, 2023
7e282df
Add language keys to english language file
shawn-jj Oct 29, 2023
8d325ff
Modify language keys to english language file
shawn-jj Oct 29, 2023
1b7e17d
made deleteFileFromDisk method static
shawn-jj Nov 2, 2023
f1d50bb
Merge branch 'main' of https://github.com/shawn-jj/jabref into fix-fo…
shawn-jj Nov 2, 2023
4a4e81f
update comment of method deleteFileFromDisk
shawn-jj Nov 2, 2023
c9d7dd9
fixed coding styles
shawn-jj Nov 3, 2023
eef105a
restored unexpected code changes
shawn-jj Nov 3, 2023
c12cd63
Merge branch 'JabRef:main' into fix-for-issue-10509
shawn-jj Nov 3, 2023
97fa0c9
Merge remote-tracking branch 'upstream/main' into fix-for-issue-10509
Siedlerchr Nov 6, 2023
1a531e7
fix logic
Siedlerchr Nov 6, 2023
7ec812b
try null
Siedlerchr Nov 6, 2023
0652ceb
todo
Siedlerchr Nov 7, 2023
17c6a8f
Unify dialogs that confirmation deleting files
shawn-jj Nov 7, 2023
4119b38
Merge branch 'main' into fix-for-issue-10509
koppor Nov 16, 2023
35a95f2
Merge branch 'JabRef:main' into fix-for-issue-10509
shawn-jj Nov 29, 2023
3364671
Get around LinkedFile in LIbraryTab, Encapsulate LinkedFile into Link…
shawn-jj Nov 29, 2023
055f170
fix style
shawn-jj Nov 29, 2023
7bf040e
restore files
shawn-jj Nov 29, 2023
7324452
Unified the different dialogs when deleting entries, removerd unneces…
shawn-jj Nov 29, 2023
5c319cc
fix csl-styles
shawn-jj Nov 29, 2023
853d0ac
try to fix csl-styles
shawn-jj Nov 30, 2023
b4e770a
try to fix csl-styles again
shawn-jj Nov 30, 2023
db5206d
try to fix csl-styles again 2
shawn-jj Nov 30, 2023
d3f57bf
try to fix csl-styles again 3
shawn-jj Nov 30, 2023
c5d7755
Merge branch 'main' into fix-for-issue-10509
koppor Jan 2, 2024
29b7d6f
Merge branch 'JabRef:main' into fix-for-issue-10509
shawn-jj Jan 3, 2024
92b590c
Update prompts in en.properties
shawn-jj Jan 3, 2024
b5ae963
Merge remote-tracking branch 'upstream/main' into fix-for-issue-10509
koppor Jan 11, 2024
888d191
New features
koppor Jan 11, 2024
dfcbb0b
Merge branch 'main' into fix-for-issue-10509
koppor Jan 11, 2024
cb444e4
Fix architecture tests
koppor Jan 11, 2024
58215e2
Introduce list of files to delete
calixtus Jan 11, 2024
b4b84b6
Merge remote-tracking branch 'shawn-jj/fix-for-issue-10509' into fork…
calixtus Jan 11, 2024
09e1d6c
Streamline 1 vs. many files
koppor Jan 11, 2024
2a49780
Fix openRewrite
koppor Jan 11, 2024
cef7a2c
Merge remote-tracking branch 'upstream/main' into fix-for-issue-10509
koppor Jan 16, 2024
d9ccf84
Discard changes to src/test/resources/org/jabref/logic/search/test-li…
koppor Jan 16, 2024
65b63d9
Merge remote-tracking branch 'origin/main' into fix-for-issue-10509
koppor Jan 24, 2024
6c2ebc5
Adapt true/false logic according to expectations
koppor Jan 24, 2024
250e168
Add "Trash" to CHANGELOG.md
koppor Jan 24, 2024
f62fa7e
Fix localization
koppor Jan 24, 2024
9c9760c
Merge branch 'main' into fix-for-issue-10509
koppor Feb 6, 2024
e065dfe
Merge remote-tracking branch 'upstream/main' into fix-for-issue-10509
koppor Feb 18, 2024
74ee156
Fix JabRef_en.properties
koppor Feb 18, 2024
77bb7a0
Add some debug statements
koppor Feb 18, 2024
eee769c
Fix preferences
koppor Feb 18, 2024
1f13409
Separate log entries by empty line
koppor Feb 18, 2024
84f94d1
More refined dialog
koppor Feb 18, 2024
f6d413a
Merge remote-tracking branch 'upstream/main' into fix-for-issue-10509
koppor Feb 18, 2024
e06a631
Merge branch 'main' into fix-for-issue-10509
koppor Feb 19, 2024
cef4f9c
Merge remote-tracking branch 'upstream/main' into fix-for-issue-10509
Siedlerchr Feb 26, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Added

- We added a popup, as well as a preferences setting item, so users can choose whether to delete files linked to selected entries when they delete entries. [#10509](https://github.com/JabRef/jabref/issues/10509)

### Changed

### Fixed
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/org/jabref/gui/LibraryTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Random;
import java.util.stream.Collectors;

import javax.swing.undo.UndoManager;

Expand All @@ -34,7 +35,9 @@
import org.jabref.gui.collab.DatabaseChangeMonitor;
import org.jabref.gui.dialogs.AutosaveUiManager;
import org.jabref.gui.entryeditor.EntryEditor;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.importer.actions.OpenDatabaseAction;
import org.jabref.gui.linkedfile.DeleteFileAction;
import org.jabref.gui.maintable.BibEntryTableViewModel;
import org.jabref.gui.maintable.MainTable;
import org.jabref.gui.maintable.MainTableDataModel;
Expand Down Expand Up @@ -414,7 +417,7 @@ public void setMode(BasePanelMode mode) {
}

/**
* Removes the selected entries from the database
* Removes the selected entries and files linked to selected entries from the database
*
* @param cut If false the user will get asked if he really wants to delete the entries, and it will be localized as "deleted". If true the action will be localized as "cut"
*/
Expand All @@ -423,7 +426,7 @@ public void delete(boolean cut) {
}

/**
* Removes the selected entries from the database
* Removes the selected entries and files linked to selected entries from the database
*
* @param cut If false the user will get asked if he really wants to delete the entries, and it will be localized as "deleted". If true the action will be localized as "cut"
*/
Expand All @@ -435,8 +438,25 @@ private void delete(boolean cut, List<BibEntry> entries) {
return;
}

// Delete selected entries
getUndoManager().addEdit(new UndoableRemoveEntries(bibDatabaseContext.getDatabase(), entries, cut));
bibDatabaseContext.getDatabase().removeEntries(entries);

if (!cut) {
List<LinkedFile> linkedFileList = entries.stream()
.flatMap(entry -> entry.getFiles().stream())
.distinct()
.toList();

if (!linkedFileList.isEmpty()) {
List<LinkedFileViewModel> viewModels = linkedFileList.stream()
.map(linkedFile -> linkedFile.toModel(null, bibDatabaseContext, null, null, preferencesService))
.collect(Collectors.toList());

new DeleteFileAction(dialogService, preferencesService, bibDatabaseContext, viewModels).execute();
}
}

ensureNotShowingBottomPanel(entries);

this.changedProperty.setValue(true);
Expand Down Expand Up @@ -669,6 +689,14 @@ public BibDatabase getDatabase() {
return bibDatabaseContext.getDatabase();
}

/**
* Initializes a pop-up dialog box to confirm whether the user wants to delete the selected entry
* Keep track of user preference:
* if the user prefers not to ask before deleting, delete the selected entry without displaying the dialog box
*
* @param numberOfEntries number of entries user is selecting
* @return true if user confirm to delete entry
*/
private boolean showDeleteConfirmationDialog(int numberOfEntries) {
if (preferencesService.getWorkspacePreferences().shouldConfirmDelete()) {
String title = Localization.lang("Delete entry");
Expand All @@ -682,7 +710,8 @@ private boolean showDeleteConfirmationDialog(int numberOfEntries) {
cancelButton = Localization.lang("Keep entries");
}

return dialogService.showConfirmationDialogWithOptOutAndWait(title,
return dialogService.showConfirmationDialogWithOptOutAndWait(
title,
message,
okButton,
cancelButton,
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,9 @@ private void setUpKeyBindings() {
if (keyBinding.isPresent()) {
switch (keyBinding.get()) {
case DELETE_ENTRY:
new DeleteFileAction(dialogService, preferencesService, databaseContext,
viewModel, listView).execute();
// Delete multiple selected files in currently opened entry
// als
deleteEntry();
event.consume();
break;
default:
Expand All @@ -262,6 +263,11 @@ private void setUpKeyBindings() {
});
}

private void deleteEntry() {
new DeleteFileAction(dialogService, preferencesService, databaseContext,
viewModel, listView.getSelectionModel().getSelectedItems()).execute();
}

public LinkedFilesEditorViewModel getViewModel() {
return viewModel;
}
Expand Down
58 changes: 35 additions & 23 deletions src/main/java/org/jabref/gui/linkedfile/DeleteFileAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ListView;

import org.jabref.gui.DialogService;
import org.jabref.gui.actions.SimpleCommand;
Expand All @@ -31,92 +30,105 @@ public class DeleteFileAction extends SimpleCommand {
private final PreferencesService preferences;
private final BibDatabaseContext databaseContext;
private final LinkedFilesEditorViewModel viewModel;
private final ListView<LinkedFileViewModel> listView;
private final List<LinkedFileViewModel> linkedFiles;

public DeleteFileAction(DialogService dialogService,
PreferencesService preferences,
BibDatabaseContext databaseContext,
LinkedFilesEditorViewModel viewModel,
ListView<LinkedFileViewModel> listView) {
List<LinkedFileViewModel> linkedFiles) {
this.dialogService = dialogService;
this.preferences = preferences;
this.databaseContext = databaseContext;
this.viewModel = viewModel;
this.listView = listView;
this.linkedFiles = List.copyOf(linkedFiles);
}

public DeleteFileAction(DialogService dialogService,
PreferencesService preferences,
BibDatabaseContext databaseContext,
List<LinkedFileViewModel> linkedFiles) {
this(dialogService, preferences, databaseContext, null, linkedFiles);
}

@Override
public void execute() {
List<LinkedFileViewModel> toBeDeleted = List.copyOf(listView.getSelectionModel().getSelectedItems());

if (toBeDeleted.isEmpty()) {
if (linkedFiles.isEmpty()) {
dialogService.notify(Localization.lang("This operation requires selected linked files."));
return;
}

if (!preferences.getFilePreferences().confirmDeleteLinkedFile()) {
deleteFiles(linkedFiles, true);
return;
}

String dialogTitle;
String dialogContent;

if (toBeDeleted.size() != 1) {
dialogTitle = Localization.lang("Delete %0 files", toBeDeleted.size());
if (linkedFiles.size() != 1) {
dialogTitle = Localization.lang("Delete %0 files", linkedFiles.size());
dialogContent = Localization.lang("Delete %0 files permanently from disk, or just remove the files from the entry? " +
"Pressing Delete will delete the files permanently from disk.", toBeDeleted.size());
"Pressing Delete will delete the files permanently from disk.", linkedFiles.size());
} else {
Optional<Path> file = toBeDeleted.get(0).getFile().findIn(databaseContext, preferences.getFilePreferences());
Optional<Path> file = linkedFiles.get(0).getFile().findIn(databaseContext, preferences.getFilePreferences());

if (file.isPresent()) {
dialogTitle = Localization.lang("Delete '%0'", file.get().getFileName().toString());
dialogContent = Localization.lang("Delete '%0' permanently from disk, or just remove the file from the entry? " +
"Pressing Delete will delete the file permanently from disk.", file.get().toString());
} else {
dialogService.notify(Localization.lang("Error accessing file '%0'.", toBeDeleted.get(0).getFile().getLink()));
dialogService.notify(Localization.lang("Error accessing file '%0'.", linkedFiles.get(0).getFile().getLink()));
return;
}
}

ButtonType removeFromEntry = new ButtonType(Localization.lang("Remove from entry"), ButtonBar.ButtonData.YES);
ButtonType deleteFromEntry = new ButtonType(Localization.lang("Delete from disk"));

Optional<ButtonType> buttonType = dialogService.showCustomButtonDialogAndWait(Alert.AlertType.INFORMATION,
dialogTitle, dialogContent, removeFromEntry, deleteFromEntry, ButtonType.CANCEL);

if (buttonType.isPresent()) {
if (buttonType.get().equals(removeFromEntry)) {
deleteFiles(toBeDeleted, false);
deleteFiles(linkedFiles, false);
}

if (buttonType.get().equals(deleteFromEntry)) {
deleteFiles(toBeDeleted, true);
deleteFiles(linkedFiles, true);
}
}
}

/**
* Deletes the files from the entry and optionally from disk.
*
* @param toBeDeleted the files to be deleted
* @param toBeDeleted the files to be deleted
* @param deleteFromDisk if true, the files are deleted from disk, otherwise they are only removed from the entry
*/
private void deleteFiles(List<LinkedFileViewModel> toBeDeleted, boolean deleteFromDisk) {
for (LinkedFileViewModel fileViewModel : toBeDeleted) {
if (fileViewModel.getFile().isOnlineLink()) {
viewModel.removeFileLink(fileViewModel);
if (viewModel != null) {
viewModel.removeFileLink(fileViewModel);
}
} else {
if (deleteFromDisk) {
deleteFileFromDisk(fileViewModel);
deleteFileHelper(databaseContext, fileViewModel.getFile());
}
if (viewModel != null) {
viewModel.getFiles().remove(fileViewModel);
}
viewModel.getFiles().remove(fileViewModel);
}
}
}

/**
* Deletes the file from disk without asking the user for confirmation.
* Helper method to delete the specified file from disk
*
* @param fileViewModel the file to be deleted
* @param linkedFile The LinkedFile (file which linked to an entry) to be deleted from disk
*/
public void deleteFileFromDisk(LinkedFileViewModel fileViewModel) {
LinkedFile linkedFile = fileViewModel.getFile();

private void deleteFileHelper(BibDatabaseContext databaseContext, LinkedFile linkedFile) {
Optional<Path> file = linkedFile.findIn(databaseContext, preferences.getFilePreferences());

if (file.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,8 @@
<TextField fx:id="fileDirectoryPattern" GridPane.columnIndex="1" GridPane.rowIndex="1"
prefWidth="300" minWidth="300" maxWidth="300"/>
</GridPane>

<Label styleClass="sectionHeader" text="%Attached files"/>
<CheckBox fx:id="confirmLinkedFileDelete" text="%Show confirmation dialog when deleting files"/>

</fx:root>
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class LinkedFilesTab extends AbstractPreferenceTabView<LinkedFilesTabView

@FXML private ComboBox<String> fileNamePattern;
@FXML private TextField fileDirectoryPattern;
@FXML private CheckBox confirmLinkedFileDelete;

private final ControlsFxVisualizer validationVisualizer = new ControlsFxVisualizer();

Expand Down Expand Up @@ -69,6 +70,7 @@ public void initialize() {
fileNamePattern.valueProperty().bindBidirectional(viewModel.fileNamePatternProperty());
fileNamePattern.itemsProperty().bind(viewModel.defaultFileNamePatternsProperty());
fileDirectoryPattern.textProperty().bindBidirectional(viewModel.fileDirectoryPatternProperty());
confirmLinkedFileDelete.selectedProperty().bindBidirectional(viewModel.confirmLinkedFileDeleteProperty());

ActionFactory actionFactory = new ActionFactory(Globals.getKeyPrefs());
actionFactory.configureIconButton(StandardActions.HELP_REGEX_SEARCH, new HelpAction(HelpFile.REGEX_SEARCH, dialogService, preferencesService.getFilePreferences()), autolinkRegexHelp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class LinkedFilesTabViewModel implements PreferenceTabViewModel {
private final BooleanProperty fulltextIndex = new SimpleBooleanProperty();
private final StringProperty fileNamePatternProperty = new SimpleStringProperty();
private final StringProperty fileDirectoryPatternProperty = new SimpleStringProperty();
private final BooleanProperty confirmLinkedFileDeleteProperty = new SimpleBooleanProperty();

private final Validator mainFileDirValidator;

Expand Down Expand Up @@ -80,6 +81,7 @@ public void setValues() {
fulltextIndex.setValue(filePreferences.shouldFulltextIndexLinkedFiles());
fileNamePatternProperty.setValue(filePreferences.getFileNamePattern());
fileDirectoryPatternProperty.setValue(filePreferences.getFileDirectoryPattern());
confirmLinkedFileDeleteProperty.setValue(filePreferences.confirmDeleteLinkedFile());

// Autolink preferences
switch (autoLinkPreferences.getCitationKeyDependency()) {
Expand Down Expand Up @@ -110,6 +112,7 @@ public void storeSettings() {
}

autoLinkPreferences.setRegularExpression(autolinkRegexKeyProperty.getValue());
filePreferences.confirmDeleteLinkedFile(confirmLinkedFileDeleteProperty.getValue());
}

ValidationStatus mainFileDirValidationStatus() {
Expand Down Expand Up @@ -178,5 +181,9 @@ public StringProperty fileDirectoryPatternProperty() {
public BooleanProperty useMainFileDirectoryProperty() {
return useMainFileDirectoryProperty;
}

public BooleanProperty confirmLinkedFileDeleteProperty() {
return this.confirmLinkedFileDeleteProperty;
}
}

18 changes: 18 additions & 0 deletions src/main/java/org/jabref/model/entry/LinkedFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
import javafx.beans.property.StringProperty;

import org.jabref.architecture.AllowedToUseLogic;
import org.jabref.gui.DialogService;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.preferences.FilePreferences;
import org.jabref.preferences.PreferencesService;

/**
* Represents the link to an external file (e.g. associated PDF file).
Expand Down Expand Up @@ -199,4 +203,18 @@ public Optional<Path> findIn(List<Path> directories) {
return Optional.empty();
}
}

public LinkedFileViewModel toModel(BibEntry entry,
BibDatabaseContext databaseContext,
TaskExecutor taskExecutor,
DialogService dialogService,
PreferencesService preferencesService) {
return new LinkedFileViewModel(
this,
entry,
databaseContext,
taskExecutor,
dialogService,
preferencesService);
}
}
17 changes: 16 additions & 1 deletion src/main/java/org/jabref/preferences/FilePreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class FilePreferences {
private final ObservableSet<ExternalFileType> externalFileTypes = FXCollections.observableSet(new TreeSet<>(Comparator.comparing(ExternalFileType::getName)));
private final BooleanProperty createBackup = new SimpleBooleanProperty();
private final ObjectProperty<Path> backupDirectory = new SimpleObjectProperty<>();
private final BooleanProperty confirmDeleteLinkedFile;

public FilePreferences(String userAndHost,
String mainFileDirectory,
Expand All @@ -47,7 +48,8 @@ public FilePreferences(String userAndHost,
Path workingDirectory,
Set<ExternalFileType> externalFileTypes,
boolean createBackup,
Path backupDirectory) {
Path backupDirectory,
boolean confirmDeleteLinkedFile) {
this.userAndHost.setValue(userAndHost);
this.mainFileDirectory.setValue(mainFileDirectory);
this.storeFilesRelativeToBibFile.setValue(storeFilesRelativeToBibFile);
Expand All @@ -59,6 +61,7 @@ public FilePreferences(String userAndHost,
this.externalFileTypes.addAll(externalFileTypes);
this.createBackup.setValue(createBackup);
this.backupDirectory.setValue(backupDirectory);
this.confirmDeleteLinkedFile = new SimpleBooleanProperty(confirmDeleteLinkedFile);
}

public String getUserAndHost() {
Expand Down Expand Up @@ -180,4 +183,16 @@ public void setBackupDirectory(Path backupPath) {
public Path getBackupDirectory() {
return this.backupDirectory.getValue();
}

public boolean confirmDeleteLinkedFile() {
return confirmDeleteLinkedFile.get();
}

public BooleanProperty confirmDeleteLinkedFileProperty() {
return confirmDeleteLinkedFile;
}

public void confirmDeleteLinkedFile(boolean confirmLinkedFileDelete) {
this.confirmDeleteLinkedFile.set(confirmLinkedFileDelete);
}
}
Loading
Loading