use of edu.mit.blocks.codeblocks.Block 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;
}
}
use of edu.mit.blocks.codeblocks.Block in project openblocks by mikaelhg.
the class TypeBlockManager method automateNegationInsertion.
/**
* assumes number and differen genus exist and number genus has ediitabel lable
*/
protected void automateNegationInsertion(Workspace workspace) {
TypeBlockManager typeBlockManager = workspace.getTypeBlockManager();
if (!typeBlockManager.isEnabled()) {
System.err.println("AutoMateNegationInsertion invoked but typeBlockManager is disabled.");
return;
}
// ====================>>>>>>>>>>>>>>>>>>>>>>>>>
// ====================focus coming in>>>>>>>>>> TODO
// ====================>>>>>>>>>>>>>>>>>>>>>>>>>
//get focus block
Long parentBlockID = typeBlockManager.focusManager.getFocusBlockID();
if (isNullBlockInstance(parentBlockID)) {
//focus on canvas
automateBlockInsertion(workspace, "number", "-");
} else {
Block parentBlock = workspace.getEnv().getBlock(parentBlockID);
if (parentBlock.isDataBlock()) {
//focus on a data block
automateBlockInsertion(workspace, "difference", null);
} else {
//focus on a non-data block
automateBlockInsertion(workspace, "number", "-");
}
}
}
use of edu.mit.blocks.codeblocks.Block in project openblocks by mikaelhg.
the class RenderableBlock method updateConnectors.
/**
* Updates the renderable block with the underlying block's before,
* after, and plug connectors.
*/
public void updateConnectors() {
Block b = workspace.getEnv().getBlock(blockID);
afterTag.setSocket(b.getAfterConnector());
beforeTag.setSocket(b.getBeforeConnector());
plugTag.setSocket(b.getPlug());
}
use of edu.mit.blocks.codeblocks.Block in project openblocks by mikaelhg.
the class RenderableBlock method moveConnectedBlocks.
//////////////////////////////////
// MOVEMENT OF CONNECTED BLOCKS //
//////////////////////////////////
/**
* Aligns all RenderableBlocks plugged into this one with the current location of this RenderableBlock.
* These RenderableBlocks to move include blocks connected at sockets and the after connector.
*/
public void moveConnectedBlocks() {
if (DEBUG) {
System.out.println("move connected blocks of this: " + this);
}
// if this hasn't been added anywhere, asking its location will break stuff
if (getParent() == null) {
return;
}
Block b = workspace.getEnv().getBlock(blockID);
Point socketLocation;
Point plugLocation;
RenderableBlock rb;
Point myScreenOffset = getLocation();
Point otherScreenOffset;
for (BlockConnector socket : BlockLinkChecker.getSocketEquivalents(b)) {
socketLocation = getSocketPixelPoint(socket);
if (socket.hasBlock()) {
rb = workspace.getEnv().getRenderableBlock(socket.getBlockID());
// needs to be found and fixed!!
if (rb == null) {
System.out.println("Block doesn't exist yet: " + socket.getBlockID());
continue;
}
plugLocation = rb.getSocketPixelPoint(BlockLinkChecker.getPlugEquivalent(workspace.getEnv().getBlock(socket.getBlockID())));
otherScreenOffset = SwingUtilities.convertPoint(rb.getParent(), rb.getLocation(), getParent());
otherScreenOffset.translate(-rb.getX(), -rb.getY());
rb.setLocation((int) Math.round((float) myScreenOffset.getX() + socketLocation.getX() - (float) otherScreenOffset.getX() - plugLocation.getX()), (int) Math.round((float) myScreenOffset.getY() + socketLocation.getY() - (float) otherScreenOffset.getY() - plugLocation.getY()));
rb.moveConnectedBlocks();
}
}
}
use of edu.mit.blocks.codeblocks.Block in project openblocks by mikaelhg.
the class RenderableBlock method calcDimensionOfSocket.
/**
* Calculates the dimensions at the specified socket
* @param socket BlockConnector to calculate the dimension of
* @return Dimension of the specified socket
*/
private Dimension calcDimensionOfSocket(BlockConnector socket) {
Dimension finalDimension = new Dimension(0, 0);
long curBlockID = socket.getBlockID();
while (curBlockID != Block.NULL) {
Block curBlock = workspace.getEnv().getBlock(curBlockID);
//System.out.println("evaluating block :" + curBlock.getBlockLabel());
RenderableBlock curRenderableBlock = workspace.getEnv().getRenderableBlock(curBlockID);
Dimension curRBSize = curRenderableBlock.getBlockSize();
//add height
finalDimension.height += curRBSize.height;
//subtract after plug
if (curBlock.hasAfterConnector()) {
finalDimension.height -= BlockConnectorShape.CONTROL_PLUG_HEIGHT;
}
//set largest width by iterating through to sockets and getting
//the max width ONLY if curBlockID == connectedToBlockID
int width = curRBSize.width;
if (curBlock.getNumSockets() > 0 && !curBlock.isInfix()) {
int maxSocWidth = getMaxWidthOfSockets(curBlockID);
//need to add the placeholder width within bottom sockets if maxSocWidth is zero
if (maxSocWidth == 0) {
// Adjust for zoom
width += 2 * BlockShape.BOTTOM_SOCKET_SIDE_SPACER * curRenderableBlock.getZoom();
}
if (maxSocWidth > 0) {
//need to minus the data plug width, otherwise it is counted twice
maxSocWidth -= BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH;
// Adjust for zoom
width += maxSocWidth * curRenderableBlock.getZoom();
}
}
if (width > finalDimension.width) {
finalDimension.width = width;
}
//move down the afters
curBlockID = workspace.getEnv().getBlock(curBlockID).getAfterBlockID();
}
return finalDimension;
}
Aggregations