Search in sources :

Example 1 with WorkspaceEvent

use of edu.mit.blocks.workspace.WorkspaceEvent in project openblocks by mikaelhg.

the class BlockLink method connect.

/**
 * This method actually connects the two blocks stored in this BlockLink object.
 */
public void connect() {
    /* Make sure to disconnect any connections that are going to be overwritten
         * by this new connection.  For example, if inserting a block between two
         * others, make sure to break that original link.*/
    if (socket.hasBlock()) {
        // save the ID of the block previously attached to (in) this
        // socket.  This is used by insertion rules to re-link the replaced
        // block to the newly-inserted block.
        lastPlugBlockID = socket.getBlockID();
        // break the link between the socket block and the block in that socket
        Block plugBlock = workspace.getEnv().getBlock(lastPlugBlockID);
        BlockConnector plugBlockPlug = BlockLinkChecker.getPlugEquivalent(plugBlock);
        if (plugBlockPlug != null && plugBlockPlug.hasBlock()) {
            Block socketBlock = workspace.getEnv().getBlock(plugBlockPlug.getBlockID());
            BlockLink link = BlockLink.getBlockLink(workspace, plugBlock, socketBlock, plugBlockPlug, socket);
            link.disconnect();
            // don't tell the block about the disconnect like we would normally do, because
            // we don't actually want it to have a chance to remove any expandable sockets
            // since the inserted block will be filling whatever socket was vacated by this
            // broken link.
            // NOTIFY WORKSPACE LISTENERS OF DISCONNECTION (not sure if this is great because the connection is immediately replaced)
            workspace.notifyListeners(new WorkspaceEvent(workspace, workspace.getEnv().getRenderableBlock(socketBlock.getBlockID()).getParentWidget(), link, WorkspaceEvent.BLOCKS_DISCONNECTED));
        }
    }
    if (plug.hasBlock()) {
        // after any insertion-esq links were broken above
        throw new RuntimeException("trying to link a plug that's already connected somewhere.");
    }
    // actually form the connection
    plug.setConnectorBlockID(socketBlockID);
    socket.setConnectorBlockID(plugBlockID);
    // notify renderable block of connection so it can redraw with stretching
    RenderableBlock socketRB = workspace.getEnv().getRenderableBlock(socketBlockID);
    socketRB.blockConnected(socket, plugBlockID);
    if (clickSound != null) {
        // System.out.println("playing click sound");
        clickSound.play();
    }
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 2 with WorkspaceEvent

use of edu.mit.blocks.workspace.WorkspaceEvent in project openblocks by mikaelhg.

the class BlockStub method disconnectBlock.

/**
 * Disconnect the given block from us. Must have a valid id.
 */
private void disconnectBlock(Long id) {
    Block b2 = workspace.getEnv().getBlock(id);
    BlockConnector conn2 = b2.getConnectorTo(getBlockID());
    BlockConnector conn = getConnectorTo(id);
    BlockLink link = BlockLink.getBlockLink(workspace, this, b2, conn, conn2);
    RenderableBlock rb = workspace.getEnv().getRenderableBlock(link.getSocketBlockID());
    link.disconnect();
    rb.blockDisconnected(link.getSocket());
    workspace.notifyListeners(new WorkspaceEvent(workspace, rb.getParentWidget(), link, WorkspaceEvent.BLOCKS_DISCONNECTED));
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 3 with WorkspaceEvent

use of edu.mit.blocks.workspace.WorkspaceEvent in project openblocks by mikaelhg.

the class RenderableBlock method mouseReleased.

public void mouseReleased(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
        if (pickedUp) {
            dragHandler.mouseReleased(e);
            // if the block was dragged before...then
            if (dragging) {
                // look for nearby link opportunities
                BlockLink link = getNearbyLink();
                WorkspaceWidget widget = null;
                // if a suitable link wasn't found, just drop the block
                if (link == null) {
                    widget = lastDragWidget;
                    stopDragging(this, widget);
                } else // otherwise, if a link WAS found...
                {
                    /* Make sure that no matter who's connecting to whom, the block
                        * that's being dragged gets dropped on the parent widget of the
                        * block that's already on the canvas.
                        */
                    if (blockID.equals(link.getSocketBlockID())) {
                        // dragged block is the socket block, so take plug's parent.
                        widget = workspace.getEnv().getRenderableBlock(link.getPlugBlockID()).getParentWidget();
                    } else {
                        // dragged block is the plug block, so take the socket block's parent.
                        widget = workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).getParentWidget();
                    }
                    // drop the block and connect its link
                    stopDragging(this, widget);
                    link.connect();
                    workspace.notifyListeners(new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCKS_CONNECTED));
                    workspace.getEnv().getRenderableBlock(link.getSocketBlockID()).moveConnectedBlocks();
                }
                // set the locations for X and Y based on zoom at 1.0
                this.unzoomedX = this.calculateUnzoomedX(this.getX());
                this.unzoomedY = this.calculateUnzoomedY(this.getY());
                workspace.notifyListeners(new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCK_MOVED, true));
                if (widget instanceof MiniMap) {
                    workspace.getMiniMap().animateAutoCenter(this);
                }
            }
        }
    }
    pickedUp = false;
    if (e.isPopupTrigger() || SwingUtilities.isRightMouseButton(e) || e.isControlDown()) {
        // add context menu at right click location to provide functionality
        // for adding new comments and removing comments
        PopupMenu popup = ContextMenu.getContextMenuFor(this);
        add(popup);
        popup.show(this, e.getX(), e.getY());
    }
    workspace.getMiniMap().repaint();
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) BlockLink(edu.mit.blocks.codeblocks.BlockLink) MiniMap(edu.mit.blocks.workspace.MiniMap) WorkspaceWidget(edu.mit.blocks.workspace.WorkspaceWidget) PopupMenu(java.awt.PopupMenu)

Example 4 with WorkspaceEvent

use of edu.mit.blocks.workspace.WorkspaceEvent in project openblocks by mikaelhg.

the class Comment method delete.

/**
 * Handle the removal of this comment from its comment source
 */
public void delete() {
    workspace.notifyListeners(new WorkspaceEvent(workspace, getCommentSource().getParentWidget(), WorkspaceEvent.BLOCK_COMMENT_REMOVED));
    getParent().remove(arrow.arrow);
    setParent(null);
    if (commentSource instanceof RenderableBlock) {
        RenderableBlock rb = (RenderableBlock) commentSource;
        rb.remove(commentLabel);
        commentLabel = null;
    }
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent)

Example 5 with WorkspaceEvent

use of edu.mit.blocks.workspace.WorkspaceEvent 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;
    }
}
Also used : Container(java.awt.Container) WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) TextualFactoryBlock(edu.mit.blocks.renderable.TextualFactoryBlock) BlockLink(edu.mit.blocks.codeblocks.BlockLink) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Point(java.awt.Point) WorkspaceWidget(edu.mit.blocks.workspace.WorkspaceWidget)

Aggregations

WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)16 BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)8 BlockLink (edu.mit.blocks.codeblocks.BlockLink)6 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)6 Block (edu.mit.blocks.codeblocks.Block)5 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)4 Point (java.awt.Point)3 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)2 Container (java.awt.Container)2 MiniMap (edu.mit.blocks.workspace.MiniMap)1 Workspace (edu.mit.blocks.workspace.Workspace)1 Dimension (java.awt.Dimension)1 PopupMenu (java.awt.PopupMenu)1 Point2D (java.awt.geom.Point2D)1 ArrayList (java.util.ArrayList)1