-
-
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.
Merge pull request #5068 from JabRef/copyLinkedFilesInContextmenu
Add copy linked files action to contextmneu in general tab
- Loading branch information
Showing
12 changed files
with
155 additions
and
70 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
61 changes: 61 additions & 0 deletions
61
src/main/java/org/jabref/gui/copyfiles/CopySingleFileAction.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,61 @@ | ||
package org.jabref.gui.copyfiles; | ||
|
||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
|
||
import org.jabref.Globals; | ||
import org.jabref.gui.DialogService; | ||
import org.jabref.gui.util.DirectoryDialogConfiguration; | ||
import org.jabref.logic.l10n.Localization; | ||
import org.jabref.logic.util.io.FileUtil; | ||
import org.jabref.model.database.BibDatabaseContext; | ||
import org.jabref.model.entry.LinkedFile; | ||
import org.jabref.model.util.OptionalUtil; | ||
import org.jabref.preferences.JabRefPreferences; | ||
|
||
public class CopySingleFileAction { | ||
|
||
private LinkedFile linkedFile; | ||
private DialogService dialogService; | ||
private BibDatabaseContext databaseContext; | ||
private final BiFunction<Path, Path, Path> resolvePathFilename = (path, file) -> { | ||
return path.resolve(file.getFileName()); | ||
}; | ||
|
||
public CopySingleFileAction(LinkedFile linkedFile, DialogService dialogService, BibDatabaseContext databaseContext) { | ||
this.linkedFile = linkedFile; | ||
this.dialogService = dialogService; | ||
this.databaseContext = databaseContext; | ||
} | ||
|
||
public void copyFile() { | ||
DirectoryDialogConfiguration dirDialogConfiguration = new DirectoryDialogConfiguration.Builder() | ||
.withInitialDirectory(Paths.get(Globals.prefs.get(JabRefPreferences.EXPORT_WORKING_DIRECTORY))) | ||
.build(); | ||
Optional<Path> exportPath = dialogService.showDirectorySelectionDialog(dirDialogConfiguration); | ||
exportPath.ifPresent(this::copyFileToDestination); | ||
|
||
} | ||
|
||
private void copyFileToDestination(Path exportPath) { | ||
Optional<Path> fileToExport = linkedFile.findIn(databaseContext, Globals.prefs.getFilePreferences()); | ||
Optional<Path> newPath = OptionalUtil.combine(Optional.of(exportPath), fileToExport, resolvePathFilename); | ||
|
||
if (newPath.isPresent()) { | ||
Path newFile = newPath.get(); | ||
boolean success = FileUtil.copyFile(fileToExport.get(), newFile, false); | ||
if (success) { | ||
dialogService.showInformationDialogAndWait(Localization.lang("Copy linked file"), Localization.lang("Sucessfully copied file to %0", newPath.map(Path::getParent).map(Path::toString).orElse(""))); | ||
} | ||
else { | ||
dialogService.showErrorDialogAndWait(Localization.lang("Copy linked file"), Localization.lang("Could not copy file to %0, maybe the file is already existing?", newPath.map(Path::getParent).map(Path::toString).orElse(""))); | ||
} | ||
} | ||
else { | ||
dialogService.showErrorDialogAndWait(Localization.lang("Could not resolve the file %0", fileToExport.map(Path::getParent).map(Path::toString).orElse(""))); | ||
} | ||
|
||
} | ||
} |
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
Oops, something went wrong.