我正在尝试在树状视图中实现拖放。我确实做到了。它 作品。在三分之一的时间。 否则,有时相关事件没有到达, 无处可去。
虽然我打了个电话,但我没有看到dragDone
事件触发
setDropCompleted(true)
事件中的dragDropped
。
如果我进行手动测试,我只是看不到事件之间的太多相关性
到达和鼠标移动。
dragDropped
个事件突然出现,一些单元格收到了
dragEnter/Exit/Over
事件,有些则没有,事件源设置为
模糊的价值观。
这就是我的测试方式(完整代码请参见DragAndDropTest.java)
@Test
public void
when_the_drag_is_dropped_the_element_is_moved(
final FxRobot robot
) {
final ZentaTreeCell startCell = findCellFor(newElement);
robot.drag(startCell);
final ZentaTreeCell targetCell = findCellFor(thing);
final int targetIndex = targetCell.getIndex();
robot.moveTo(targetCell);
robot.dropTo(targetCell);
WaitForAsyncUtils.waitForFxEvents();
final ZentaTreeCell endCell = findCellFor(newElement);
assertEquals(targetIndex + 1, endCell.getIndex());
}
以下是我的TreeCell
实现的相关行(完整代码为ZentaTreeCell):
public class ZentaTreeCell extends TreeCell < ZentaElement > {
public static final DataFormat ZENTA_FORMAT =
new DataFormat("Zenta Elements");
private final TreeView < ZentaElement > treeView;
public ZentaTreeCell(final TreeView < ZentaElement > view) {
super();
treeView = view;
setOnDragDetected((e) - > dragDetected(e));
setOnDragEntered((e) - > dragEntered(e));
setOnDragOver((e) - > dragOver(e));
setOnDragExited((e) - > dragExited(e));
setOnDragDropped((e) - > dragDropped(e));
setOnDragDone((e) - > dragDone(e));
}
private void dragDone(final DragEvent e) {
System.out.println("done\n\tat " + this + "\n\t event: " + e);
}
private void dragDropped(final DragEvent e) {
System.out.println("dropped\n\tat " + this + "\n\t event: " + e);
e.setDropCompleted(true);
e.getDragboard().clear();
e.consume();
getStyleClass().remove("dragtarget");
final ZentaTreeCell gestureSource = (ZentaTreeCell)
e.getGestureSource();
final ZentaTreeCell gestureTarget = (ZentaTreeCell)
e.getGestureTarget();
final TreeItem < ZentaElement > draggedItem = gestureSource.getTreeItem();
final TreeItem < ZentaElement > draggedItemParent =
draggedItem.getParent();
final TreeItem < ZentaElement > droppeditem = gestureTarget.getTreeItem();
final TreeItem < ZentaElement > droppedItemParent =
droppeditem.getParent();
final int indexInParent =
droppedItemParent.getChildren().indexOf(droppeditem);
draggedItemParent.getChildren().remove(draggedItem);
droppedItemParent.getChildren().add(indexInParent + 1, draggedItem);
treeView.getSelectionModel().select(draggedItem);
}
private void dragOver(final DragEvent e) {
System.out.println("over\n\tat " + this + "\n\t event: " + e);
e.acceptTransferModes(TransferMode.MOVE);
e.consume();
}
private void dragEntered(final DragEvent e) {
System.out.println("entered\n\tat " + this + "\n\t event: " + e);
getStyleClass().add("dragtarget");
e.consume();
}
private void dragExited(final DragEvent e) {
System.out.println("exited\n\tat " + this + "\n\t event: " + e);
getStyleClass().remove("dragtarget");
e.consume();
}
public void dragDetected(final MouseEvent e) {
System.out.println("detected\n\tat " + this + "\n\t event: " + e);
System.out.println("dragDetected");
final Dragboard dragBoard = startDragAndDrop(TransferMode.MOVE);
final Map < DataFormat, Object > map = new HashMap < > ();
final ZentaElement item = getItem();
map.put(ZENTA_FORMAT, item);
System.out.println("item: " + item);
dragBoard.setContent(map);
e.consume();
}
}
我想我在这里错过了一些东西,但是阅读了所有 有关拖放的文档,我看不到。