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

Add error dialog "Problem finding files" #6842

Merged
merged 15 commits into from
Dec 24, 2020
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
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/JabRefFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ private MenuBar createMenu() {

new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.SET_FILE_LINKS, new AutoLinkFilesAction(this, prefs, stateManager, undoManager, Globals.TASK_EXECUTOR)),
factory.createMenuItem(StandardActions.SET_FILE_LINKS, new AutoLinkFilesAction(dialogService, prefs, stateManager, undoManager, Globals.TASK_EXECUTOR)),

new SeparatorMenuItem(),

Expand Down
49 changes: 30 additions & 19 deletions src/main/java/org/jabref/gui/externalfiles/AutoLinkFilesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import javafx.concurrent.Task;

import org.jabref.gui.DialogService;
import org.jabref.gui.JabRefFrame;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
Expand All @@ -34,47 +33,59 @@ public class AutoLinkFilesAction extends SimpleCommand {
private final UndoManager undoManager;
private final TaskExecutor taskExecutor;

public AutoLinkFilesAction(JabRefFrame frame, PreferencesService preferences, StateManager stateManager, UndoManager undoManager, TaskExecutor taskExecutor) {
this.dialogService = frame.getDialogService();
public AutoLinkFilesAction(DialogService dialogService, PreferencesService preferences, StateManager stateManager, UndoManager undoManager, TaskExecutor taskExecutor) {
this.dialogService = dialogService;
this.preferences = preferences;
this.stateManager = stateManager;
this.undoManager = undoManager;
this.taskExecutor = taskExecutor;

this.executable.bind(needsDatabase(this.stateManager).and(needsEntriesSelected(stateManager)));
this.statusMessage.bind(BindingsHelper.ifThenElse(executable, "", Localization.lang("This operation requires one or more entries to be selected.")));

}

@Override
public void execute() {
BibDatabaseContext database = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null"));
List<BibEntry> entries = stateManager.getSelectedEntries();

final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));
AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
final BibDatabaseContext database = stateManager.getActiveDatabase().orElseThrow(() -> new NullPointerException("Database null"));
final List<BibEntry> entries = stateManager.getSelectedEntries();
final AutoSetFileLinksUtil util = new AutoSetFileLinksUtil(
database,
preferences.getFilePreferences(),
preferences.getAutoLinkPreferences(),
ExternalFileTypes.getInstance());
Task<List<BibEntry>> linkFilesTask = new Task<>() {
final NamedCompound nc = new NamedCompound(Localization.lang("Automatically set file links"));

Task<AutoSetFileLinksUtil.LinkFilesResult> linkFilesTask = new Task<>() {
@Override
protected List<BibEntry> call() {
protected AutoSetFileLinksUtil.LinkFilesResult call() {
return util.linkAssociatedFiles(entries, nc);
}

@Override
protected void succeeded() {
if (!getValue().isEmpty()) {
if (nc.hasEdits()) {
nc.end();
undoManager.addEdit(nc);
}
dialogService.notify(Localization.lang("Finished automatically setting external links."));
} else {
dialogService.notify(Localization.lang("Finished automatically setting external links.") + " " + Localization.lang("No files found."));
AutoSetFileLinksUtil.LinkFilesResult result = getValue();

if (!result.getFileExceptions().isEmpty()) {
dialogService.showWarningDialogAndWait(
Localization.lang("Automatically set file links"),
Localization.lang("Problem finding files. See error log for details."));
return;
}

if (result.getChangedEntries().isEmpty()) {
dialogService.showWarningDialogAndWait("Automatically set file links",
Localization.lang("Finished automatically setting external links.") + "\n"
+ Localization.lang("No files found."));
return;
}

if (nc.hasEdits()) {
nc.end();
undoManager.addEdit(nc);
}

dialogService.notify(Localization.lang("Finished automatically setting external links.") + " "
+ Localization.lang("Changed %0 entries.", String.valueOf(result.getChangedEntries().size())));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,31 @@

public class AutoSetFileLinksUtil {

public static class LinkFilesResult {
private final List<BibEntry> changedEntries = new ArrayList<>();
private final List<IOException> fileExceptions = new ArrayList<>();

protected void addBibEntry(BibEntry bibEntry) {
changedEntries.add(bibEntry);
}

protected void addFileException(IOException exception) {
fileExceptions.add(exception);
}

public List<BibEntry> getChangedEntries() {
return changedEntries;
}

public List<IOException> getFileExceptions() {
return fileExceptions;
}
}

private static final Logger LOGGER = LoggerFactory.getLogger(AutoSetFileLinksUtil.class);
private List<Path> directories;
private AutoLinkPreferences autoLinkPreferences;
private ExternalFileTypes externalFileTypes;
private final List<Path> directories;
private final AutoLinkPreferences autoLinkPreferences;
private final ExternalFileTypes externalFileTypes;

public AutoSetFileLinksUtil(BibDatabaseContext databaseContext, FilePreferences filePreferences, AutoLinkPreferences autoLinkPreferences, ExternalFileTypes externalFileTypes) {
this(databaseContext.getFileDirectories(filePreferences), autoLinkPreferences, externalFileTypes);
Expand All @@ -46,36 +67,41 @@ private AutoSetFileLinksUtil(List<Path> directories, AutoLinkPreferences autoLin
this.externalFileTypes = externalFileTypes;
}

public List<BibEntry> linkAssociatedFiles(List<BibEntry> entries, NamedCompound ce) {
List<BibEntry> changedEntries = new ArrayList<>();
for (BibEntry entry : entries) {
public LinkFilesResult linkAssociatedFiles(List<BibEntry> entries, NamedCompound ce) {
LinkFilesResult result = new LinkFilesResult();

for (BibEntry entry : entries) {
List<LinkedFile> linkedFiles = new ArrayList<>();

try {
linkedFiles = findAssociatedNotLinkedFiles(entry);
} catch (IOException e) {
result.addFileException(e);
LOGGER.error("Problem finding files", e);
}

if (ce != null) {
boolean changed = false;

for (LinkedFile linkedFile : linkedFiles) {
// store undo information
String newVal = FileFieldWriter.getStringRepresentation(linkedFile);

String oldVal = entry.getField(StandardField.FILE).orElse(null);

UndoableFieldChange fieldChange = new UndoableFieldChange(entry, StandardField.FILE, oldVal, newVal);
ce.addEdit(fieldChange);
changed = true;

DefaultTaskExecutor.runInJavaFXThread(() -> {
entry.addFile(linkedFile);
});
}

changedEntries.add(entry);
if (changed) {
result.addBibEntry(entry);
}
}
}
return changedEntries;
return result;
}

public List<LinkedFile> findAssociatedNotLinkedFiles(BibEntry entry) throws IOException {
Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,6 @@ File\ not\ found=File not found

Filter=Filter

Finished\ automatically\ setting\ external\ links.=Finished automatically setting external links.

Filter\ groups=Filter groups

Finished\ writing\ XMP\ for\ %0\ file\ (%1\ skipped,\ %2\ errors).=Finished writing XMP for %0 file (%1 skipped, %2 errors).
Expand Down Expand Up @@ -640,6 +638,7 @@ Primary\ sort\ criterion=Primary sort criterion
Problem\ with\ parsing\ entry=Problem with parsing entry
Processing\ %0=Processing %0
Pull\ changes\ from\ shared\ database=Pull changes from shared database
Problem\ finding\ files.\ See\ error\ log\ for\ details.=Problem finding files. See error log for details.

Pushed\ citations\ to\ %0=Pushed citations to %0

Expand Down Expand Up @@ -1193,6 +1192,9 @@ Update\ timestamp\ on\ modification=Update timestamp on modification
All\ key\ bindings\ will\ be\ reset\ to\ their\ defaults.=All key bindings will be reset to their defaults.

Automatically\ set\ file\ links=Automatically set file links
Finished\ automatically\ setting\ external\ links.=Finished automatically setting external links.
Changed\ %0\ entries.=Changed %0 entries.

Resetting\ all\ key\ bindings=Resetting all key bindings

Network=Network
Expand Down