/** * 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()); } } }
/** * @param block * @requires block must be a valid block. That is, block may not be such that block == null || * block.getBlockID() == null || block.getBlockID() == Block.NULL || block.getBlockID() == -1 * || Block.getBlock(block.getBlockID()) == null || * Block.getBlock(block.getBlockID()).getGenusName() == null || * Block.getBlock(block.getBlockID()).getGenusName().length() == 0 || * Block.getBlock(block.getBlockID()).getBlockLabel() == null * @modifies Objects modified by this method is undefined * @effects The effects of this method is unknown */ private void addBlock(RenderableBlock block) { // check invariant if (block == null || block.getBlockID() == null || block.getBlockID().equals(Block.NULL) || workspace.getEnv().getBlock(block.getBlockID()) == null || workspace.getEnv().getBlock(block.getBlockID()).getGenusName() == null || workspace.getEnv().getBlock(block.getBlockID()).getGenusName().length() == 0 || workspace.getEnv().getBlock(block.getBlockID()).getBlockLabel() == null) { throw new RuntimeException( "Invariant Violated: may not pass an invalid instance of renderabel block"); } // ignore default arguments block.ignoreDefaultArguments(); this.blockCanvas.getCanvas().add(block, 0); block.setLocation(0, 0); Long parentBlockID = this.focusManager.getFocusBlockID(); if (invalidBlockID(parentBlockID)) { new BlockDropAnimator( workspace, this.focusManager.getCanvasPoint(), block, workspace.getEnv().getRenderableBlock(parentBlockID)); } else { RenderableBlock parentBlock = workspace.getEnv().getRenderableBlock(parentBlockID); new BlockDropAnimator( workspace, SwingUtilities.convertPoint( parentBlock, this.focusManager.getBlockPoint(), this.blockCanvas.getCanvas()), block, workspace.getEnv().getRenderableBlock(parentBlockID)); } this.focusManager.setFocus(block.getBlockID()); }
/** @overrides WorkspaceWidget.blockDropped() */ @Override public void blockDropped(RenderableBlock block) { // add to view at the correct location Component oldParent = block.getParent(); block.setLocation( SwingUtilities.convertPoint(oldParent, block.getLocation(), this.pageJComponent)); addBlock(block); this.pageJComponent.setComponentZOrder(block, 0); this.pageJComponent.revalidate(); }
/** * @param block - the new block being added whose position must be revalidated * @requires block != null * @modifies block.location or this page's abstract width * @effects shifts this block into the page or increases the width of this page to fit the new * block. It must then notify listeners that the page's size may have changed */ public void reformBlockPosition(RenderableBlock block) { // move blocks in Point p = SwingUtilities.convertPoint(block.getParent(), block.getLocation(), this.pageJComponent); if (p.x < block.getHighlightStrokeWidth() / 2 + 1) { block.setLocation(block.getHighlightStrokeWidth() / 2 + 1, p.y); block.moveConnectedBlocks(); // the block has moved, so update p p = SwingUtilities.convertPoint(block.getParent(), block.getLocation(), this.pageJComponent); } else if (p.x + block.getWidth() + block.getHighlightStrokeWidth() / 2 + 1 > this.pageJComponent.getWidth()) { this.setPixelWidth(p.x + block.getWidth() + block.getHighlightStrokeWidth() / 2 + 1); } if (p.y < block.getHighlightStrokeWidth() / 2 + 1) { block.setLocation(p.x, block.getHighlightStrokeWidth() / 2 + 1); block.moveConnectedBlocks(); } else if (p.y + block.getStackBounds().height + block.getHighlightStrokeWidth() / 2 + 1 > this.pageJComponent.getHeight()) { block.setLocation( p.x, this.pageJComponent.getHeight() - block.getStackBounds().height - block.getHighlightStrokeWidth() / 2 + 1); block.moveConnectedBlocks(); } if (block.hasComment()) { // p = SwingUtilities.convertPoint(block.getComment().getParent(), // block.getComment().getLocation(), this.pageJComponent); p = block.getComment().getLocation(); if (p.x + block.getComment().getWidth() + 1 > this.pageJComponent.getWidth()) { this.setPixelWidth(p.x + block.getComment().getWidth() + 1); } } // repaint all pages PageChangeEventManager.notifyListeners(); }
private void loadDefaultArdublockProgram() { /* InputStream defaultArdublockProgram = this.getClass().getResourceAsStream(DEFAULT_ARDUBLOCK_PROGRAM_PATH); final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); final DocumentBuilder builder; final Document doc; try { builder = factory.newDocumentBuilder(); doc = builder.parse(defaultArdublockProgram); final Element projectRoot = doc.getDocumentElement(); workspaceController.resetWorkspace(); workspaceController.loadProjectFromElement(projectRoot); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); workspaceController.loadFreshWorkspace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); workspaceController.loadFreshWorkspace(); } */ Workspace workspace = workspaceController.getWorkspace(); Page page = workspace.getPageNamed("Main"); FactoryManager manager = workspace.getFactoryManager(); Block newBlock; newBlock = new Block(workspace, "loop", false); FactoryRenderableBlock factoryRenderableBlock = new FactoryRenderableBlock(workspace, manager, newBlock.getBlockID()); RenderableBlock renderableBlock = factoryRenderableBlock.createNewInstance(); renderableBlock.setLocation(100, 100); page.addBlock(renderableBlock); }
private void pasteStack(BlockNode node) { // ====================>>>>>>>>>>>>>>>>>>>>>>>>> // ====================focus coming in>>>>>>>>>> TODO // ====================>>>>>>>>>>>>>>>>>>>>>>>>> if (node == null) { return; } WorkspaceWidget widget = null; Iterable<WorkspaceWidget> widgets = null; Point spot = null; if (invalidBlockID(focusManager.getFocusBlockID())) { // canvas has focus Point location = SwingUtilities.convertPoint( this.blockCanvas.getCanvas(), this.focusManager.getCanvasPoint(), workspace); widget = workspace.getWidgetAt(location); spot = SwingUtilities.convertPoint( this.blockCanvas.getCanvas(), this.focusManager.getCanvasPoint(), widget.getJComponent()); } else { RenderableBlock focusRenderable = workspace.getEnv().getRenderableBlock(focusManager.getFocusBlockID()); widget = focusRenderable.getParentWidget(); spot = focusRenderable.getLocation(); } if (widget == null) { // TODO: To be examined and fixed, occurs on macs JOptionPane.showMessageDialog( frame, "Please click somewhere on the canvas first.", "Error", JOptionPane.PLAIN_MESSAGE); // throw new RuntimeException("Why are we adding a block to a null widget?"); } else { // checks to see if the copied block still exists if (BlockUtilities.blockExists(workspace, node)) { // create mirror block and mirror childrens spot.translate(10, 10); RenderableBlock mirror = BlockUtilities.makeRenderable(workspace, node, widget); mirror.setLocation(spot); mirror.moveConnectedBlocks(); // make sure the childrens are placed correctly } else { // TODO: future version, allow them to paste JOptionPane.showMessageDialog( frame, "You cannot paste blocks that are currently NOT on the canvas." + "\nThis function will be available in a future version.\n", "Error", JOptionPane.PLAIN_MESSAGE); } } }