Search in sources :

Example 16 with RenderableBlock

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

the class BlockStackSorterUtil method loadPageFrom.

// ////////////////////////
// SAVING AND LOADING	//
// ////////////////////////
public ArrayList<RenderableBlock> loadPageFrom(Node pageNode, boolean importingPage) {
    // note: this code is duplicated in BlockCanvas.loadSaveString().
    NodeList pageChildren = pageNode.getChildNodes();
    Node pageChild;
    ArrayList<RenderableBlock> loadedBlocks = new ArrayList<RenderableBlock>();
    HashMap<Long, Long> idMapping = importingPage ? new HashMap<Long, Long>() : null;
    if (importingPage) {
        reset();
    }
    for (int i = 0; i < pageChildren.getLength(); i++) {
        pageChild = pageChildren.item(i);
        if (pageChild.getNodeName().equals("PageBlocks")) {
            NodeList blocks = pageChild.getChildNodes();
            Node blockNode;
            for (int j = 0; j < blocks.getLength(); j++) {
                blockNode = blocks.item(j);
                RenderableBlock rb = RenderableBlock.loadBlockNode(workspace, blockNode, this, idMapping);
                // save the loaded blocks to add later
                loadedBlocks.add(rb);
            }
            // should only have one set of page blocks
            break;
        }
    }
    return loadedBlocks;
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Point(java.awt.Point)

Example 17 with RenderableBlock

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

the class BlockStackSorterUtil method setPageName.

// ////////////////////////////
// Rendering Mutators		//
// ////////////////////////////
/**
 * @param newName - the new name of this page.
 *
 * @requires newName != null
 * @modifies this.name
 * @effects sets the name of this page to be newName.
 */
public void setPageName(String newName) {
    if (pageDrawer.equals(this.pageJComponent.getName())) {
        pageDrawer = newName;
    }
    this.pageJComponent.setName(newName);
    this.collapse.setText(newName);
    // iterate through blocks and update the ones that are page label enabled
    for (RenderableBlock block : this.getBlocks()) {
        if (workspace.getEnv().getBlock(block.getBlockID()).isPageLabelSetByPage()) {
            workspace.getEnv().getBlock(block.getBlockID()).setPageLabel(this.getPageName());
            block.repaintBlock();
        }
    }
    PageChangeEventManager.notifyListeners();
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 18 with RenderableBlock

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

the class BlockStackSorterUtil method loadState.

/**
 * @overrides ISupportMomento.loadState()
 */
@Override
public void loadState(Object memento) {
    assert (memento instanceof PageState) : "ISupportMemento contract violated in Page";
    if (memento instanceof PageState) {
        PageState state = (PageState) memento;
        // load basic page information
        this.setPageName(state.name);
        this.setPageId(state.id);
        this.setPageColor(state.color);
        this.setPixelWidth(state.width);
        // Load block information
        Map<Long, Object> renderableBlockStates = state.renderableBlocks;
        List<Long> unloadedRenderableBlockStates = new LinkedList<Long>();
        List<Long> loadedBlocks = new LinkedList<Long>();
        for (Long id : renderableBlockStates.keySet()) {
            unloadedRenderableBlockStates.add(id);
        }
        // against all the blocks that already exist.
        for (RenderableBlock existingBlock : getBlocks()) {
            Long existingBlockID = existingBlock.getBlockID();
            if (renderableBlockStates.containsKey(existingBlockID)) {
                existingBlock.loadState(renderableBlockStates.get(existingBlockID));
                unloadedRenderableBlockStates.remove(existingBlockID);
                loadedBlocks.add(existingBlockID);
            }
        }
        ArrayList<RenderableBlock> blocksToRemove = new ArrayList<RenderableBlock>();
        // Now, find all the blocks that don't exist in the save state and flag them to be removed.
        for (RenderableBlock existingBlock : this.getBlocks()) {
            Long existingBlockID = existingBlock.getBlockID();
            if (!loadedBlocks.contains(existingBlockID)) {
                blocksToRemove.add(existingBlock);
            }
        }
        // iterator.
        for (RenderableBlock toBeRemovedBlock : blocksToRemove) {
            this.removeBlock(toBeRemovedBlock);
        }
        // Finally, add all the remaining blocks that weren't there before
        ArrayList<RenderableBlock> blocksToAdd = new ArrayList<RenderableBlock>();
        for (Long newBlockID : unloadedRenderableBlockStates) {
            RenderableBlock newBlock = new RenderableBlock(workspace, this, newBlockID);
            newBlock.loadState(renderableBlockStates.get(newBlockID));
            blocksToAdd.add(newBlock);
        }
        this.addBlocks(blocksToAdd);
        this.pageJComponent.repaint();
    }
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 19 with RenderableBlock

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

the class BlockStackSorterUtil method getState.

/**
 * @overrides ISupportMomento.getState
 */
@Override
public Object getState() {
    PageState state = new PageState();
    // Populate basic page information
    state.name = getPageName();
    state.id = getPageId();
    state.color = getPageColor();
    state.width = this.pageJComponent.getWidth();
    // Fill in block information
    for (RenderableBlock rb : this.getBlocks()) {
        state.renderableBlocks.put(rb.getBlockID(), rb.getState());
    }
    return state;
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 20 with RenderableBlock

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

the class BlockStackSorterUtil method sortBlockStacks.

/**
 * This method serves to help clients sort blocks within a page
 * in some manner.
 *
 * @param page
 * @param topLevelBlocks
 *
 * @requires page != null && topLevelBlocks != null
 * @modifies the location of all topLevelBlocks
 * @effects sort the topLevelBlocks and move them to an order location on the page
 */
protected static void sortBlockStacks(Page page, Collection<RenderableBlock> topLevelBlocks) {
    blocksToArrange.clear();
    positioningBounds.setBounds(BUFFER_BETWEEN_BLOCKS, BUFFER_BETWEEN_BLOCKS, 0, BUFFER_BETWEEN_BLOCKS);
    // created an ordered list of blocks based on x-coordinate position
    blocksToArrange.addAll(topLevelBlocks);
    // Naively places blocks from top to bottom, left to right.
    for (RenderableBlock block : blocksToArrange) {
        Rectangle bounds = block.getStackBounds();
        if (positioningBounds.height + bounds.height > page.getJComponent().getHeight()) {
            // need to go to next column
            positioningBounds.x = positioningBounds.x + positioningBounds.width + BUFFER_BETWEEN_BLOCKS;
            positioningBounds.width = 0;
            positioningBounds.height = BUFFER_BETWEEN_BLOCKS;
        }
        block.setLocation(positioningBounds.x, positioningBounds.height);
        // sets the x and y position for when workspace is unzoomed
        block.setUnzoomedX(block.calculateUnzoomedX(positioningBounds.x));
        block.setUnzoomedY(block.calculateUnzoomedY(positioningBounds.height));
        block.moveConnectedBlocks();
        // update positioning bounds
        positioningBounds.width = Math.max(positioningBounds.width, bounds.width);
        positioningBounds.height = positioningBounds.height + bounds.height + BUFFER_BETWEEN_BLOCKS;
        if (positioningBounds.x + positioningBounds.width > page.getJComponent().getWidth()) {
            // resize page to the difference
            page.addPixelWidth(positioningBounds.x + positioningBounds.width - page.getJComponent().getWidth());
        }
    }
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Rectangle(java.awt.Rectangle)

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