Skip to content

Commit

Permalink
Merge pull request #5068 from JabRef/copyLinkedFilesInContextmenu
Browse files Browse the repository at this point in the history
Add copy linked files action to contextmneu in general tab
  • Loading branch information
Siedlerchr committed Jul 6, 2019
2 parents a50cb34 + c2cacfe commit add35be
Show file tree
Hide file tree
Showing 12 changed files with 155 additions and 70 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/jabref/gui/copyfiles/CopyFilesAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public CopyFilesAction(StateManager stateManager, DialogService dialogService) {
this.executable.bind(needsDatabase(this.stateManager).and(needsEntriesSelected(stateManager)));
}

private void showDialog(List<CopyFilesResultItemViewModel> data, BibDatabaseContext database) {
private void showDialog(List<CopyFilesResultItemViewModel> data) {
if (data.isEmpty()) {
dialogService.showInformationDialogAndWait(Localization.lang("Copy linked files to folder..."), Localization.lang("No linked files found for export."));
return;
}
CopyFilesDialogView dialog = new CopyFilesDialogView(database, new CopyFilesResultListDependency(data));
CopyFilesDialogView dialog = new CopyFilesDialogView(new CopyFilesResultListDependency(data));
dialog.showAndWait();
}

Expand All @@ -57,7 +57,7 @@ public void execute() {
Localization.lang("Copy linked files to folder..."),
exportTask);
Globals.TASK_EXECUTOR.execute(exportTask);
exportTask.setOnSucceeded((e) -> showDialog(exportTask.getValue(), database));
exportTask.setOnSucceeded((e) -> showDialog(exportTask.getValue()));
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import org.jabref.gui.util.BaseDialog;
import org.jabref.gui.util.ValueTableCellFactory;
import org.jabref.logic.l10n.Localization;
import org.jabref.model.database.BibDatabaseContext;

import com.airhacks.afterburner.views.ViewLoader;
import de.jensd.fx.glyphs.materialdesignicons.MaterialDesignIcon;
Expand All @@ -24,7 +23,7 @@ public class CopyFilesDialogView extends BaseDialog<Void> {
@FXML private TableColumn<CopyFilesResultItemViewModel, String> colFile;
private final CopyFilesDialogViewModel viewModel;

public CopyFilesDialogView(BibDatabaseContext bibDatabaseContext, CopyFilesResultListDependency results) {
public CopyFilesDialogView(CopyFilesResultListDependency results) {
this.setTitle(Localization.lang("Result"));

this.getDialogPane().getButtonTypes().addAll(ButtonType.OK);
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/org/jabref/gui/copyfiles/CopySingleFileAction.java
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("")));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ private void addLinkedFileFromURL(URL url, BibEntry entry, Path targetDirectory)
basePanel.getBibDatabaseContext(),
Globals.TASK_EXECUTOR,
dialogService,
JabRefPreferences.getInstance(), ExternalFileTypes.getInstance());
JabRefPreferences.getInstance().getXMPPreferences(),
JabRefPreferences.getInstance().getFilePreferences(),
ExternalFileTypes.getInstance());

