Search in sources :

Example 1 with RenderableBlock

use of edu.mit.blocks.renderable.RenderableBlock 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 RenderableBlock

use of edu.mit.blocks.renderable.RenderableBlock 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 RenderableBlock

use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.

the class FactoryManager method addDynamicBlocks.

public void addDynamicBlocks(Collection<RenderableBlock> blocks, String drawer) {
    // find canvas
    for (FactoryCanvas canvas : this.dynamicCanvases) {
        if (canvas.getName().equals(drawer)) {
            for (RenderableBlock block : blocks) {
                if (block == null || Block.NULL.equals(block.getBlockID())) {
                    continue;
                }
                canvas.addBlock(block);
                workspace.notifyListeners(new WorkspaceEvent(workspace, this, block.getBlockID(), WorkspaceEvent.BLOCK_ADDED));
            }
            canvas.layoutBlocks();
            return;
        }
    }
    this.printError("Drawer not found: " + drawer);
    return;
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock)

Example 4 with RenderableBlock

use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.

the class FactoryManager method addStaticBlocks.

/**
 * Add blocks to drawer if drawer can be found.  Add graphically
 * and alos throw event.  Do nothing if no drawer if specified
 * name is found.
 *
 * @param blocks
 * @param drawer
 */
public void addStaticBlocks(Collection<RenderableBlock> blocks, String drawer) {
    // find canvas
    for (FactoryCanvas canvas : this.staticCanvases) {
        if (canvas.getName().equals(drawer)) {
            for (RenderableBlock block : blocks) {
                if (block == null || Block.NULL.equals(block.getBlockID())) {
                    continue;
                }
                canvas.addBlock(block);
                workspace.notifyListeners(new WorkspaceEvent(workspace, this, block.getBlockID(), WorkspaceEvent.BLOCK_ADDED));
            }
            canvas.layoutBlocks();
            return;
        }
    }
    this.printError("Drawer not found: " + drawer);
    return;
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock)

Example 5 with RenderableBlock

use of edu.mit.blocks.renderable.RenderableBlock in project openblocks by mikaelhg.

the class FactoryManager method addSubsetBlocks.

/**
 * Adds the specified RenderableBlocks to the drawer with the specified drawerName.  Do nothing
 * if the drawer with the specified drawerName does not exist.
 * @param blocks Collection of RenderableBlocks to the drawer with name: drawerName
 * @param drawerName String name of the drawer to add blocks to
 */
public void addSubsetBlocks(Collection<RenderableBlock> blocks, String drawerName) {
    // find canvas //TODO generalize the following code to one method where you pass in the blocks, drawername, and canvas types
    for (FactoryCanvas canvas : this.subsetCanvases) {
        if (canvas.getName().equals(drawerName)) {
            for (RenderableBlock block : blocks) {
                if (block == null || Block.NULL.equals(block.getBlockID())) {
                    continue;
                }
                canvas.addBlock(block);
                workspace.notifyListeners(new WorkspaceEvent(workspace, this, block.getBlockID(), WorkspaceEvent.BLOCK_ADDED));
            }
            canvas.layoutBlocks();
            return;
        }
    }
    this.printError("Drawer not found: " + drawerName);
    return;
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock)

Aggregations

RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)35 Point (java.awt.Point)9 FactoryRenderableBlock (edu.mit.blocks.renderable.FactoryRenderableBlock)7 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)6 Block (edu.mit.blocks.codeblocks.Block)5 BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)5 ArrayList (java.util.ArrayList)4 Color (java.awt.Color)3 Point2D (java.awt.geom.Point2D)3 Node (org.w3c.dom.Node)3 NodeList (org.w3c.dom.NodeList)3 BlockLink (edu.mit.blocks.codeblocks.BlockLink)2 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)2 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)2 Component (java.awt.Component)2 Rectangle (java.awt.Rectangle)2 Matcher (java.util.regex.Matcher)2 JComponent (javax.swing.JComponent)2 BlockStub (edu.mit.blocks.codeblocks.BlockStub)1 CBorderlessButton (edu.mit.blocks.codeblockutil.CBorderlessButton)1