Search in sources :

Example 21 with BlockConnector

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

the class RenderableBlock method startDragging.

private void startDragging(RenderableBlock renderable, WorkspaceWidget widget) {
    renderable.pickedUp = true;
    renderable.lastDragWidget = widget;
    if (renderable.hasComment()) {
        renderable.comment.setConstrainComment(false);
    }
    Component oldParent = renderable.getParent();
    Workspace workspace = renderable.getWorkspace();
    workspace.addToBlockLayer(renderable);
    renderable.setLocation(SwingUtilities.convertPoint(oldParent, renderable.getLocation(), workspace));
    renderable.setHighlightParent(workspace);
    for (BlockConnector socket : BlockLinkChecker.getSocketEquivalents(workspace.getEnv().getBlock(renderable.blockID))) {
        if (socket.hasBlock()) {
            startDragging(workspace.getEnv().getRenderableBlock(socket.getBlockID()), widget);
        }
    }
}
Also used : BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Component(java.awt.Component) JComponent(javax.swing.JComponent) Workspace(edu.mit.blocks.workspace.Workspace)

Example 22 with BlockConnector

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

the class RenderableBlock method updateSocketSpace.

/**
 * Updates the socket socket space of the specified connectedSocket of this after a block
 * connection/disconnection.  The socket space specifies the dimensions of the block
 * with id connectedToBlockID. RenderableBlock will use these dimensions to
 * determine the appropriate bounds to stretch the connectedSocket by.
 * @param connectedSocket BlockConnector which block connection/disconnection occurred
 * @param connectedToBlockID the Long block ID of the block connected/disconnected to the specified connectedSocket
 * @param isConnected boolean flag to determine if a block connected or disconnected to the connectedSocket
 */
private void updateSocketSpace(BlockConnector connectedSocket, long connectedToBlockID, boolean isConnected) {
    // System.out.println("updating socket space of :" + connectedSocket.getLabel() +" of rb: "+this);
    if (!isConnected) {
        // remove the mapping
        this.getConnectorTag(connectedSocket).setDimension(null);
    } else {
        // and we have a before, then skip and recurse up
        if (getBlock().getBeforeBlockID() != Block.NULL && BlockConnectorShape.isCommandConnector(connectedSocket) && connectedSocket.getPositionType() == BlockConnector.PositionType.BOTTOM) {
            // get before connector
            Long beforeID = getBlock().getBeforeBlockID();
            BlockConnector beforeSocket = workspace.getEnv().getBlock(beforeID).getConnectorTo(getBlockID());
            workspace.getEnv().getRenderableBlock(beforeID).updateSocketSpace(beforeSocket, getBlockID(), true);
            return;
        }
        // add dimension to the mapping
        this.getConnectorTag(connectedSocket).setDimension(calcDimensionOfSocket(connectedSocket));
    }
    // reform shape with new socket dimension
    reformBlockShape();
    // next time, redraw with new positions and moving children blocks
    clearBufferedImage();
    // after everything on this block has been updated, recurse upward if possible
    BlockConnector plugEquiv = BlockLinkChecker.getPlugEquivalent(getBlock());
    if (plugEquiv != null && plugEquiv.hasBlock()) {
        Long plugID = plugEquiv.getBlockID();
        BlockConnector socketEquiv = workspace.getEnv().getBlock(plugID).getConnectorTo(getBlockID());
        // update the socket space of a connected before/parent block
        workspace.getEnv().getRenderableBlock(plugID).updateSocketSpace(socketEquiv, getBlockID(), true);
    }
}
Also used : BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 23 with BlockConnector

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

the class RenderableBlock method getMaxWidthOfSockets.

/**
 * Helper method for updateSocketSpace and calcStackDim.
 * Returns the maximum width of the specified blockID's socket blocks
 * @param blockID the Long blockID of the desired block
 */
public int getMaxWidthOfSockets(Long blockID) {
    int width = 0;
    Block block = workspace.getEnv().getBlock(blockID);
    RenderableBlock rb = workspace.getEnv().getRenderableBlock(blockID);
    for (BlockConnector socket : block.getSockets()) {
        Dimension socketDim = rb.getSocketSpaceDimension(socket);
        if (socketDim != null) {
            if (socketDim.width > width) {
                width = socketDim.width;
            }
        }
    }
    return width;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Dimension(java.awt.Dimension) Point(java.awt.Point)

Example 24 with BlockConnector

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

the class BlockLabel method textChanged.

protected void textChanged(String text) {
    if ((this.labelType.equals(BlockLabel.Type.NAME_LABEL) || this.labelType.equals(BlockLabel.Type.PORT_LABEL)) && workspace.getEnv().getBlock(blockID).isLabelEditable()) {
        if (this.labelType.equals(BlockLabel.Type.NAME_LABEL)) {
            workspace.getEnv().getBlock(blockID).setBlockLabel(text);
        }
        BlockConnector plug = workspace.getEnv().getBlock(blockID).getPlug();
        // has stubs, update them.
        if (plug != null && plug.getBlockID() != Block.NULL) {
            if (workspace.getEnv().getBlock(plug.getBlockID()) != null) {
                if (workspace.getEnv().getBlock(plug.getBlockID()).isProcedureDeclBlock() && workspace.getEnv().getBlock(plug.getBlockID()).hasStubs()) {
                    // nor desired to call the connectors changed event again.
                    if (workspace.getEnv().getRenderableBlock(plug.getBlockID()).isLoading()) {
                        BlockStub.parentConnectorsChanged(workspace, plug.getBlockID());
                    }
                }
            }
        }
        RenderableBlock rb = workspace.getEnv().getRenderableBlock(blockID);
        if (rb != null) {
            workspace.notifyListeners(new WorkspaceEvent(workspace, rb.getParentWidget(), blockID, WorkspaceEvent.BLOCK_RENAMED));
        }
    }
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 25 with BlockConnector

use of edu.mit.blocks.codeblocks.BlockConnector 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

BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)26 Block (edu.mit.blocks.codeblocks.Block)14 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)9 Point (java.awt.Point)9 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)8 BlockLink (edu.mit.blocks.codeblocks.BlockLink)4 ArrayList (java.util.ArrayList)3 BlockStub (edu.mit.blocks.codeblocks.BlockStub)2 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)2 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)2 Dimension (java.awt.Dimension)2 Workspace (edu.mit.blocks.workspace.Workspace)1 Component (java.awt.Component)1 Container (java.awt.Container)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 Point2D (java.awt.geom.Point2D)1 JComponent (javax.swing.JComponent)1