Search in sources :

Example 6 with BlockLink

use of edu.mit.blocks.codeblocks.BlockLink in project openblocks by mikaelhg.

the class BlockUtilities method makeRenderable.

public static RenderableBlock makeRenderable(Workspace workspace, BlockNode node, WorkspaceWidget widget) {
    // genusName may not be null
    String genusName = node.getGenusName();
    RenderableBlock renderable = BlockUtilities.getBlock(workspace, genusName, node.getLabel());
    if (renderable == null) {
        throw new RuntimeException("No children block exists for this genus: " + genusName);
    }
    // assume not null
    Block block = workspace.getEnv().getBlock(renderable.getBlockID());
    widget.blockDropped(renderable);
    for (int i = 0; i < node.getChildren().size(); i++) {
        BlockConnector socket = block.getSocketAt(i);
        BlockNode child = node.getChildren().get(i);
        RenderableBlock childRenderable = makeRenderable(workspace, child, widget);
        Block childBlock = workspace.getEnv().getBlock(childRenderable.getBlockID());
        // link blocks
        BlockLink link;
        if (childBlock.hasPlug()) {
            link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getPlug());
        } else if (childBlock.hasBeforeConnector()) {
            link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getBeforeConnector());
        } else {
            link = null;
        }
        // assume link is not null
        link.connect();
        workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED));
    }
    if (node.getAfterNode() != null) {
        // assume has after connector
        BlockConnector socket = block.getAfterConnector();
        BlockNode child = node.getAfterNode();
        RenderableBlock childRenderable = makeRenderable(workspace, child, widget);
        Block childBlock = workspace.getEnv().getBlock(childRenderable.getBlockID());
        // link blocks
        BlockLink link;
        if (childBlock.hasPlug()) {
            link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getPlug());
        } else if (childBlock.hasBeforeConnector()) {
            link = BlockLinkChecker.canLink(workspace, block, childBlock, socket, childBlock.getBeforeConnector());
        } else {
            link = null;
        }
        // assume link is not null
        link.connect();
        workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED));
    }
    return renderable;
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) Block(edu.mit.blocks.codeblocks.Block) BlockLink(edu.mit.blocks.codeblocks.BlockLink) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Aggregations

BlockLink (edu.mit.blocks.codeblocks.BlockLink)6 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)6 Block (edu.mit.blocks.codeblocks.Block)4 BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)4 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)3 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)2 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)2 Point (java.awt.Point)2 MiniMap (edu.mit.blocks.workspace.MiniMap)1 Container (java.awt.Container)1 PopupMenu (java.awt.PopupMenu)1