Skip to content

Commit

Permalink
Fix for issue 6966: open all files of multiple entries (#7709)
Browse files Browse the repository at this point in the history
* Fix #7416

* sync with origin

* core functions implemented

* moved function to OpenExternalFileAction; fixed a bug about checking if there is any linked file present; modified CHANGELOG.md; added javadoc

* remove dialog

* (incomplete) open all linked files of an entry; rewrite the boolean binding; question: open a large number of files

* Dialog: ask user whether to continue when opening more than 10 files

* fixed a bug: wrong position of the codes of dialog

* Modified binding; changed a variable name
  • Loading branch information
XDZhelheim authored May 13, 2021
1 parent b9c48f5 commit 84c8849
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- We added a feature that allows the user to copy highlighted text in the preview window. [#6962](https://github.com/JabRef/jabref/issues/6962)
- We added a feature that allows you to create new BibEntry via paste arxivId [#2292](https://github.com/JabRef/jabref/issues/2292)
- We added a feature that allows the user to choose whether to trust the target site when unable to find a valid certification path from the file download site. [#7616](https://github.com/JabRef/jabref/issues/7616)
- We added a feature that allows the user to open all linked files of multiple selected entries by "Open file" option. [#6966](https://github.com/JabRef/jabref/issues/6966)
- We added a keybinding preset for new entries. [#7705](https://github.com/JabRef/jabref/issues/7705)

### Changed
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/jabref/gui/actions/ActionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ public static BooleanExpression isFilePresentForSelectedEntry(StateManager state
return BooleanExpression.booleanExpression(fileIsPresent);
}

/**
* Check if at least one of the selected entries has linked files
* <br>
* Used in {@link org.jabref.gui.maintable.OpenExternalFileAction} when multiple entries selected
* @param stateManager manager for the state of the GUI
* @return a boolean binding
*/
public static BooleanExpression hasLinkedFileForSelectedEntries(StateManager stateManager) {
return BooleanExpression.booleanExpression(EasyBind.reduce(stateManager.getSelectedEntries(),
entries -> entries.anyMatch(entry -> !entry.getFiles().isEmpty())));
}

public static BooleanExpression isOpenMultiDatabase(TabPane tabbedPane) {
return Bindings.size(tabbedPane.getTabs()).greaterThan(1);
}
Expand Down
57 changes: 40 additions & 17 deletions src/main/java/org/jabref/gui/maintable/OpenExternalFileAction.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jabref.gui.maintable;

import java.util.LinkedList;
import java.util.List;

import org.jabref.gui.DialogService;
Expand All @@ -11,10 +12,13 @@
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.preferences.PreferencesService;

public class OpenExternalFileAction extends SimpleCommand {

private final int FILES_LIMIT = 10;

private final DialogService dialogService;
private final StateManager stateManager;
private final PreferencesService preferencesService;
Expand All @@ -24,32 +28,51 @@ public OpenExternalFileAction(DialogService dialogService, StateManager stateMan
this.stateManager = stateManager;
this.preferencesService = preferencesService;

this.executable.bind(ActionHelper.isFilePresentForSelectedEntry(stateManager, preferencesService)
.and(ActionHelper.needsEntriesSelected(1, stateManager)));
this.executable.bind(ActionHelper.hasLinkedFileForSelectedEntries(stateManager)
.and(ActionHelper.needsEntriesSelected(stateManager))
);
}

/**
* Open all linked files of the selected entries. If opening too many files, pop out a dialog to ask the user if to continue.
* <br>
* If some selected entries have linked file and others do not, ignore the latter.
*/
@Override
public void execute() {
stateManager.getActiveDatabase().ifPresent(databaseContext -> {
final List<BibEntry> selectedEntries = stateManager.getSelectedEntries();

if (selectedEntries.size() != 1) {
dialogService.notify(Localization.lang("This operation requires exactly one item to be selected."));
return;
List<LinkedFileViewModel> linkedFileViewModelList = new LinkedList<>();
LinkedFileViewModel linkedFileViewModel;

for (BibEntry entry:selectedEntries) {
for (LinkedFile linkedFile:entry.getFiles()) {
linkedFileViewModel = new LinkedFileViewModel(
linkedFile,
entry,
databaseContext,
Globals.TASK_EXECUTOR,
dialogService,
preferencesService.getXmpPreferences(),
preferencesService.getFilePreferences(),
ExternalFileTypes.getInstance());

linkedFileViewModelList.add(linkedFileViewModel);
}
}

// ask the user when detecting # of files > FILES_LIMIT
if (linkedFileViewModelList.size() > FILES_LIMIT) {
boolean continueOpening = dialogService.showConfirmationDialogAndWait(Localization.lang("Opening large number of files"),
Localization.lang("You are about to open %0 files. Continue?", Integer.toString(linkedFileViewModelList.size())),
Localization.lang("Continue"), Localization.lang("Cancel"));
if (!continueOpening) {
return;
}
}

final BibEntry entry = selectedEntries.get(0);

LinkedFileViewModel linkedFileViewModel = new LinkedFileViewModel(
entry.getFiles().get(0),
entry,
databaseContext,
Globals.TASK_EXECUTOR,
dialogService,
preferencesService.getXmpPreferences(),
preferencesService.getFilePreferences(),
ExternalFileTypes.getInstance());
linkedFileViewModel.open();
linkedFileViewModelList.forEach(LinkedFileViewModel::open);
});
}
}
3 changes: 3 additions & 0 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,9 @@ Clears\ the\ field\ completely.=Clears the field completely.
Directory\ not\ found=Directory not found
Main\ file\ directory\ not\ set.\ Check\ the\ preferences\ (linked\ files)\ or\ the\ library\ properties.=Main file directory not set. Check the preferences (linked files) or the library properties.
This\ operation\ requires\ exactly\ one\ item\ to\ be\ selected.=This operation requires exactly one item to be selected.
Opening\ large\ number\ of\ files=Opening large number of files
You\ are\ about\ to\ open\ %0\ files.\ Continue?=You are about to open %0 files. Continue?
Continue=Continue
Importing\ in\ %0\ format=Importing in %0 format
Female\ name=Female name
Female\ names=Female names
Expand Down

0 comments on commit 84c8849

Please sign in to comment.