Search in sources :

Example 6 with BlockConnector

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

the class BlockUtilities method makeNodeWithStack.

public static BlockNode makeNodeWithStack(Workspace workspace, Long blockID) {
    if (isNullBlockInstance(workspace, blockID)) {
        return null;
    }
    Block block = workspace.getEnv().getBlock(blockID);
    String genus = block.getGenusName();
    String parentGenus = block instanceof BlockStub ? ((BlockStub) block).getParentGenus() : null;
    String label;
    if (!block.labelMustBeUnique() || block instanceof BlockStub) {
        label = block.getBlockLabel();
    } else {
        label = null;
    }
    BlockNode node = new BlockNode(genus, parentGenus, label);
    for (BlockConnector socket : block.getSockets()) {
        if (socket.hasBlock()) {
            node.addChild(makeNodeWithStack(workspace, socket.getBlockID()));
        }
    }
    if (block.hasAfterConnector()) {
        node.setAfter(makeNodeWithStack(workspace, block.getAfterBlockID()));
    }
    return node;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockStub(edu.mit.blocks.codeblocks.BlockStub) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 7 with BlockConnector

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

the class CollapseLabel method collapseSockets.

/**
 * Toggles visibility of all blocks connected to sockets
 * NB Sockets on procedure blocks do not have afterBlocks
 */
void collapseSockets(Long block_id) {
    Block block = workspace.getEnv().getBlock(block_id);
    for (BlockConnector socket : block.getSockets()) {
        if (socket.getBlockID() != Block.NULL) {
            collapseBlock(socket.getBlockID());
            collapseAfterBlocks(socket.getBlockID());
        }
    }
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 8 with BlockConnector

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

the class FocusTraversalManager method focusPrevBlock.

/**
 * Reassigns the focus to the "previous block" of the current focusBlock.
 * If the current focusblock is at location n of the flatten linear vector
 * of the block tree structure, then the "previous block" is located at n-1.
 * In other words, the previous block is the innermost block of the previous
 * socket of the parent block of the focusblock.
 *
 * @requires 	pointFocusOwner != null &&
 * 				focusblock.getSockets() != null &&
 * 				focusblock.getSockets() is not empty
 * @modifies this.focusblock
 * @effects this.focusblock now points to the "previous block"
 * 			as described in method overview;
 * @return true if the new focus is on a block that isn't null
 */
public boolean focusPrevBlock() {
    // return focus to canvas if no focusblock does not exist
    if (invalidBlock(focusBlock) || !workspace.getEnv().getRenderableBlock(focusBlock).isVisible()) {
        setFocus(canvasFocusPoint, Block.NULL);
        return false;
    }
    Block currentBlock = getBlock(focusBlock);
    // set plug to be previous block
    Block previousBlock = getPlugBlock(currentBlock);
    // if plug is null, set before to be previous block
    if (previousBlock == null) {
        previousBlock = getBeforeBlock(currentBlock);
    }
    // If before is ALSO null, jump to bottom of the stack;
    if (previousBlock == null) {
        previousBlock = getBottomRightBlock(currentBlock);
    } else {
        // If at least a plug block OR (but not both) before block exist,
        // then get innermost block of the previous socket of the previous block
        // assumes previousBlock.getSockets is not empty, not null
        Block beforeBlock = previousBlock;
        // ASSUMPTION BEING MADE: assume that the list below is constructed to
        // have all the sockets FOLLOWED by FOLLOWED by the after connector
        // THE ORDER MUST BE KEPT TO WORK CORRECTLY!  We cannot use
        // BlockLinkChecker.getSocketEquivalents because the specification does
        // not guarantee this precise ordering.  Futhermore, an interable
        // has no defined order.  However, as of this writing, the current implementation
        // of that method does seem to produce this ordering.  But we're still not using it.
        List<BlockConnector> connections = new ArrayList<BlockConnector>();
        for (BlockConnector socket : previousBlock.getSockets()) {
            // add sockets
            connections.add(socket);
        }
        // add after connector
        connections.add(previousBlock.getAfterConnector());
        // now traverse the connections
        for (BlockConnector connector : connections) {
            if (connector == null || connector.getBlockID() == Block.NULL || getBlock(connector.getBlockID()) == null) {
                // if null socket, move on to next socket
                continue;
            }
            if (connector.getBlockID().equals(currentBlock.getBlockID())) {
                // reached back to current block
                if (!beforeBlock.getBlockID().equals(previousBlock.getBlockID())) {
                    // if previous block was never updated, go to bottom of stack
                    previousBlock = getBottomRightBlock(previousBlock);
                }
                setFocus(previousBlock.getBlockID());
                return true;
            }
            // update previous block
            previousBlock = getBlock(connector.getBlockID());
        }
        // so it seems liek all sockets are null (or sockets exist),
        // so just get the bottom of the stack
        previousBlock = getBottomRightBlock(previousBlock);
    }
    setFocus(previousBlock.getBlockID());
    return true;
}
Also used : ArrayList(java.util.ArrayList) Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 9 with BlockConnector

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

the class TypeBlockManager method deleteBlockAndChildren.

/**
 * @requires the current block with focus must exist with non-null
 * 			 ID in a non-null widget with a non-null parent
 * @modifies the current block with focus
 * @effects  removes the current block with focus and children
 * 			 from the GUI and destroys the link
 * 			 between the block with focus and it's parent
 * 			 block if one exist and children blocks
 * 			 if it has childrens.
 */
private void deleteBlockAndChildren() {
    // Do not delete null block references.  Otherwise, get Block and RenderableBlock instances.
    if (isNullBlockInstance(focusManager.getFocusBlockID())) {
        throw new RuntimeException("TypeBlockManager: deleting a null block references.");
    }
    Block block = workspace.getEnv().getBlock(focusManager.getFocusBlockID());
    RenderableBlock renderable = workspace.getEnv().getRenderableBlock(block.getBlockID());
    // get workspace widget associated with current focus
    WorkspaceWidget widget = renderable.getParentWidget();
    // do not delete block instances in null widgets
    if (widget == null) {
        throw new RuntimeException("TypeBlockManager: do not delete blocks with no parent widget.");
    // return;
    }
    // get parent container of this graphical representation
    Container container = renderable.getParent();
    // do not delete block instances in null parents
    if (container == null) {
        throw new RuntimeException("TypeBlockManager: do not delete blocks with no parent container.");
    // return;
    }
    // get the Block's location on the canvas
    Point location = SwingUtilities.convertPoint(renderable, new Point(0, 0), this.blockCanvas.getCanvas());
    // for every valid and active connection, disconnect it.
    Long parentID = null;
    if (validConnection(block.getPlug())) {
        parentID = block.getPlugBlockID();
        this.disconnectBlock(block, widget);
        if (validConnection(block.getAfterConnector())) {
            disconnectBlock(workspace.getEnv().getBlock(block.getAfterBlockID()), widget);
        }
    } else if (validConnection(block.getBeforeConnector())) {
        parentID = block.getBeforeBlockID();
        BlockConnector parentConnectorToBlock = workspace.getEnv().getBlock(parentID).getConnectorTo(block.getBlockID());
        this.disconnectBlock(block, widget);
        if (validConnection(block.getAfterConnector())) {
            Long afterBlockID = block.getAfterBlockID();
            disconnectBlock(workspace.getEnv().getBlock(afterBlockID), widget);
            if (parentID != null) {
                BlockLink link = BlockLinkChecker.canLink(workspace, workspace.getEnv().getBlock(parentID), workspace.getEnv().getBlock(afterBlockID), parentConnectorToBlock, workspace.getEnv().getBlock(afterBlockID).getBeforeConnector());
                if (link != null) {
                    link.connect();
                    workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED));
                    workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).repaintBlock();
                    workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).repaint();
                    workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).moveConnectedBlocks();
                    workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaintBlock();
                    workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaint();
                }
            }
        }
    } else if (validConnection(block.getAfterConnector())) {
        parentID = block.getAfterBlockID();
    }
    // remove form widget and container
    this.removeChildrenBlock(renderable, widget, container);
    // Otherwise, give the focus to the canvas (NOT BLOCK CANVAS)
    if (invalidBlockID(parentID)) {
        this.focusManager.setFocus(location, Block.NULL);
        this.blockCanvas.getCanvas().requestFocus();
        return;
    } else {
        this.focusManager.setFocus(parentID);
        this.blockCanvas.getCanvas().requestFocus();
        return;
    }
}
Also used : Container(java.awt.Container) WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) TextualFactoryBlock(edu.mit.blocks.renderable.TextualFactoryBlock) BlockLink(edu.mit.blocks.codeblocks.BlockLink) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Point(java.awt.Point) WorkspaceWidget(edu.mit.blocks.workspace.WorkspaceWidget)

