Search in sources :

Example 11 with RenderableBlock

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

Example 12 with RenderableBlock

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

the class TypeBlockManager method automateBlockInsertion.

/**
 * @param workspace The workspace in use
 * @param genusName
 * @param label
 *
 * @requires if (label != null) then associated block.isLabelEditable() should return true
 * @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.
 * 			Then update any focus and block connections.
 */
protected void automateBlockInsertion(Workspace workspace, String genusName, String label) {
    TypeBlockManager typeBlockManager = workspace.getTypeBlockManager();
    if (!typeBlockManager.isEnabled()) {
        System.err.println("AutoMateBlockInsertion invoked but typeBlockManager is disabled.");
        return;
    }
    // if genus is null, DO NOT insert a new block, DO NOT change the focus
    if (genusName == null) {
        return;
    }
    // get matching textual Block
    RenderableBlock createdRB = BlockUtilities.getBlock(workspace, genusName, null);
    if (createdRB == null) {
        return;
    } else {
        // does not need to have a unique label
        if (label != null && workspace.getEnv().getBlock(createdRB.getBlockID()).isLabelEditable() && !workspace.getEnv().getBlock(createdRB.getBlockID()).labelMustBeUnique()) {
            workspace.getEnv().getBlock(createdRB.getBlockID()).setBlockLabel(label);
        }
        // add block
        typeBlockManager.addBlock(createdRB);
    }
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 13 with RenderableBlock

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

the class TypeBlockManager method pasteStack.

private void pasteStack(BlockNode node) {
    // ====================>>>>>>>>>>>>>>>>>>>>>>>>>
    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);
            // make sure the childrens are placed correctly
            mirror.moveConnectedBlocks();
        } 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);
        }
    }
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Point(java.awt.Point) WorkspaceWidget(edu.mit.blocks.workspace.WorkspaceWidget)

Example 14 with RenderableBlock

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

the class TypeBlockManager method removeChildrenBlock.

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

Example 15 with RenderableBlock

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

the class TypeBlockManager method displayAutoCompletePanel.

/**
 * @requires this.blockCanvas.getCanvas() != null
 * @param character
 */
private void displayAutoCompletePanel(char character) {
    // ====================>>>>>>>>>>>>>>>>>>>>>>>>>
    if (invalidBlockID(focusManager.getFocusBlockID())) {
        // canvas has focus
        this.blockCanvas.getCanvas().add(autoCompletePanel, JLayeredPane.DRAG_LAYER);
        autoCompletePanel.setLocation(this.focusManager.getCanvasPoint());
        autoCompletePanel.setVisible(true);
        autoCompletePanel.requestFocus();
    } else {
        // renderableblock has focus
        this.blockCanvas.getCanvas().add(autoCompletePanel, JLayeredPane.DRAG_LAYER);
        RenderableBlock block = workspace.getEnv().getRenderableBlock(focusManager.getFocusBlockID());
        Point location = SwingUtilities.convertPoint(block, this.focusManager.getBlockPoint(), this.blockCanvas.getCanvas());
        location.translate(10, 10);
        autoCompletePanel.setLocation(location);
        autoCompletePanel.setVisible(true);
        autoCompletePanel.requestFocus();
    }
    autoCompletePanel.setText(String.valueOf(character));
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Point(java.awt.Point)

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