Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add drag and drop support for files in General Tab #3200

Merged
merged 2 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
37 changes: 33 additions & 4 deletions src/main/java/org/jabref/gui/fieldeditors/LinkedFilesEditor.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<LinkedFileViewModel> items = listView.itemsProperty().get();

if (dragboard.hasFiles()) {
List<LinkedFileViewModel> 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) {
Expand Down Expand Up @@ -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<LinkedFileViewModel> linkedFiles = dragboard.getFiles().stream().map(File::toPath).map(viewModel::fromFile).collect(Collectors.toList());
items.addAll(linkedFiles);
}
event.setDropCompleted(success);
event.consume();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ private static LinkedFile fromFile(Path file, List<Path> fileDirectories) {
return new LinkedFile("", relativePath.toString(), suggestedFileType.getName());
}

public LinkedFileViewModel fromFile(Path file) {
List<Path> fileDirectories = databaseContext.getFileDirectoriesAsPaths(Globals.prefs.getFileDirectoryPreferences());

LinkedFile linkedFile = fromFile(file, fileDirectories);
return new LinkedFileViewModel(linkedFile, entry, databaseContext);

}

public boolean isFulltextLookupInProgress() {
return fulltextLookupInProgress.get();
}
Expand Down