diff --git a/src/main/java/org/jabref/gui/maintable/MainTable.java b/src/main/java/org/jabref/gui/maintable/MainTable.java index 553f22a98b21..d418bc0a209f 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTable.java +++ b/src/main/java/org/jabref/gui/maintable/MainTable.java @@ -1,7 +1,6 @@ package org.jabref.gui.maintable; import java.awt.Color; -import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -13,7 +12,6 @@ import javax.swing.undo.UndoManager; import javafx.collections.ListChangeListener; -import javafx.collections.ObservableList; import javafx.scene.control.ScrollPane; import javafx.scene.control.SelectionMode; import javafx.scene.control.TableRow; @@ -57,6 +55,7 @@ public class MainTable extends TableView { private static final Logger LOGGER = LoggerFactory.getLogger(MainTable.class); + private static final DataFormat SERIALIZED_MIME_TYPE = new DataFormat("application/x-java-serialized-object"); private static GeneralRenderer defRenderer; private static GeneralRenderer reqRenderer; @@ -279,62 +278,75 @@ public void paste() { } } - private void handleOnDragOver(BibEntryTableViewModel originalItem, DragEvent event) { - if ((event.getGestureSource() != originalItem) && LocalDragboard.INSTANCE.hasType(DragAndDropDataFormats.BIBENTRY_LIST_CLASS)) { + private void handleOnDragOver(TableRow row, BibEntryTableViewModel originalItem, DragEvent event) { + Dragboard db = event.getDragboard(); + + /* + * if ((event.getGestureSource() != originalItem) && LocalDragboard.INSTANCE.hasType(DragAndDropDataFormats.BIBENTRY_LIST_CLASS)) { event.acceptTransferModes(TransferMode.MOVE); } + */ + if ((event.getGestureSource() != originalItem) && db.hasContent(SERIALIZED_MIME_TYPE)) { + event.acceptTransferModes(TransferMode.COPY_OR_MOVE); + + } + if (event.getDragboard().hasFiles()) { event.acceptTransferModes(TransferMode.COPY, TransferMode.LINK); } + event.consume(); } private void handleOnDragEntered(TableRow row, BibEntryTableViewModel entry, MouseDragEvent event) { // Support the following gesture to select entries: click on one row -> hold mouse button -> move over other rows // We need to select all items between the starting row and the row where the user currently hovers the mouse over // It is not enough to just select the currently hovered row since then sometimes rows are not marked selected if the user moves to fast - TableRow sourceRow = (TableRow) event.getGestureSource(); - getSelectionModel().selectRange(sourceRow.getIndex(), row.getIndex()); + // TableRow sourceRow = (TableRow) event.getGestureSource(); + // getSelectionModel().selectRange(sourceRow.getIndex(), row.getIndex()); + event.consume(); } private void handleOnDragDetected(TableRow row, BibEntryTableViewModel entry, MouseEvent event) { // Start drag'n'drop - row.startFullDrag(); + //row.startFullDrag(); - List entries = getSelectionModel().getSelectedItems().stream().map(BibEntryTableViewModel::getEntry).collect(Collectors.toList()); + if (!row.isEmpty()) { + Integer index = row.getIndex(); + Dragboard db = row.startDragAndDrop(TransferMode.MOVE); + db.setDragView(row.snapshot(null, null)); + ClipboardContent cc = new ClipboardContent(); + cc.put(SERIALIZED_MIME_TYPE, index); + db.setContent(cc); - if (entries != null) { - ClipboardContent content = new ClipboardContent(); - Dragboard dragboard = startDragAndDrop(TransferMode.MOVE); - content.put(DragAndDropDataFormats.ENTRIES, ""); - dragboard.setContent(content); + List entries = getSelectionModel().getSelectedItems().stream().map(BibEntryTableViewModel::getEntry).collect(Collectors.toList()); LocalDragboard.INSTANCE.putValue(DragAndDropDataFormats.BIBENTRY_LIST_CLASS, entries); + event.consume(); } - - event.consume(); } - private void handleOnDragDropped(BibEntryTableViewModel originalItem, DragEvent event) { + private void handleOnDragDropped(TableRow row, BibEntryTableViewModel originalItem, DragEvent event) { - boolean success = false; + Dragboard db = event.getDragboard(); + if (db.hasContent(SERIALIZED_MIME_TYPE)) { + int draggedIndex = (Integer) db.getContent(SERIALIZED_MIME_TYPE); - ObservableList items = this.itemsProperty().get(); + BibEntry entry = this.getItems().get(draggedIndex).getEntry(); + model.removeEntry(entry); + int dropIndex; - if (LocalDragboard.INSTANCE.hasType(DragAndDropDataFormats.BIBENTRY_LIST_CLASS)) { - List parsedEntries = LocalDragboard.INSTANCE.getValue(DragAndDropDataFormats.BIBENTRY_LIST_CLASS); - success = true; - } - if (event.getDragboard().hasContent(DataFormat.FILES)) { + if (row.isEmpty()) { + dropIndex = this.getItems().size(); + } else { + dropIndex = row.getIndex(); + } - List files = event.getDragboard().getFiles(); - System.out.println(files); + this.getItems().add(dropIndex, new BibEntryTableViewModel(entry)); + event.setDropCompleted(true); + this.getSelectionModel().select(dropIndex); + event.consume(); } - event.setDropCompleted(success); - event.consume(); - } - - public void addSelectionListener(ListChangeListener listener) { getSelectionModel().getSelectedItems().addListener(listener); } diff --git a/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java b/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java index 6ee7c5d7c2da..23f3846b69d1 100644 --- a/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java +++ b/src/main/java/org/jabref/gui/maintable/MainTableDataModel.java @@ -22,10 +22,10 @@ public class MainTableDataModel { private final FilteredList entriesFiltered; private final SortedList entriesSorted; + private final ObservableList allEntries; public MainTableDataModel(BibDatabaseContext context) { - ObservableList allEntries = context.getDatabase().getEntries(); - + allEntries = context.getDatabase().getEntries(); ObservableList entriesViewModel = BindingsHelper.mapBacked(allEntries, BibEntryTableViewModel::new); entriesFiltered = new FilteredList<>(entriesViewModel); @@ -69,6 +69,11 @@ private Optional createGroupMatcher(List selectedGrou return Optional.of(searchRules); } + public void removeEntry(BibEntry entry) { + this.allEntries.remove(entry); + + } + public ObservableList getEntriesFiltered() { return entriesSorted; } diff --git a/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java b/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java index 72d1de5e572d..f8f37013b7f6 100644 --- a/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java +++ b/src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java @@ -24,10 +24,10 @@ public class ViewModelTableRowFactory implements Callback, Table private BiConsumer onMouseClickedEvent; private Function contextMenuFactory; private TriConsumer, S, ? super MouseEvent> toOnDragDetected; - private BiConsumer toOnDragDropped; + private TriConsumer, S, ? super DragEvent> toOnDragDropped; private BiConsumer toOnDragEntered; private BiConsumer toOnDragExited; - private BiConsumer toOnDragOver; + private TriConsumer, S, ? super DragEvent> toOnDragOver; private TriConsumer, S, ? super MouseDragEvent> toOnMouseDragEntered; public ViewModelTableRowFactory withOnMouseClickedEvent(BiConsumer onMouseClickedEvent) { @@ -50,7 +50,7 @@ public ViewModelTableRowFactory setOnDragDetected(BiConsumer setOnDragDropped(BiConsumer toOnDragDropped) { + public ViewModelTableRowFactory setOnDragDropped(TriConsumer, S, ? super DragEvent> toOnDragDropped) { this.toOnDragDropped = toOnDragDropped; return this; } @@ -75,7 +75,7 @@ public ViewModelTableRowFactory setOnDragExited(BiConsumer setOnDragOver(BiConsumer toOnDragOver) { + public ViewModelTableRowFactory setOnDragOver(TriConsumer, S, ? super DragEvent> toOnDragOver) { this.toOnDragOver = toOnDragOver; return this; } @@ -113,7 +113,7 @@ public TableRow call(TableView tableView) { if (toOnDragDropped != null) { row.setOnDragDropped(event -> { if (!row.isEmpty()) { - toOnDragDropped.accept(row.getItem(), event); + toOnDragDropped.accept(row, row.getItem(), event); } }); } @@ -134,7 +134,7 @@ public TableRow call(TableView tableView) { if (toOnDragOver != null) { row.setOnDragOver(event -> { if (!row.isEmpty()) { - toOnDragOver.accept(row.getItem(), event); + toOnDragOver.accept(row, row.getItem(), event); } }); } diff --git a/src/main/java/org/jabref/model/database/BibDatabase.java b/src/main/java/org/jabref/model/database/BibDatabase.java index 40e6abcc0ff5..0fa0997dc0e1 100644 --- a/src/main/java/org/jabref/model/database/BibDatabase.java +++ b/src/main/java/org/jabref/model/database/BibDatabase.java @@ -116,7 +116,7 @@ public boolean containsEntryWithId(String id) { } public ObservableList getEntries() { - return FXCollections.unmodifiableObservableList(entries); + return entries; } /**