-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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-issue-4489 #4538
fix-for-issue-4489 #4538
Changes from 5 commits
cde9b4e
0bd59b3
3f67c97
69cc478
fd9448f
2e3c902
af710b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
package org.jabref.gui.externalfiles; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.util.ArrayList; | ||
|
@@ -11,18 +10,23 @@ | |
|
||
import javax.swing.SwingUtilities; | ||
|
||
import javafx.beans.property.BooleanProperty; | ||
import javafx.beans.property.ListProperty; | ||
import javafx.beans.property.SimpleBooleanProperty; | ||
import javafx.beans.property.SimpleListProperty; | ||
import javafx.collections.FXCollections; | ||
|
||
import org.jabref.Globals; | ||
import org.jabref.gui.BasePanel; | ||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.actions.BaseAction; | ||
import org.jabref.gui.undo.UndoableFieldChange; | ||
import org.jabref.gui.fieldeditors.LinkedFileViewModel; | ||
import org.jabref.gui.util.BackgroundTask; | ||
import org.jabref.gui.util.DefaultTaskExecutor; | ||
import org.jabref.logic.importer.FulltextFetchers; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.model.FieldChange; | ||
import org.jabref.model.entry.BibEntry; | ||
import org.jabref.model.entry.FieldName; | ||
import org.jabref.model.entry.LinkedFile; | ||
import org.jabref.preferences.JabRefPreferences; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -39,6 +43,10 @@ public class FindFullTextAction implements BaseAction { | |
private final BasePanel basePanel; | ||
private final DialogService dialogService; | ||
|
||
//Stuff for downloading full texts | ||
private final FulltextFetchers fetcher = new FulltextFetchers(JabRefPreferences.getInstance().getImportFormatPreferences()); | ||
private final ListProperty<LinkedFileViewModel> files = new SimpleListProperty<>(FXCollections.observableArrayList(LinkedFileViewModel::getObservables)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears to me that you only add elements to this list, but never query its contents. In this case, you can probably remove this variable. |
||
|
||
public FindFullTextAction(DialogService dialogService, BasePanel basePanel) { | ||
this.basePanel = basePanel; | ||
this.dialogService = dialogService; | ||
|
@@ -47,8 +55,9 @@ public FindFullTextAction(DialogService dialogService, BasePanel basePanel) { | |
@Override | ||
public void action() { | ||
BackgroundTask.wrap(this::findFullTexts) | ||
.onSuccess(downloads -> SwingUtilities.invokeLater(() -> downloadFullTexts(downloads))) | ||
.executeWith(Globals.TASK_EXECUTOR); | ||
.onSuccess(downloads -> SwingUtilities.invokeLater(() -> downloadFullTexts(downloads))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you no longer need the swing utitilies call here because you effectively removed the swing code which needed this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK! Lambdas (and programming in general) are all new to me, I'm still getting used to them. I have replaced this with a simple method reference to downLoadFullTexts. Thanks for the suggestion. I was also able to remove the now unnecessary import of swing also. |
||
.executeWith(Globals.TASK_EXECUTOR); | ||
|
||
} | ||
|
||
private Map<Optional<URL>, BibEntry> findFullTexts() { | ||
|
@@ -101,40 +110,53 @@ private void downloadFullTexts(Map<Optional<URL>, BibEntry> downloads) { | |
|
||
return; | ||
} | ||
DownloadExternalFile fileDownload = new DownloadExternalFile(dialogService, | ||
basePanel.getBibDatabaseContext(), entry); | ||
try { | ||
fileDownload.download(result.get(), "application/pdf", file -> { | ||
DefaultTaskExecutor.runInJavaFXThread(() -> { | ||
Optional<FieldChange> fieldChange = entry.addFile(file); | ||
if (fieldChange.isPresent()) { | ||
UndoableFieldChange edit = new UndoableFieldChange(entry, FieldName.FILE, | ||
entry.getField(FieldName.FILE).orElse(null), fieldChange.get().getNewValue()); | ||
basePanel.getUndoManager().addEdit(edit); | ||
basePanel.markBaseChanged(); | ||
} | ||
}); | ||
|
||
}); | ||
} catch (IOException e) { | ||
LOGGER.warn("Problem downloading file", e); | ||
basePanel.output(Localization.lang("Full text document download failed for entry %0", | ||
entry.getCiteKeyOptional().orElse(Localization.lang("undefined")))); | ||
} | ||
basePanel.output(Localization.lang("Finished downloading full text document for entry %0.", | ||
entry.getCiteKeyOptional().orElse(Localization.lang("undefined")))); | ||
} else { | ||
String title = Localization.lang("No full text document found"); | ||
String message = Localization.lang("No full text document found for entry %0.", | ||
entry.getCiteKeyOptional().orElse(Localization.lang("undefined"))); | ||
|
||
basePanel.output(message); | ||
DefaultTaskExecutor.runInJavaFXThread(() -> dialogService.showErrorDialogAndWait(title, message)); | ||
//Download full text | ||
BackgroundTask | ||
.wrap(() -> fetcher.findFullTextPDF(entry)) | ||
.onSuccess(url -> addLinkedFileFromURL(result.get(), entry)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since you already have the url in |
||
.executeWith(Globals.TASK_EXECUTOR); | ||
|
||
} else { | ||
dialogService.notify(Localization.lang("No full text document found for entry %0.", | ||
entry.getCiteKeyOptional().orElse(Localization.lang("undefined")))); | ||
} | ||
finishedTasks.add(result); | ||
} | ||
for (Optional<URL> result : finishedTasks) { | ||
downloads.remove(result); | ||
} | ||
} | ||
|
||
/** | ||
* This method attaches a linked file from a URL (if not already linked) to an entry and using the key and value pair | ||
* from the findFullTexts map | ||
* @param url the url "key" | ||
* @param entry the entry "value" | ||
*/ | ||
private void addLinkedFileFromURL(URL url, BibEntry entry) { | ||
|
||
LinkedFile newLinkedFile = new LinkedFile(url, ""); | ||
|
||
if (!entry.getFiles().contains(newLinkedFile)) { | ||
|
||
LinkedFileViewModel onlineFile = new LinkedFileViewModel( | ||
newLinkedFile, | ||
entry, | ||
basePanel.getBibDatabaseContext(), | ||
Globals.TASK_EXECUTOR, | ||
dialogService, | ||
JabRefPreferences.getInstance()); | ||
|
||
files.add(onlineFile); | ||
onlineFile.download(); | ||
|
||
entry.addFile(newLinkedFile); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here you probably need to add the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tobiasdiez thanks for all of the great feedback! I should have everything wrapped up by tomorrow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tobiasdiez I think I've gotten everything taken care of. Thanks again for the help! |
||
dialogService.notify(Localization.lang("Finished downloading full text document for entry %0.", | ||
entry.getCiteKeyOptional().orElse(Localization.lang("undefined")))); | ||
} else { | ||
dialogService.notify(Localization.lang("Full text document for entry %0 already linked.", | ||
entry.getCiteKeyOptional().orElse(Localization.lang("undefined")))); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We try to get rid of explicit calling JabRefPreferences., the ideal solution would be pass the ImportFormatPrefernces as parameter in the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And while you are onto this class, you could refactor it to extend the SimpleCommand class (see for example WriteXMP Action)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Siedlerchr So, it should not implement the BaseAction interface anymore then, correct?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, replace the base action with SimpleCommand, instead of an action method you have an execute method, it's technically just a different name, the behavior is the same and you can just rename it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Siedlerchr BasePanel.java doesn't like that because now the second argument when putting the actions in the map that actions are stored in is no longer a "BaseAction". How should I proceed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can get rid of that action entry in the action map, look at the setup of the actions in the menus, There should be the SimpleCommand variant
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the help! I think I've got everything fixed up.