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;
}
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;
}
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());
}
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));
}
}
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;
}
Aggregations