use of edu.mit.blocks.workspace.WorkspaceEvent in project openblocks by mikaelhg.
the class TypeBlockManager method removeChildrenBlock.
private void removeChildrenBlock(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 : workspace.getEnv().getBlock(renderable.getBlockID()).getSockets()) {
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 it is a procedure block, we want to delete the entire stack
if (workspace.getEnv().getBlock(renderable.getBlockID()).isProcedureDeclBlock()) {
if (workspace.getEnv().getBlock(renderable.getBlockID()).getAfterBlockID() != Block.NULL) {
removeAfterBlock(workspace.getEnv().getRenderableBlock(workspace.getEnv().getBlock(renderable.getBlockID()).getAfterBlockID()), widget, container);
this.disconnectBlock(workspace.getEnv().getBlock(workspace.getEnv().getBlock(renderable.getBlockID()).getAfterBlockID()), widget);
}
}
if (renderable.hasComment()) {
renderable.removeComment();
}
workspace.notifyListeners(new WorkspaceEvent(workspace, widget, renderable.getBlockID(), WorkspaceEvent.BLOCK_REMOVED));
}
use of edu.mit.blocks.workspace.WorkspaceEvent in project openblocks by mikaelhg.
the class BlockDropAnimator method actionPerformed.
// Keep moving the block until it reaches it's destination
public void actionPerformed(ActionEvent e) {
if (childBlock.getLocation().distance(focusPoint) < 75) {
// if parent block exist, then preform automatic linking
childBlock.setLocation(focusPoint);
if (parentBlock != null && parentBlock.getBlockID() != null && !parentBlock.getBlockID().equals(Block.NULL)) {
BlockLink link = LinkFinderUtil.connectBlocks(workspace, workspace.getEnv().getBlock(childBlock.getBlockID()), workspace.getEnv().getBlock(parentBlock.getBlockID()));
if (link == null) {
dropBlock(childBlock);
childBlock.repaintBlock();
childBlock.repaint();
} else {
// drop and link the new block
link.connect();
dropBlock(childBlock);
workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED));
workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).moveConnectedBlocks();
workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaintBlock();
workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).repaint();
}
} else {
dropBlock(childBlock);
childBlock.repaintBlock();
childBlock.repaint();
}
// stop the timer
timer.stop();
if (workspace.getEnv().getBlock(childBlock.getBlockID()).getGenusName().equals("number")) {
childBlock.switchToLabelEditingMode(false);
} else {
childBlock.switchToLabelEditingMode(true);
}
// TODO: check if focumanager's before parent is same as
// the parent we have here and check if new focusblock is child block
PageChangeEventManager.notifyListeners();
} else {
// childBlock.setLocation(focusPoint);
// TODO: This needs to change if the parent block doesn't have any more sockets for children
// Need to adjust focusPoint somehow.
childBlock.setLocation((int) (focusPoint.getX() * 0.67) + (int) (childBlock.getX() * 0.34), (int) (focusPoint.getY() * 0.67) + (int) (childBlock.getY() * 0.34));
}
}
use of edu.mit.blocks.workspace.WorkspaceEvent 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));
}
use of edu.mit.blocks.workspace.WorkspaceEvent 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));
}
use of edu.mit.blocks.workspace.WorkspaceEvent in project openblocks by mikaelhg.
the class RenderableBlock method linkDefArgs.
/**
* Links the default arguments of this block if it has any and if this block has not already linked its
* default args in this session. Re-linking this block's default args everytime it gets dropped/moved
* within the block canvas can get annoying.
*/
public void linkDefArgs() {
if (!linkedDefArgsBefore && getBlock().hasDefaultArgs()) {
Iterator<Long> ids = getBlock().linkAllDefaultArgs().iterator();
Iterator<BlockConnector> sockets = getBlock().getSockets().iterator();
Long id;
BlockConnector socket;
// Store the ids, sockets, and blocks we need to update.
List<Long> idList = new ArrayList<Long>();
List<BlockConnector> socketList = new ArrayList<BlockConnector>();
List<RenderableBlock> argList = new ArrayList<RenderableBlock>();
while (ids.hasNext() && sockets.hasNext()) {
id = ids.next();
socket = sockets.next();
if (id != Block.NULL) {
// for each block id, create a new RenderableBlock
RenderableBlock arg = new RenderableBlock(workspace, this.getParentWidget(), id);
arg.setZoomLevel(this.zoom);
// getParentWidget().addBlock(arg);
// arg.repaint();
// this.getParent().add(arg);
// set the location of the def arg at
Point myLocation = getLocation();
Point2D socketPt = getSocketPixelPoint(socket);
Point2D plugPt = arg.getSocketPixelPoint(arg.getBlock().getPlug());
arg.setLocation((int) (socketPt.getX() + myLocation.x - plugPt.getX()), (int) (socketPt.getY() + myLocation.y - plugPt.getY()));
// update the socket space of at this socket
this.getConnectorTag(socket).setDimension(new Dimension(arg.getBlockWidth() - (int) BlockConnectorShape.NORMAL_DATA_PLUG_WIDTH, arg.getBlockHeight()));
// drop each block to this parent's widget/component
// getParentWidget().blockDropped(arg);
getParentWidget().addBlock(arg);
idList.add(id);
socketList.add(socket);
argList.add(arg);
}
}
int size = idList.size();
for (int i = 0; i < size; i++) {
workspace.notifyListeners(new WorkspaceEvent(workspace, this.getParentWidget(), argList.get(i).getBlockID(), WorkspaceEvent.BLOCK_ADDED, true));
// must call this method to update the dimensions of this
// TODO ria in the future would be good to just link the default args
// but first creating a block link object and then connecting
// something like notifying the renderableblock to update its dimensions will be
// take care of
this.blockConnected(socketList.get(i), idList.get(i));
argList.get(i).repaint();
}
this.redrawFromTop();
linkedDefArgsBefore = true;
}
}
Aggregations