From e1564914ca8fd5dc2ba0e8ffa5e2277be0a0e7f4 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Wed, 6 Sep 2017 12:26:06 +0200 Subject: [PATCH 1/2] Add drag and drop support for files in General Tab --- .../gui/fieldeditors/LinkedFilesEditor.java | 37 +++++++++++++++++-- .../LinkedFilesEditorViewModel.java | 8 ++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java index 68ce5473702..27745013a06 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java @@ -1,6 +1,9 @@ package org.jabref.gui.fieldeditors; +import java.io.File; +import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import javafx.beans.binding.Bindings; import javafx.collections.ObservableList; @@ -63,14 +66,40 @@ public LinkedFilesEditor(String fieldName, DialogService dialogService, BibDatab listView.setCellFactory(cellFactory); + setUpFilesDragAndDrop(); Bindings.bindContentBidirectional(listView.itemsProperty().get(), viewModel.filesProperty()); setUpKeyBindings(); } + private void setUpFilesDragAndDrop() { + listView.setOnDragOver(event -> { + if (event.getDragboard().hasFiles()) { + event.acceptTransferModes(TransferMode.COPY, TransferMode.LINK); + } + }); + + listView.setOnDragDropped(event -> { + Dragboard dragboard = event.getDragboard(); + boolean success = false; + ObservableList items = listView.itemsProperty().get(); + + if (dragboard.hasFiles()) { + List linkedFiles = dragboard.getFiles().stream().map(File::toPath).map(viewModel::fromFile).collect(Collectors.toList()); + items.addAll(linkedFiles); + } + event.setDropCompleted(success); + event.consume(); + }); + + } + private void handleOnDragOver(LinkedFileViewModel originalItem, DragEvent event) { if ((event.getGestureSource() != originalItem) && event.getDragboard().hasContent(DragAndDropDataFormats.LINKED_FILE)) { event.acceptTransferModes(TransferMode.MOVE); } + if (event.getDragboard().hasFiles()) { + event.acceptTransferModes(TransferMode.COPY, TransferMode.LINK); + } } private void handleOnDragDetected(@SuppressWarnings("unused") LinkedFileViewModel linkedFile, MouseEvent event) { @@ -103,15 +132,15 @@ private void handleOnDragDropped(LinkedFileViewModel originalItem, DragEvent eve break; } } - int thisIdx = items.indexOf(originalItem); items.set(draggedIdx, originalItem); items.set(thisIdx, transferedItem); - - event.setDropCompleted(success); - event.consume(); success = true; } + if (dragboard.hasFiles()) { + List linkedFiles = dragboard.getFiles().stream().map(File::toPath).map(viewModel::fromFile).collect(Collectors.toList()); + items.addAll(linkedFiles); + } event.setDropCompleted(success); event.consume(); diff --git a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java index 8f5b9713e5e..36150ef3df6 100644 --- a/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java +++ b/src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditorViewModel.java @@ -95,6 +95,14 @@ private static LinkedFile fromFile(Path file, List fileDirectories) { return new LinkedFile("", relativePath.toString(), suggestedFileType.getName()); } + public LinkedFileViewModel fromFile(Path file) { + List fileDirectories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences()); + + LinkedFile linkedFile = fromFile(file, fileDirectories); + return new LinkedFileViewModel(linkedFile, entry, databaseContext); + + } + public boolean isFulltextLookupInProgress() { return fulltextLookupInProgress.get(); } From ffbaf4fabd52fe70278c7047f7fb28c9de88f057 Mon Sep 17 00:00:00 2001 From: Siedlerchr Date: Wed, 6 Sep 2017 15:23:04 +0200 Subject: [PATCH 2/2] add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd34f313489..c1c83c5ff64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ We refer to [GitHub issues](https://github.com/JabRef/jabref/issues) by using `# - We increased the size of the keywords and file text areas in the entry editor - When the entry that is currently shown in the entry editor is deleted externally, the editor is now closed automatically [#2946](https://github.com/JabRef/jabref/issues/2946) - We added reordering of file and link entries in the `General`-Tab [3165, comment](https://github.com/JabRef/jabref/issues/3165#issuecomment-326269715) +- We added drag and drop support for adding files directly in the `General`-Tab. The dragged files are currently only linked from their existing directory. For more advanced features use the `Add files` dialog. [#koppor#244](https://github.com/koppor/jabref/issues/244) ### Fixed