Search in sources :

Example 1 with Orientation

use of javafx.geometry.Orientation in project Gargoyle by callakrsos.

the class DockPane method dock.

/**
	 * Dock the node into this dock pane at the given docking position relative to the sibling in the
	 * layout. This is used to relatively position the dock nodes to other nodes given their preferred
	 * size.
	 *
	 * @param node The node that is to be docked into this dock pane.
	 * @param dockPos The docking position of the node relative to the sibling.
	 * @param sibling The sibling of this node in the layout.
	 */
public void dock(Node node, DockPos dockPos, Node sibling) {
    DockNodeEventHandler dockNodeEventHandler = new DockNodeEventHandler(node);
    dockNodeEventFilters.put(node, dockNodeEventHandler);
    node.addEventFilter(DockEvent.DOCK_OVER, dockNodeEventHandler);
    SplitPane split = (SplitPane) root;
    if (split == null) {
        split = new SplitPane();
        split.getItems().add(node);
        root = split;
        this.getChildren().add(root);
        return;
    }
    // find the parent of the sibling
    if (sibling != null && sibling != root) {
        Stack<Parent> stack = new Stack<Parent>();
        stack.push((Parent) root);
        while (!stack.isEmpty()) {
            Parent parent = stack.pop();
            ObservableList<Node> children = parent.getChildrenUnmodifiable();
            if (parent instanceof SplitPane) {
                SplitPane splitPane = (SplitPane) parent;
                children = splitPane.getItems();
            }
            for (int i = 0; i < children.size(); i++) {
                if (children.get(i) == sibling) {
                    split = (SplitPane) parent;
                } else if (children.get(i) instanceof Parent) {
                    stack.push((Parent) children.get(i));
                }
            }
        }
    }
    Orientation requestedOrientation = (dockPos == DockPos.LEFT || dockPos == DockPos.RIGHT) ? Orientation.HORIZONTAL : Orientation.VERTICAL;
    // if the orientation is different then reparent the split pane
    if (split.getOrientation() != requestedOrientation) {
        if (split.getItems().size() > 1) {
            SplitPane splitPane = new SplitPane();
            if (split == root && sibling == root) {
                this.getChildren().set(this.getChildren().indexOf(root), splitPane);
                splitPane.getItems().add(split);
                root = splitPane;
            } else {
                split.getItems().set(split.getItems().indexOf(sibling), splitPane);
                splitPane.getItems().add(sibling);
            }
            split = splitPane;
        }
        split.setOrientation(requestedOrientation);
    }
    // finally dock the node to the correct split pane
    ObservableList<Node> splitItems = split.getItems();
    double magnitude = 0;
    if (splitItems.size() > 0) {
        if (split.getOrientation() == Orientation.HORIZONTAL) {
            for (Node splitItem : splitItems) {
                magnitude += splitItem.prefWidth(0);
            }
        } else {
            for (Node splitItem : splitItems) {
                magnitude += splitItem.prefHeight(0);
            }
        }
    }
    if (dockPos == DockPos.LEFT || dockPos == DockPos.TOP) {
        int relativeIndex = 0;
        if (sibling != null && sibling != root) {
            relativeIndex = splitItems.indexOf(sibling);
        }
        splitItems.add(relativeIndex, node);
        if (splitItems.size() > 1) {
            if (split.getOrientation() == Orientation.HORIZONTAL) {
                split.setDividerPosition(relativeIndex, node.prefWidth(0) / (magnitude + node.prefWidth(0)));
            } else {
                split.setDividerPosition(relativeIndex, node.prefHeight(0) / (magnitude + node.prefHeight(0)));
            }
        }
    } else if (dockPos == DockPos.RIGHT || dockPos == DockPos.BOTTOM) {
        int relativeIndex = splitItems.size();
        if (sibling != null && sibling != root) {
            relativeIndex = splitItems.indexOf(sibling) + 1;
        }
        splitItems.add(relativeIndex, node);
        if (splitItems.size() > 1) {
            if (split.getOrientation() == Orientation.HORIZONTAL) {
                split.setDividerPosition(relativeIndex - 1, 1 - node.prefWidth(0) / (magnitude + node.prefWidth(0)));
            } else {
                split.setDividerPosition(relativeIndex - 1, 1 - node.prefHeight(0) / (magnitude + node.prefHeight(0)));
            }
        }
    }
}
Also used : Parent(javafx.scene.Parent) Node(javafx.scene.Node) SplitPane(javafx.scene.control.SplitPane) Orientation(javafx.geometry.Orientation) Stack(java.util.Stack)

Aggregations

Stack (java.util.Stack)1 Orientation (javafx.geometry.Orientation)1 Node (javafx.scene.Node)1 Parent (javafx.scene.Parent)1 SplitPane (javafx.scene.control.SplitPane)1