Search in sources :

Example 21 with Block

use of edu.mit.blocks.codeblocks.Block 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 22 with Block

use of edu.mit.blocks.codeblocks.Block 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 23 with Block

use of edu.mit.blocks.codeblocks.Block 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 24 with Block

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

the class BlockLabel method genusChanged.

protected void genusChanged(String genus) {
    if (widget.hasSiblings()) {
        Block oldBlock = workspace.getEnv().getBlock(blockID);
        oldBlock.changeGenusTo(genus);
        RenderableBlock rb = workspace.getEnv().getRenderableBlock(blockID);
        rb.repaintBlock();
        workspace.notifyListeners(new WorkspaceEvent(workspace, rb.getParentWidget(), blockID, WorkspaceEvent.BLOCK_GENUS_CHANGED));
    }
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) Block(edu.mit.blocks.codeblocks.Block)

Example 25 with Block

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

Block (edu.mit.blocks.codeblocks.Block)29 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)15 BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)14 Point (java.awt.Point)7 BlockStub (edu.mit.blocks.codeblocks.BlockStub)5 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)5 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)5 BlockLink (edu.mit.blocks.codeblocks.BlockLink)4 FactoryRenderableBlock (edu.mit.blocks.renderable.FactoryRenderableBlock)3 ArrayList (java.util.ArrayList)3 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)2 Color (java.awt.Color)2 Dimension (java.awt.Dimension)2 Matcher (java.util.regex.Matcher)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 Workspace (edu.mit.blocks.workspace.Workspace)1 Container (java.awt.Container)1 LinkedHashMap (java.util.LinkedHashMap)1 StringTokenizer (java.util.StringTokenizer)1