Example 10 with BlockConnector

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

the class TypeBlockManager method removeChildrenBlock.

private void removeChildrenBlock(RenderableBlock renderable, WorkspaceWidget widget, Container container) {
    widget.removeBlock(renderable);
    container.remove(renderable);
    container.validate();
    container.repaint();
    renderable.setParentWidget(null);
    // Workspace.getInstance().notifyListeners(new WorkspaceEvent(widget, renderable.getBlockID(), WorkspaceEvent.BLOCK_REMOVED));
    for (BlockConnector child : workspace.getEnv().getBlock(renderable.getBlockID()).getSockets()) {
        if (child == null || child.getBlockID().equals(Block.NULL)) {
            continue;
        }
        RenderableBlock childRenderable = workspace.getEnv().getRenderableBlock(child.getBlockID());
        if (childRenderable == null) {
            continue;
        }
        removeBlock(childRenderable, widget, container);
    }
    // If it is a procedure block, we want to delete the entire stack
    if (workspace.getEnv().getBlock(renderable.getBlockID()).isProcedureDeclBlock()) {
        if (workspace.getEnv().getBlock(renderable.getBlockID()).getAfterBlockID() != Block.NULL) {
            removeAfterBlock(workspace.getEnv().getRenderableBlock(workspace.getEnv().getBlock(renderable.getBlockID()).getAfterBlockID()), widget, container);
            this.disconnectBlock(workspace.getEnv().getBlock(workspace.getEnv().getBlock(renderable.getBlockID()).getAfterBlockID()), widget);
        }
    }
    if (renderable.hasComment()) {
        renderable.removeComment();
    }
    workspace.notifyListeners(new WorkspaceEvent(workspace, widget, renderable.getBlockID(), WorkspaceEvent.BLOCK_REMOVED));
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) 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