Skip to content

Commit

Permalink
try around with dnd on maintable
Browse files Browse the repository at this point in the history
does not yet work because of f*cking unmodiefable collections argh!
  • Loading branch information
Siedlerchr committed Feb 23, 2018
1 parent 9eee5ae commit 924e06f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 39 deletions.
72 changes: 42 additions & 30 deletions src/main/java/org/jabref/gui/maintable/MainTable.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -57,6 +55,7 @@
public class MainTable extends TableView<BibEntryTableViewModel> {

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;
Expand Down Expand Up @@ -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<BibEntryTableViewModel> 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<BibEntryTableViewModel> 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<BibEntryTableViewModel> sourceRow = (TableRow<BibEntryTableViewModel>) event.getGestureSource();
getSelectionModel().selectRange(sourceRow.getIndex(), row.getIndex());
// TableRow<BibEntryTableViewModel> sourceRow = (TableRow<BibEntryTableViewModel>) event.getGestureSource();
// getSelectionModel().selectRange(sourceRow.getIndex(), row.getIndex());
event.consume();
}

private void handleOnDragDetected(TableRow<BibEntryTableViewModel> row, BibEntryTableViewModel entry, MouseEvent event) {
// Start drag'n'drop
row.startFullDrag();
//row.startFullDrag();

List<BibEntry> 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<BibEntry> 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<BibEntryTableViewModel> 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<BibEntryTableViewModel> 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<BibEntry> 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<File> 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<? super BibEntryTableViewModel> listener) {
getSelectionModel().getSelectedItems().addListener(listener);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
public class MainTableDataModel {
private final FilteredList<BibEntryTableViewModel> entriesFiltered;
private final SortedList<BibEntryTableViewModel> entriesSorted;
private final ObservableList<BibEntry> allEntries;

public MainTableDataModel(BibDatabaseContext context) {
ObservableList<BibEntry> allEntries = context.getDatabase().getEntries();

allEntries = context.getDatabase().getEntries();
ObservableList<BibEntryTableViewModel> entriesViewModel = BindingsHelper.mapBacked(allEntries, BibEntryTableViewModel::new);

entriesFiltered = new FilteredList<>(entriesViewModel);
Expand Down Expand Up @@ -69,6 +69,11 @@ private Optional<MatcherSet> createGroupMatcher(List<GroupTreeNode> selectedGrou
return Optional.of(searchRules);
}

public void removeEntry(BibEntry entry) {
this.allEntries.remove(entry);

}

public ObservableList<BibEntryTableViewModel> getEntriesFiltered() {
return entriesSorted;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/jabref/gui/util/ViewModelTableRowFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ public class ViewModelTableRowFactory<S> implements Callback<TableView<S>, Table
private BiConsumer<S, ? super MouseEvent> onMouseClickedEvent;
private Function<S, ContextMenu> contextMenuFactory;
private TriConsumer<TableRow<S>, S, ? super MouseEvent> toOnDragDetected;
private BiConsumer<S, ? super DragEvent> toOnDragDropped;
private TriConsumer<TableRow<S>, S, ? super DragEvent> toOnDragDropped;
private BiConsumer<S, ? super DragEvent> toOnDragEntered;
private BiConsumer<S, ? super DragEvent> toOnDragExited;
private BiConsumer<S, ? super DragEvent> toOnDragOver;
private TriConsumer<TableRow<S>, S, ? super DragEvent> toOnDragOver;
private TriConsumer<TableRow<S>, S, ? super MouseDragEvent> toOnMouseDragEntered;

public ViewModelTableRowFactory<S> withOnMouseClickedEvent(BiConsumer<S, ? super MouseEvent> onMouseClickedEvent) {
Expand All @@ -50,7 +50,7 @@ public ViewModelTableRowFactory<S> setOnDragDetected(BiConsumer<S, ? super Mouse
return this;
}

public ViewModelTableRowFactory<S> setOnDragDropped(BiConsumer<S, ? super DragEvent> toOnDragDropped) {
public ViewModelTableRowFactory<S> setOnDragDropped(TriConsumer<TableRow<S>, S, ? super DragEvent> toOnDragDropped) {
this.toOnDragDropped = toOnDragDropped;
return this;
}
Expand All @@ -75,7 +75,7 @@ public ViewModelTableRowFactory<S> setOnDragExited(BiConsumer<S, ? super DragEve
return this;
}

public ViewModelTableRowFactory<S> setOnDragOver(BiConsumer<S, ? super DragEvent> toOnDragOver) {
public ViewModelTableRowFactory<S> setOnDragOver(TriConsumer<TableRow<S>, S, ? super DragEvent> toOnDragOver) {
this.toOnDragOver = toOnDragOver;
return this;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public TableRow<S> call(TableView<S> tableView) {
if (toOnDragDropped != null) {
row.setOnDragDropped(event -> {
if (!row.isEmpty()) {
toOnDragDropped.accept(row.getItem(), event);
toOnDragDropped.accept(row, row.getItem(), event);
}
});
}
Expand All @@ -134,7 +134,7 @@ public TableRow<S> call(TableView<S> tableView) {
if (toOnDragOver != null) {
row.setOnDragOver(event -> {
if (!row.isEmpty()) {
toOnDragOver.accept(row.getItem(), event);
toOnDragOver.accept(row, row.getItem(), event);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public boolean containsEntryWithId(String id) {
}

public ObservableList<BibEntry> getEntries() {
return FXCollections.unmodifiableObservableList(entries);
return entries;
}

/**
Expand Down

0 comments on commit 924e06f

Please sign in to comment.