Skip to content

Commit

Permalink
Refactored to remove utility class
Browse files Browse the repository at this point in the history
  • Loading branch information
calixtus committed Mar 6, 2023
1 parent 13e7b49 commit 06b8079
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.jabref.gui.externalfiletype.ExternalFileType;
import org.jabref.gui.externalfiletype.ExternalFileTypes;
import org.jabref.gui.externalfiletype.UnknownExternalFileType;
import org.jabref.gui.linkedfile.AttachFileFromURLAction;
import org.jabref.gui.util.BackgroundTask;
import org.jabref.gui.util.BindingsHelper;
import org.jabref.gui.util.FileDialogConfiguration;
Expand Down Expand Up @@ -213,11 +214,11 @@ public void fetchFulltext() {
preferences.getImportFormatPreferences(),
preferences.getImporterPreferences());
Optional<String> urlField = entry.getField(StandardField.URL);
Boolean download_success = false;
boolean download_success = false;
if (urlField.isPresent()) {
download_success = downloadFile(urlField.get());
}
if (!urlField.isPresent() || !download_success) {
if (urlField.isEmpty() || !download_success) {
BackgroundTask
.wrap(() -> fetcher.findFullTextPDF(entry))
.onRunning(() -> fulltextLookupInProgress.setValue(true))
Expand All @@ -234,8 +235,8 @@ public void fetchFulltext() {
}

public void addFromURL() {
LinkedFilesEditorViewModelUtil util = new LinkedFilesEditorViewModelUtil(dialogService, entry);
util.getUrlForDownloadFromClipBoardOrEntry().ifPresent(this::downloadFile);
AttachFileFromURLAction.getUrlForDownloadFromClipBoardOrEntry(dialogService, entry)
.ifPresent(this::downloadFile);
}

private void addFromURLAndDownload(URL url) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import java.net.URL;
import java.util.Optional;

import org.jabref.gui.ClipBoardManager;
import org.jabref.gui.DialogService;
import org.jabref.gui.LibraryTab;
import org.jabref.gui.StateManager;
import org.jabref.gui.actions.ActionHelper;
import org.jabref.gui.actions.SimpleCommand;
import org.jabref.gui.fieldeditors.LinkedFileViewModel;
import org.jabref.gui.fieldeditors.LinkedFilesEditorViewModelUtil;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.LinkedFile;
import org.jabref.model.entry.field.StandardField;
import org.jabref.preferences.PreferencesService;

public class AttachFileFromURLAction extends SimpleCommand {
Expand All @@ -25,10 +25,10 @@ public class AttachFileFromURLAction extends SimpleCommand {
private final PreferencesService preferencesService;
private final TaskExecutor taskExecutor;

public AttachFileFromURLAction(LibraryTab libraryTab,
DialogService dialogService,
public AttachFileFromURLAction(DialogService dialogService,
StateManager stateManager,
TaskExecutor taskExecutor, PreferencesService preferencesService) {
TaskExecutor taskExecutor,
PreferencesService preferencesService) {
this.stateManager = stateManager;
this.dialogService = dialogService;
this.taskExecutor = taskExecutor;
Expand All @@ -53,8 +53,7 @@ public void execute() {

BibEntry entry = stateManager.getSelectedEntries().get(0);

LinkedFilesEditorViewModelUtil util = new LinkedFilesEditorViewModelUtil(dialogService, entry);
Optional<String> urlforDownload = util.getUrlForDownloadFromClipBoardOrEntry();
Optional<String> urlforDownload = getUrlForDownloadFromClipBoardOrEntry(dialogService, entry);

if (urlforDownload.isEmpty()) {
return;
Expand All @@ -74,4 +73,21 @@ public void execute() {
dialogService.showErrorDialogAndWait(Localization.lang("Invalid URL"), exception);
}
}

public static Optional<String> getUrlForDownloadFromClipBoardOrEntry(DialogService dialogService, BibEntry entry) {
String clipText = ClipBoardManager.getContents();
Optional<String> urlText;
String urlField = entry.getField(StandardField.URL).orElse("");
if (clipText.startsWith("http://") || clipText.startsWith("https://") || clipText.startsWith("ftp://")) {
urlText = dialogService.showInputDialogWithDefaultAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"), clipText);
} else if (urlField.startsWith("http://") || urlField.startsWith("https://") || urlField.startsWith("ftp://")) {
urlText = dialogService.showInputDialogWithDefaultAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"), urlField);
} else {
urlText = dialogService.showInputDialogAndWait(
Localization.lang("Download file"), Localization.lang("Enter URL to download"));
}
return urlText;
}
}
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/maintable/RightClickMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static ContextMenu create(BibEntryTableViewModel entry,
new SeparatorMenuItem(),

factory.createMenuItem(StandardActions.ATTACH_FILE, new AttachFileAction(libraryTab, dialogService, stateManager, preferencesService.getFilePreferences())),
factory.createMenuItem(StandardActions.ATTACH_FILE_FROM_URL, new AttachFileFromURLAction(libraryTab, dialogService, stateManager, taskExecutor, preferencesService)),
factory.createMenuItem(StandardActions.ATTACH_FILE_FROM_URL, new AttachFileFromURLAction(dialogService, stateManager, taskExecutor, preferencesService)),
factory.createMenuItem(StandardActions.OPEN_FOLDER, new OpenFolderAction(dialogService, stateManager, preferencesService)),
factory.createMenuItem(StandardActions.OPEN_EXTERNAL_FILE, new OpenExternalFileAction(dialogService, stateManager, preferencesService)),

Expand Down

0 comments on commit 06b8079

Please sign in to comment.