Search in sources :

Example 26 with BlockConnector

use of edu.mit.blocks.codeblocks.BlockConnector in project openblocks by mikaelhg.

the class MiniMap method blockDropped.

/**
 * @param block
 * @modifies block
 * @effects  Set block to whatever page it is on is one exist.
 * 			 Otherwise, set it to whatever Widget is currently underneath
 * 			 it.
 */
public void blockDropped(RenderableBlock block) {
    // TODO - djwendel - this needs to choose the widget of the top block of the stack, not the widget under the block necessarily
    Point location = block.getLocation();
    if (location.getY() <= 0) {
        location.setLocation(location.getX(), 1);
    }
    // find page that it should drop on. w could be null.  so watch out.
    WorkspaceWidget w = workspace.getWidgetAt(location);
    for (Page page : this.blockCanvas.getPages()) {
        if (page.contains(SwingUtilities.convertPoint(block.getParent(), location, page.getJComponent()))) {
            w = page;
        }
    }
    /* before dropping, check if any connected blocks were dropped elsewhere.
        if they were, drop there instead. (this relies on the fact that stacks
        of blocks are dropped bottom-first, so the place to check is at the
        socket blocks.  If stacks were dropped top-first, this would check the
        blocks up the stack rather than down the stack. TODO - this doesn't
        always work, because there can be multiple "branches" that might get
        dropped on different places.  Need to find a way to drop according the
        widget of the top block of the stack...*/
    RenderableBlock socketBlock;
    for (BlockConnector con : (workspace.getEnv().getBlock(block.getBlockID()).getSockets())) {
        socketBlock = block.getWorkspace().getEnv().getRenderableBlock(con.getBlockID());
        if (socketBlock != null) {
            w = socketBlock.getParentWidget();
        }
    }
    w.blockDropped(block);
    this.repaint();
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Point(java.awt.Point)

Aggregations

BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)26 Block (edu.mit.blocks.codeblocks.Block)14 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)9 Point (java.awt.Point)9 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)8 BlockLink (edu.mit.blocks.codeblocks.BlockLink)4 ArrayList (java.util.ArrayList)3 BlockStub (edu.mit.blocks.codeblocks.BlockStub)2 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)2 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)2 Dimension (java.awt.Dimension)2 Workspace (edu.mit.blocks.workspace.Workspace)1 Component (java.awt.Component)1 Container (java.awt.Container)1 Rectangle (java.awt.Rectangle)1 AffineTransform (java.awt.geom.AffineTransform)1 Point2D (java.awt.geom.Point2D)1 JComponent (javax.swing.JComponent)1