Search in sources :

Example 1 with Workspace

use of edu.mit.blocks.workspace.Workspace in project openblocks by mikaelhg.

the class BlockDropAnimator method dropBlock.

/**
     * drops block by adding to widget and throwing workspace event
     * @param block
     *
     * @requires block != null; block.blockID != null; block.blockID != Block.NULL
     * 			 workspace widget != null
     */
private static void dropBlock(RenderableBlock block) {
    if (block == null) {
        throw new RuntimeException("Invariant Violated: child block was null");
    }
    Workspace workspace = block.getWorkspace();
    Page p = workspace.getCurrentPage(block);
    if (p == null) {
        throw new RuntimeException("Invariant Violated: child block was located on a null widget");
    }
    //add this block to that page.
    p.blockDropped(block);
}
Also used : Page(edu.mit.blocks.workspace.Page) Workspace(edu.mit.blocks.workspace.Workspace)

Example 2 with Workspace

use of edu.mit.blocks.workspace.Workspace in project openblocks by mikaelhg.

the class RenderableBlock method startDragging.

private void startDragging(RenderableBlock renderable, WorkspaceWidget widget) {
    renderable.pickedUp = true;
    renderable.lastDragWidget = widget;
    if (renderable.hasComment()) {
        renderable.comment.setConstrainComment(false);
    }
    Component oldParent = renderable.getParent();
    Workspace workspace = renderable.getWorkspace();
    workspace.addToBlockLayer(renderable);
    renderable.setLocation(SwingUtilities.convertPoint(oldParent, renderable.getLocation(), workspace));
    renderable.setHighlightParent(workspace);
    for (BlockConnector socket : BlockLinkChecker.getSocketEquivalents(workspace.getEnv().getBlock(renderable.blockID))) {
        if (socket.hasBlock()) {
            startDragging(workspace.getEnv().getRenderableBlock(socket.getBlockID()), widget);
        }
    }
}
Also used : BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Component(java.awt.Component) JComponent(javax.swing.JComponent) Workspace(edu.mit.blocks.workspace.Workspace)

Example 3 with Workspace

use of edu.mit.blocks.workspace.Workspace in project openblocks by mikaelhg.

the class BlockUtilities method deleteBlock.

public static void deleteBlock(RenderableBlock block) {
    block.setLocation(0, 0);
    WorkspaceWidget widget = block.getParentWidget();
    if (widget != null) {
        widget.removeBlock(block);
    }
    Container parent = block.getParent();
    if (parent != null) {
        parent.remove(block);
        parent.validate();
    }
    block.setParentWidget(null);
    Workspace workspace = block.getWorkspace();
    workspace.notifyListeners(new WorkspaceEvent(workspace, widget, block.getBlockID(), WorkspaceEvent.BLOCK_REMOVED));
}
Also used : Container(java.awt.Container) WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) WorkspaceWidget(edu.mit.blocks.workspace.WorkspaceWidget) Workspace(edu.mit.blocks.workspace.Workspace)

Example 4 with Workspace

use of edu.mit.blocks.workspace.Workspace 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)

Aggregations

Workspace (edu.mit.blocks.workspace.Workspace)4 Block (edu.mit.blocks.codeblocks.Block)1 BlockConnector (edu.mit.blocks.codeblocks.BlockConnector)1 BlockStub (edu.mit.blocks.codeblocks.BlockStub)1 Page (edu.mit.blocks.workspace.Page)1 WorkspaceEvent (edu.mit.blocks.workspace.WorkspaceEvent)1 WorkspaceWidget (edu.mit.blocks.workspace.WorkspaceWidget)1 Component (java.awt.Component)1 Container (java.awt.Container)1 JComponent (javax.swing.JComponent)1