Search in sources :

Example 16 with Block

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

the class RenderableBlock method mouseDragged.

public void mouseDragged(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
        if (pickedUp) {
            Point pp = SwingUtilities.convertPoint(this, e.getPoint(), workspace.getMiniMap());
            if (workspace.getMiniMap().contains(pp)) {
                workspace.getMiniMap().blockDragged(this, e.getPoint());
                lastDragWidget = workspace.getMiniMap();
                return;
            }
            // drag this block if appropriate (checks bounds first)
            dragHandler.mouseDragged(e);
            // Find the widget under the mouse
            dragHandler.myLoc.move(getX() + dragHandler.mPressedX, getY() + dragHandler.mPressedY);
            Point p = SwingUtilities.convertPoint(this.getParent(), dragHandler.myLoc, workspace);
            WorkspaceWidget widget = workspace.getWidgetAt(p);
            if (widget == null) {
                // not on a workspace widget, cancel dragging
                return;
            }
            //if this is the first call to mouseDragged
            if (!dragging) {
                Block block = getBlock();
                BlockConnector plug = BlockLinkChecker.getPlugEquivalent(block);
                if (plug != null && plug.hasBlock()) {
                    Block parent = workspace.getEnv().getBlock(plug.getBlockID());
                    BlockConnector socket = parent.getConnectorTo(blockID);
                    BlockLink link = BlockLink.getBlockLink(workspace, block, parent, plug, socket);
                    link.disconnect();
                    //socket is removed internally from block's socket list if socket is expandable
                    workspace.getEnv().getRenderableBlock(parent.getBlockID()).blockDisconnected(socket);
                    //NOTIFY WORKSPACE LISTENERS OF DISCONNECTION
                    workspace.notifyListeners(new WorkspaceEvent(workspace, widget, link, WorkspaceEvent.BLOCKS_DISCONNECTED));
                }
                startDragging(this, widget);
            }
            // drag this block and all attached to it
            drag(this, dragHandler.dragDX, dragHandler.dragDY, widget, true);
            workspace.getMiniMap().repaint();
        }
    }
}
Also used : WorkspaceEvent(edu.mit.blocks.workspace.WorkspaceEvent) Block(edu.mit.blocks.codeblocks.Block) BlockLink(edu.mit.blocks.codeblocks.BlockLink) BlockConnector(edu.mit.blocks.codeblocks.BlockConnector) Point(java.awt.Point) WorkspaceWidget(edu.mit.blocks.workspace.WorkspaceWidget)

Example 17 with Block

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

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

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

the class Workspace method loadState.

public void loadState(Object memento) {
    assert memento instanceof WorkspaceState : "";
    if (memento instanceof WorkspaceState) {
        WorkspaceState state = (WorkspaceState) memento;
        //Load the blocks state
        for (Long blockID : state.blockStates.keySet()) {
            Block toBeUpdated = getEnv().getBlock(blockID);
            toBeUpdated.loadState(state.blockStates.get(blockID));
        }
        //Load the canvas state
        blockCanvas.loadState(state.blockCanvasState);
    }
}
Also used : Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

Example 20 with Block

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

the class WorkspaceEnvironment method addBlock.

public void addBlock(Block block) {
    long id = block.getBlockID();
    if (this.allBlocks.containsKey(id)) {
        Block dup = this.allBlocks.get(id);
        System.out.println("pre-existing block is: " + dup + " with genus " + dup.getGenusName() + " and label " + dup.getBlockLabel());
        assert !this.allBlocks.containsKey(id) : "Block id: " + id + " already exists!  BlockGenus " + block.getGenusName() + " label: " + block.getBlockLabel();
    }
    this.allBlocks.put(id, block);
}
Also used : Block(edu.mit.blocks.codeblocks.Block) RenderableBlock(edu.mit.blocks.renderable.RenderableBlock)

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