Search in sources :

Example 26 with RenderableBlock

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

the class Workspace method setWorkspaceZoom.

/**
 * Sets the Workspace zoom at the specified zoom level
 * @param newZoom the desired zoom level
 */
public void setWorkspaceZoom(double newZoom) {
    double oldZoom = this.zoom;
    int cDX = 0, cDY = 0;
    this.zoom = newZoom;
    BlockUtilities.setZoomLevel(newZoom);
    for (RenderableBlock block : getRenderableBlocks()) {
        block.setZoomLevel(newZoom);
    }
    for (RenderableBlock block : getFactoryManager().getBlocks()) {
        block.setZoomLevel(newZoom);
    }
    for (Page p : getBlockCanvas().getPages()) {
        for (RenderableBlock block : p.getTopLevelBlocks()) {
            // moved yet. otherwise, the unzoomed X and Y are calculated in RenderableBlock
            if (block.getUnzoomedX() == 0.0 && block.getUnzoomedY() == 0.0) {
                if (newZoom == 1.0) {
                    block.setUnzoomedX(block.getX());
                    block.setUnzoomedY(block.getY());
                } else {
                    block.setUnzoomedX(block.calculateUnzoomedX(block.getX()));
                    block.setUnzoomedY(block.calculateUnzoomedY(block.getY()));
                }
            } else {
            }
            if (block.hasComment()) {
                // determine the new relative position of the comment based on the current relative position
                cDX = (int) ((block.getComment().getX() - block.getX()) / oldZoom * newZoom);
                cDY = (int) ((block.getComment().getY() - block.getY()) / oldZoom * newZoom);
            }
            // calculates the new position based on the initial position when zoom is at 1.0
            block.setLocation((int) (block.getUnzoomedX() * this.zoom), (int) (block.getUnzoomedY() * this.zoom));
            if (block.hasComment()) {
                // Set the comment location to the new relative position
                block.getComment().setLocation(block.getX() + cDX, block.getY() + cDY);
            }
            block.redrawFromTop();
            block.repaint();
        }
    }
    Page.setZoomLevel(newZoom);
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Point(java.awt.Point)

Example 27 with RenderableBlock

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

the class BlockStackSorterUtil method addLoadedBlocks.

public void addLoadedBlocks(Collection<RenderableBlock> loadedBlocks, boolean importingPage) {
    for (RenderableBlock rb : loadedBlocks) {
        if (rb != null) {
            // add graphically
            getRBParent().addToBlockLayer(rb);
            rb.setHighlightParent(this.getRBParent());
            // System.out.println("loading rb to canvas: "+rb+" at: "+rb.getBounds());
            // add internallly
            workspace.notifyListeners(new WorkspaceEvent(workspace, this, rb.getBlockID(), WorkspaceEvent.BLOCK_ADDED));
            if (importingPage) {
                workspace.getEnv().getBlock(rb.getBlockID()).setFocus(false);
                rb.resetHighlight();
                rb.clearBufferedImage();
            }
        }
    }
    // blocks, etc.
    for (RenderableBlock rb : this.getTopLevelBlocks()) {
        rb.redrawFromTop();
        if (rb.isCollapsed()) {
            // This insures that blocks connected to a collapsed top level block
            // are located properly and have the proper visibility set.
            // This doesn't work until all blocks are loaded and dimensions are set.
            rb.updateCollapse();
        }
    }
    this.pageJComponent.revalidate();
    this.pageJComponent.repaint();
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 28 with RenderableBlock

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

the class PageDrawerLoadingUtils method loadBlockDrawerSets.

public static void loadBlockDrawerSets(Workspace workspace, Element root, FactoryManager manager) {
    Pattern attrExtractor = Pattern.compile("\"(.*)\"");
    Matcher nameMatcher;
    NodeList drawerSetNodes = root.getElementsByTagName("BlockDrawerSet");
    Node drawerSetNode;
    for (int i = 0; i < drawerSetNodes.getLength(); i++) {
        drawerSetNode = drawerSetNodes.item(i);
        if (drawerSetNode.getNodeName().equals("BlockDrawerSet")) {
            NodeList drawerNodes = drawerSetNode.getChildNodes();
            Node drawerNode;
            // retreive drawer information of this bar
            for (int j = 0; j < drawerNodes.getLength(); j++) {
                drawerNode = drawerNodes.item(j);
                if (drawerNode.getNodeName().equals("BlockDrawer")) {
                    String drawerName = null;
                    Color buttonColor = Color.blue;
                    StringTokenizer col;
                    nameMatcher = attrExtractor.matcher(drawerNode.getAttributes().getNamedItem("name").toString());
                    if (nameMatcher.find()) {
                        // will be true
                        drawerName = nameMatcher.group(1);
                    }
                    // get drawer's color:
                    Node colorNode = drawerNode.getAttributes().getNamedItem("button-color");
                    if (colorNode != null) {
                        nameMatcher = attrExtractor.matcher(colorNode.toString());
                        if (nameMatcher.find()) {
                            // will be true
                            col = new StringTokenizer(nameMatcher.group(1));
                            if (col.countTokens() == 3) {
                                buttonColor = new Color(Integer.parseInt(col.nextToken()), Integer.parseInt(col.nextToken()), Integer.parseInt(col.nextToken()));
                            } else {
                                buttonColor = Color.BLACK;
                            }
                        }
                    }
                    manager.addStaticDrawer(drawerName, buttonColor);
                    // get block genuses in drawer and create blocks
                    NodeList drawerBlocks = drawerNode.getChildNodes();
                    Node blockNode;
                    ArrayList<RenderableBlock> drawerRBs = new ArrayList<RenderableBlock>();
                    for (int k = 0; k < drawerBlocks.getLength(); k++) {
                        blockNode = drawerBlocks.item(k);
                        if (blockNode.getNodeName().equals("BlockGenusMember")) {
                            String genusName = blockNode.getTextContent();
                            assert workspace.getEnv().getGenusWithName(genusName) != null : "Unknown BlockGenus: " + genusName;
                            Block newBlock;
                            // don't link factory blocks to their stubs because they will
                            // forever remain inside the drawer and never be active
                            newBlock = new Block(workspace, genusName, false);
                            drawerRBs.add(new FactoryRenderableBlock(workspace, manager, newBlock.getBlockID()));
                        }
                    }
                    manager.addStaticBlocks(drawerRBs, drawerName);
                }
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock) Matcher(java.util.regex.Matcher) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Color(java.awt.Color) ArrayList(java.util.ArrayList) StringTokenizer(java.util.StringTokenizer) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock) Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock)

Example 29 with RenderableBlock

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

the class PageDrawerLoadingUtils method loadPagesAndDrawers.

public static void loadPagesAndDrawers(Workspace workspace, Element root, FactoryManager manager) {
    List<Page> pageList = new ArrayList<Page>();
    // pagesToAdd is needed so that we can add pages all at once
    // to the page bar once all the the pages been loaded
    // Before adding all the pages, this method makes a check
    // if there is only one page with an empty name - if so, it will just
    // add the page to the workspace/block canvas but not add it to this
    // LinkedHashMap<Page, PageBlockDrawer> pagesToAdd = new LinkedHashMap<Page, PageBlockDrawer>();
    LinkedHashMap<String, ArrayList<RenderableBlock>> blocksForDrawers = new LinkedHashMap<String, ArrayList<RenderableBlock>>();
    LinkedHashMap<Page, ArrayList<RenderableBlock>> blocksForPages = new LinkedHashMap<Page, ArrayList<RenderableBlock>>();
    NodeList pagesRoot = root.getElementsByTagName("Pages");
    if (pagesRoot != null) {
        // isBlankPage denotes if the page being loaded is a default blank page
        // in other words, the project did not specify any pages for their environment.
        // EvoBeaker does this
        boolean isBlankPage = false;
        Node pagesNode = pagesRoot.item(0);
        if (pagesNode == null) {
            // short-circuit exit if there's nothing to load
            return;
        }
        Node opt_item = pagesNode.getAttributes().getNamedItem("drawer-with-page");
        if (opt_item != null) {
            Matcher nameMatcher = attrExtractor.matcher(opt_item.toString());
            if (nameMatcher.find()) {
                Workspace.everyPageHasDrawer = nameMatcher.group(1).equals("yes") ? true : false;
            }
        }
        opt_item = pagesNode.getAttributes().getNamedItem("is-blank-page");
        if (opt_item != null) {
            Matcher nameMatcher = attrExtractor.matcher(opt_item.toString());
            if (nameMatcher.find()) {
                isBlankPage = nameMatcher.group(1).equals("yes") ? true : false;
            }
        }
        // whether pages should show a control to collapse them or not
        boolean collapsiblePages = getBooleanValue(pagesNode, "collapsible-pages");
        Page page;
        NodeList pages = pagesNode.getChildNodes();
        Node pageNode;
        String pageName;
        String pageDrawer;
        Color pageColor;
        boolean pageInFullView;
        int pageWidth;
        String pageId;
        for (int i = 0; i < pages.getLength(); i++) {
            // find them
            pageNode = pages.item(i);
            if (pageNode.getNodeName().equals("Page")) {
                // a page entry
                pageName = getNodeValue(pageNode, "page-name");
                pageColor = getColorValue(pageNode, "page-color");
                pageWidth = getIntValue(pageNode, "page-width");
                pageDrawer = getNodeValue(pageNode, "page-drawer");
                pageInFullView = getBooleanValue(pageNode, "page-infullview");
                pageId = getNodeValue(pageNode, "page-id");
                page = new Page(workspace, pageName, pageWidth, 0, pageDrawer, pageInFullView, pageColor, collapsiblePages);
                page.setPageId(pageId);
                NodeList pageNodes = pageNode.getChildNodes();
                String drawer = null;
                if (Workspace.everyPageHasDrawer) {
                    // create drawer instance
                    manager.addDynamicDrawer(page.getPageDrawer());
                    ArrayList<RenderableBlock> drawerBlocks = new ArrayList<RenderableBlock>();
                    for (int k = 0; k < pageNodes.getLength(); k++) {
                        Node drawerNode = pageNodes.item(k);
                        if (drawerNode.getNodeName().equals("PageDrawer")) {
                            NodeList genusMembers = drawerNode.getChildNodes();
                            String genusName;
                            for (int j = 0; j < genusMembers.getLength(); j++) {
                                Node genusMember = genusMembers.item(j);
                                if (genusMember.getNodeName().equals("BlockGenusMember")) {
                                    genusName = genusMember.getTextContent();
                                    assert workspace.getEnv().getGenusWithName(genusName) != null : "Unknown BlockGenus: " + genusName;
                                    Block block = new Block(workspace, genusName);
                                    drawerBlocks.add(new FactoryRenderableBlock(workspace, manager, block.getBlockID()));
                                }
                            }
                            blocksForDrawers.put(drawer, drawerBlocks);
                            // there can only be one drawer for this page
                            break;
                        }
                    }
                }
                if (isBlankPage) {
                    // place a blank page as the first page
                    workspace.putPage(page, 0);
                    // we anticipate only one page
                    break;
                } else {
                    // we add to the end of the set of pages
                    int position = pageList.size();
                    // add to workspace
                    if (position == 0) {
                        // replace the blank default page
                        workspace.putPage(page, 0);
                    } else {
                        workspace.addPage(page, position);
                    }
                    pageList.add(position, page);
                }
                blocksForPages.put(page, page.loadPageFrom(pageNode, false));
            }
        }
        // add blocks in drawers
        for (String d : blocksForDrawers.keySet()) {
            manager.addDynamicBlocks(blocksForDrawers.get(d), d);
        }
        // blocks in pages
        for (Page p : blocksForPages.keySet()) {
            p.addLoadedBlocks(blocksForPages.get(p), false);
        }
    }
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock) Matcher(java.util.regex.Matcher) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Color(java.awt.Color) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock) Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) FactoryRenderableBlock(edu.mit.blocks.renderable.FactoryRenderableBlock)

Example 30 with RenderableBlock

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

the class FactoryCanvas method layoutBlocks.

void layoutBlocks() {
    RenderableBlock rb;
    int maxWidth = 20;
    int tx = BORDER_WIDTH;
    int ty = BORDER_WIDTH;
    for (Component c : this.getComponents()) {
        if (c instanceof RenderableBlock) {
            rb = (RenderableBlock) c;
            rb.setBounds(tx, ty, rb.getBlockWidth(), rb.getBlockHeight());
            ty = ty + BORDER_WIDTH + rb.getBlockHeight();
            rb.repaint();
            if (maxWidth < rb.getBlockWidth() + BORDER_WIDTH) {
                maxWidth = rb.getBlockWidth() + BORDER_WIDTH;
            }
        }
    }
    this.setPreferredSize(new Dimension(maxWidth, ty));
}
Also used : RenderableBlock(edu.mit.blocks.renderable.RenderableBlock) Dimension(java.awt.Dimension) JComponent(javax.swing.JComponent) Component(java.awt.Component)

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