TreeView的水平节点方向错误

时间:2018-08-06 11:30:12

标签: javafx drag-and-drop treeview orientation

我面临着与DnD上正确的节点方向有关的问题。复选框绘制得太左。 MCVE在这里:

public final class TestplaygroundUi extends Application {

    private final DataFormat objectDataFormat = new DataFormat("application/x-java-serialized-object");

    /**
     * Constructor.
     */
    public TestplaygroundUi() {
        // empty.
    }

    /**
     * @param args Program arguments.
     */
    public static void main(final String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) throws Exception {

        TreeItem<PlanningItem> treeItemRoot = new TreeItem<>(new PlanningItem(1.0), new Label("ROOT"));

        TreeItem<PlanningItem> nodeItemA = new TreeItem<>(new PlanningItem(2), new Label("A"));
        TreeItem<PlanningItem> nodeItemB = new TreeItem<>(new PlanningItem(2), new Label("B"));
        treeItemRoot.getChildren().add(nodeItemA);
        treeItemRoot.getChildren().add(nodeItemB);

        TreeItem<PlanningItem> nodeItemA1 = new TreeItem<>(new PlanningItem("A1"), new Label("A1"));
        TreeItem<PlanningItem> nodeItemB1 = new TreeItem<>(new PlanningItem("B1"), new Label("B1"));
        nodeItemA.getChildren().add(nodeItemA1);
        nodeItemB.getChildren().add(nodeItemB1);

        TreeView<PlanningItem> treeView = new TreeView<>(treeItemRoot);

        treeView.setCellFactory(new Callback<TreeView<PlanningItem>, TreeCell<PlanningItem>>() {
            @Override
            public PlanningCheckBoxTreeCell call(TreeView<PlanningItem> siTreeView) {
                final PlanningCheckBoxTreeCell cell = new PlanningCheckBoxTreeCell();

                cell.setOnDragDetected(new EventHandler<MouseEvent>() {
                    public void handle(MouseEvent event) {
                        System.out.println("onDragDetected");

                        Dragboard db = treeView.startDragAndDrop(TransferMode.ANY);
                        TreeItem<PlanningItem> item = treeView.getSelectionModel().getSelectedItem();

                        ClipboardContent content = new ClipboardContent();
                        content.put(objectDataFormat, item.getValue());

                        // content has to be defined before it is set in the Dragboard.
                        db.setContent(content);

                        event.consume();
                    }
                });

                cell.setOnDragOver(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        System.out.println("onDragOver");

                        if (event.getGestureSource() == treeView) {

                            if (event.getDragboard().hasContent(objectDataFormat)) {
                                event.acceptTransferModes(TransferMode.COPY_OR_MOVE);
                            }

                        } else {
                            event.acceptTransferModes(TransferMode.NONE);
                        }

                        event.consume();
                    }
                });

                cell.setOnDragEntered(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        /* the drag-and-drop gesture entered the target */
                        System.out.println("onDragEntered");
                        /* show to the user that it is an actual gesture target */
                        if (event.getGestureSource() != cell
                                && event.getDragboard().hasContent(objectDataFormat)) {
                            cell.setTextFill(Color.GREEN);
                        }

                        event.consume();
                    }
                });

                cell.setOnDragExited(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        System.out.println("onDragExited");

                        /* mouse moved away, remove the graphical cues */
                        cell.setTextFill(Color.BLACK);

                        event.consume();
                    }
                });

                cell.setOnDragDropped(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        System.out.println("onDragDropped");

                        Dragboard db = event.getDragboard();
                        boolean success = false;

                        PlanningItem source = (PlanningItem) db.getContent(objectDataFormat);

                        @SuppressWarnings("unchecked")
                        TreeItem<PlanningItem> sourceTreeItem = ((TreeView<PlanningItem>) event
                                .getGestureSource()).getSelectionModel().getSelectedItem();

                        TreeItem<PlanningItem> parent = sourceTreeItem.getParent();
                        cell.getTreeItem().getChildren().add(sourceTreeItem);
                        parent.getChildren().remove(sourceTreeItem);

                        event.setDropCompleted(success);

                        // controller.tvProject.setNodeOrientation(NodeOrientation.LEFT_TO_RIGHT);
                        // controller.tvProject.refresh();

                        System.out.println("Drop Completed.");

                        event.consume();
                    }
                });

                cell.setOnDragDone(new EventHandler<DragEvent>() {
                    public void handle(DragEvent event) {
                        /* the drag-and-drop gesture ended */
                        System.out.println("onDragDone");
                        /* if the data was successfully moved, clear it */
                        if (event.getTransferMode() == TransferMode.MOVE) {
                            cell.setText("");
                        }

                        event.consume();
                    }
                });

                return cell;
            };
        });

        StackPane root = new StackPane();
        root.getChildren().add(treeView);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.setTitle("Test TreeView");
        primaryStage.show();
    }
}

当将拖放的项目拖回原始位置时,似乎还留下了一些假象(没有标签的存储箱)。

PlanningItem

public class PlanningItem implements Serializable {

    private static final long serialVersionUID = 1L;
    private Double scene = null;
    private Integer path = null;
    private String move = null;

    /**
     * @return the scene
     */
    public Double getScene() {
        return scene;
    }

    /**
     * @param scene the scene to set
     */
    private void setScene(Double scene) {
        this.scene = scene;
    }

    /**
     * @return the path
     */
    public Integer getPath() {
        return path;
    }

    /**
     * @param path the path to set
     */
    private void setPath(Integer path) {
        this.path = path;
    }

    /**
     * @return the move
     */
    public String getMove() {
        return move;
    }

    /**
     * @param move the move to set
     */
    private void setMove(String move) {
        this.move = move;
    }

    public PlanningItem(Object item) {
        super();
        if (item instanceof Double) {
            setScene((Double) item);
        } else if (item instanceof Integer) {
            setPath((Integer) item);
        } else if (item instanceof String) {
            setMove((String) item);
        }
    }

}

更新原始程序的屏幕截图:

TreeView with wrong node orientation

更新 PlanningCheckBoxTreeCell

public class PlanningCheckBoxTreeCell extends CheckBoxTreeCell<PlanningItem> {

    public PlanningCheckBoxTreeCell() {
    }

    @Override
    public void updateItem(PlanningItem item, boolean empty) {
        super.updateItem(item, empty);
        if (!empty) {
            setText(null);
        }

    }
}

0 个答案:

没有答案