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