private void pasteStack(BlockNode node) { // ====================>>>>>>>>>>>>>>>>>>>>>>>>> // ====================focus coming in>>>>>>>>>> TODO // ====================>>>>>>>>>>>>>>>>>>>>>>>>> if (node == null) { return; } WorkspaceWidget widget = null; Iterable<WorkspaceWidget> widgets = null; Point spot = null; if (invalidBlockID(focusManager.getFocusBlockID())) { // canvas has focus Point location = SwingUtilities.convertPoint( this.blockCanvas.getCanvas(), this.focusManager.getCanvasPoint(), workspace); widget = workspace.getWidgetAt(location); spot = SwingUtilities.convertPoint( this.blockCanvas.getCanvas(), this.focusManager.getCanvasPoint(), widget.getJComponent()); } else { RenderableBlock focusRenderable = workspace.getEnv().getRenderableBlock(focusManager.getFocusBlockID()); widget = focusRenderable.getParentWidget(); spot = focusRenderable.getLocation(); } if (widget == null) { // TODO: To be examined and fixed, occurs on macs JOptionPane.showMessageDialog( frame, "Please click somewhere on the canvas first.", "Error", JOptionPane.PLAIN_MESSAGE); // throw new RuntimeException("Why are we adding a block to a null widget?"); } else { // checks to see if the copied block still exists if (BlockUtilities.blockExists(workspace, node)) { // create mirror block and mirror childrens spot.translate(10, 10); RenderableBlock mirror = BlockUtilities.makeRenderable(workspace, node, widget); mirror.setLocation(spot); mirror.moveConnectedBlocks(); // make sure the childrens are placed correctly } else { // TODO: future version, allow them to paste JOptionPane.showMessageDialog( frame, "You cannot paste blocks that are currently NOT on the canvas." + "\nThis function will be available in a future version.\n", "Error", JOptionPane.PLAIN_MESSAGE); } } }
/** * @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)); }
public static void deleteBlock(RenderableBlock block) { block.setLocation(0, 0); WorkspaceWidget widget = block.getParentWidget(); if (widget != null) { widget.removeBlock(block); } Container parent = block.getParent(); if (parent != null) { parent.remove(block); parent.validate(); } block.setParentWidget(null); Workspace workspace = block.getWorkspace(); workspace.notifyListeners( new WorkspaceEvent(workspace, widget, block.getBlockID(), WorkspaceEvent.BLOCK_REMOVED)); }
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)); }
public static RenderableBlock makeRenderable( Workspace workspace, BlockNode node, WorkspaceWidget widget) { String genusName = node.getGenusName(); // genusName may not be null RenderableBlock renderable = BlockUtilities.getBlock(workspace, genusName, node.getLabel()); if (renderable == null) { throw new RuntimeException("No children block exists for this genus: " + genusName); } Block block = Block.getBlock(renderable.getBlockID()); // assume not null 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 = Block.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, RenderableBlock.getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED)); } if (node.getAfterNode() != null) { BlockConnector socket = block.getAfterConnector(); // assume has after connector BlockNode child = node.getAfterNode(); RenderableBlock childRenderable = makeRenderable(workspace, child, widget); Block childBlock = Block.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, RenderableBlock.getRenderableBlock(link.getPlugBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_CONNECTED)); } return renderable; }