-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue 9646: Right-click context menu "Attach file from URL" (#9648)
* implement "Attach feile from URL" in RightCLickMenu * add changelog * fix checkstyle violations * fix some checkstyles * add l10n * checkstyle * extract duplicate to new class * Refactored to remove utility class --------- Co-authored-by: Siedlerchr <siedlerkiller@gmail.com> Co-authored-by: Carl Christian Snethlage <50491877+calixtus@users.noreply.github.com>
- Loading branch information
1 parent
ee93bfb
commit 51b36c1
Showing
6 changed files
with
104 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/main/java/org/jabref/gui/linkedfile/AttachFileFromURLAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.jabref.gui.linkedfile; | ||
|
||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.Optional; | ||
|
||
import org.jabref.gui.ClipBoardManager; | ||
import org.jabref.gui.DialogService; | ||
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.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 { | ||
|
||
private final StateManager stateManager; | ||
private final DialogService dialogService; | ||
private final PreferencesService preferencesService; | ||
private final TaskExecutor taskExecutor; | ||
|
||
public AttachFileFromURLAction(DialogService dialogService, | ||
StateManager stateManager, | ||
TaskExecutor taskExecutor, | ||
PreferencesService preferencesService) { | ||
this.stateManager = stateManager; | ||
this.dialogService = dialogService; | ||
this.taskExecutor = taskExecutor; | ||
this.preferencesService = preferencesService; | ||
|
||
this.executable.bind(ActionHelper.needsEntriesSelected(1, stateManager)); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (stateManager.getActiveDatabase().isEmpty()) { | ||
dialogService.notify(Localization.lang("This operation requires an open library.")); | ||
return; | ||
} | ||
|
||
if (stateManager.getSelectedEntries().size() != 1) { | ||
dialogService.notify(Localization.lang("This operation requires exactly one item to be selected.")); | ||
return; | ||
} | ||
|
||
BibDatabaseContext databaseContext = stateManager.getActiveDatabase().get(); | ||
|
||
BibEntry entry = stateManager.getSelectedEntries().get(0); | ||
|
||
Optional<String> urlforDownload = getUrlForDownloadFromClipBoardOrEntry(dialogService, entry); | ||
|
||
if (urlforDownload.isEmpty()) { | ||
return; | ||
} | ||
|
||
try { | ||
URL url = new URL(urlforDownload.get()); | ||
LinkedFileViewModel onlineFile = new LinkedFileViewModel( | ||
new LinkedFile(url, ""), | ||
entry, | ||
databaseContext, | ||
taskExecutor, | ||
dialogService, | ||
preferencesService); | ||
onlineFile.download(); | ||
} catch (MalformedURLException exception) { | ||
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters