Search in sources :

Example 11 with BlockConnector

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

the class FocusTraversalManager method focusNextBlock.

// ////////////////////////////////////
// Focus Traversal Handling Methods //
// ////////////////////////////////////
/**
 * Reassigns the focus to the "next 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 "next block" is located at n+1.
 * In other words, the previous block is the parent block of the next
 * 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 "next block"
 * 			as described in method overview;
 * @return true if the new focus is on a block that isn't null
 */
public boolean focusNextBlock() {
    // return focus to canvas if no focusblock does not exist
    if (invalidBlock(focusBlock) || !workspace.getEnv().getRenderableBlock(focusBlock).isVisible()) {
        setFocus(canvasFocusPoint, Block.NULL);
        return false;
    }
    // give focus to any preceeding socket of current block
    Block currentBlock = getBlock(focusBlock);
    for (BlockConnector socket : currentBlock.getSockets()) {
        if (socket != null && !invalidBlock(socket.getBlockID())) {
            // give focus to socket block
            setFocus(socket.getBlockID());
            return true;
        }
    }
    // give focus to after block of current block
    Long afterBlock = currentBlock.getAfterBlockID();
    if (!invalidBlock(afterBlock)) {
        setFocus(afterBlock);
        return true;
    }
    // current block != null.....invariant checke in getNextNode()
    Block nextBlock = this.getNextNode(currentBlock);
    // check invariant
    if (nextBlock == null) {
        throw new RuntimeException("Invariant Violated: return value of getNextNode() may not be null");
    }
    // set focus
    setFocus(nextBlock.getBlockID());
    return true;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 12 with BlockConnector

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

the class FocusTraversalManager method getNextNode.

// /////////////////////
// TRAVERSING STACKS //
// /////////////////////
/**
 * @requires currentBlock != null
 * @param currentBlock
 * @return currentBlock or NON-NULL block that is the next node of currentBlock
 */
private Block getNextNode(Block currentBlock) {
    // check invarient
    if (invalidBlock(currentBlock)) {
        throw new RuntimeException("Invariant Violated: may not resurve over a null instance of currentBlock");
    }
    // if plug not null, then let plug be parent of current block
    Block parentBlock = getBlock(currentBlock.getPlugBlockID());
    // otherwise if after not null, then let after be parent of current block
    if (invalidBlock(parentBlock)) {
        parentBlock = getBlock(currentBlock.getBeforeBlockID());
    }
    // if plug and after are both null, then return currentBlock
    if (invalidBlock(parentBlock)) {
        return currentBlock;
    }
    // socket index of current block with respect to its parent
    int i = parentBlock.getSocketIndex(parentBlock.getConnectorTo(currentBlock.getBlockID()));
    // int i == 0 if not current block not a socket of parent
    if (i != -1 && i >= 0) {
        for (BlockConnector parentSocket : parentBlock.getSockets()) {
            if (parentSocket == null || invalidBlock(parentSocket.getBlockID()) || parentBlock.getSocketIndex(parentSocket) <= i) {
                continue;
            } else {
                return getBlock(parentSocket.getBlockID());
            }
        }
    }
    // return afterblock of parent
    if (invalidBlock(parentBlock.getAfterBlockID())) {
        return getNextNode(parentBlock);
    }
    if (parentBlock.getAfterBlockID().equals(currentBlock.getBlockID())) {
        return getNextNode(parentBlock);
    }
    // This is top of the block, so return currentBlock
    return getBlock(parentBlock.getAfterBlockID());
}
Also used : Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Point(java.awt.Point)

Example 13 with BlockConnector

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

the class FocusTraversalManager method getBottomRightBlock.

/**
 * For a given block, returns the innermost (bottom-rightmost)
 * block in the substack.
 * @requires block !=null block.getBlockID != Block.NULL
 * @param block the top block of the substack.
 * @return the innermost block in the substack.
 * 		   such that the innermost block != null
 */
private Block getBottomRightBlock(Block block) {
    // check invariant
    if (block == null || block.getBlockID() == Block.NULL) {
        throw new RuntimeException("Invariant Violated: may not" + "iterate for innermost block over a null instance of Block");
    }
    // returnblock = next deepest node on far right
    Block returnBlock = null;
    // find deepest node, that is, bottom most block in stack.
    returnBlock = getAfterBlock(block);
    if (returnBlock != null) {
        return getBottomRightBlock(returnBlock);
    }
    // move to the next socket in line:
    for (BlockConnector socket : block.getSockets()) {
        // assumes socket!=null
        Block socketBlock = getBlock(socket.getBlockID());
        if (socketBlock != null) {
            returnBlock = socketBlock;
        }
    }
    if (returnBlock != null) {
        return getBottomRightBlock(returnBlock);
    }
    // check invariant
    if (returnBlock != null) {
        throw new RuntimeException("Invariant Violated: may not " + "return a null instance of block as the innermost block");
    }
    // If we can't traverse any deeper, then this is innermost Block.
    return block;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 14 with BlockConnector

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

the class TypeBlockManager method removeBlock.

/**
 * @param renderable
 * @param widget
 * @param container
 *
 * @requires renderable != null && renderable.blockID != null && renderable.blockID != Block.NULL
 * 			 && widget != null && container != null
 * @modifies renderable && children blocks connected to renderable
 * @effects removes renderable from container and widget and re-renders
 * 			renderable block, widget, and container appropriately.
 * 			Repeats for all of renderable's children.
 */
private void removeBlock(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 : BlockLinkChecker.getSocketEquivalents(workspace.getEnv().getBlock(renderable.getBlockID()))) {
        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 (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)

Example 15 with BlockConnector

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

the class TypeBlockManager method disconnectBlock.

/**
 * @param childBlock
 * @param widget
 *
 * @requires widget != null
 * @modifies
 * @effects Does nothing if: childBlock is invalid (null)
 * 			Otherwise, remove childBlock from it's parent block
 * 			if the childBlock has a parent.  If it does not have
 * 			a parent, do nothing.
 */
private void disconnectBlock(Block childBlock, WorkspaceWidget widget) {
    if (childBlock == null || invalidBlockID(childBlock.getBlockID())) {
        return;
    }
    BlockConnector childPlug = BlockLinkChecker.getPlugEquivalent(childBlock);
    if (childPlug == null || !childPlug.hasBlock() || isNullBlockInstance(childPlug.getBlockID())) {
        return;
    }
    Block parentBlock = workspace.getEnv().getBlock(childPlug.getBlockID());
    BlockConnector parentSocket = parentBlock.getConnectorTo(childBlock.getBlockID());
    if (parentSocket == null) {
        return;
    }
    // disconector if child connector exists and has a block connected to it
    BlockLink link = BlockLink.getBlockLink(workspace, childBlock, parentBlock, childPlug, parentSocket);
    if (link == null) {
        return;
    }
    link.disconnect();
    RenderableBlock parentRenderable = workspace.getEnv().getRenderableBlock(parentBlock.getBlockID());
    if (parentRenderable == null) {
        throw new RuntimeException("INCONSISTANCY VIOLATION: " + "parent block was valid, non-null, and existed.\n\tBut yet, when we get it's renderable" + "representation, we recieve a null instance.\n\tIf the Block instance of an ID is non-null" + "then its graphical RenderableBlock should be non-null as well");
    }
    parentRenderable.blockDisconnected(parentSocket);
    workspace.notifyListeners(new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCKS_DISCONNECTED));
}
Also used : 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)

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