private AbstractDropTarget makeDropTarget()

in kit/src/main/java/com/oracle/javafx/scenebuilder/kit/editor/panel/hierarchy/HierarchyDNDController.java [235:398]


    private AbstractDropTarget makeDropTarget(
            final TreeItem<HierarchyItem> treeItem,
            final DroppingMouseLocation location) {

        assert location != null;

        final TreeItem<HierarchyItem> rootTreeItem = panelController.getRoot();
        final FXOMObject dropTargetObject;
        final AbstractDropTarget result;
        Accessory accessory = null; // Used if we insert as accessory (drop over a place holder)
        int targetIndex = -1; // Used if we insert as sub components

        final FXOMDocument document = panelController.getEditorController().getFxomDocument();
        if (document == null || document.getFxomRoot() == null) {
            return new RootDropTarget();
        }
        // TreeItem is null when dropping below the datas
        // => the drop target is the root
        if (treeItem == null) {
            dropTargetObject = rootTreeItem.getValue().getFxomObject();

        } else {
            final HierarchyItem item = treeItem.getValue();
            assert item != null;

            // When the TreeItem is a place holder :
            // - if the place holder is empty
            //      the drop target is the place holder parent
            //      the accessory is set to the place holder value
            // whatever the location value is.
            // - otherwise
            //      the drop target is the place holder item 
            //      the accessory is set to null
            //      the target index is set depending on the location value
            //------------------------------------------------------------------
            if (item.isPlaceHolder()) { // (1)

                assert treeItem != rootTreeItem;
                assert item instanceof HierarchyItemBorderPane
                        || item instanceof HierarchyItemGraphic
                        || item instanceof HierarchyItemDialogPane;

                if (item.isEmpty()) {
                    // Set the drop target
                    final TreeItem<HierarchyItem> parentTreeItem = treeItem.getParent();
                    assert parentTreeItem != null; // Because of (1)
                    dropTargetObject = parentTreeItem.getValue().getFxomObject();
                    // Set the accessory
                    if (item instanceof HierarchyItemBorderPane) {
                        accessory = ((HierarchyItemBorderPane) item).getPosition();
                    } else if (item instanceof HierarchyItemDialogPane) {
                        accessory = ((HierarchyItemDialogPane) item).getAccessory();
                    } else if (item instanceof HierarchyItemExpansionPanel) {
                        accessory = ((HierarchyItemExpansionPanel) item).getAccessory();
                    } else if (item instanceof HierarchyItemExpandedPanel) {
                        accessory = ((HierarchyItemExpandedPanel) item).getAccessory();
                    } else {
                        accessory = Accessory.GRAPHIC;
                    }
                } else {
                    // Set the drop target
                    dropTargetObject = item.getFxomObject();
                    // Set the accessory
                    accessory = null;
                    // Set the target index
                    switch (location) {
                        case CENTER:
                        case TOP:
                            targetIndex = -1; // Insert at last position
                            break;
                        case BOTTOM:
                            if (treeItem.isLeaf() || !treeItem.isExpanded()) {
                                targetIndex = -1; // Insert at last position
                            } else {
                                targetIndex = 0; // Insert at first position
                            }
                            break;
                        default:
                            assert false;
                            break;

                    }
                }
            } //
            // TreeItem is not a place holder:
            // we set the drop target, accessory and target index 
            // depending on the mouse location value
            //------------------------------------------------------------------
            else {
                switch (location) {

                    // REPARENTING
                    case CENTER:
                        dropTargetObject = item.getFxomObject();
                        targetIndex = -1; // Insert at last position
                        break;

                    // REORDERING ABOVE
                    case TOP:
                        // Dropping on TOP of the root TreeItem
                        if (treeItem == rootTreeItem) { // (2)
                            dropTargetObject = item.getFxomObject();
                            targetIndex = -1; // Insert at last position
                        } else {
                            // If the parent accepts sub components,
                            // this is a reordering gesture and the target is the parent
                            final DragController dragController
                                    = panelController.getEditorController().getDragController();
                            final AbstractDragSource dragSource = dragController.getDragSource();
                            final TreeItem<HierarchyItem> parentTreeItem = treeItem.getParent();
                            assert parentTreeItem != null; // Because of (2)
                            final FXOMObject parentObject = parentTreeItem.getValue().getFxomObject();
                            final DesignHierarchyMask parentMask = new DesignHierarchyMask(parentObject);
                            if (parentMask.isAcceptingSubComponent(dragSource.getDraggedObjects())) {
                                dropTargetObject = parentTreeItem.getValue().getFxomObject();
                                targetIndex = item.getFxomObject().getIndexInParentProperty();
                            } // Otherwise, attempt to set an accessory on the current TreeItem
                            else {
                                dropTargetObject = item.getFxomObject();
                            }
                        }
                        break;

                    // REORDERING BELOW
                    case BOTTOM:
                        // Dropping on BOTTOM of the root TreeItem
                        if (treeItem == rootTreeItem) { // (3)
                            dropTargetObject = item.getFxomObject();
                            targetIndex = 0; // Insert at first position
                        } else {
                            if (treeItem.isLeaf() || !treeItem.isExpanded()) {
                                // If the parent accepts sub components,
                                // this is a reordering gesture and the target is the parent
                                final DragController dragController
                                        = panelController.getEditorController().getDragController();
                                final AbstractDragSource dragSource = dragController.getDragSource();
                                final TreeItem<HierarchyItem> parentTreeItem = treeItem.getParent();
                                assert parentTreeItem != null; // Because of (3)
                                final FXOMObject parentObject = parentTreeItem.getValue().getFxomObject();
                                final DesignHierarchyMask parentMask = new DesignHierarchyMask(parentObject);
                                if (parentMask.isAcceptingSubComponent(dragSource.getDraggedObjects())) {
                                    dropTargetObject = parentTreeItem.getValue().getFxomObject();
                                    targetIndex = item.getFxomObject().getIndexInParentProperty() + 1;
                                } // Otherwise, attempt to set an accessory on the current TreeItem
                                else {
                                    dropTargetObject = item.getFxomObject();
                                }
                            } else {
                                dropTargetObject = item.getFxomObject();
                                targetIndex = 0; // Insert at first position
                            }
                        }
                        break;
                    default:
                        assert false;
                        dropTargetObject = null;
                        break;
                }
            }
        }

        result = makeDropTarget(dropTargetObject, accessory, targetIndex);
        return result;
    }