Search in sources :

Example 26 with Block

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

the class BlockUtilities method cloneBlock.

public static RenderableBlock cloneBlock(Block myblock) {
    String mygenusname = myblock.getGenusName();
    String label = myblock.getBlockLabel();
    final Workspace workspace = myblock.getWorkspace();
    //from its genus label.
    if (!myblock.getInitialLabel().equals(myblock.getBlockLabel())) {
        //acquire prefix and suffix length from myblock label
        int prefixLength = myblock.getLabelPrefix().length();
        int suffixLength = myblock.getLabelSuffix().length();
        //weird, but its the way block labels were designed.
        if (//TODO we could do this outside of this method, even in constructor
        prefixLength > 0 || suffixLength > 0) {
            label = myblock.getBlockLabel().substring(prefixLength, myblock.getBlockLabel().length() - suffixLength);
        }
    }
    //check genus instance counter and if label unique - change label accordingly
    //also check if label already has a value at the end, if so update counter to have the max value
    //TODO ria need to make this smarter
    //some issues to think about:
    // - what if they throw out an instance, such as setup2? should the next time they take out
    //   a setup block, should it have setup2 on it?  but wouldn't that be confusing?
    // - when we load up a new project with some instances with numbered labels, how do we keep
    //   track of new instances relative to these old ones?
    // - the old implementation just iterated through all the instances of a particular genus in the
    //   workspace and compared a possible label to the current labels of that genus.  if there wasn't
    //   any current label that matched the possible label, it returned that label.  do we want to do this?
    //   is there something more efficient?
    //labelWithIndex will have the instance value
    String labelWithIndex = label;
    int value;
    //initialize value that will be appended to the end of the label
    if (instanceCounter.containsKey(mygenusname)) {
        value = instanceCounter.get(mygenusname).intValue();
    } else {
        value = 0;
    }
    //iterate until label is valid
    while (!isLabelValid(myblock, labelWithIndex)) {
        value++;
        labelWithIndex = labelWithIndex + value;
    }
    //set valid label and save current instance number
    instanceCounter.put(mygenusname, new Integer(value));
    if (//only set it if the label actually changed...
    !labelWithIndex.equals(label)) {
        label = labelWithIndex;
    }
    Block block;
    if (myblock instanceof BlockStub) {
        Block parent = ((BlockStub) myblock).getParent();
        block = new BlockStub(workspace, parent.getBlockID(), parent.getGenusName(), parent.getBlockLabel(), myblock.getGenusName());
    } else {
        block = new Block(workspace, myblock.getGenusName(), label);
    }
    // TODO - djwendel - create a copy of the RB properties too, using an RB copy constructor.  Don't just use the genus.
    //RenderableBlock renderable = new RenderableBlock(this.getParentWidget(), block.getBlockID());
    RenderableBlock renderable = new RenderableBlock(workspace, null, block.getBlockID());
    renderable.setZoomLevel(BlockUtilities.zoom);
    renderable.redrawFromTop();
    renderable.repaint();
    return renderable;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockStub(edu.mit.blocks.codeblocks.BlockStub) Workspace(edu.mit.blocks.workspace.Workspace)

Example 27 with Block

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

the class RenderableBlock method getMaxWidthOfSockets.

/**
     * Helper method for updateSocketSpace and calcStackDim.
     * Returns the maximum width of the specified blockID's socket blocks
     * @param blockID the Long blockID of the desired block
     */
public int getMaxWidthOfSockets(Long blockID) {
    int width = 0;
    Block block = workspace.getEnv().getBlock(blockID);
    RenderableBlock rb = workspace.getEnv().getRenderableBlock(blockID);
    for (BlockConnector socket : block.getSockets()) {
        Dimension socketDim = rb.getSocketSpaceDimension(socket);
        if (socketDim != null) {
            if (socketDim.width > width) {
                width = socketDim.width;
            }
        }
    }
    return width;
}
Also used : Block(edu.mit.blocks.codeblocks.Block) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Dimension(java.awt.Dimension) Point(java.awt.Point)

Example 28 with Block

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

the class TypeBlockManager method automateAddition.

protected void automateAddition(Workspace workspace, char character) {
    TypeBlockManager typeBlockManager = workspace.getTypeBlockManager();
    if (!typeBlockManager.isEnabled()) {
        System.err.println("AutoMateMultiplication invoked but typeBlockManager is disabled.");
        return;
    }
    //get focus block
    Long parentBlockID = typeBlockManager.focusManager.getFocusBlockID();
    if (isNullBlockInstance(parentBlockID)) {
        //focus on canvas
        automateBlockInsertion(workspace, "sum", null);
    } else {
        Block parentBlock = workspace.getEnv().getBlock(parentBlockID);
        if (parentBlock.getGenusName().equals("string")) {
            //focus on string block
            automateBlockInsertion(workspace, "string-append", null);
        } else if (parentBlock.getGenusName().equals("string-append")) {
            //focus on string append block
            automateBlockInsertion(workspace, "string-append", null);
        } else {
            //focus on any other block
            automateBlockInsertion(workspace, "sum", null);
        }
    }
}
Also used : Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) TextualFactoryBlock(edu.mit.blocks.renderable.TextualFactoryBlock)

Example 29 with Block

use of edu.mit.blocks.codeblocks.Block 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

Block (edu.mit.blocks.codeblocks.Block)29 RenderableBlock (edu.mit.blocks.renderable.RenderableBlock)15 BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)14 Point (java.awt.Point)7 BlockStub (edu.mit.blocks.codeblocks.BlockStub)5 TextualFactoryBlock (edu.mit.blocks.renderable.TextualFactoryBlock)5 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)5 BlockLink (edu.mit.blocks.codeblocks.BlockLink)4 FactoryRenderableBlock (edu.mit.blocks.renderable.FactoryRenderableBlock)3 ArrayList (java.util.ArrayList)3 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)2 Color (java.awt.Color)2 Dimension (java.awt.Dimension)2 Matcher (java.util.regex.Matcher)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 Workspace (edu.mit.blocks.workspace.Workspace)1 Container (java.awt.Container)1 LinkedHashMap (java.util.LinkedHashMap)1 StringTokenizer (java.util.StringTokenizer)1