try {
URLDownload urlDownload = new URLDownload(newLinkedFile.getLink());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import org.jabref.model.metadata.FilePreferences;
import org.jabref.model.strings.StringUtil;
import org.jabref.model.util.OptionalUtil;
import org.jabref.preferences.JabRefPreferences;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -73,19 +72,20 @@ public LinkedFileViewModel(LinkedFile linkedFile,
BibDatabaseContext databaseContext,
TaskExecutor taskExecutor,
DialogService dialogService,
JabRefPreferences preferences,
XmpPreferences xmpPreferences,
FilePreferences filePreferences,
ExternalFileTypes externalFileTypes) {

this.linkedFile = linkedFile;
this.filePreferences = preferences.getFilePreferences();
this.filePreferences = filePreferences;
this.linkedFileHandler = new LinkedFileHandler(linkedFile, entry, databaseContext, filePreferences);
this.databaseContext = databaseContext;
this.entry = entry;
this.dialogService = dialogService;
this.taskExecutor = taskExecutor;
this.externalFileTypes = externalFileTypes;

xmpPreferences = preferences.getXMPPreferences();
this.xmpPreferences = xmpPreferences;

downloadOngoing.bind(downloadProgress.greaterThanOrEqualTo(0).and(downloadProgress.lessThan(1)));
canWriteXMPMetadata.setValue(!linkedFile.isOnlineLink() && linkedFile.getFileType().equalsIgnoreCase("pdf"));
}
Expand Down
28 changes: 19 additions & 9 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.DragAndDropDataFormats;
import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider;
import org.jabref.gui.copyfiles.CopySingleFileAction;
import org.jabref.gui.keyboard.KeyBinding;
import org.jabref.gui.util.TaskExecutor;
import org.jabref.gui.util.ViewModelListCellFactory;
Expand All @@ -48,23 +49,28 @@ public class LinkedFilesEditor extends HBox implements FieldEditorFX {
@FXML private final LinkedFilesEditorViewModel viewModel;
@FXML private ListView<LinkedFileViewModel> listView;

private final DialogService dialogService;
private final BibDatabaseContext databaseContext;

public LinkedFilesEditor(String fieldName, DialogService dialogService, BibDatabaseContext databaseContext, TaskExecutor taskExecutor, AutoCompleteSuggestionProvider<?> suggestionProvider,
FieldCheckers fieldCheckers,
JabRefPreferences preferences) {
this.viewModel = new LinkedFilesEditorViewModel(fieldName, suggestionProvider, dialogService, databaseContext, taskExecutor, fieldCheckers, preferences);

this.viewModel = new LinkedFilesEditorViewModel(fieldName, suggestionProvider, dialogService, databaseContext, taskExecutor, fieldCheckers, preferences);
this.dialogService = dialogService;
this.databaseContext = databaseContext;
ViewLoader.view(this)
.root(this)
.load();

ViewModelListCellFactory<LinkedFileViewModel> cellFactory = new ViewModelListCellFactory<LinkedFileViewModel>()
.withTooltip(LinkedFileViewModel::getDescription)
.withGraphic(LinkedFilesEditor::createFileDisplay)
.withContextMenu(this::createContextMenuForFile)
.withOnMouseClickedEvent(this::handleItemMouseClick)
.setOnDragDetected(this::handleOnDragDetected)
.setOnDragDropped(this::handleOnDragDropped)
.setOnDragOver(this::handleOnDragOver);
.withTooltip(LinkedFileViewModel::getDescription)
.withGraphic(LinkedFilesEditor::createFileDisplay)
.withContextMenu(this::createContextMenuForFile)
.withOnMouseClickedEvent(this::handleItemMouseClick)
.setOnDragDetected(this::handleOnDragDetected)
.setOnDragDropped(this::handleOnDragDropped)
.setOnDragOver(this::handleOnDragOver);

listView.setCellFactory(cellFactory);

Expand Down Expand Up @@ -234,6 +240,10 @@ private ContextMenu createContextMenuForFile(LinkedFileViewModel linkedFile) {
renameAndMoveFile.setOnAction(event -> linkedFile.moveToDefaultDirectoryAndRename());
renameAndMoveFile.setDisable(linkedFile.getFile().isOnlineLink() || linkedFile.isGeneratedPathSameAsOriginal());

MenuItem copyLinkedFile = new MenuItem(Localization.lang("Copy linked file to folder..."));
copyLinkedFile.setOnAction(event -> new CopySingleFileAction(linkedFile.getFile(), dialogService, databaseContext).copyFile());
copyLinkedFile.setDisable(linkedFile.getFile().isOnlineLink());

MenuItem deleteFile = new MenuItem(Localization.lang("Permanently delete local file"));
deleteFile.setOnAction(event -> viewModel.deleteFile(linkedFile));
deleteFile.setDisable(linkedFile.getFile().isOnlineLink());
Expand All @@ -248,7 +258,7 @@ private ContextMenu createContextMenuForFile(LinkedFileViewModel linkedFile) {
if (linkedFile.getFile().isOnlineLink()) {
menu.getItems().add(download);
}
menu.getItems().addAll(renameFile, renameFileName, moveFile, renameAndMoveFile, deleteLink, deleteFile);
menu.getItems().addAll(renameFile, renameFileName, moveFile, renameAndMoveFile, copyLinkedFile, deleteLink, deleteFile);

return menu;
}
Expand Down
Loading

0 comments on commit add35be

Please sign in to comment.