Search in sources :

Example 21 with RenderableBlock

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

the class FocusTraversalManager method workspaceEventOccurred.

// /////////////////////////////
// WORKSPACE LISTENER METHOD //
// /////////////////////////////
/**
 * Subscription: BLOCK_ADDED events.
 * Action: add this.mouselistener to the block referanced by event
 * @requires block reference in event is not null
 * @modifies this.blockFocusOwner && event.block
 * @effects Add this.mouselistener to this.blockFocusOwner
 * 			removes focus from this.blockFocusOwner
 * 			adds focus to e.getSource iff e.getSource
 * 			is instance of BlockCanvas and RenderableBlock
 */
public void workspaceEventOccurred(WorkspaceEvent event) {
    switch(event.getEventType()) {
        case WorkspaceEvent.BLOCK_ADDED:
            // only add focus manager as listener to blocks added to pages
            if (!(event.getSourceWidget() instanceof Page)) {
                break;
            }
            RenderableBlock rb = workspace.getEnv().getRenderableBlock(event.getSourceBlockID());
            if (rb == null) {
                break;
            }
            // only add once
            for (MouseListener l : rb.getMouseListeners()) {
                if (l.equals(this)) {
                    return;
                // TODO: this shouldn't return, it should break
                // but you can't double break in java
                }
            }
            rb.addMouseListener(this);
            rb.addKeyListener(this);
            setFocus(event.getSourceBlockID());
            rb.grabFocus();
            break;
    }
}
Also used : MouseListener(java.awt.event.MouseListener) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Page(edu.mit.blocks.workspace.Page)

Example 22 with RenderableBlock

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

the class TypeBlockManager method automateBlockInsertion.

/**
 * @requires none
 * @modifies 	focusManager.focusblock &&
 * 				focusManager.focuspoint &&
 * 				blockCanvas
 * @effects Do nothing if "genusName" does not map to a valid block.
 * 			Otherwise, create and add a new block with matching genus
 * 			and label properties to one of the following:
 * 				1. the current block with focus at (0,0)
 * 				   relative to that block.
 * 				2. the current block with focus at next
 * 				   applicable socket location
 * 				3. the canvas at the last mouse click point.
 * 			If label is not an empty string, then set the block label
 * 			to that string.
 * 			Then update any focus and block connections.
 */
protected void automateBlockInsertion(Workspace workspace, TextualFactoryBlock block, String label) {
    TypeBlockManager typeBlockManager = workspace.getTypeBlockManager();
    if (!typeBlockManager.isEnabled()) {
        System.err.println("AutoMateBlockInsertion invoked but typeBlockManager is disabled.");
        return;
    }
    RenderableBlock createdRB = createRenderableBlock(block);
    // sets the label of the block to whatever the user typed (should only be numbers)
    if (label != EMPTY_LABEL_NAME) {
        createdRB.getBlock().setBlockLabel(label);
    }
    // changes the plus number labels back to +
    if (label.equals(NUMBER_PLUS_OPERATION_LABEL)) {
        createdRB.getBlock().setBlockLabel(PLUS_OPERATION_LABEL);
    }
    // changes the plus text labels back to +
    if (label.equals(TEXT_PLUS_OPERATION_LABEL)) {
        createdRB.getBlock().setBlockLabel(PLUS_OPERATION_LABEL);
    }
    if (createdRB == null) {
        return;
    } else {
        typeBlockManager.addBlock(createdRB);
    }
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 23 with RenderableBlock

use of edu.mit.blocks.renderable.RenderableBlock 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));
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector)

Example 24 with RenderableBlock

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

the class TypeBlockManager method traverseFocus.

private void traverseFocus(Direction dir) {
    if (isNullBlockInstance(focusManager.getFocusBlockID())) {
        if (dir == Direction.UP) {
            blockCanvas.getVerticalModel().setValue(blockCanvas.getVerticalModel().getValue() - 5);
        } else if (dir == Direction.DOWN) {
            blockCanvas.getVerticalModel().setValue(blockCanvas.getVerticalModel().getValue() + 5);
        } else if (dir == Direction.LEFT) {
            blockCanvas.getHorizontalModel().setValue(blockCanvas.getHorizontalModel().getValue() - 5);
        } else if (dir == Direction.RIGHT) {
            blockCanvas.getHorizontalModel().setValue(blockCanvas.getHorizontalModel().getValue() + 5);
        } else if (dir == Direction.ESCAPE) {
            // according to the focus manager, the canvas already
            // has focus. So, just request focus again.
            this.blockCanvas.getCanvas().requestFocus();
        } else if (dir == Direction.ENTER) {
        }
    } else {
        if (dir == Direction.UP) {
            focusManager.focusBeforeBlock();
        } else if (dir == Direction.DOWN) {
            focusManager.focusAfterBlock();
        } else if (dir == Direction.LEFT) {
            focusManager.focusPrevBlock();
        } else if (dir == Direction.RIGHT) {
            focusManager.focusNextBlock();
        } else if (dir == Direction.ESCAPE) {
            RenderableBlock block = workspace.getEnv().getRenderableBlock(focusManager.getFocusBlockID());
            Point location = SwingUtilities.convertPoint(block, new Point(0, 0), this.blockCanvas.getCanvas());
            this.focusManager.setFocus(location, Block.NULL);
            this.blockCanvas.getCanvas().requestFocus();
        } else if (dir == Direction.ENTER) {
            workspace.getEnv().getRenderableBlock(focusManager.getFocusBlockID()).switchToLabelEditingMode(true);
        }
    }
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Point(java.awt.Point)

Example 25 with RenderableBlock

use of edu.mit.blocks.renderable.RenderableBlock 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));
}
Also used : 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)

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